7.1.5.1.1 : Le main_cpp20.cpp
Comme toujours, on commence pas les includes :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <vector> #include <algorithm> //Some doc at : https://en.cppreference.com/w/cpp/header/execution #include <execution> #include "asterics_hpc.h" ///Do the Hadamard product /** @param[out] vecRes : vector of results of tabX*tabY * @param vecX : input vector * @param vecY : input vector */ void hadamard_product(std::vector<float> & vecRes, const std::vector<float> & vecX, const std::vector<float> & vecY){ std::transform(EXECUTION_POLICY, std::begin(vecX), std::end(vecX), std::begin(vecY), std::begin(vecRes), [](float xi, float yi){ return xi * yi; }); } ///Get the number of cycles per elements of the Hadamard product /** @param nbElement : number of elements of the tables * @param nbRepetition : number of repetition to evaluate the function hadamard_product */ void evaluateHadamardProduct(long unsigned int nbElement, long unsigned int nbRepetition){ |
On déclare nos tableaux et on les initialise :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
std::vector<float> vecX, vecY, vecRes; vecRes.resize(nbElement); for(size_t i(0lu); i < nbElement; ++i){ vecX.push_back(i*32lu%17lu); vecY.push_back(i*57lu%31lu); } float res(0.0f); //Stating the timer long unsigned int beginTime(rdtsc()); for(long unsigned int i(0lu); i < nbRepetition; ++i){ hadamard_product(vecRes, vecX, vecY); res += vecRes[0]; } //Get the time of the nbRepetition calls long unsigned int elapsedTime((double)(rdtsc() - beginTime)/((double)nbRepetition)); double cyclePerElement(((double)elapsedTime)/((double)nbElement)); std::cout << "evaluateHadamardProduct C++20 : nbElement = "<<nbElement<<", cyclePerElement = " << cyclePerElement << " cy/el, elapsedTime = " << elapsedTime << " cy, res = " << res << std::endl; std::cerr << nbElement << "\t" << cyclePerElement << "\t" << elapsedTime << std::endl; } |
Ensuite, la fonction main :
1 2 3 4 5 6 7 8 9 |
int main(int argc, char** argv){ std::cout << "Hadamard product C++ 20" << std::endl; evaluateHadamardProduct(1000lu, 1000000lu); evaluateHadamardProduct(2000lu, 1000000lu); evaluateHadamardProduct(3000lu, 1000000lu); evaluateHadamardProduct(5000lu, 1000000lu); evaluateHadamardProduct(10000lu, 1000000lu); return 0; } |
Le main_cpp20.cpp complet :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
/*************************************** Auteur : Pierre Aubert Mail : pierre.aubert@lapp.in2p3.fr Licence : CeCILL-C ****************************************/ #include <iostream> #include <vector> #include <algorithm> //Some doc at : https://en.cppreference.com/w/cpp/header/execution #include <execution> #include "asterics_hpc.h" ///Do the Hadamard product /** @param[out] vecRes : vector of results of tabX*tabY * @param vecX : input vector * @param vecY : input vector */ void hadamard_product(std::vector<float> & vecRes, const std::vector<float> & vecX, const std::vector<float> & vecY){ std::transform(EXECUTION_POLICY, std::begin(vecX), std::end(vecX), std::begin(vecY), std::begin(vecRes), [](float xi, float yi){ return xi * yi; }); } ///Get the number of cycles per elements of the Hadamard product /** @param nbElement : number of elements of the tables * @param nbRepetition : number of repetition to evaluate the function hadamard_product */ void evaluateHadamardProduct(long unsigned int nbElement, long unsigned int nbRepetition){ std::vector<float> vecX, vecY, vecRes; vecRes.resize(nbElement); for(size_t i(0lu); i < nbElement; ++i){ vecX.push_back(i*32lu%17lu); vecY.push_back(i*57lu%31lu); } float res(0.0f); //Stating the timer long unsigned int beginTime(rdtsc()); for(long unsigned int i(0lu); i < nbRepetition; ++i){ hadamard_product(vecRes, vecX, vecY); res += vecRes[0]; } //Get the time of the nbRepetition calls long unsigned int elapsedTime((double)(rdtsc() - beginTime)/((double)nbRepetition)); double cyclePerElement(((double)elapsedTime)/((double)nbElement)); std::cout << "evaluateHadamardProduct C++20 : nbElement = "<<nbElement<<", cyclePerElement = " << cyclePerElement << " cy/el, elapsedTime = " << elapsedTime << " cy, res = " << res << std::endl; std::cerr << nbElement << "\t" << cyclePerElement << "\t" << elapsedTime << std::endl; } int main(int argc, char** argv){ std::cout << "Hadamard product C++ 20" << std::endl; evaluateHadamardProduct(1000lu, 1000000lu); evaluateHadamardProduct(2000lu, 1000000lu); evaluateHadamardProduct(3000lu, 1000000lu); evaluateHadamardProduct(5000lu, 1000000lu); evaluateHadamardProduct(10000lu, 1000000lu); return 0; } |