OpenLB 1.7
Loading...
Searching...
No Matches
timer.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2011 Lukas Baron, Mathias J. Krause
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#ifndef TIMER_HH
25#define TIMER_HH
26
27#include "timer.h"
30
31namespace olb {
32
33namespace util {
34
35template<typename T>
36Timer<T>::Timer(int maxTimeSteps, size_t numFluidCells, unsigned int printModeSummary)
37 : clout(std::cout,"Timer"),
38 deltaTS(0), curTS(0), maxTS(maxTimeSteps),
39 numFC(numFluidCells), rtRemMs(1) // avoids some stupid numbers in first call of printStep() (not for T=double)
40
41{
42 tp = nullptr;
43 this->_printModeSummary = printModeSummary;
44}
45
46template<typename T>
47T Timer<T>::timevalDiffTimeMs(timeval end, timeval start)
48{
49 const T msDiff ( 1000*(end.tv_sec - start.tv_sec)
50 +(end.tv_usec-start.tv_usec)/1000 );
51 return util::max<T>(msDiff, 1.0);
52}
53
54template<typename T>
56{
57 T mlups = (numFC * deltaTS) / (timevalDiffTimeMs(msTimeCur, msTimeLast)*1000);
58 return mlups;
59}
60
61template<typename T>
63{
64 T mlupps = getMLUPs()/singleton::mpi().getSize();
65#ifdef PARALLEL_MODE_OMP
66 mlupps /= singleton::omp().getSize();
67#endif
68 return mlupps;
69}
70
71template<typename T>
73{
74 T tmlups = (numFC * curTS) / double(timevalDiffTimeMs(msTimeEnd, msTimeStart)*1000);
75 return tmlups;
76}
77
78template<typename T>
80{
81 T tmlupps = getTotalMLUPs()/singleton::mpi().getSize();
82#ifdef PARALLEL_MODE_OMP
83 tmlupps /= singleton::omp().getSize();
84#endif
85 return tmlupps;
86}
87
88
89template<typename T>
91{
92 sTimeStart = time(tp); // time in s
93 gettimeofday(&msTimeStart, nullptr); // time in ms
94 gettimeofday(&msTimeCur, nullptr); // time in ms, here only necessary for MLUP-calculations
95 cpuTimeStart = clock(); //cpu-time
96}
97
98template<typename T>
99void Timer<T>::update(int currentTimeStep) // Is int sufficient? Is it possible/desirable to have non-integer time steps?
100{
101
102 cpuTimeCur = clock(); // CPU-time
103 sTimeCur = time(tp); // time in s
104 msTimeLast = msTimeCur;
105 gettimeofday(&msTimeCur, nullptr); // time in ms
106
107 // calculate and update missing time-values
108 deltaTS = currentTimeStep - curTS; // this makes multiple calls
109 curTS = currentTimeStep; // of update() critical
110
111 rtPas = difftime(sTimeCur,sTimeStart); // here calculation is based on s-time
112 rtTot = rtPas*maxTS/util::max<int>(curTS, 1);
113 rtRem = rtTot-rtPas;
114
115 rtPasMs = timevalDiffTimeMs(msTimeCur, msTimeStart); // here with ms-time as timeval-value
116 rtTotMs = rtPasMs*maxTS/util::max<int>(curTS, 1);
117 rtRemMs = rtTotMs-rtPasMs;
118
119 ctPas = (cpuTimeCur-cpuTimeStart)/CLOCKS_PER_SEC; // and here the same for CPU-time
120 ctTot = ctPas*maxTS/util::max<int>(curTS, 1);
121 ctRem = ctTot-ctPas;
122
123}
124
125template<typename T>
127{
128 cpuTimeEnd = clock(); // cpu-time
129 sTimeEnd = time(tp); // time in s
130 gettimeofday(&msTimeEnd, nullptr); // time in ms
131}
132
133template<typename T>
135{
136 return (cpuTimeEnd-cpuTimeStart)/CLOCKS_PER_SEC;
137}
138
139template<typename T>
141{
142 return difftime(sTimeEnd,sTimeStart);
143}
144
145template<typename T>
147{
148 return timevalDiffTimeMs(msTimeEnd, msTimeStart);
149}
150
151template<typename T>
152void Timer<T>::print(std::size_t currentTimeStep, int printMode)
153{
154 if (currentTimeStep!=curTS) {
155 update(currentTimeStep);
156 }
157 printStep(printMode);
158}
159
160template<typename T>
161void Timer<T>::printStep(int printMode)
162{
163 printStep(clout, printMode);
164}
165
166template<typename T>
167void Timer<T>::printStep(OstreamManager& fout, int printMode)
168{
169 switch (printMode) {
170 case 0: //single-line layout, usable for data extraction as csv
171 fout
172 << "step=" << curTS << "; "
173 // << "stepMax=" << maxTS << "; "
174 << "percent=" << 100.0*curTS/maxTS << "; "
175 << "passedTime=" << (double)rtPasMs/1000 << "; "
176 // << "totalTime=" << (double)rtTotMs/1000 << "; "
177 << "remTime=" << rtRemMs/1000 << "; "
178 << "MLUPs=" << getMLUPs()
179 << std::endl;
180 break;
181
182 case 1: //single-line layout (not conform with output-rules)
183 fout
184 << "latticeTS: "
185 << curTS << "/" << maxTS << " (" << 100*curTS/maxTS << "%); "
186 << "pas/totTime: "
187 << std::setprecision(2) << std::fixed << (double)rtPasMs/1000 << "/"
188 << std::setprecision(1) << std::fixed << (double)rtTotMs/1000 << "s; "
189 << "remTime: "
190 << std::setw(2) << (int)((double)rtRemMs/1000)/60 << "m " << std::setfill('0') << std::setw(4) << (double)((int)((double)rtRemMs/100)%600)/10 << "s; "
191 << std::setfill(' ')
192 << "MLUPs: " << getMLUPs()
193 << std::endl;
194 break;
195
196 case 2: //pretty double line layout in colums, but non-conform
197 fout
198 << std::setw(21) << std::left << "Lattice-Timesteps"
199 << std::setw(17) << std::left << "| CPU time/estim"
200 << std::setw(18) << std::left << "| REAL time/estim"
201 << std::setw(6) << std::left << "| ETA"
202 << std::setw(6) << std::left << "| MLUPs"
203 << std::endl << std::right
204 << std::setw(6) << std::setprecision(2) << std::fixed << curTS << "/" << std::setw(6) << maxTS << " (" << std::setw(3) << 100*curTS/maxTS << "%) |"
205 << std::setw(7) << ctPas << "/" << std::setw(7) << ctTot << " |"
206 << std::setw(8) << (double)rtPasMs/1000 << "/" << std::setw(7) << (double)rtTotMs/1000 << " |"
207 << std::setw(4) << (int)rtRemMs/1000+1 << " |"
208 << std::setw(6) << getMLUPs()
209 << std::endl;
210 break;
211
212 case 3: //performance output only
213 fout
214 << "step " << curTS << "; "
215 << "MLUPs=" << std::setw(8) << getMLUPs() << ", MLUPps=" << std::setw(8) << getMLUPps() << std::endl;
216 break;
217
218 default:
219 fout << "Error in function printStep in class_timer.h: printMode="<<printMode<<" not found" << std::endl << std::flush;
220 }
221}
222
223template<typename T>
225{
226 printSummary(clout);
227}
228
229template<typename T>
231{
232 fout << std::endl;
233 fout << "----------------Summary:Timer----------------" << std::endl;
234 fout << "measured time (rt) : " << (int)getTotalRealTimeMs()/1000 << "." << (int)getTotalRealTimeMs()-(int)getTotalRealTimeMs()/1000*1000 << "s" << std::endl;
235 fout << "measured time (cpu): " << std::setprecision(3) << std::fixed << getTotalCpuTime() << "s" << std::endl;
236 if (numFC > 0 && curTS > 0) {
237 fout << "average MLUPs : " << getTotalMLUPs() << std::endl;
238 fout << "average MLUPps: " << getTotalMLUPps() << std::endl;
239 }
240 fout << "---------------------------------------------" << std::endl;
241}
242
243template<typename T>
245{
246 printShortSummary(clout);
247}
248
249template<typename T>
251{
252 fout << "realTime=" << (int)getTotalRealTimeMs()/1000 << "." << (int)getTotalRealTimeMs()-(int)getTotalRealTimeMs()/1000*1000
253 << "; cpuTime=" << std::setprecision(3) << std::fixed << getTotalCpuTime() << std::endl;
254}
255
256
257// Factory function /////////////////////////////////
258
259template<typename T, typename DESCRIPTOR>
260Timer<T>* createTimer(XMLreader& param, const UnitConverter<T,DESCRIPTOR>& converter, size_t numLatticePoints)
261{
262 OstreamManager clout(std::cout,"createTimer");
263
264 // initialize parameters with some default values
265 T physMaxT = T();
266 T physStartT = T();
267 int printModeSummary = 0;
268
269 // fetch xml Data and error handling
270 if ( ! param["Application"]["PhysParameters"]["PhysMaxTime"].read(physMaxT) ) {
271 if ( ! param["Application"]["PhysParam"]["MaxTime"].read(physStartT) ) {
272 clout << "PhysMaxTime not found" << std::endl;
273 }
274 else {
275 clout << "Application::PhysParam::MaxTime needs to be renamed to Application::PhysParameters::PhysMaxTime" << std::endl;
276 }
277 }
278 // read the variable for the mode of printSummary
279 param.readOrWarn<int>("Output","Timer","PrintModeSummary", printModeSummary,true,false,false);
280
281// if ( ! param["Application"]["PhysParam"]["MaxStartTime"].read(physStartT) ) {
282// clout << "PhysStartTime not found" << std::endl;
283// }
284
285 // variable processing according to the constructor
286 int maxT = converter.getLatticeTime(physMaxT) + converter.getLatticeTime(physStartT);
287
288 //return some default values that produce reasonable output (e.g.
289 // zero); in best case there should be no output at all (TODO)
290 return new Timer<T>(maxT, numLatticePoints, printModeSummary);
291}
292
293} // namespace util
294
295} // namespace olb
296
297#endif
class for marking output with some text
Conversion between physical and lattice units, as well as discretization.
constexpr size_t getLatticeTime(T physTime) const
conversion from physical to lattice time
bool readOrWarn(std::string name_parameter_1, std::string name_parameter_2, std::string name_parameter_3, ParameterType &var, bool defaultAvailable=true, bool exitIfMissing=false, bool showWarning=true) const
This wrapper function reads the given parameter from the "type_parameter" and "name_parameter_1" or "...
Definition xmlReader.h:178
int getSize() const
Returns the number of processes.
How to use in Code:
Definition timer.h:72
Timer(int maxTimeSteps, std::size_t numFluidCells=1, unsigned int printModeSummary=0)
initializes timer with the given values, abbreviation to Timer() + initialize(int,...
Definition timer.hh:36
void printStep(int printMode=0)
Prints a one-line-summary of the values calculated in update() for use during computation.
Definition timer.hh:161
void printShortSummary()
Prints a short summary containing only time consumptions (real and cpu time)
Definition timer.hh:244
T getMLUPs()
returns Million DESCRIPTOR Site Updates per second (all processes together)
Definition timer.hh:55
T getTotalRealTime()
Returns the total measured time between start() and stop() in seconds.
Definition timer.hh:140
void update(int currentTimeStep)
Updates all time values of interest during computation.
Definition timer.hh:99
void print(std::size_t currentTimeStep, int printMode=0)
Performs an update() followed by a printStep().
Definition timer.hh:152
T getTotalMLUPps()
returns average Million DESCRIPTOR Site Updates per second and process between start() and stop()
Definition timer.hh:79
T getTotalMLUPs()
returns average Million DESCRIPTOR Site Updates per second between start() and stop()
Definition timer.hh:72
double getTotalCpuTime()
Returns the total cpu time in seconds between start() and stop().
Definition timer.hh:134
T getMLUPps()
returns Million DESCRIPTOR Site Updates per second and process
Definition timer.hh:62
T getTotalRealTimeMs()
Returns the total measured time between start() and stop() in ms.
Definition timer.hh:146
void start()
(Re-)sets start value for time measurement.
Definition timer.hh:90
T timevalDiffTimeMs(timeval end, timeval start)
returns the time difference between two timeval objects in ms
Definition timer.hh:47
void stop()
Terminates time measurement and sets end value.
Definition timer.hh:126
void printSummary()
Prints a (short) summary containing the overall time consumption in real and in cpu time for use afte...
Definition timer.hh:224
Wrapper functions that simplify the use of MPI.
MpiManager & mpi()
ompManager omp()
Timer< T > * createTimer(XMLreader &param, const UnitConverter< T, DESCRIPTOR > &converter, size_t numLatticePoints)
Definition timer.hh:260
Top level namespace for all of OpenLB.
int getSize() const
This class allows calculation and display of various time data including remaining runtime data in cp...