2.1.1.1 : The main.cpp file
First, we have to include the appropriate files :
1 2 3 |
#include <iostream> using namespace std; |
Then, we define our hadamard kernel :
1 2 3 4 5 6 7 8 9 10 11 |
///Do the Hadamard product /** @param[out] tabResult : table of results of tabX*tabY * @param tabX : input table * @param tabY : input table * @param nbElement : number of elements in the tables */ void hadamard_product(float* tabResult, const float* tabX, const float* tabY, long unsigned int nbElement){ for(long unsigned int i(0lu); i < nbElement; ++i){ tabResult[i] = tabX[i]*tabY[i]; } } |
We add the function to evaluate our kernel :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
///Get the number of cycles per elements of the Hadamard product /** @param nbElement : number of elements of the tables */ void evaluateHadamardProduct(long unsigned int nbElement){ //Allocation of the tables float * tabResult = new float[nbElement]; float * tabX = new float[nbElement]; float * tabY = new float[nbElement]; //Initialisation of the tables for(long unsigned int i(0lu); i < nbElement; ++i){ tabX[i] = (float)(i*32lu%17lu); tabY[i] = (float)(i*57lu%31lu); } //Do the hadamard product hadamard_product(tabResult, tabX, tabY, 4000lu); cout << "tabResult[0] = " << tabResult[0] << endl; //Deallocate the tables delete[] tabResult; delete[] tabX; delete[] tabY; } |
Finally, we call the function to evaluate several points in order to make plots :
1 2 3 4 5 6 7 8 9 |
int main(int argc, char** argv){ cout << "Hadamard product" << endl; evaluateHadamardProduct(1000lu); evaluateHadamardProduct(2000lu); evaluateHadamardProduct(3000lu); evaluateHadamardProduct(5000lu); evaluateHadamardProduct(10000lu); return 0; } |
The full 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 52 53 54 55 |
/*************************************** Auteur : Pierre Aubert Mail : pierre.aubert@lapp.in2p3.fr Licence : CeCILL-C ****************************************/ #include <iostream> using namespace std; ///Do the Hadamard product /** @param[out] tabResult : table of results of tabX*tabY * @param tabX : input table * @param tabY : input table * @param nbElement : number of elements in the tables */ void hadamard_product(float* tabResult, const float* tabX, const float* tabY, long unsigned int nbElement){ for(long unsigned int i(0lu); i < nbElement; ++i){ tabResult[i] = tabX[i]*tabY[i]; } } ///Get the number of cycles per elements of the Hadamard product /** @param nbElement : number of elements of the tables */ void evaluateHadamardProduct(long unsigned int nbElement){ //Allocation of the tables float * tabResult = new float[nbElement]; float * tabX = new float[nbElement]; float * tabY = new float[nbElement]; //Initialisation of the tables for(long unsigned int i(0lu); i < nbElement; ++i){ tabX[i] = (float)(i*32lu%17lu); tabY[i] = (float)(i*57lu%31lu); } //Do the hadamard product hadamard_product(tabResult, tabX, tabY, 4000lu); cout << "tabResult[0] = " << tabResult[0] << endl; //Deallocate the tables delete[] tabResult; delete[] tabX; delete[] tabY; } int main(int argc, char** argv){ cout << "Hadamard product" << endl; evaluateHadamardProduct(1000lu); evaluateHadamardProduct(2000lu); evaluateHadamardProduct(3000lu); evaluateHadamardProduct(5000lu); evaluateHadamardProduct(10000lu); return 0; } |
You can get the file here.