PhoenixHardware  0.2.0
Tools to get hardware information
Loading...
Searching...
No Matches
PFunctionPerf.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5****************************************/
6
7#include <math.h>
8#include "PFunctionPerf.h"
9
11
13PFunctionPerf::PFunctionPerf(const std::string & name){
15}
16
18
23
28
30
34 copyPFunctionPerf(other);
35 return *this;
36}
37
39
41void PFunctionPerf::setName(const std::string & name){
42 p_name = name;
43}
44
46
48void PFunctionPerf::resize(size_t nbThread){
49 p_timeBegin.resize(nbThread);
50 p_nbCall.resize(nbThread);
51 p_fullTime.resize(nbThread);
52 p_fullSqrTime.resize(nbThread);
53 p_minTime.resize(nbThread);
54 p_maxTime.resize(nbThread);
55 reset();
56}
57
59
61void PFunctionPerf::start(size_t threadIndex){
62 p_timeBegin[threadIndex] = phoenix_hardware_rdtsc();
63}
64
66
68void PFunctionPerf::stop(size_t threadIndex){
69 size_t elapsedTime = phoenix_hardware_rdtsc() - p_timeBegin[threadIndex];
70 double dTime = elapsedTime;
71 p_fullTime[threadIndex] += dTime;
72 p_fullSqrTime[threadIndex] += dTime*dTime;
73 if(elapsedTime > p_maxTime[threadIndex]){p_maxTime[threadIndex] = elapsedTime;}
74 if(elapsedTime < p_minTime[threadIndex]){p_minTime[threadIndex] = elapsedTime;}
75 ++(p_nbCall[threadIndex]);
76}
77
79
81void PFunctionPerf::reset(size_t threadIndex){
82 p_timeBegin[threadIndex] = 0lu;
83 p_nbCall[threadIndex] = 0lu;
84 p_fullTime[threadIndex] = 0.0;
85 p_fullSqrTime[threadIndex] = 0.0;
86 p_minTime[threadIndex] = -1lu;
87 p_maxTime[threadIndex] = 0lu;
88}
89
92 for(size_t i(0lu); i < p_timeBegin.size(); ++i){
93 reset(i);
94 }
95}
96
98
101template<typename T>
102T reduceVectorVale(const std::vector<T> & vec){
103 T sum(0);
104 for(typename std::vector<T>::const_iterator it(vec.begin()); it != vec.end(); ++it){
105 sum += *it;
106 }
107 return sum;
108}
109
111
118void PFunctionPerf::getPerf(size_t & nbCall, double & fullTime, double & averageTime, double & stdTime, size_t & minTime, size_t & maxTime) const{
119 nbCall = reduceVectorVale(p_nbCall);
120 if(nbCall != 0lu){
121 fullTime = reduceVectorVale(p_fullTime);
122 averageTime = fullTime/((double)nbCall);
123 stdTime = sqrt((reduceVectorVale(p_fullSqrTime)/((double)nbCall)) - (averageTime*averageTime));
124 minTime = reduceVectorVale(p_minTime)*0.5;
125 maxTime = reduceVectorVale(p_maxTime)*0.5;
126 }else{
127 fullTime = 0.0;
128 averageTime = 0.0;
129 stdTime = 0.0;
130 minTime = 0.0;
131 maxTime = 0.0;
132 }
133}
134
136
138const std::string & PFunctionPerf::getName() const{
139 return p_name;
140}
141
143
145void PFunctionPerf::print(std::ostream & out) const{
146 size_t nbCall(0lu);
147 double fullTime(0.0), averageTime(0.0), stdTime(0.0);
148 size_t minTime(0lu), maxTime(0lu);
149 getPerf(nbCall, fullTime, averageTime, stdTime, minTime, maxTime);
150 out << "Function "<<p_name<<"(nbCall = " << nbCall << ", fullTime = " << fullTime << " cy, averageTime = " << averageTime << " cy, stdTime = " << stdTime << " cy, minTime = " << minTime << " cy, maxTime = " << maxTime << " cy)" << std::endl;
151}
152
154
156void PFunctionPerf::printCsv(std::ostream & out) const{
157 size_t nbCall(0lu);
158 double fullTime(0.0), averageTime(0.0), stdTime(0.0);
159 size_t minTime(0lu), maxTime(0lu);
160 getPerf(nbCall, fullTime, averageTime, stdTime, minTime, maxTime);
161 out <<p_name<<"," << nbCall << "," << fullTime << "," << averageTime << "," << stdTime << "," << minTime << "," << maxTime << std::endl;
162}
163
165
168 p_name = other.p_name;
169 p_timeBegin = other.p_timeBegin;
170 p_nbCall = other.p_nbCall;
171 p_fullTime = other.p_fullTime;
173 p_minTime = other.p_minTime;
174 p_maxTime = other.p_maxTime;
175}
176
180void PFunctionPerf::initialisationPFunctionPerf(const std::string & name){
181 p_name = name;
182 resize(1lu);
183}
184
185
186
187
188
T reduceVectorVale(const std::vector< T > &vec)
Sum the given vector.
void resize(size_t nbThread)
Resize the PFunctionPerf by respect to the number of thread which call the function.
PFunctionPerf & operator=(const PFunctionPerf &other)
Definition of equal operator of PFunctionPerf.
void stop(size_t threadIndex=0lu)
Stop the timing of one function.
std::string p_name
Name of the function to profile.
void printCsv(std::ostream &out=std::cout) const
Print the performance of the function in CSV format.
void print(std::ostream &out=std::cout) const
Print the performance of the function.
void copyPFunctionPerf(const PFunctionPerf &other)
Copy function of PFunctionPerf.
virtual ~PFunctionPerf()
Destructor of PFunctionPerf.
void setName(const std::string &name)
Set the name of the function profiled by the current PFunctionPerf.
PFunctionPerf(const std::string &name="")
Default constructor of PFunctionPerf.
std::vector< double > p_fullTime
Full time of execution for the current function (compute the averaged time with nbCall)
std::vector< size_t > p_maxTime
Maximum execution time.
void getPerf(size_t &nbCall, double &fullTime, double &averageTime, double &stdTime, size_t &minTime, size_t &maxTime) const
Get the performances of the current function.
std::vector< double > p_fullSqrTime
Sum of all square times (used to compute standard deviation)
void start(size_t threadIndex=0lu)
Start the timing for one function.
std::vector< size_t > p_nbCall
Number of calls of the current function.
const std::string & getName() const
Get the name of the function to profile.
void initialisationPFunctionPerf(const std::string &name)
Initialisation function of the class PFunctionPerf.
std::vector< size_t > p_minTime
Minimum execution time.
std::vector< size_t > p_timeBegin
Begin time of the current performance evaluation.
void reset()
Reset the profiling.