RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
Timer.hpp
Go to the documentation of this file.
1 
13 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
14 // Copyright (c) Lawrence Livermore National Security, LLC and other
15 // RAJA Project Developers. See top-level LICENSE and COPYRIGHT
16 // files for dates and other details. No copyright assignment is required
17 // to contribute to RAJA.
18 //
19 // SPDX-License-Identifier: (BSD-3-Clause)
20 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
21 
22 #ifndef RAJA_Timer_HPP
23 #define RAJA_Timer_HPP
24 
25 #include "RAJA/config.hpp"
26 
27 #if defined(RAJA_USE_CALIPER)
28 #include <caliper/Annotation.h>
29 #endif
30 
31 
32 // libstdc++ on BGQ only has gettimeofday for some reason
33 #if defined(__bgq__) && (!defined(_LIBCPP_VERSION))
34 
35 #include <sys/time.h>
36 #include <chrono>
37 
38 namespace RAJA
39 {
50 class BGQTimer
51 {
52 public:
53  using ElapsedType = double;
54 
55 private:
56  using TimeType = timeval;
57  using DurationType = std::chrono::duration<ElapsedType>;
58 
59 public:
60  BGQTimer() : tstart(), tstop(), telapsed(0) {}
61 
62  void start() { gettimeofday(&tstart, 0); }
63 
64  void stop()
65  {
66  gettimeofday(&tstop, 0);
67  auto start = std::chrono::seconds(tstart.tv_sec) +
68  std::chrono::microseconds(tstart.tv_usec);
69  auto stop = std::chrono::seconds(tstop.tv_sec) +
70  std::chrono::microseconds(tstop.tv_usec);
71  telapsed += DurationType(stop - start).count();
72  }
73 
74  ElapsedType elapsed() const { return telapsed; }
75 
76  void reset() { telapsed = 0; }
77 
78 private:
79  TimeType tstart;
80  TimeType tstop;
81  ElapsedType telapsed;
82 };
83 
84 using TimerBase = BGQTimer;
85 } // namespace RAJA
86 
87 
88 #elif defined(RAJA_USE_CHRONO)
89 
90 #include <chrono>
91 
92 namespace RAJA
93 {
103 class ChronoTimer
104 {
105 public:
106  using ElapsedType = double;
107 
108 private:
109  using ClockType = std::chrono::steady_clock;
110  using TimeType = ClockType::time_point;
111  using DurationType = std::chrono::duration<ElapsedType>;
112 
113 public:
114  ChronoTimer() : tstart(ClockType::now()), tstop(ClockType::now()), telapsed(0)
115  {}
116 
117  void start() { tstart = ClockType::now(); }
118 
119  void stop()
120  {
121  tstop = ClockType::now();
122  telapsed +=
123  std::chrono::duration_cast<DurationType>(tstop - tstart).count();
124  }
125 
126  ElapsedType elapsed() const { return telapsed; }
127 
128  void reset() { telapsed = 0; }
129 
130 private:
131  TimeType tstart;
132  TimeType tstop;
133  ElapsedType telapsed;
134 };
135 
136 using TimerBase = ChronoTimer;
137 } // namespace RAJA
138 
139 
140 #elif defined(RAJA_USE_GETTIME)
141 
142 #include <time.h>
143 
144 namespace RAJA
145 {
155 class GettimeTimer
156 {
157 public:
158  using ElapsedType = double;
159 
160 private:
161  using TimeType = timespec;
162 
163 public:
164  GettimeTimer() : telapsed(0), stime_elapsed(0), nstime_elapsed(0) { ; }
165 
166  void start() { clock_gettime(CLOCK_MONOTONIC, &tstart); }
167 
168  void stop()
169  {
170  clock_gettime(CLOCK_MONOTONIC, &tstop);
171  set_elapsed();
172  }
173 
174  ElapsedType elapsed() const { return (stime_elapsed + nstime_elapsed); }
175 
176  void reset()
177  {
178  stime_elapsed = 0;
179  nstime_elapsed = 0;
180  }
181 
182 private:
183  TimeType tstart;
184  TimeType tstop;
185  ElasedType telapsed;
186 
187  ElapsedType stime_elapsed;
188  ElapsedType nstime_elapsed;
189 
190  void set_elapsed()
191  {
192  stime_elapsed += static_cast<ElapsedType>(tstop.tv_sec - tstart.tv_sec);
193  nstime_elapsed +=
194  static_cast<ElapsedType>(tstop.tv_nsec - tstart.tv_nsec) / 1000000000.0;
195  }
196 };
197 
198 using TimerBase = GettimeTimer;
199 } // namespace RAJA
200 
201 #elif defined(RAJA_USE_CLOCK)
202 
203 #include <time.h>
204 
205 namespace RAJA
206 {
207 
217 class ClockTimer
218 {
219 public:
220  using ElapsedType = double;
221 
222 private:
223  using TimeType = clock_t;
224 
225 public:
226  ClockTimer() : telapsed(0) { ; }
227 
228  void start() { tstart = clock(); }
229 
230  void stop()
231  {
232  tstop = clock();
233  set_elapsed();
234  }
235 
236  ElapsedType elapsed() const
237  {
238  return static_cast<ElapsedType>(telapsed) / CLOCKS_PER_SEC;
239  }
240 
241  void reset() { telapsed = 0; }
242 
243 private:
244  TimeType tstart;
245  TimeType tstop;
246  long double telapsed;
247 
248  void set_elapsed() { telapsed += (tstop - tstart); }
249 };
250 
251 using TimerBase = ClockTimer;
252 } // namespace RAJA
253 
254 #else
255 
256 #error RAJA_TIMER is undefined!
257 
258 #endif
259 
260 namespace RAJA
261 {
262 
263 class Timer : public TimerBase
264 {
265 public:
266  using TimerBase::start;
267  using TimerBase::stop;
268 
269 #if defined(RAJA_USE_CALIPER)
270  void start(const char* name) { cali::Annotation(name).begin(); }
271 
272  void stop(const char* name) { cali::Annotation(name).end(); }
273 #else
274  void start(const char*) { start(); }
275 
276  void stop(const char*) { stop(); }
277 #endif
278 };
279 
280 } // namespace RAJA
281 
282 #endif // closing endif for header file include guard
Definition: Timer.hpp:264
void start(const char *)
Definition: Timer.hpp:274
void stop(const char *)
Definition: Timer.hpp:276
Definition: AlignedRangeIndexSetBuilders.cpp:35