Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Timer.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
20 #ifndef GALOIS_TIMER_H
21 #define GALOIS_TIMER_H
22 
23 #include <chrono>
24 
25 #include "galois/config.h"
26 #include "galois/gstl.h"
27 
28 namespace galois {
29 
31 class Timer {
32  typedef std::chrono::steady_clock clockTy;
33  // typedef std::chrono::high_resolution_clock clockTy;
34  std::chrono::time_point<clockTy> startT, stopT;
35 
36 public:
37  void start();
38  void stop();
39  uint64_t get() const;
40  uint64_t get_usec() const;
41 };
42 
46  Timer ltimer;
47  uint64_t acc;
48 
49 public:
51 
52  void start();
54  void stop();
55  uint64_t get() const;
56  uint64_t get_usec() const;
58  TimeAccumulator& operator+=(const Timer& rhs);
59 };
60 
63 class StatTimer : public TimeAccumulator {
64  gstl::Str name_;
65  gstl::Str region_;
66  bool valid_;
67 
68 public:
69  StatTimer(const char* name, const char* region);
70 
71  StatTimer(const char* const n) : StatTimer(n, nullptr) {}
72 
73  StatTimer() : StatTimer(nullptr, nullptr) {}
74 
75  StatTimer(const StatTimer&) = delete;
76  StatTimer(StatTimer&&) = delete;
77  StatTimer& operator=(const StatTimer&) = delete;
78  StatTimer& operator=(StatTimer&&) = delete;
79 
80  ~StatTimer();
81 
82  void start();
83  void stop();
84  uint64_t get_usec() const;
85 };
86 
87 template <bool Enable>
88 class CondStatTimer : public StatTimer {
89 public:
90  CondStatTimer(const char* const n, const char* region)
91  : StatTimer(n, region) {}
92 
93  CondStatTimer(const char* region) : CondStatTimer("Time", region) {}
94 };
95 
96 template <>
97 class CondStatTimer<false> {
98 public:
99  CondStatTimer(const char*) {}
100  CondStatTimer(const char* const, const char*) {}
101 
102  void start() const {}
103  void stop() const {}
104  uint64_t get_usec() const { return 0; }
105 };
106 
107 template <typename F>
108 void timeThis(const F& f, const char* const name) {
109  StatTimer t("Time", name);
110 
111  t.start();
112 
113  f();
114 
115  t.stop();
116 }
117 
118 } // end namespace galois
119 #endif
uint64_t get_usec() const
Definition: Timer.cpp:49
~StatTimer()
Definition: Timer.cpp:71
TimeAccumulator & operator+=(const TimeAccumulator &rhs)
Definition: Timer.cpp:51
A simple timer.
Definition: Timer.h:31
void start()
Definition: Timer.cpp:25
CondStatTimer(const char *)
Definition: Timer.h:99
CondStatTimer(const char *const, const char *)
Definition: Timer.h:100
CondStatTimer(const char *region)
Definition: Timer.h:93
CondStatTimer(const char *const n, const char *region)
Definition: Timer.h:90
StatTimer()
Definition: Timer.h:73
TimeAccumulator()
Definition: Timer.cpp:39
void start() const
Definition: Timer.h:102
void timeThis(const F &f, const char *const name)
Definition: Timer.h:108
uint64_t get_usec() const
Definition: Timer.h:104
void start()
Definition: Timer.cpp:41
A multi-start time accumulator.
Definition: Timer.h:45
void start()
Definition: Timer.cpp:82
uint64_t get_usec() const
Definition: Timer.cpp:92
StatTimer & operator=(const StatTimer &)=delete
void stop()
Definition: Timer.cpp:27
Definition: Timer.h:88
StatTimer(const char *const n)
Definition: Timer.h:71
void stop()
adds the current timed interval to the total
Definition: Timer.cpp:43
void stop() const
Definition: Timer.h:103
void stop()
Definition: Timer.cpp:87
Galois Timer that automatically reports stats upon destruction Provides statistic interface around ti...
Definition: Timer.h:63
uint64_t get_usec() const
Definition: Timer.cpp:34
std::basic_string< char, std::char_traits< char >, Pow2Alloc< char >> Str
Definition: gstl.h:75