8.2.1 : The main.cpp

The main.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
#include <iostream>
#include "asterics_hpc.h"

using namespace std;

///Do the Hadamard product
/**	@param tabValue : input table
 * 	@param nbElement : number of elements in the input table
 * 	@return sum of all the elements of the input table
*/
float reduction(const float * tabValue, long unsigned int nbElement){
	float res(0.0f);
	for(long unsigned int i(0lu); i < nbElement; ++i){
		res += tabValue[i];
	}
	return res;
}

///Get the number of cycles per elements of the reduction
/**	@param nbElement : number of elements of the tables
 * 	@param nbRepetition : number of repetition to evaluate the function reduction
*/
void evaluateReduction(long unsigned int nbElement, long unsigned int nbRepetition){
	float * tabValue = (float*)asterics_malloc(sizeof(float)*nbElement);
	for(long unsigned int i(0lu); i < nbElement; ++i){
		tabValue[i] = (float)(i*32lu%17lu);
	}
	
	long unsigned int beginTime(rdtsc());
	for(long unsigned int i(0lu); i < nbRepetition; ++i){
		//We do not used the result of the function, so GCC7 deduces that this loop is useless and do not compile the code except in -O0 mode
		reduction(tabValue, nbElement);
	}
	long unsigned int elapsedTime((double)(rdtsc() - beginTime)/((double)nbRepetition));
	
	double cyclePerElement(((double)elapsedTime)/((double)nbElement));
	cout << "evaluateReduction : nbElement = "<<nbElement<<", cyclePerElement = " << cyclePerElement << " cy/el, elapsedTime = " << elapsedTime << " cy" << endl;
	cerr << nbElement << "\t" << cyclePerElement << "\t" << elapsedTime << endl;
	
	asterics_free(tabValue);
}

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