OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
IpplTimings.h
Go to the documentation of this file.
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// Copyright (c) 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
25// All rights reserved
26//
27// This file is part of IPPL.
28//
29// IPPL is free software: you can redistribute it and/or modify
30// it under the terms of the GNU General Public License as published by
31// the Free Software Foundation, either version 3 of the License, or
32// (at your option) any later version.
33//
34// You should have received a copy of the GNU General Public License
35// along with IPPL. If not, see <https://www.gnu.org/licenses/>.
36//*************************************************************************/
37#ifndef IPPL_TIMINGS_H
38#define IPPL_TIMINGS_H
39
40#include "Utility/Timer.h"
41#include "Utility/my_auto_ptr.h"
42
43#include <exception>
44#include <limits>
45#include <map>
46#include <string>
47#include <stack>
48#include <vector>
49
51public:
52 // typedef for reference to a timer
53 typedef unsigned int TimerRef;
54
55 // constructor
56 IpplTimerInfo() : name(""), cpuTime(0.0), wallTime(0.0), indx(std::numeric_limits<TimerRef>::max()) {
57 clear();
58 }
59
60 // destructor
62
63 // timer operations
64 void start() {
65 if (!running) {
66 running = true;
67 t.stop();
68 t.clear();
69 t.start();
70 }
71 else {
72 throw std::runtime_error("Timer '" + name + "' already running");
73 }
74 }
75
76 void stop() {
77 if (running) {
78 t.stop();
79 running = false;
80 cpuTime += t.cpu_time();
82 }
83 else {
84 throw std::runtime_error("Timer '" + name + "' already idling");
85 }
86 }
87
88 void clear() {
89 t.stop();
90 t.clear();
91 running = false;
92 }
93
94 // the IPPL timer that this object manages
96
97 // the name of this timer
98 std::string name;
99
100 // the accumulated time
101 double cpuTime;
102 double wallTime;
103
104 // is the timer turned on right now?
106
107 // an index value for this timer
109};
110
111struct Timing {
112 // typedef for reference to a timer
113 typedef unsigned int TimerRef;
114
115 // a typedef for the timer information object
117
118 // Default constructor
119 Timing();
120
121 // Destructor - clear out the existing timers
122 ~Timing();
123
124 // create a timer, or get one that already exists
125 TimerRef getTimer(const char*);
126
127 // start a timer
128 void startTimer(TimerRef);
129
130 // stop a timer, and accumulate it's values
131 void stopTimer(TimerRef);
132
133 // clear a timer, by turning it off and throwing away its time
134 void clearTimer(TimerRef);
135
136 // return a TimerInfo struct by asking for the name
137 TimerInfo* infoTimer(const char* nm) {
138 return TimerMap[std::string(nm)];
139 }
140
141 // print the results to standard out
142 void print();
143
144 // print the results to a file
145 void print(const std::string &fn,
146 const std::map<std::string, unsigned int>& problemSize);
147
148 // type of storage for list of TimerInfo
149 typedef std::vector<my_auto_ptr<TimerInfo> > TimerList_t;
150 typedef std::map<std::string, TimerInfo*> TimerMap_t;
151
152private:
153 // a list of timer info structs
155
156 // a map of timers, keyed by string
158};
159
160
162public:
163 // typedef for reference to a timer
165
166 // a typedef for the timer information object
168
169 // create a timer, or get one that already exists
170 static TimerRef getTimer(const char* nm) {
171 return instance->getTimer(nm);
172 }
173
174 // start a timer
175 static void startTimer(TimerRef t) {
177 }
178
179 // stop a timer, and accumulate it's values
180 static void stopTimer(TimerRef t) {
182 }
183
184 // clear a timer, by turning it off and throwing away its time
185 static void clearTimer(TimerRef t) {
187 }
188
189 // return a TimerInfo struct by asking for the name
190 static TimerInfo *infoTimer(const char *nm) {
191 return instance->infoTimer(nm);
192 }
193
194 // print the results to standard out
195 static void print() {
196 instance->print();
197 }
198
199 // print the results to a file
200 static void print(std::string fn,
201 const std::map<std::string, unsigned int>& problemSize = std::map<std::string, unsigned int>()) {
202 instance->print(fn, problemSize);
203 }
204
205 static void stash();
206 static void pop();
207
208private:
209 // type of storage for list of TimerInfo
212
213 // Default constructor
214 IpplTimings();
215
216 // Destructor - clear out the existing timers
217 ~IpplTimings();
218
220 static std::stack<Timing*> stashedInstance;
221};
222
223#endif
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
std::string name
Definition IpplTimings.h:98
TimerRef indx
unsigned int TimerRef
Definition IpplTimings.h:53
unsigned int TimerRef
void clearTimer(TimerRef)
TimerInfo * infoTimer(const char *nm)
IpplTimerInfo TimerInfo
std::map< std::string, TimerInfo * > TimerMap_t
TimerMap_t TimerMap
void print()
void stopTimer(TimerRef)
TimerList_t TimerList
void startTimer(TimerRef)
TimerRef getTimer(const char *)
std::vector< my_auto_ptr< TimerInfo > > TimerList_t
Timing::TimerRef TimerRef
static void stash()
Timing::TimerList_t TimerList_t
static TimerRef getTimer(const char *nm)
static TimerInfo * infoTimer(const char *nm)
Timing::TimerMap_t TimerMap_t
static std::stack< Timing * > stashedInstance
Timing::TimerInfo TimerInfo
static void stopTimer(TimerRef t)
static Timing * instance
static void pop()
static void print()
static void clearTimer(TimerRef t)
static void print(std::string fn, const std::map< std::string, unsigned int > &problemSize=std::map< std::string, unsigned int >())
static void startTimer(TimerRef t)
Definition Timer.h:27
void start()
Definition Timer.cpp:32
void stop()
Definition Timer.cpp:37
void clear()
Definition Timer.cpp:27
double clock_time()
Definition Timer.cpp:48
double cpu_time()
Definition Timer.cpp:52