PhoenixMicroBenchmark  1.3.0
Toolset for function micro-benchmarking
Loading...
Searching...
No Matches
PFunctionTimer.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 <iostream>
8#include <algorithm>
9#include <fstream>
10
11#include "PFunctionTimer.h"
12
14
17PFunctionTimer::PFunctionTimer(size_t nbMeasure, size_t nbElement){
18 initialisationPFunctionTimer(nbMeasure, nbElement);
19}
20
22
27
32
34
38 copyPFunctionTimer(other);
39 return *this;
40}
41
43
45void PFunctionTimer::setNbElement(size_t nbElement){
46 p_nbElement = nbElement;
47}
48
50
52void PFunctionTimer::resize(size_t nbMeasure){
53 p_nbMeasure = nbMeasure;
56}
57
59
64void phoenix_vectorToToml(std::ofstream & fs, const std::vector<double> & vecValue, const std::string & tableName, double nbElement){
65 std::string comma("");
66 fs << tableName << " = [" << std::endl;
67 for(double value : vecValue){
68 fs << comma << "\t" << value/nbElement;
69 comma = ",\n";
70 }
71 fs << "\n]" << std::endl;
72}
73
75
78bool PFunctionTimer::savePerf(const std::string & fileName) const{
79 if(p_vecEllapsedTimeCycle.size() == 0lu || p_vecEllapsedTimeNs.size() == 0lu){
80 std::cerr << "PFunctionTimer::savePerf : cannot save empty vector in file '"<<fileName<<"'" << std::endl;
81 return false;
82 }
83 std::ofstream fs;
84 fs.open(fileName);
85 if(!fs.is_open()){
86 std::cerr << "PFunctionTimer::savePerf : cannot save file '"<<fileName<<"'" << std::endl;
87 return false;
88 }
89 fs << "#Global performances" << std::endl;
90 fs << "[Performance]" << std::endl;
91 fs << "nb_element = " << p_nbElement << std::endl;
92 fs << "nb_measure = " << p_nbMeasure << std::endl;
93 fs << std::endl;
94 std::vector<double> orderPerfNs, orderPerfCy;
95 getSortedPerf(orderPerfNs, orderPerfCy);
96
97 //Let's save the best performances
98 saveQuantilePerf(fs, orderPerfNs, orderPerfCy, "best", 0lu);
99 //Let's save the median performances
100 saveQuantilePerf(fs, orderPerfNs, orderPerfCy, "median", p_nbMeasure/2lu);
101
102 fs << std::endl;
103 fs << "#Detailed performances" << std::endl;
104 fs << "[Detail]" << std::endl;
105 phoenix_vectorToToml(fs, p_vecEllapsedTimeNs, "measure_nanosecond", 1.0);
106 phoenix_vectorToToml(fs, p_vecEllapsedTimeCycle, "measure_cycles", 1.0);
107 phoenix_vectorToToml(fs, p_vecEllapsedTimeNs, "measure_nanosecond_per_element", (double)p_nbElement);
108 phoenix_vectorToToml(fs, p_vecEllapsedTimeCycle, "measure_cycles_per_element", (double)p_nbElement);
109 //Let's save the ratio of cycles over nanoseconds
110 std::vector<double> vecCyPerNs(p_vecEllapsedTimeCycle.size());
111 std::transform(p_vecEllapsedTimeCycle.begin(), p_vecEllapsedTimeCycle.end(),
112 p_vecEllapsedTimeNs.begin(), vecCyPerNs.begin(),
113 [](double cy, double ns){
114 return cy/ns;
115 });
116 phoenix_vectorToToml(fs, vecCyPerNs, "cycles_over_nanosecond", 1.0);
117 fs.close();
118 return true;
119}
120
122
125void PFunctionTimer::getSortedPerf(std::vector<double> & orderPerfNs, std::vector<double> & orderPerfCy) const{
126 orderPerfNs = p_vecEllapsedTimeNs;
127 orderPerfCy = p_vecEllapsedTimeCycle;
128 std::sort(orderPerfNs.begin(), orderPerfNs.end());
129 std::sort(orderPerfCy.begin(), orderPerfCy.end());
130}
131
133
136 return p_nbElement;
137}
138
140
148
150
153void PFunctionTimer::initialisationPFunctionTimer(size_t nbMeasure, size_t nbElement){
154 setNbElement(nbElement);
155 resize(nbMeasure);
156}
157
159
165void PFunctionTimer::saveQuantilePerf(std::ofstream & fs, const std::vector<double> & orderPerfNs, const std::vector<double> & orderPerfCy,
166 const std::string & name, size_t valueIndex) const
167{
168 fs << "#"<<name<<" performance in nano seconds" << std::endl;
169 fs << name << "_nanosecond = " << orderPerfNs[valueIndex] << std::endl;
170 fs << "#Best performance in cycles" << std::endl;
171 fs << name << "_cycle = " << orderPerfCy[valueIndex] << std::endl;
172 fs << std::endl;
173 fs << "#"<<name<<" performance in nano seconds per elements" << std::endl;
174 fs << name << "_nanosecond_per_element = " << orderPerfNs[valueIndex]/p_nbElement << std::endl;
175 fs << "#"<<name<<" performance in cycles per elements" << std::endl;
176 fs << name << "_cycle_per_element = " << orderPerfCy[valueIndex]/p_nbElement << std::endl;
177 fs << "#"<<name<<" performance in Giga Elements per second" << std::endl;
178 fs << name << "_giga_element_per_second = " << p_nbElement/orderPerfNs[valueIndex] << std::endl;
179 fs << std::endl;
180}
181
182
183
void phoenix_vectorToToml(std::ofstream &fs, const std::vector< double > &vecValue, const std::string &tableName, double nbElement)
Save a vector of values into a toml stream.
bool savePerf(const std::string &fileName) const
Save the performance of the function in a file.
size_t p_nbElement
Number of elements the function is computing (supposed to be constant for all the performance tests).
PFunctionTimer & operator=(const PFunctionTimer &other)
Definition of equal operator of PFunctionTimer.
std::vector< double > p_vecEllapsedTimeNs
Ellapsed time of the function in nanoseconds.
virtual ~PFunctionTimer()
Destructor of PFunctionTimer.
PFunctionTimer(size_t nbMeasure=0lu, size_t nbElement=0lu)
Default constructor of PFunctionTimer.
void saveQuantilePerf(std::ofstream &fs, const std::vector< double > &orderPerfNs, const std::vector< double > &orderPerfCy, const std::string &name, size_t valueIndex) const
Save the performance of the given quantile.
size_t p_nbMeasure
Number of performance measures we want on this function.
void getSortedPerf(std::vector< double > &orderPerfNs, std::vector< double > &orderPerfCy) const
Get the ordered performances.
void resize(size_t nbMeasure)
Resize the number of possible measures.
std::vector< double > p_vecEllapsedTimeCycle
Ellapsed time of the function in cycles.
void copyPFunctionTimer(const PFunctionTimer &other)
Copy function of PFunctionTimer.
void initialisationPFunctionTimer(size_t nbMeasure, size_t nbElement)
Initialisation function of the class PFunctionTimer.
void setNbElement(size_t nbElement)
Set the number of elements to consider for the function timing.
size_t getNbElement() const
Get the number of elements computed in the performance test.