QUDA  1.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
timer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef __CUDACC_RTC__
4 
5 namespace quda {
6  // dummy Implementation that can safely be parsed by nvrtc
7  enum QudaProfileType { };
8 
9  class TimeProfile {
10  public:
11  TimeProfile(std::string fname);
12  TimeProfile(std::string fname, bool use_global);
13  void Print();
14  void Start_(const char *func, const char *file, int line, QudaProfileType idx);
15  void Stop_(const char *func, const char *file, int line, QudaProfileType idx);
16  void Reset_(const char *func, const char *file, int line);
17  double Last(QudaProfileType idx);
18  void PrintGlobal();
19  bool isRunning(QudaProfileType idx);
20  };
21 }
22 
23 #else
24 
25 #include <sys/time.h>
26 
27 #ifdef INTERFACE_NVTX
28 #if QUDA_NVTX_VERSION == 3
29 #include "nvtx3/nvToolsExt.h"
30 #else
31 #include "nvToolsExt.h"
32 #endif
33 #endif
34 
35 namespace quda {
36 
42  struct Timer {
44  double time;
45 
47  double last;
48 
50  timeval start;
51 
53  timeval stop;
54 
56  bool running;
57 
59  int count;
60 
61  Timer() : time(0.0), last(0.0), running(false), count(0) { ; }
62 
63  void Start(const char *func, const char *file, int line) {
64  if (running) {
65  printfQuda("ERROR: Cannot start an already running timer (%s:%d in %s())\n", file, line, func);
66  errorQuda("Aborting");
67  }
68  gettimeofday(&start, NULL);
69  running = true;
70  }
71 
72  void Stop(const char *func, const char *file, int line) {
73  if (!running) {
74  printfQuda("ERROR: Cannot stop an unstarted timer (%s:%d in %s())\n", file, line, func);
75  errorQuda("Aborting");
76  }
77  gettimeofday(&stop, NULL);
78 
79  long ds = stop.tv_sec - start.tv_sec;
80  long dus = stop.tv_usec - start.tv_usec;
81  last = ds + 0.000001*dus;
82  time += last;
83  count++;
84 
85  running = false;
86  }
87 
88  double Last() { return last; }
89 
90  void Reset(const char *func, const char *file, int line) {
91  if (running) {
92  printfQuda("ERROR: Cannot reset a started timer (%s:%d in %s())\n", file, line, func);
93  errorQuda("Aborting");
94  }
95  time = 0.0;
96  last = 0.0;
97  count = 0;
98  }
99 
100  };
101 
117  // lower level counters used in the dslash and api profiling
146  };
147 
148 #ifdef INTERFACE_NVTX
149 
150 
151 
152 #define PUSH_RANGE(name,cid) { \
153  int color_id = cid; \
154  color_id = color_id%nvtx_num_colors;\
155  nvtxEventAttributes_t eventAttrib = {0}; \
156  eventAttrib.version = NVTX_VERSION; \
157  eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE; \
158  eventAttrib.colorType = NVTX_COLOR_ARGB; \
159  eventAttrib.color = nvtx_colors[color_id]; \
160  eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII; \
161  eventAttrib.message.ascii = name; \
162  eventAttrib.category = cid;\
163  nvtxRangePushEx(&eventAttrib); \
164 }
165 #define POP_RANGE nvtxRangePop();
166 #else
167 #define PUSH_RANGE(name,cid)
168 #define POP_RANGE
169 #endif
170 
171  class TimeProfile {
172  std::string fname;
173 #ifdef INTERFACE_NVTX
174  static const uint32_t nvtx_colors[];// = { 0x0000ff00, 0x000000ff, 0x00ffff00, 0x00ff00ff, 0x0000ffff, 0x00ff0000, 0x00ffffff };
175  static const int nvtx_num_colors;// = sizeof(nvtx_colors)/sizeof(uint32_t);
176 #endif
178  static std::string pname[];
179 
180  bool switchOff;
182 
183  // global timer
186  static int global_total_level[QUDA_PROFILE_COUNT]; // zero initialize
187 
188  static void StopGlobal(const char *func, const char *file, int line, QudaProfileType idx) {
189 
190  global_total_level[idx]--;
191  if (global_total_level[idx]==0) global_profile[idx].Stop(func,file,line);
192 
193  // switch off total timer if we need to
194  if (global_switchOff[idx]) {
195  global_total_level[idx]--;
196  if (global_total_level[idx]==0) global_profile[idx].Stop(func,file,line);
197  global_switchOff[idx] = false;
198  }
199  }
200 
201  static void StartGlobal(const char *func, const char *file, int line, QudaProfileType idx) {
202  // if total timer isn't running, then start it running
203  if (!global_profile[idx].running) {
204  global_profile[idx].Start(func,file,line);
205  global_total_level[idx]++;
206  global_switchOff[idx] = true;
207  }
208 
209  if (global_total_level[idx]==0) global_profile[idx].Start(func,file,line);
210  global_total_level[idx]++;
211  }
212 
213  public:
214  TimeProfile(std::string fname) : fname(fname), switchOff(false), use_global(true) { ; }
215 
216  TimeProfile(std::string fname, bool use_global) : fname(fname), switchOff(false), use_global(use_global) { ; }
217 
219  void Print();
220 
221  void Start_(const char *func, const char *file, int line, QudaProfileType idx) {
222  // if total timer isn't running, then start it running
223  if (!profile[QUDA_PROFILE_TOTAL].running && idx != QUDA_PROFILE_TOTAL) {
224  profile[QUDA_PROFILE_TOTAL].Start(func,file,line);
225  switchOff = true;
226  }
227 
228  profile[idx].Start(func, file, line);
229  PUSH_RANGE(fname.c_str(),idx)
230  if (use_global) StartGlobal(func,file,line,idx);
231  }
232 
233 
234  void Stop_(const char *func, const char *file, int line, QudaProfileType idx) {
235  profile[idx].Stop(func, file, line);
236  POP_RANGE
237 
238  // switch off total timer if we need to
239  if (switchOff && idx != QUDA_PROFILE_TOTAL) {
240  profile[QUDA_PROFILE_TOTAL].Stop(func,file,line);
241  switchOff = false;
242  }
243  if (use_global) StopGlobal(func,file,line,idx);
244  }
245 
246  void Reset_(const char *func, const char *file, int line) {
247  for (int idx=0; idx<QUDA_PROFILE_COUNT; idx++)
248  profile[idx].Reset(func, file, line);
249  }
250 
251  double Last(QudaProfileType idx) {
252  return profile[idx].last;
253  }
254 
255  static void PrintGlobal();
256 
257  bool isRunning(QudaProfileType idx) { return profile[idx].running; }
258 
259  };
260 
261 } // namespace quda
262 
263 #endif
264 
265 
266 #undef PUSH_RANGE
267 #undef POP_RANGE
268 
269 #define TPSTART(idx) Start_(__func__, __FILE__, __LINE__, idx)
270 #define TPSTOP(idx) Stop_(__func__, __FILE__, __LINE__, idx)
271 #define TPRESET() Reset_(__func__, __FILE__, __LINE__)
272 
void Stop_(const char *func, const char *file, int line, QudaProfileType idx)
Definition: timer.h:234
static std::string pname[]
Definition: timer.h:178
void Start_(const char *func, const char *file, int line, QudaProfileType idx)
Definition: timer.h:221
#define errorQuda(...)
Definition: util_quda.h:121
bool switchOff
Definition: timer.h:180
static void StartGlobal(const char *func, const char *file, int line, QudaProfileType idx)
Definition: timer.h:201
static int global_total_level[QUDA_PROFILE_COUNT]
Definition: timer.h:186
timeval start
Definition: timer.h:50
bool isRunning(QudaProfileType idx)
Definition: timer.h:257
bool running
Definition: timer.h:56
double Last(QudaProfileType idx)
Definition: timer.h:251
#define POP_RANGE
Definition: timer.h:168
Timer profile[QUDA_PROFILE_COUNT]
Definition: timer.h:177
static void StopGlobal(const char *func, const char *file, int line, QudaProfileType idx)
Definition: timer.h:188
void Print()
Definition: timer.cpp:7
void Start(const char *func, const char *file, int line)
Definition: timer.h:63
QudaProfileType
Definition: timer.h:103
void Reset_(const char *func, const char *file, int line)
Definition: timer.h:246
bool use_global
Definition: timer.h:181
static bool global_switchOff[QUDA_PROFILE_COUNT]
Definition: timer.h:185
double Last()
Definition: timer.h:88
int count
Definition: timer.h:59
double time
Definition: timer.h:44
static Timer global_profile[QUDA_PROFILE_COUNT]
Definition: timer.h:184
timeval stop
Definition: timer.h:53
double last
Definition: timer.h:47
#define printfQuda(...)
Definition: util_quda.h:115
std::string fname
Definition: timer.h:172
TimeProfile(std::string fname)
Definition: timer.h:214
Timer()
Definition: timer.h:61
#define PUSH_RANGE(name, cid)
Definition: timer.h:167
static void PrintGlobal()
Definition: timer.cpp:81
void Stop(const char *func, const char *file, int line)
Definition: timer.h:72
void Reset(const char *func, const char *file, int line)
Definition: timer.h:90
TimeProfile(std::string fname, bool use_global)
Definition: timer.h:216