7.4.1 : The main_intrinsics.cpp

There is the main_intrinsics.cpp file :
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
59
60
61
62
63
64
65
66
67
//AVX intrinsic functions
#include <immintrin.h>

#include <iostream>
#include "asterics_hpc.h"

using namespace std;

///Do the Hadamard product
/**	@param[out] tabResult : table of results of tabX*tabY
 * 	@param scal : multiplication scalar (a)
 * 	@param tabX : input table
 * 	@param tabY : input table
 * 	@param nbElement : number of elements in the tables
*/
void saxpy(float* tabResult, float scal, const float * tabX, const float* tabY, long unsigned int nbElement){
	__m256 vecScal = _mm256_broadcast_ss(&scal);
	long unsigned int vecSize(VECTOR_ALIGNEMENT/sizeof(float));
	long unsigned int nbVec(nbElement/vecSize);
	for(long unsigned int i(0lu); i < nbVec; ++i){
		// tabResult = scal*tabX + tabY
		__m256 vecX = _mm256_load_ps(tabX + i*vecSize);
		__m256 vecAX = _mm256_mul_ps(vecX, vecScal);
		__m256 vecY = _mm256_load_ps(tabY + i*vecSize);
		__m256 vecRes = _mm256_add_ps(vecAX, vecY);
		_mm256_store_ps(tabResult + i*vecSize, vecRes);
	}
}

///Get the number of cycles per elements of the saxpy
/**	@param nbElement : number of elements of the tables
 * 	@param nbRepetition : number of repetition to evaluate the function saxpy
*/
void evaluateSaxpy(long unsigned int nbElement, long unsigned int nbRepetition){
	float * tabResult = (float*)asterics_malloc(sizeof(float)*nbElement);
	float * tabX = (float*)asterics_malloc(sizeof(float)*nbElement);
	float * tabY = (float*)asterics_malloc(sizeof(float)*nbElement);
	float scal(4.0f);
	for(long unsigned int i(0lu); i < nbElement; ++i){
		tabX[i] = (float)(i*32lu%17lu);
		tabY[i] = (float)(i*57lu%31lu);
	}
	
	long unsigned int beginTime(rdtsc());
	for(long unsigned int i(0lu); i < nbRepetition; ++i){
		saxpy(tabResult, scal, tabX, tabY, nbElement);
	}
	long unsigned int elapsedTime((double)(rdtsc() - beginTime)/((double)nbRepetition));
	
	double cyclePerElement(((double)elapsedTime)/((double)nbElement));
	cout << "evaluateSaxpy : nbElement = "<<nbElement<<", cyclePerElement = " << cyclePerElement << " cy/el, elapsedTime = " << elapsedTime << " cy" << endl;
	cerr << nbElement << "\t" << cyclePerElement << "\t" << elapsedTime << endl;
	
	asterics_free(tabResult);
	asterics_free(tabX);
	asterics_free(tabY);
}

int main(int argc, char** argv){
	cout << "Saxpy intrinsics" << endl;
	evaluateSaxpy(1000lu, 1000000lu);
	evaluateSaxpy(2000lu, 1000000lu);
	evaluateSaxpy(3000lu, 1000000lu);
	evaluateSaxpy(5000lu, 1000000lu);
	evaluateSaxpy(10000lu, 1000000lu);
	return 0;
}