IPPL API Reference
Independent Parallel Particle Layer C++ API
Loading...
Searching...
No Matches
IpplTimings.h
1//
2// Class IpplTimings
3// IpplTimings - a simple singleton class which lets the user create and
4// timers that can be printed out at the end of the program.
5//
6// General usage
7// 1) create a timer:
8// IpplTimings::TimerRef val = IpplTimings::getTimer("timer name");
9// This will either create a new one, or return a ref to an existing one
10//
11// 2) start a timer:
12// IpplTimings::startTimer(val);
13// This will start the referenced timer running. If it is already running,
14// it will not change anything.
15//
16// 3) stop a timer:
17// IpplTimings::stopTimer(val);
18// This will stop the timer, assuming it was running, and add in the
19// time to the accumulating time for that timer.
20//
21// 4) print out the results:
22// IpplTimings::print();
23//
24#ifndef IPPL_TIMINGS_H
25#define IPPL_TIMINGS_H
26
27#include <exception>
28#include <limits>
29#include <map>
30#include <stack>
31#include <string>
32#include <vector>
33
34#include "Utility/PAssert.h"
35#include "Utility/Timer.h"
36#include "Utility/my_auto_ptr.h"
37
38// a simple class used to store timer values
40public:
41 // typedef for reference to a timer
42 typedef unsigned int TimerRef;
43
44 // constructor
46 : name("")
47 , wallTime(0.0)
48 , indx(std::numeric_limits<TimerRef>::max()) {
49 clear();
50 }
51
52 // destructor
54
55 // timer operations
56 void start() {
57 if (!running) {
58 running = true;
59 t.stop();
60 t.clear();
61 t.start();
62 }
63 }
64
65 void stop() {
66 if (running) {
67 t.stop();
68 running = false;
69 wallTime += t.elapsed();
70 }
71 }
72
73 void clear() {
74 t.stop();
75 t.clear();
76 running = false;
77 }
78
79 // the IPPL timer that this object manages
80 Timer t;
81
82 // the name of this timer
83 std::string name;
84
85 // the accumulated time
86 double wallTime;
87
88 // is the timer turned on right now?
89 bool running;
90
91 // an index value for this timer
92 TimerRef indx;
93};
94
95struct Timing {
96 // typedef for reference to a timer
97 typedef unsigned int TimerRef;
98
99 // a typedef for the timer information object
100 typedef IpplTimerInfo TimerInfo;
101
102 // Default constructor
103 Timing();
104
105 // Destructor - clear out the existing timers
106 ~Timing();
107
108 // create a timer, or get one that already exists
109 TimerRef getTimer(const char*);
110
111 // start a timer
112 void startTimer(TimerRef);
113
114 // stop a timer, and accumulate it's values
115 void stopTimer(TimerRef);
116
117 // clear a timer, by turning it off and throwing away its time
118 void clearTimer(TimerRef);
119
120 // return a TimerInfo struct by asking for the name
121 TimerInfo* infoTimer(const char* nm) { return TimerMap[std::string(nm)]; }
122
123 // print the results to standard out
124 void print();
125
126 // print the results to a file
127 void print(const std::string& fn, const std::map<std::string, unsigned int>& problemSize);
128
129 // type of storage for list of TimerInfo
130 typedef std::vector<my_auto_ptr<TimerInfo> > TimerList_t;
131 typedef std::map<std::string, TimerInfo*> TimerMap_t;
132
133private:
134 // a list of timer info structs
135 TimerList_t TimerList;
136
137 // a map of timers, keyed by string
138 TimerMap_t TimerMap;
139};
140
142public:
143 // typedef for reference to a timer
144 typedef Timing::TimerRef TimerRef;
145
146 // a typedef for the timer information object
148
149 // create a timer, or get one that already exists
150 static TimerRef getTimer(const char* nm) { return instance->getTimer(nm); }
151
152 // start a timer
153 static void startTimer(TimerRef t) { instance->startTimer(t); }
154
155 // stop a timer, and accumulate it's values
156 static void stopTimer(TimerRef t) { instance->stopTimer(t); }
157
158 // clear a timer, by turning it off and throwing away its time
159 static void clearTimer(TimerRef t) { instance->clearTimer(t); }
160
161 // return a TimerInfo struct by asking for the name
162 static TimerInfo* infoTimer(const char* nm) { return instance->infoTimer(nm); }
163
164 // print the results to standard out
165 static void print() { instance->print(); }
166
167 // print the results to a file
168 static void print(std::string fn, const std::map<std::string, unsigned int>& problemSize =
169 std::map<std::string, unsigned int>()) {
170 instance->print(fn, problemSize);
171 }
172
173 static void stash();
174 static void pop();
175
176private:
177 // type of storage for list of TimerInfo
178 typedef Timing::TimerList_t TimerList_t;
179 typedef Timing::TimerMap_t TimerMap_t;
180
181 // Default constructor
182 IpplTimings();
183
184 // Destructor - clear out the existing timers
185 ~IpplTimings();
186
187 static Timing* instance;
188 static std::stack<Timing*> stashedInstance;
189};
190
191#endif
Definition IpplTimings.h:39
Definition IpplTimings.h:141
Definition Timer.h:17
Definition IpplTimings.h:95