PandaRoot
L1Timer.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
13 #ifndef _L1Timer_H
14 #define _L1Timer_H
15 
16 /*
17  *=====================================================
18  *
19  * Authors: I.Kulakov
20  *
21  */
22 
23 #include "TStopwatch.h"
24 #include <iostream>
25 #include <iomanip>
26 #include <string>
27 #include <vector>
28 #include <map>
29 
30 using std::cout;
31 using std::endl;
32 using std::ios;
33 using std::map;
34 using std::setw;
35 using std::string;
36 using std::vector;
37 
38 class TimerInfo {
39  public:
40  TimerInfo() : fName(""), fReal(0), fCpu(0){};
41  TimerInfo(const string &name) : fName(name), fReal(0), fCpu(0){};
42 
43  void Clear()
44  {
45  fReal = 0;
46  fCpu = 0;
47  }
48 
49  void operator=(TStopwatch &sw)
50  {
51  fReal = sw.RealTime();
52  fCpu = sw.CpuTime();
53  };
54  void operator+=(TStopwatch &sw)
55  {
56  fReal += sw.RealTime();
57  fCpu += sw.CpuTime();
58  };
59  void operator+=(const TimerInfo &t)
60  {
61  fReal += t.fReal;
62  fCpu += t.fCpu;
63  }
64  TimerInfo operator/(const float f) const
65  {
66  TimerInfo r;
67  r.fName = fName;
68  r.fReal = fReal / f;
69  r.fCpu = fCpu / f;
70  return r;
71  }
72 
73  // void Print(){ cout << fReal << "/" << fCpu; };
74  void PrintReal() { cout << fReal; };
75  float Real() { return fReal; };
76  string &Name() { return fName; };
77 
78  private:
79  string fName;
80  float fReal, fCpu;
81 };
82 
84  public:
85  void Clear()
86  {
87  for (unsigned int i = 0; i < fTIs.size(); i++)
88  fTIs[i].Clear();
89  };
90 
91  void Add(string name)
92  {
93  fNameToI[name] = fTIs.size();
94  fTIs.push_back(TimerInfo(name));
95  };
96  TimerInfo &operator[](string name) { return fTIs[fNameToI[name]]; };
97  TimerInfo &operator[](int i) { return fTIs[i]; };
99  {
100  for (unsigned int i = 0; i < fTIs.size(); ++i)
101  fTIs[i] += t[i];
102  }
104  {
106  r.fNameToI = fNameToI;
107  r.fTIs.resize(fTIs.size());
108  for (unsigned int i = 0; i < fTIs.size(); ++i) {
109  r.fTIs[i] = fTIs[i] / f;
110  }
111  return r;
112  }
113 
114  void PrintReal(int f = 0)
115  {
116  if (f) {
117  PrintNames();
118  cout << endl;
119  }
120  fTIs[0].PrintReal();
121  for (unsigned int i = 1; i < fTIs.size(); ++i) {
122  cout << " | " << setw(fTIs[i].Name().size());
123  fTIs[i].PrintReal();
124  }
125  if (f)
126  cout << endl;
127  };
128  void PrintNames()
129  {
130  cout << fTIs[0].Name();
131  for (unsigned int i = 1; i < fTIs.size(); ++i) {
132  cout << " | " << fTIs[i].Name();
133  }
134  };
135 
136  private:
137  map<string, int> fNameToI;
138  vector<TimerInfo> fTIs;
139 };
140 
142  public:
144  L1CATFTimerInfo(int n) { fTIIs.resize(n); };
145  void SetNIter(int n) { fTIIs.resize(n); };
146 
147  void Clear()
148  {
149  for (unsigned int i = 0; i < fTIIs.size(); i++)
150  fTIIs[i].Clear();
151  fTIAll.Clear();
152  };
153 
154  void Add(string name)
155  {
156  for (unsigned int i = 0; i < fTIIs.size(); ++i)
157  fTIIs[i].Add(name);
158  fTIAll.Add(name);
159  }; // use after setniter
160  L1CATFIterTimerInfo &GetTimerAll() { return fTIAll; };
161  L1CATFIterTimerInfo &operator[](int i) { return fTIIs[i]; };
163  {
164  for (unsigned int i = 0; i < fTIIs.size(); ++i)
165  fTIIs[i] += t[i];
166  fTIAll += t.GetAllInfo();
167  }
169  {
170  L1CATFTimerInfo r;
171  r.fTIAll = fTIAll / f;
172  r.SetNIter(fTIIs.size());
173  for (unsigned int i = 0; i < fTIIs.size(); ++i) {
174  r.fTIIs[i] = fTIIs[i] / f;
175  }
176  return r;
177  }
178 
179  void Calc()
180  {
181  fTIAll = fTIIs[0];
182  for (unsigned int i = 1; i < fTIIs.size(); ++i)
183  fTIAll += fTIIs[i];
184  }
185 
186  L1CATFIterTimerInfo &GetAllInfo() { return fTIAll; };
187  void PrintReal()
188  {
189  // save original cout flags
190  std::ios_base::fmtflags coutFlags = cout.flags();
191 
192  int old_precision = cout.precision(1);
193  cout.setf(ios::fixed);
194  cout << " stage "
195  << " : ";
196  fTIAll.PrintNames();
197  cout << endl;
198  for (unsigned int i = 0; i < fTIIs.size(); ++i) {
199  cout << " iter " << i << " : ";
200  fTIIs[i].PrintReal();
201  cout << endl;
202  }
203  cout << " all "
204  << " : ";
205  fTIAll.PrintReal();
206  cout << endl;
207 
208  // restore original cout flags
209  cout.flags(coutFlags);
210  cout.precision(old_precision);
211  };
212 
213  private:
214  vector<L1CATFIterTimerInfo> fTIIs;
215  L1CATFIterTimerInfo fTIAll;
216 };
217 
218 #endif
L1CATFIterTimerInfo & GetTimerAll()
Definition: L1Timer.h:160
TimerInfo operator/(const float f) const
Definition: L1Timer.h:64
L1CATFTimerInfo(int n)
Definition: L1Timer.h:144
void SetNIter(int n)
Definition: L1Timer.h:145
L1CATFIterTimerInfo & GetAllInfo()
Definition: L1Timer.h:186
void Clear()
Definition: L1Timer.h:147
TimerInfo(const string &name)
Definition: L1Timer.h:41
TimerInfo & operator[](string name)
Definition: L1Timer.h:96
void operator=(TStopwatch &sw)
Definition: L1Timer.h:49
unsigned int i
Definition: P4_F32vec4.h:33
string & Name()
Definition: L1Timer.h:76
L1CATFTimerInfo operator/(float f)
Definition: L1Timer.h:168
void PrintReal()
Definition: L1Timer.h:74
void Calc()
Definition: L1Timer.h:179
void Clear()
Definition: L1Timer.h:43
void Add(string name)
Definition: L1Timer.h:91
L1CATFIterTimerInfo & operator[](int i)
Definition: L1Timer.h:161
float Real()
Definition: L1Timer.h:75
L1CATFIterTimerInfo operator/(float f)
Definition: L1Timer.h:103
void operator+=(TStopwatch &sw)
Definition: L1Timer.h:54
TimerInfo()
Definition: L1Timer.h:40
void operator+=(const TimerInfo &t)
Definition: L1Timer.h:59
void PrintReal(int f=0)
Definition: L1Timer.h:114
float f
Definition: P4_F32vec4.h:32
void PrintReal()
Definition: L1Timer.h:187
TimerInfo & operator[](int i)
Definition: L1Timer.h:97
void operator+=(L1CATFTimerInfo &t)
Definition: L1Timer.h:162
void operator+=(L1CATFIterTimerInfo &t)
Definition: L1Timer.h:98
void Add(string name)
Definition: L1Timer.h:154