OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
IpplTimings.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef IPPL_TIMINGS_H
12 #define IPPL_TIMINGS_H
13 
14 /*************************************************************************
15  * IpplTimings - a simple singleton class which lets the user create and
16  * timers that can be printed out at the end of the program.
17  *
18  * General usage
19  * 1) create a timer:
20  * IpplTimings::TimerRef val = IpplTimings::getTimer("timer name");
21  * This will either create a new one, or return a ref to an existing one
22  *
23  * 2) start a timer:
24  * IpplTimings::startTimer(val);
25  * This will start the referenced timer running. If it is already running,
26  * it will not change anything.
27  *
28  * 3) stop a timer:
29  * IpplTimings::stopTimer(val);
30  * This will stop the timer, assuming it was running, and add in the
31  * time to the accumulating time for that timer.
32  *
33  * 4) print out the results:
34  * IpplTimings::print();
35  *
36  *************************************************************************/
37 
38 // include files
39 #include "Utility/Timer.h"
40 #include "Utility/my_auto_ptr.h"
41 
42 #include <string>
43 #include <vector>
44 #include <map>
45 #include <limits>
46 #include <stack>
47 
48 #ifdef TIMERDEBUG
49 #include <exception>
50 #endif
51 
52 // a simple class used to store timer values
54 {
55 public:
56  // typedef for reference to a timer
57  typedef unsigned int TimerRef;
58 
59  // constructor
60  IpplTimerInfo() : name(""), cpuTime(0.0), wallTime(0.0), indx(std::numeric_limits<TimerRef>::max()) {
61  clear();
62  }
63 
64  // destructor
66 
67  // timer operations
68  void start() {
69  if (!running) {
70  running = true;
71  t.stop();
72  t.clear();
73  t.start();
74  }
75 #ifdef TIMERDEBUG
76  else {
77  throw std::runtime_error("Timer '" + name + "' already running");
78  }
79 #endif
80  }
81 
82  void stop() {
83  if (running) {
84  t.stop();
85  running = false;
86  cpuTime += t.cpu_time();
87  wallTime += t.clock_time();
88  }
89 #ifdef TIMERDEBUG
90  else {
91  throw std::runtime_error("Timer '" + name + "' already idling");
92  }
93 #endif
94  }
95 
96  void clear() {
97  t.stop();
98  t.clear();
99  running = false;
100  }
101 
102  // the IPPL timer that this object manages
104 
105  // the name of this timer
106  std::string name;
107 
108  // the accumulated time
109  double cpuTime;
110  double wallTime;
111 
112  // is the timer turned on right now?
113  bool running;
114 
115  // an index value for this timer
117 };
118 
119 struct Timing
120 {
121  // typedef for reference to a timer
122  typedef unsigned int TimerRef;
123 
124  // a typedef for the timer information object
126 
127  // Default constructor
128  Timing();
129 
130  // Destructor - clear out the existing timers
131  ~Timing();
132 
133  // create a timer, or get one that already exists
134  TimerRef getTimer(const char *);
135 
136  // start a timer
137  void startTimer(TimerRef);
138 
139  // stop a timer, and accumulate it's values
140  void stopTimer(TimerRef);
141 
142  // clear a timer, by turning it off and throwing away its time
143  void clearTimer(TimerRef);
144 
145  // return a TimerInfo struct by asking for the name
146  TimerInfo *infoTimer(const char *nm) {
147  return TimerMap[std::string(nm)];
148  }
149 
150  // print the results to standard out
151  void print();
152 
153  // print the results to a file
154  void print(const std::string &fn,
155  const std::map<std::string, unsigned int> &problemSize);
156 
157 
158  // type of storage for list of TimerInfo
159  typedef std::vector<my_auto_ptr<TimerInfo> > TimerList_t;
160  typedef std::map<std::string, TimerInfo *> TimerMap_t;
161 
162 private:
163  // a list of timer info structs
165 
166  // a map of timers, keyed by string
168 };
169 
170 
171 
173 {
174 public:
175  // typedef for reference to a timer
177 
178  // a typedef for the timer information object
180 
181  // create a timer, or get one that already exists
182  static TimerRef getTimer(const char * nm) {
183  return instance->getTimer(nm);
184  }
185 
186  // start a timer
187  static void startTimer(TimerRef t) {
188  instance->startTimer(t);
189  }
190 
191  // stop a timer, and accumulate it's values
192  static void stopTimer(TimerRef t) {
193  instance->stopTimer(t);
194  }
195 
196  // clear a timer, by turning it off and throwing away its time
197  static void clearTimer(TimerRef t) {
198  instance->clearTimer(t);
199  }
200 
201  // return a TimerInfo struct by asking for the name
202  static TimerInfo *infoTimer(const char *nm) {
203  return instance->infoTimer(nm);
204  }
205 
206  // print the results to standard out
207  static void print() {
208  instance->print();
209  }
210 
211  // print the results to a file
212  static void print(std::string fn,
213  const std::map<std::string, unsigned int> &problemSize = std::map<std::string, unsigned int>()) {
214  instance->print(fn, problemSize);
215  }
216 
217  static void stash();
218  static void pop();
219 
220 private:
221  // type of storage for list of TimerInfo
224 
225  // Default constructor
226  IpplTimings();
227 
228  // Destructor - clear out the existing timers
229  ~IpplTimings();
230 
231 
232  static Timing *instance;
233  static std::stack<Timing*> stashedInstance;
234 };
235 
236 #endif
237 
238 /***************************************************************************
239  * $RCSfile: IpplTimings.h,v $ $Author: adelmann $
240  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
241  ***************************************************************************/
242 
243 /***************************************************************************
244  * $RCSfile: addheaderfooter,v $ $Author: adelmann $
245  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:17 $
246  * IPPL_VERSION_ID: $Id: addheaderfooter,v 1.1.1.1 2003/01/23 07:40:17 adelmann Exp $
247  ***************************************************************************/
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:84
void clear()
Definition: IpplTimings.h:96
std::string name
Definition: IpplTimings.h:106
TimerRef indx
Definition: IpplTimings.h:116
double cpuTime
Definition: IpplTimings.h:109
unsigned int TimerRef
Definition: IpplTimings.h:57
double wallTime
Definition: IpplTimings.h:110
void start()
Definition: IpplTimings.h:68
unsigned int TimerRef
Definition: IpplTimings.h:122
TimerInfo * infoTimer(const char *nm)
Definition: IpplTimings.h:146
void clearTimer(TimerRef)
Definition: IpplTimings.cpp:81
std::map< std::string, TimerInfo * > TimerMap_t
Definition: IpplTimings.h:160
IpplTimerInfo TimerInfo
Definition: IpplTimings.h:125
TimerMap_t TimerMap
Definition: IpplTimings.h:167
void print()
Definition: IpplTimings.cpp:90
void stopTimer(TimerRef)
Definition: IpplTimings.cpp:72
TimerList_t TimerList
Definition: IpplTimings.h:164
void startTimer(TimerRef)
Definition: IpplTimings.cpp:63
TimerRef getTimer(const char *)
Definition: IpplTimings.cpp:44
std::vector< my_auto_ptr< TimerInfo > > TimerList_t
Definition: IpplTimings.h:159
static TimerInfo * infoTimer(const char *nm)
Definition: IpplTimings.h:202
Timing::TimerRef TimerRef
Definition: IpplTimings.h:176
static void stash()
Timing::TimerList_t TimerList_t
Definition: IpplTimings.h:222
static TimerRef getTimer(const char *nm)
Definition: IpplTimings.h:182
Timing::TimerMap_t TimerMap_t
Definition: IpplTimings.h:223
static std::stack< Timing * > stashedInstance
Definition: IpplTimings.h:233
Timing::TimerInfo TimerInfo
Definition: IpplTimings.h:179
static void stopTimer(TimerRef t)
Definition: IpplTimings.h:192
static Timing * instance
Definition: IpplTimings.h:232
static void pop()
static void print()
Definition: IpplTimings.h:207
static void clearTimer(TimerRef t)
Definition: IpplTimings.h:197
static void print(std::string fn, const std::map< std::string, unsigned int > &problemSize=std::map< std::string, unsigned int >())
Definition: IpplTimings.h:212
static void startTimer(TimerRef t)
Definition: IpplTimings.h:187
Definition: Timer.h:28
void start()
Definition: Timer.cpp:34
void stop()
Definition: Timer.cpp:39
void clear()
Definition: Timer.cpp:29
double clock_time()
Definition: Timer.cpp:50
double cpu_time()
Definition: Timer.cpp:65