Line data Source code
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
39 : class IpplTimerInfo {
40 : public:
41 : // typedef for reference to a timer
42 : typedef unsigned int TimerRef;
43 :
44 : // constructor
45 344 : IpplTimerInfo()
46 688 : : name("")
47 344 : , wallTime(0.0)
48 688 : , indx(std::numeric_limits<TimerRef>::max()) {
49 344 : clear();
50 344 : }
51 :
52 : // destructor
53 0 : ~IpplTimerInfo() {}
54 :
55 : // timer operations
56 336 : void start() {
57 336 : if (!running) {
58 312 : running = true;
59 312 : t.stop();
60 312 : t.clear();
61 312 : t.start();
62 : }
63 336 : }
64 :
65 252 : void stop() {
66 252 : if (running) {
67 252 : t.stop();
68 252 : running = false;
69 252 : wallTime += t.elapsed();
70 : }
71 252 : }
72 :
73 344 : void clear() {
74 344 : t.stop();
75 344 : t.clear();
76 344 : running = false;
77 344 : }
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 :
95 : struct 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 :
133 : private:
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 :
141 : class IpplTimings {
142 : public:
143 : // typedef for reference to a timer
144 : typedef Timing::TimerRef TimerRef;
145 :
146 : // a typedef for the timer information object
147 : typedef Timing::TimerInfo TimerInfo;
148 :
149 : // create a timer, or get one that already exists
150 344 : static TimerRef getTimer(const char* nm) { return instance->getTimer(nm); }
151 :
152 : // start a timer
153 336 : static void startTimer(TimerRef t) { instance->startTimer(t); }
154 :
155 : // stop a timer, and accumulate it's values
156 252 : 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 0 : static void print() { instance->print(); }
166 :
167 : // print the results to a file
168 0 : static void print(std::string fn, const std::map<std::string, unsigned int>& problemSize =
169 : std::map<std::string, unsigned int>()) {
170 0 : instance->print(fn, problemSize);
171 0 : }
172 :
173 : static void stash();
174 : static void pop();
175 :
176 : private:
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
|