4.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 ajouter un tableaux d'indices puisque nous n'aurons plus de boucle for :
1
2
	std::vector<size_t> tabIndex(nbElement);
	std::iota(tabIndex.begin(), tabIndex.end(), 0lu);


On initialise tabX :
1
2
	std::transform(std::execution::par_unseq, std::begin(tabIndex), std::end(tabIndex), std::begin(tabX),
			[](size_t i){ return i*19lu%11; });


On initialise tabY :
1
2
	std::transform(std::execution::par_unseq, std::begin(tabIndex), std::end(tabIndex), std::begin(tabY),
			[](size_t i){ return i*27lu%19; });


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
/***************************************
	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::vector<size_t> tabIndex(nbElement);
	std::iota(tabIndex.begin(), tabIndex.end(), 0lu);
	std::transform(std::execution::par_unseq, std::begin(tabIndex), std::end(tabIndex), std::begin(tabX),
			[](size_t i){ return i*19lu%11; });
	std::transform(std::execution::par_unseq, std::begin(tabIndex), std::end(tabIndex), std::begin(tabY),
			[](size_t i){ return i*27lu%19; });
	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.