1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/* This file is part of the OpenLB library
*
* Copyright (C) 2011 Lukas Baron, Mathias J. Krause
* E-mail contact: info@openlb.net
* The most recent release of OpenLB can be downloaded at
* <http://www.openlb.net/>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef TIMER_HH
#define TIMER_HH
#include "timer.h"
#include "communication/mpiManager.h"
namespace olb {
namespace util {
template<typename T>
Timer<T>::Timer(int maxTimeSteps, size_t numFluidCells)
: clout(std::cout,"Timer"),
deltaTS(0), curTS(0), maxTS(maxTimeSteps),
numFC(numFluidCells), rtRemMs(1) // avoids some stupid numbers in first call of printStep() (not for T=double)
{
tp = nullptr;
}
template<typename T>
T Timer<T>::timevalDiffTimeMs(timeval end, timeval start)
{
T msDiff;
msDiff = 1000*(end.tv_sec - start.tv_sec)
+(end.tv_usec-start.tv_usec)/1000;
return std::max<T>(msDiff, 1.0);
}
template<typename T>
T Timer<T>::getMLUPs()
{
T mlups = (numFC * deltaTS) / (timevalDiffTimeMs(msTimeCur, msTimeLast)*1000);
return mlups;
}
template<typename T>
T Timer<T>::getMLUPps()
{
T mlupps = getMLUPs()/singleton::mpi().getSize();
return mlupps;
}
template<typename T>
T Timer<T>::getTotalMLUPs()
{
T tmlups = ((T)numFC * curTS) / (timevalDiffTimeMs(msTimeEnd, msTimeStart)*1000);
return tmlups;
}
template<typename T>
T Timer<T>::getTotalMLUPps()
{
T tmlupps = getTotalMLUPs()/singleton::mpi().getSize();
return tmlupps;
}
template<typename T>
void Timer<T>::start()
{
sTimeStart = time(tp); // time in s
gettimeofday(&msTimeStart, nullptr); // time in ms
gettimeofday(&msTimeCur, nullptr); // time in ms, here only necessary for MLUP-calculations
cpuTimeStart = clock(); //cpu-time
}
template<typename T>
void Timer<T>::update(int currentTimeStep) // Is int sufficient? Is it possible/desirable to have non-integer time steps?
{
cpuTimeCur = clock(); // CPU-time
sTimeCur = time(tp); // time in s
msTimeLast = msTimeCur;
gettimeofday(&msTimeCur, nullptr); // time in ms
// calculate and update missing time-values
deltaTS = currentTimeStep - curTS; // this makes multiple calls
curTS = currentTimeStep; // of update() critical
rtPas = difftime(sTimeCur,sTimeStart); // here calculation is based on s-time
rtTot = rtPas*maxTS/std::max<int>(curTS, 1);
rtRem = rtTot-rtPas;
rtPasMs = timevalDiffTimeMs(msTimeCur, msTimeStart); // here with ms-time as timeval-value
rtTotMs = rtPasMs*maxTS/std::max<int>(curTS, 1);
rtRemMs = rtTotMs-rtPasMs;
ctPas = (cpuTimeCur-cpuTimeStart)/CLOCKS_PER_SEC; // and here the same for CPU-time
ctTot = ctPas*maxTS/std::max<int>(curTS, 1);
ctRem = ctTot-ctPas;
}
template<typename T>
void Timer<T>::stop()
{
cpuTimeEnd = clock(); // cpu-time
sTimeEnd = time(tp); // time in s
gettimeofday(&msTimeEnd, nullptr); // time in ms
}
template<typename T>
double Timer<T>::getTotalCpuTime()
{
return (cpuTimeEnd-cpuTimeStart)/CLOCKS_PER_SEC;
}
template<typename T>
T Timer<T>::getTotalRealTime()
{
return difftime(sTimeEnd,sTimeStart);
}
template<typename T>
T Timer<T>::getTotalRealTimeMs()
{
return timevalDiffTimeMs(msTimeEnd, msTimeStart);
}
template<typename T>
void Timer<T>::print(int currentTimeStep, int printMode)
{
if (currentTimeStep!=curTS) {
update(currentTimeStep);
}
printStep(printMode);
}
template<typename T>
void Timer<T>::printStep(int printMode)
{
switch (printMode) {
case 0: //single-line layout, usable for data extraction as csv
clout
<< "step=" << curTS << "; "
// << "stepMax=" << maxTS << "; "
<< "percent=" << 100.0*curTS/maxTS << "; "
<< "passedTime=" << (double)rtPasMs/1000 << "; "
// << "totalTime=" << (double)rtTotMs/1000 << "; "
<< "remTime=" << rtRemMs/1000 << "; "
<< "MLUPs=" << getMLUPs()
<< std::endl;
break;
case 1: //single-line layout (not conform with output-rules)
clout
<< "latticeTS: "
<< curTS << "/" << maxTS << " (" << 100*curTS/maxTS << "%); "
<< "pas/totTime: "
<< std::setprecision(2) << std::fixed << (double)rtPasMs/1000 << "/"
<< std::setprecision(1) << std::fixed << (double)rtTotMs/1000 << "s; "
<< "remTime: "
<< std::setw(2) << (int)((double)rtRemMs/1000)/60 << "m " << std::setfill('0') << std::setw(4) << (double)((int)((double)rtRemMs/100)%600)/10 << "s; "
<< std::setfill(' ')
<< "MLUPs: " << getMLUPs()
<< std::endl;
break;
case 2: //pretty double line layout in colums, but non-conform
clout
<< std::setw(21) << std::left << "Lattice-Timesteps"
<< std::setw(17) << std::left << "| CPU time/estim"
<< std::setw(18) << std::left << "| REAL time/estim"
<< std::setw(6) << std::left << "| ETA"
<< std::setw(6) << std::left << "| MLUPs"
<< std::endl << std::right
<< std::setw(6) << std::setprecision(2) << std::fixed << curTS << "/" << std::setw(6) << maxTS << " (" << std::setw(3) << 100*curTS/maxTS << "%) |"
<< std::setw(7) << ctPas << "/" << std::setw(7) << ctTot << " |"
<< std::setw(8) << (double)rtPasMs/1000 << "/" << std::setw(7) << (double)rtTotMs/1000 << " |"
<< std::setw(4) << (int)rtRemMs/1000+1 << " |"
<< std::setw(6) << getMLUPs()
<< std::endl;
break;
case 3: //performance output only
clout
<< "step " << curTS << "; "
<< "MLUPs=" << std::setw(8) << getMLUPs() << ", MLUPps=" << std::setw(8) << getMLUPps() << std::endl;
break;
default:
clout << "Error in function printStep in class_timer.h: printMode="<<printMode<<" not found" << std::endl << std::flush;
}
}
template<typename T>
void Timer<T>::printSummary()
{
clout << std::endl;
clout << "----------------Summary:Timer----------------" << std::endl;
clout << "measured time (rt) : " << (int)getTotalRealTimeMs()/1000 << "." << (int)getTotalRealTimeMs()-(int)getTotalRealTimeMs()/1000*1000 << "s" << std::endl;
clout << "measured time (cpu): " << std::setprecision(3) << std::fixed << getTotalCpuTime() << "s" << std::endl;
clout << "average MLUPs : " << getTotalMLUPs() << std::endl;
clout << "average MLUPps: " << getTotalMLUPps() << std::endl;
clout << "---------------------------------------------" << std::endl;
}
template<typename T>
void Timer<T>::printShortSummary()
{
clout << "realTime=" <<
|