5.1.3 : Le main.cpp
Comme toujours, on commence pas les includes :
1 2 3 4 5 6 7 |
#include <iostream> #include <vector> #include <numeric> #include <algorithm> //Some doc at : https://en.cppreference.com/w/cpp/header/execution #include <execution> |
Ensuite, la fonction main :
1 |
int main(int argc, char** argv){ |
On définit le nombre d'éléments que l'on veut dans nos tableaux :
1 |
size_t nbElement(100000000); |
On déclare nos tableaux et on les initialise :
1 |
std::vector<float> tabX(nbElement), tabY(nbElement); |
On initialise tabX avec std::generate :
1 2 3 4 5 |
std::generate(std::execution::par_unseq, tabX.begin(), tabX.end(), [i = 0] () mutable { float val(i*19lu%11); i++; return val; }); |
On initialise tabY :
1 2 3 4 5 |
std::generate(std::execution::par_unseq, tabY.begin(), tabY.end(), [i = 0] () mutable { float val(i*27lu%19); i++; return val; }); |
On déclare notre produit scalaire (la somme par défaut est 0) :
1 2 3 |
float res = std::transform_reduce(std::execution::par_unseq, std::begin(tabX), std::end(tabX), std::begin(tabY), 0.0f, std::plus{}, [](float xi, float yi){ return xi * yi; }); |
On affiche le résulat :
1 |
std::cout << "res = " << res << std::endl; |
Puis on termine la fonction main :
1 2 |
return 0; } |
Le main.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 |
/*************************************** Auteur : Pierre Aubert Mail : pierre.aubert@lapp.in2p3.fr Licence : CeCILL-C ****************************************/ #include <iostream> #include <vector> #include <numeric> #include <algorithm> //Some doc at : https://en.cppreference.com/w/cpp/header/execution #include <execution> int main(int argc, char** argv){ size_t nbElement(100000000); std::vector<float> tabX(nbElement), tabY(nbElement); std::generate(std::execution::par_unseq, tabX.begin(), tabX.end(), [i = 0] () mutable { float val(i*19lu%11); i++; return val; }); std::generate(std::execution::par_unseq, tabY.begin(), tabY.end(), [i = 0] () mutable { float val(i*27lu%19); i++; return val; }); float res = std::transform_reduce(std::execution::par_unseq, std::begin(tabX), std::end(tabX), std::begin(tabY), 0.0f, std::plus{}, [](float xi, float yi){ return xi * yi; }); std::cout << "res = " << res << std::endl; return 0; } |
Le fichier main.cpp est disponible ici.