3.2.1.2.2 : Le fichier source

Écrivons le fichier hadamard_product_vectorize.cpp :



Il nous faut tout d'abord inclure un header qui permet de définir l'alignement des données en float avec PLIB_VECTOR_SIZE_FLOAT :

1
#include "phoenix_intrinsics.h"


Ensuite nous incluons notre header :

1
#include "hadamard_product_vectorize.h"


Ensuite nous implémentons notre produit de hadamard en prennant en compte le fait que nous devons indiquer au compilateur que les tableaux que nous utiliserons seront bien alignés en mémoire sur PLIB_VECTOR_SIZE_BYTE_FLOAT octets avec la fonction __builtin_assume_aligned :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
///Do the Hadamard product
/**	@param[out] ptabResult : table of results of tabX*tabY
 * 	@param ptabX : input table
 * 	@param ptabY : input table
 * 	@param nbElement : number of elements in the tables
*/
void hadamard_product_vectorize(float* __restrict__ ptabResult, const float* __restrict__ ptabX, const float* __restrict__ ptabY, size_t nbElement){
	float* tabResult = (float*)__builtin_assume_aligned(ptabResult, PLIB_VECTOR_SIZE_BYTE_FLOAT);
	const float* tabX = (const float*)__builtin_assume_aligned(ptabX, PLIB_VECTOR_SIZE_BYTE_FLOAT);
	const float* tabY = (const float*)__builtin_assume_aligned(ptabY, PLIB_VECTOR_SIZE_BYTE_FLOAT);
	
	for(size_t i(0lu); i < nbElement; ++i){
		tabResult[i] = tabX[i]*tabY[i];
	}
}


Le fichier hadamard_product_vectorize.cpp complet :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/
#include "phoenix_intrinsics.h"
#include "hadamard_product_vectorize.h"
///Do the Hadamard product
/**	@param[out] ptabResult : table of results of tabX*tabY
 * 	@param ptabX : input table
 * 	@param ptabY : input table
 * 	@param nbElement : number of elements in the tables
*/
void hadamard_product_vectorize(float* __restrict__ ptabResult, const float* __restrict__ ptabX, const float* __restrict__ ptabY, size_t nbElement){
	float* tabResult = (float*)__builtin_assume_aligned(ptabResult, PLIB_VECTOR_SIZE_BYTE_FLOAT);
	const float* tabX = (const float*)__builtin_assume_aligned(ptabX, PLIB_VECTOR_SIZE_BYTE_FLOAT);
	const float* tabY = (const float*)__builtin_assume_aligned(ptabY, PLIB_VECTOR_SIZE_BYTE_FLOAT);
	
	for(size_t i(0lu); i < nbElement; ++i){
		tabResult[i] = tabX[i]*tabY[i];
	}
}


Vous pouvez le télécharger ici.