DASH  0.3.0
Timer.h
1 #ifndef DASH__UTIL__TIMER_H_
2 #define DASH__UTIL__TIMER_H_
3 
4 #include <dash/internal/Config.h>
5 
6 // Timestamp interfaces:
7 #include <dash/util/Timestamp.h>
8 #include <dash/util/TimeMeasure.h>
9 
10 #include <climits>
11 
12 // Timestamp implementations:
13 #if defined(DASH__UTIL__TIMER_PAPI)
14 # include <dash/util/internal/TimestampPAPI.h>
15 #else
16 # include <dash/util/internal/TimestampCounterPosix.h>
17 # include <dash/util/internal/TimestampClockPosix.h>
18 #endif
19 
20 #include <dash/internal/Logging.h>
21 
22 namespace dash {
23 namespace util {
24 
25 template<TimeMeasure::MeasureMode TimerType>
26 class Timer;
27 
29 // Specialization for clock-based timer
30 
31 template<>
32 class Timer<TimeMeasure::Clock> {
33 public:
34  typedef Timestamp::counter_t timestamp_t;
35 
36 private:
37  typedef Timer self_t;
38 
39 private:
40  timestamp_t timestampStart;
41 
42 private:
43 #if defined(DASH__UTIL__TIMER_PAPI)
44  // PAPI support, use measurements from PAPI
45  typedef dash::util::internal::TimestampPAPI<TimeMeasure::Clock>
46  Timestamp_t;
47 #elif defined(DASH__UTIL__TIMER_POSIX)
48  // POSIX platform
49  typedef dash::util::internal::TimestampClockPosix
50  Timestamp_t;
51 #else
52  // No PAPI, no POSIX
53  #error "dash::util::Timer requires POSIX platform or PAPI"
54 #endif
55 
56 public:
57  typedef Timestamp_t timestamp_type;
58  typedef timestamp_t timestamp;
59 
60 public:
61  inline Timer()
62  : timestampStart(Timer::Now())
63  {
64  }
65 
66  inline Timer(const self_t& other) = default;
67 
68  inline Timer & operator=(const self_t & other)
69  {
70  if (this != &other) {
71  timestampStart = other.timestampStart;
72  }
73  return *this;
74  }
75 
79  inline double Elapsed() const
80  {
81  timestamp_t now;
82  Timestamp_t timestamp;
83  now = timestamp.Value();
84  return (static_cast<double>(now - timestampStart) *
85  static_cast<double>(Timestamp_t::FrequencyPrescale())) /
86  static_cast<double>(Timestamp_t::FrequencyScaling());
87  }
88 
92  inline const timestamp_t & Start() const
93  {
94  return timestampStart;
95  }
96 
100  inline static double ElapsedSince(timestamp_t timestamp)
101  {
102  Timestamp_t now;
103  return (static_cast<double>(now.Value() - timestamp) *
104  static_cast<double>(Timestamp_t::FrequencyPrescale())) /
105  static_cast<double>(Timestamp_t::FrequencyScaling());
106  }
107 
111  inline static timestamp_t Now()
112  {
113  Timestamp_t timestamp;
114  return timestamp.Value();
115  }
116 
120  inline static double FromInterval(
121  const timestamp_t & start,
122  const timestamp_t & end)
123  {
124  return (static_cast<double>(end - start) *
125  static_cast<double>(Timestamp_t::FrequencyPrescale())) /
126  static_cast<double>(Timestamp_t::FrequencyScaling());
127  }
128 
132  inline static double FromInterval(
133  const double & start,
134  const double & end)
135  {
136  return ((end - start) *
137  Timestamp_t::FrequencyPrescale() /
138  Timestamp_t::FrequencyScaling());
139  }
140 
141  inline static void Calibrate(
142  unsigned int freq = 0)
143  {
144  DASH_LOG_DEBUG("Timer<Clock>::Calibrate(freq)", freq);
145  Timestamp_t::Calibrate(freq);
146  }
147 
148  inline static const char * TimerName()
149  {
150  return Timestamp_t::TimerName();
151  }
152 
153  inline static Timestamp::counter_t TimestampInfinity()
154  {
155  return Timestamp_t::TimestampInfinity();
156  }
157 
158  inline static Timestamp::counter_t TimestampNegInfinity() {
159  return Timestamp_t::TimestampNegInfinity();
160  }
161 
162  inline static double FrequencyScaling()
163  {
164  return Timestamp_t::FrequencyScaling();
165  }
166 };
167 
169 // Specialization for counter-based timer
170 
171 template<>
172 class Timer<TimeMeasure::Counter> {
173 public:
174  typedef Timestamp::counter_t timestamp_t;
175 
176 private:
177  typedef Timer self_t;
178 
179 private:
180  timestamp_t timestampStart;
181 
182 private:
183 #if defined(DASH__UTIL__TIMER_PAPI)
184  // PAPI support, use measurements from PAPI
185  typedef dash::util::internal::TimestampPAPI<TimeMeasure::Counter>
186  Timestamp_t;
187 #elif defined(DASH__UTIL__TIMER_POSIX)
188  // POSIX platform
189  typedef dash::util::internal::TimestampCounterPosix
190  Timestamp_t;
191 #else
192  // No PAPI, no POSIX
193  #error "dash::util::Timer requires POSIX platform or PAPI"
194 #endif
195 
196 public:
197  typedef Timestamp_t timestamp_type;
198  typedef timestamp_t timestamp;
199 
200 public:
201  inline Timer()
202  : timestampStart(Timer::Now())
203  { }
204 
205  inline Timer(const self_t& other) = default;
206 
207  inline Timer & operator=(const self_t & other)
208  {
209  if (this != &other) {
210  timestampStart = other.timestampStart;
211  }
212  return *this;
213  }
214 
218  inline double Elapsed() const
219  {
220  timestamp_t now;
221  Timestamp_t timestamp;
222  now = timestamp.Value();
223  return (static_cast<double>(now - timestampStart) *
224  static_cast<double>(Timestamp_t::FrequencyPrescale())) /
225  static_cast<double>(Timestamp_t::FrequencyScaling());
226  }
227 
231  inline const timestamp_t & Start() const
232  {
233  return timestampStart;
234  }
235 
239  inline static double ElapsedSince(timestamp_t timestamp)
240  {
241  Timestamp_t now;
242  return (static_cast<double>(now.Value() - timestamp) *
243  static_cast<double>(Timestamp_t::FrequencyPrescale())) /
244  static_cast<double>(Timestamp_t::FrequencyScaling());
245  }
246 
250  inline static timestamp_t Now()
251  {
252  Timestamp_t timestamp;
253  return timestamp.Value();
254  }
255 
259  inline static double FromInterval(
260  const timestamp_t & start,
261  const timestamp_t & end)
262  {
263  return (static_cast<double>(end - start) *
264  static_cast<double>(Timestamp_t::FrequencyPrescale())) /
265  static_cast<double>(Timestamp_t::FrequencyScaling());
266  }
267 
271  inline static double FromInterval(
272  const double & start,
273  const double & end)
274  {
275  return ((end - start) *
276  Timestamp_t::FrequencyPrescale() /
277  Timestamp_t::FrequencyScaling());
278  }
279 
280  inline static void Calibrate(
281  unsigned int freq = 0)
282  {
283  DASH_LOG_DEBUG("Timer<Counter>::Calibrate(freq)", freq);
284  Timestamp_t::Calibrate(freq);
285  }
286 
287  inline static const char * TimerName()
288  {
289  return Timestamp_t::TimerName();
290  }
291 
292  inline static Timestamp::counter_t TimestampInfinity()
293  {
294  return Timestamp_t::TimestampInfinity();
295  }
296 
297  inline static Timestamp::counter_t TimestampNegInfinity()
298  {
299  return Timestamp_t::TimestampNegInfinity();
300  }
301 
302  inline static double FrequencyScaling()
303  {
304  return Timestamp_t::FrequencyScaling();
305  }
306 };
307 
308 } // namespace util
309 } // namespace dash
310 
311 #endif // DASH__UTIL__TIMER_H_
constexpr auto end(RangeType &&range) -> decltype(std::forward< RangeType >(range).end())
Definition: Range.h:98
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
const timestamp_t & Start() const
Returns timestamp from instantiation of this Timer.
Definition: Timer.h:231
static double ElapsedSince(timestamp_t timestamp)
Microseconds elapsed since given timestamp.
Definition: Timer.h:239
static double FromInterval(const timestamp_t &start, const timestamp_t &end)
Convert interval of two timestamp values to mircoseconds.
Definition: Timer.h:259
static timestamp_t Now()
Produces current timestamp.
Definition: Timer.h:111
static double ElapsedSince(timestamp_t timestamp)
Microseconds elapsed since given timestamp.
Definition: Timer.h:100
const timestamp_t & Start() const
Returns timestamp from instantiation of this Timer.
Definition: Timer.h:92
static double FromInterval(const timestamp_t &start, const timestamp_t &end)
Convert interval of two timestamp values to microseconds.
Definition: Timer.h:120
double Elapsed() const
Microseconds elapsed since instantiation of this Timer object.
Definition: Timer.h:79
static double FromInterval(const double &start, const double &end)
Convert interval of two timestamp values to mircoseconds.
Definition: Timer.h:132
double Elapsed() const
Microseconds elapsed since instantiation of this Timer object.
Definition: Timer.h:218
static timestamp_t Now()
Produces current timestamp.
Definition: Timer.h:250
static double FromInterval(const double &start, const double &end)
Convert interval of two timestamp values to mircoseconds.
Definition: Timer.h:271