Directory: | ./ |
---|---|
File: | tmp_project/PhoenixMicroBenchmark/src/micro_benchmark_common.cpp |
Date: | 2025-03-14 12:14:21 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 70 | 94 | 74.5% |
Branches: | 79 | 113 | 69.9% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include <cmath> | ||
8 | #include <sstream> | ||
9 | #include "micro_benchmark_common.h" | ||
10 | |||
11 | ///Fill the map of ordered time with the vector | ||
12 | /** @param[out] mapOrderTime : map of the ordered ellapsed times | ||
13 | * @param vecTime : input ellapsed times | ||
14 | */ | ||
15 | ✗ | void micro_benchmarkVecToMap(MapOrderedTime & mapOrderTime, const VecEllapsedTime & vecTime){ | |
16 | ✗ | for(VecEllapsedTime::const_iterator it(vecTime.begin()); it != vecTime.end(); ++it){ | |
17 | ✗ | MapOrderedTime::iterator itFindTime = mapOrderTime.find(*it); | |
18 | ✗ | if(itFindTime != mapOrderTime.end()){ | |
19 | ✗ | itFindTime->second += 1lu; | |
20 | }else{ | ||
21 | ✗ | mapOrderTime[*it] = 1lu; | |
22 | } | ||
23 | } | ||
24 | } | ||
25 | |||
26 | ///Compute the total computing time and associated error with the given data | ||
27 | /** @param[out] ellapsedTimeNs : ellapsed time in ns | ||
28 | * @param[out] ellapsedTimeErrorNs : error on the ellapsed time in ns | ||
29 | * @param mapOrderTime : map of the ordered ellapsed times | ||
30 | * @param nbValueToBeUsed : number of values to be used in the map to compute the performance of the function | ||
31 | */ | ||
32 | ✗ | void micro_benchmarkComputeTime(double & ellapsedTimeNs, double & ellapsedTimeErrorNs, const MapOrderedTime & mapOrderTime, size_t nbValueToBeUsed){ | |
33 | ✗ | double sumValue(0.0), sumSquare(0.0); | |
34 | ✗ | size_t nbValue(0lu); | |
35 | ✗ | for(MapOrderedTime::const_iterator it(mapOrderTime.begin()); it != mapOrderTime.end() && nbValue < nbValueToBeUsed; ++it){ | |
36 | ✗ | double val(it->first); | |
37 | |||
38 | ✗ | sumValue += val*((double)it->second); | |
39 | ✗ | sumSquare += val*val*((double)it->second); | |
40 | ✗ | nbValue += it->second; | |
41 | } | ||
42 | ✗ | ellapsedTimeNs = sumValue/((double)nbValue); | |
43 | ✗ | double meanSquare(sumSquare/((double)nbValue)); | |
44 | ✗ | ellapsedTimeErrorNs = std::sqrt(meanSquare - ellapsedTimeNs*ellapsedTimeNs); | |
45 | } | ||
46 | |||
47 | ///Help function of the micro benchmarking program argument parse | ||
48 | 2 | void micro_benchmarkHelpFunction(){ | |
49 | 2 | std::cout << "micro_benchmarkParseArg : expect only a list of INT such as: \"1000,2000,3000,4000\"" << std::endl; | |
50 | 2 | } | |
51 | |||
52 | ///Tells if a chararacter is in a string | ||
53 | /** @param str : string to be analysed | ||
54 | * @param ch : char to be searched in str | ||
55 | * @return true if ch is in str, false otherwise | ||
56 | */ | ||
57 | 37 | bool findCharInString(const std::string& str, char ch){ | |
58 | 37 | std::string::const_iterator it = str.begin(); | |
59 |
2/2✓ Branch 2 taken 127 times.
✓ Branch 3 taken 30 times.
|
157 | while(it != str.end()){ |
60 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 2 taken 120 times.
|
127 | if(*it == ch) return true; |
61 | 120 | ++it; | |
62 | } | ||
63 | 30 | return false; | |
64 | } | ||
65 | |||
66 | ///Cut a string on determined chars | ||
67 | /** @param strIn : input string | ||
68 | * @param setChars : set of characters on which to cut | ||
69 | * @return vector of cutted string | ||
70 | */ | ||
71 | 4 | std::vector<std::string> cutStringOnChars(const std::string & strIn, const std::string & setChars){ | |
72 | 4 | std::vector<std::string> vecOut; | |
73 |
1/1✓ Branch 2 taken 4 times.
|
4 | std::string strTmp(""); |
74 |
2/2✓ Branch 4 taken 37 times.
✓ Branch 5 taken 4 times.
|
41 | for(std::string::const_iterator it(strIn.begin()); it != strIn.end(); ++it){ |
75 |
3/3✓ Branch 2 taken 37 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 30 times.
|
37 | if(findCharInString(setChars, *it)){ |
76 |
3/4✓ Branch 1 taken 7 times.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 7 times.
|
7 | if(strTmp != ""){vecOut.push_back(strTmp);} |
77 |
1/1✓ Branch 1 taken 7 times.
|
7 | strTmp = ""; |
78 | }else{ | ||
79 |
1/1✓ Branch 2 taken 30 times.
|
30 | strTmp += *it; |
80 | } | ||
81 | } | ||
82 |
3/4✓ Branch 1 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
|
4 | if(strTmp != ""){vecOut.push_back(strTmp);} |
83 | 8 | return vecOut; | |
84 | 4 | } | |
85 | |||
86 | ///Call the evalFunc by respect to the arguments given to the program | ||
87 | /** @param argc : number of arguments passed to the program | ||
88 | * @param argv : table of arguments passed to the program | ||
89 | * @param evalFunc : function which evaluates the performance of an other function | ||
90 | * @return 0 on success, -1 otherwise | ||
91 | */ | ||
92 | 2 | int micro_benchmarkParseArg(int argc, char** argv, EvaluateTimeFct evalFunc){ | |
93 |
2/3✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
|
2 | PIN_THREAD_TO_CORE |
94 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if(argc != 2){ |
95 |
1/1✓ Branch 1 taken 1 times.
|
1 | micro_benchmarkHelpFunction(); |
96 | 1 | return -1; | |
97 | } | ||
98 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string argument(argv[1]); |
99 |
2/6✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
|
1 | if(argument == "--help" || argument == "-h"){ |
100 |
1/1✓ Branch 1 taken 1 times.
|
1 | micro_benchmarkHelpFunction(); |
101 | 1 | return 0; | |
102 | } | ||
103 | ✗ | std::vector<std::string> vecSize(cutStringOnChars(argument, ", \t\n")); | |
104 | ✗ | for(std::vector<std::string>::iterator it(vecSize.begin()); it != vecSize.end(); ++it){ | |
105 | ✗ | std::stringstream converterStr(*it); | |
106 | ✗ | size_t nbElement(0lu); | |
107 | ✗ | converterStr >> nbElement; | |
108 | ✗ | evalFunc(nbElement); | |
109 | } | ||
110 | ✗ | return 0; | |
111 | 1 | } | |
112 | |||
113 | ///Help function of the micro benchmarking program argument parse | ||
114 | 1 | void micro_benchmarkHelpFunction2d(){ | |
115 | 1 | std::cout << "micro_benchmarkParseArg2d : expect only two list of INT such as: \"1000,2000,3000,4000\"" << std::endl; | |
116 | 1 | } | |
117 | |||
118 | ///Call the evalFunc by respect to the arguments given to the program | ||
119 | /** @param argc : number of arguments passed to the program | ||
120 | * @param argv : table of arguments passed to the program | ||
121 | * @param evalFunc : function which evaluates the performance of an other function | ||
122 | * @return 0 on success, -1 otherwise | ||
123 | */ | ||
124 | 3 | int micro_benchmarkParseArg2d(int argc, char** argv, EvaluateTimeFct2d evalFunc){ | |
125 |
2/3✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
|
3 | PIN_THREAD_TO_CORE |
126 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if(argc != 3){ |
127 |
1/1✓ Branch 1 taken 1 times.
|
1 | micro_benchmarkHelpFunction2d(); |
128 | 1 | return -1; | |
129 | } | ||
130 |
2/2✓ Branch 2 taken 2 times.
✓ Branch 6 taken 2 times.
|
4 | std::string argumentX(argv[1]), argumentY(argv[2]); |
131 | |||
132 |
2/2✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
|
4 | std::vector<std::string> vecSizeX(cutStringOnChars(argumentX, ", \t\n")); |
133 |
2/2✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
|
4 | std::vector<std::string> vecSizeY(cutStringOnChars(argumentY, ", \t\n")); |
134 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
2 | if(vecSizeX.size() != vecSizeY.size()){ |
135 |
6/6✓ Branch 1 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 8 taken 1 times.
✓ Branch 12 taken 1 times.
✓ Branch 15 taken 1 times.
✓ Branch 18 taken 1 times.
|
1 | std::cerr << "Vector X and Y must ave the same size vecSizeX("<<vecSizeX.size()<<") != vecSizeY("<<vecSizeY.size()<<")" << std::endl; |
136 |
6/6✓ Branch 1 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 8 taken 1 times.
✓ Branch 12 taken 1 times.
✓ Branch 15 taken 1 times.
✓ Branch 18 taken 1 times.
|
1 | std::cout << "Vector X and Y must ave the same size vecSizeX("<<vecSizeX.size()<<") != vecSizeY("<<vecSizeY.size()<<")" << std::endl; |
137 | 1 | return -1; | |
138 | } | ||
139 |
2/2✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1 times.
|
4 | for(std::vector<std::string>::iterator itX(vecSizeX.begin()); itX != vecSizeX.end(); ++itX){ |
140 |
1/1✓ Branch 3 taken 3 times.
|
3 | std::stringstream converterStrX(*itX); |
141 | 3 | size_t nbElementX(0lu); | |
142 |
1/1✓ Branch 1 taken 3 times.
|
3 | converterStrX >> nbElementX; |
143 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "\t" << nbElementX; |
144 | 3 | } | |
145 |
1/1✓ Branch 1 taken 1 times.
|
1 | std::cout << std::endl; |
146 | |||
147 |
2/2✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1 times.
|
4 | for(std::vector<std::string>::iterator itY(vecSizeY.begin()); itY != vecSizeY.end(); ++itY){ |
148 |
1/1✓ Branch 3 taken 3 times.
|
3 | std::stringstream converterStrY(*itY); |
149 | 3 | size_t nbElementY(0lu); | |
150 |
1/1✓ Branch 1 taken 3 times.
|
3 | converterStrY >> nbElementY; |
151 |
1/1✓ Branch 1 taken 3 times.
|
3 | std::cout << nbElementY; |
152 |
2/2✓ Branch 4 taken 9 times.
✓ Branch 5 taken 3 times.
|
12 | for(std::vector<std::string>::iterator itX(vecSizeX.begin()); itX != vecSizeX.end(); ++itX){ |
153 |
1/1✓ Branch 3 taken 9 times.
|
9 | std::stringstream converterStrX(*itX); |
154 | 9 | size_t nbElementX(0lu); | |
155 |
1/1✓ Branch 1 taken 9 times.
|
9 | converterStrX >> nbElementX; |
156 |
1/1✓ Branch 1 taken 9 times.
|
9 | double res = evalFunc(nbElementX, nbElementY); |
157 |
6/6✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 7 taken 9 times.
✓ Branch 10 taken 9 times.
✓ Branch 13 taken 9 times.
✓ Branch 16 taken 9 times.
|
9 | std::cerr << nbElementX << "\t" << nbElementY << "\t" << res << std::endl; |
158 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
|
9 | std::cout << "\t" << res; |
159 | 9 | } | |
160 |
1/1✓ Branch 1 taken 3 times.
|
3 | std::cerr << std::endl; |
161 |
1/1✓ Branch 1 taken 3 times.
|
3 | std::cout << std::endl; |
162 | 3 | } | |
163 | 1 | return 0; | |
164 | 2 | } | |
165 | |||
166 | |||
167 | |||
168 |