3.4.3.5.2 : Les sources hadamard.cpp
Développons le fichier hadamard.cpp :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//some doc at : https://en.cppreference.com/w/cpp/header/algorithm
#include <algorithm>

//Some doc at : https://en.cppreference.com/w/cpp/header/execution
#include <execution>


#include "hadamard.h"

///Do a classical hadamard product
/**	@param[out] tabRes : table of the result
 * 	@param tabX : talbe of x values
 * 	@param tabY : table of y values
 * 	@param nbElement : number of elements in the tables
*/
void hadamard_product(float * tabRes, const float* tabX, const float* tabY, size_t nbElement){
	std::transform(std::execution::par_unseq, tabX, tabX + nbElement, tabY, tabRes,
			[](float xi, float yi){ return xi * yi; });
}


La fonction std::transform permet d'appeler une fonction lambda sur tous les élements d'un ou plusieurs tableaux. Son premier paramètre permet de définir si on veut une exécution :
  • séquentielle : std::execution::seq (C++17)
  • parallèle : std::execution::par (C++17)
  • vectorielle : std::execution::unseq (depuis C++20 voir la doc)
  • parallèle et vectorielle : std::execution::par_unseq (C++17)


Puis on passe les bornes du premier tableau d'entrée tabX et tabX + nbElement. Ensuite on passe les autres tableaux d'entrée, dans notre cas juste tabY. Le dernier tableau est un résultat tabRes. Et enfin, la fonction lambda qui prend les entrées x et y et renvoie un résultat.

Le fichier hadamard.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
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/

//some doc at : https://en.cppreference.com/w/cpp/header/algorithm
#include <algorithm>

//Some doc at : https://en.cppreference.com/w/cpp/header/execution
#include <execution>


#include "hadamard.h"

///Do a classical hadamard product
/**	@param[out] tabRes : table of the result
 * 	@param tabX : talbe of x values
 * 	@param tabY : table of y values
 * 	@param nbElement : number of elements in the tables
*/
void hadamard_product(float * tabRes, const float* tabX, const float* tabY, size_t nbElement){
	std::transform(std::execution::par_unseq, tabX, tabX + nbElement, tabY, tabRes,
			[](float xi, float yi){ return xi * yi; });
}


Le fichier hadamard.cpp est disponible ici.

C'est implémentation est courtes certes, mais elle n'en est pas moins complexe pour les non-avertis.