VPTissue Reference Manual
IndividualRecords.h
Go to the documentation of this file.
1 #ifndef UTIL_CLOCK_MAN_INDIVIDUAL_RECORDS_H_INCLUDED
2 #define UTIL_CLOCK_MAN_INDIVIDUAL_RECORDS_H_INCLUDED
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
22 #include "Utils.h"
23 
24 #include <chrono>
25 #include <cmath>
26 #include <iostream>
27 #include <iomanip>
28 #include <list>
29 #include <map>
30 #include <numeric>
31 #include <string>
32 #include <vector>
33 
34 namespace SimPT_Sim {
35 namespace ClockMan {
36 
42 template<typename T = std::chrono::seconds>
44 {
45 public:
46  using Duration = T;
47 
49  void Clear()
50  {
51  m_map.clear();
52  }
53 
55  unsigned int GetCount(const std::string& name) const
56  {
57  return IsPresent(name) ? (m_map.find(name)->second).size() : 0;
58  }
59 
61  Duration GetCumulative(const std::string& name) const
62  {
63  Duration cumul = T::duration::zero();
64  if (IsPresent(name)) {
65  std::vector<Duration> const& v = m_map.find(name)->second;
66  cumul = std::accumulate(v.begin(), v.end(), T::duration::zero());
67  }
68  return cumul;
69  }
70 
72  Duration GetMean(const std::string& name) const
73  {
74  Duration cumul = T::duration::zero();
75  unsigned int const count = GetCount(name);
76  if (count > 0U) {
77  std::vector<Duration> const& v = m_map.find(name)->second;
78  cumul = std::accumulate(v.begin(), v.end(), T::duration::zero());
79  cumul /= count;
80  }
81  return cumul;
82  }
83 
85  Duration GetMinimum(const std::string& name) const
86  {
87  Duration minimum = T::duration::zero();
88  if (GetCount(name) > 0U) {
89  std::vector<Duration> const& v = m_map.find(name)->second;
90  if (v.size() > 0) {
91  minimum = v[0];
92  for (auto const& el : v) {
93  minimum = ( (minimum <= el) ? minimum : el );
94  }
95  }
96  }
97  return minimum;
98  }
99 
101  std::list<std::string> GetNames() const
102  {
103  std::list<std::string> l;
104  for (auto const& p : m_map) {
105  l.push_back(p.first);
106  }
107  return l;
108  }
109 
111  std::vector<Duration> GetRecord(const std::string& name) const
112  {
113  std::vector<Duration> v;
114  if (IsPresent(name)) {
115  v = m_map.find(name)->second;
116  }
117  return v;
118  }
119 
121  template <typename U = std::chrono::seconds>
123  {
125  for (auto const& p : m_map) {
126  for (auto const& v : p.second) {
127  rec.Record(p.first, std::chrono::duration_cast<U>(v));
128  }
129  }
130  return rec;
131  }
132 
135  {
136  return *this;
137  }
138 
140  Duration GetStandardDeviation(const std::string& name) const
141  {
142  Duration deviation = T::duration::zero();
143  if (IsPresent(name)) {
144  std::vector<Duration> const& v = m_map.find(name)->second;
145  Duration const mean = GetMean(name);
146  typename Duration::rep cov = 0;
147  for (auto const& e : v) {
148  typename Duration::rep const x = (e-mean).count();
149  cov += x * x;
150  }
151  typename Duration::rep const x = std::sqrt(cov);
152  deviation = Duration(x);
153  }
154  return deviation;
155  }
156 
158  bool IsPresent(const std::string& name) const
159  {
160  return (m_map.find(name) != m_map.end());
161  }
162 
164  template<typename U>
165  void Merge(const IndividualRecords<U>& extra)
166  {
167  for (auto const& name : extra.GetNames()) {
168  for (auto const& elem : extra.GetRecord(name)) {
169  Record(name, std::chrono::duration_cast<T>(elem));
170  }
171  }
172  }
173 
176  {
177  for (auto const& name : extra.GetNames()) {
178  if(IsPresent(name)) {
179  m_map[name].insert(m_map[name].end(),
180  extra.m_map.at(name).begin(), extra.m_map.at(name).end());
181  } else {
182  m_map[name] = extra.m_map.at(name);
183  }
184  }
185  }
186 
188  template<typename R, typename P>
189  void Record(const std::string& name, const std::chrono::duration<R, P>& duration)
190  {
191  m_map[name].push_back(std::chrono::duration_cast<T>(duration));
192  }
193 
194 private:
195  std::map<std::string, std::vector<Duration>> m_map;
196 };
197 
201 inline std::ostream& operator<<(std::ostream& os,
203 {
204  using namespace std;
205  using namespace std::chrono;
206 
207  os << right << "timings: " << endl
208  << setw(14) << "name" << " | "
209  << setw(25) << "(hh:mm:ss: ms: us) | (ms) | "
210  << setw(7) << "count" << " | "
211  << setw(25) << "(hh:mm:ss: ms: us) | (ms) | " << endl
212  << string(80, '-') << endl;
213 
214  auto name_list = dr.GetNames();
215  name_list.sort();
216  for (auto const& name : name_list) {
217  auto const count = dr.GetCount(name);
218  auto const cumul_val = dr.GetCumulative(name);
219  auto const cumul = duration_cast<microseconds>(cumul_val);
220  auto const avg_val = (count != 0) ? (cumul_val / count) : microseconds::zero();
221  auto const avg = duration_cast<microseconds>(avg_val);
222 
223  os << right
224  << setw(14) << name << " | "
225  << setw(18) << Utils::ToColonString(cumul) << " | "
226  << setw(8) << scientific << setprecision(4) << cumul.count() << " | "
227  << setw(7) << count << " | "
228  << setw(18) << Utils::ToColonString(avg) << " | "
229  << setw(8) << scientific << setprecision(4) << avg.count() << " | "<< endl;
230  }
231  return os;
232 }
233 
237 inline std::ostream& operator<<(std::ostream& os,
239 {
240  using namespace std;
241  using namespace std::chrono;
242 
243  os << right << "timings: " << endl
244  << setw(14) << "name" << " | "
245  << setw(22) << "(hh:mm:ss: ms) | (ms) | "
246  << setw(7) << "count" << " | "
247  << setw(22) << "(hh:mm:ss: ms) | (ms) | " << endl
248  << string(80, '-') << endl;
249 
250  auto name_list = dr.GetNames();
251  name_list.sort();
252  for (auto const& name : name_list) {
253  auto const count = dr.GetCount(name);
254  auto const cumul_val = dr.GetCumulative(name);
255  auto const cumul = duration_cast<milliseconds>(cumul_val);
256  auto const avg_val = (count != 0) ? (cumul_val / count) : milliseconds::zero();
257  auto const avg = duration_cast<milliseconds>(avg_val);
258 
259  os << right
260  << setw(14) << name << " | "
261  << setw(14) << Utils::ToColonString(cumul) << " | "
262  << setw(8) << scientific << setprecision(4) << cumul.count() << " | "
263  << setw(7) << count << " | "
264  << setw(14) << Utils::ToColonString(avg) << " | "
265  << setw(8) << scientific << setprecision(4) << avg.count() << " | "<< endl;
266  }
267  return os;
268 }
269 
273 inline std::ostream& operator<<(std::ostream& os,
275 {
276  using namespace std;
277  using namespace std::chrono;
278 
279  os << right << "timings: " << endl
280  << setw(14) << "name" << " | "
281  << setw(20) << " (hh:mm:ss) | (s) | "
282  << setw(7) << "count" << " | "
283  << setw(20) << " (hh:mm:ss) | (s) | " << endl
284  << string(76, '-') << endl;
285 
286  auto name_list = dr.GetNames();
287  name_list.sort();
288  for (auto const& name : name_list) {
289  auto const count = dr.GetCount(name);
290  auto const cumul_val = dr.GetCumulative(name);
291  auto const cumul = duration_cast<seconds>(cumul_val);
292  auto const avg_val = (count != 0) ? (cumul_val / count) : seconds::zero();
293  auto const avg = duration_cast<seconds>(avg_val);
294 
295  os << right
296  << setw(14) << name << " | "
297  << setw(12) << Utils::ToColonString(cumul) << " | "
298  << setw(7) << scientific << setprecision(4) << cumul.count() << " | "
299  << setw(7) << count << " | "
300  << setw(12) << Utils::ToColonString(avg) << " | "
301  << setw(7) << scientific << setprecision(4) << avg.count() << " | "<< endl;
302  }
303  return os;
304 }
305 
309 template<typename T>
310 inline std::ostream& operator<<(std::ostream& os, IndividualRecords<T> const& dr)
311 {
312  using namespace std;
313  using namespace std::chrono;
314 
315  os << right << "duration records: name | mean | stddev" << endl;
316 
317  auto name_list = dr.GetNames();
318  name_list.sort();
319  for (auto const& name : name_list) {
320  auto const mean = dr.GetMean(name);
321  auto const stddev = dr.GetStandardDeviation(name);
322 
323  os << right
324  << setw(15) << name << " | "
325  << setw(15) << Utils::ToColonString(mean)
326  << setw(10) << scientific << setprecision(4) << mean.count() << " | "
327  << setw(15) << Utils::ToColonString(stddev)
328  << setw(10) << scientific << setprecision(4) << stddev.count() << endl;
329  }
330  return os;
331 }
332 
333 } // namespace
334 } // namespace
335 
336 #endif // end-of-include-guard
STL namespace.
IndividualRecords< Duration > GetRecords() const
Return records for all names in durations Duration (so no casting).
Interface for TimeKeeper::Utils.
std::list< std::string > GetNames() const
Return list of names.
static std::string ToColonString(std::chrono::seconds d)
Procude string in hh:mm:ss format.
Definition: Utils.h:42
Duration GetMinimum(const std::string &name) const
Return minimum time for records with name.
std::vector< Duration > GetRecord(const std::string &name) const
Return records associated with given name.
Namespace for the core simulator.
IndividualRecords< U > GetRecords() const
Return records for all names in durations U (casting if required).
Duration GetStandardDeviation(const std::string &name) const
Return standard deviation for record with given name.
void Merge(const IndividualRecords< Duration > &extra)
Merge an extra set of records of same Duration (no casting).
void Clear()
Clear the entire set of records.
void Record(const std::string &name, const std::chrono::duration< R, P > &duration)
Record the duration for the given name.
Duration GetMean(const std::string &name) const
Return cumulative time for records with name.
unsigned int GetCount(const std::string &name) const
Return count for records with name.
bool IsPresent(const std::string &name) const
Return whether there are records associated with a given name.
Duration GetCumulative(const std::string &name) const
Return cumulative time for records with name.
std::ostream & operator<<(std::ostream &os, CumulativeRecords< std::chrono::nanoseconds > const &dr)
Nicely formated output with nanoseconds.
void Merge(const IndividualRecords< U > &extra)
Merge an extra set of records (casting durations if required).
Utility class to record durations.