timer.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18
19 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
28#ifndef EWOMS_TIMER_HH
29#define EWOMS_TIMER_HH
30
31#include <chrono>
32
33#if HAVE_MPI
34#include <mpi.h>
35#endif
36
37namespace Opm {
48class Timer
49{
50 struct TimeData
51 {
52 std::chrono::high_resolution_clock::time_point realtimeData;
53 std::clock_t cputimeData;
54 };
55public:
57 { halt(); }
58
62 void start()
63 {
64 isStopped_ = false;
65 measure_(startTime_);
66 }
67
73 double stop()
74 {
75 if (!isStopped_) {
76 TimeData stopTime;
77
78 measure_(stopTime);
79
80 const auto& t1 = startTime_.realtimeData;
81 const auto& t2 = stopTime.realtimeData;
82 std::chrono::duration<double> dt =
83 std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1);
84
85 realTimeElapsed_ += dt.count();
86 cpuTimeElapsed_ +=
87 static_cast<double>(stopTime.cputimeData
88 - startTime_.cputimeData)/CLOCKS_PER_SEC;
89 }
90
91 isStopped_ = true;
92
93 return realTimeElapsed_;
94 }
95
99 void halt()
100 {
101 isStopped_ = true;
102 cpuTimeElapsed_ = 0.0;
103 realTimeElapsed_ = 0.0;
104 }
105
109 void reset()
110 {
111 cpuTimeElapsed_ = 0.0;
112 realTimeElapsed_ = 0.0;
113
114 measure_(startTime_);
115 }
116
121 double realTimeElapsed() const
122 {
123 if (isStopped_)
124 return realTimeElapsed_;
125
126 TimeData stopTime;
127
128 measure_(stopTime);
129
130 const auto& t1 = startTime_.realtimeData;
131 const auto& t2 = stopTime.realtimeData;
132 std::chrono::duration<double> dt =
133 std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1);
134
135 return realTimeElapsed_ + dt.count();
136 }
137
143 double elapsed() const
144 { return realTimeElapsed(); }
145
150 double cpuTimeElapsed() const
151 {
152 if (isStopped_)
153 return cpuTimeElapsed_;
154
155 TimeData stopTime;
156
157 measure_(stopTime);
158
159 const auto& t1 = startTime_.cputimeData;
160 const auto& t2 = stopTime.cputimeData;
161
162 return cpuTimeElapsed_ + static_cast<double>(t2 - t1)/CLOCKS_PER_SEC;
163 }
164
170 double globalCpuTimeElapsed() const
171 {
172 double val = cpuTimeElapsed();
173 double globalVal = val;
174
175#if HAVE_MPI
176 MPI_Reduce(&val,
177 &globalVal,
178 /*count=*/1,
179 MPI_DOUBLE,
180 MPI_SUM,
181 /*rootRank=*/0,
182 MPI_COMM_WORLD);
183#endif
184
185 return globalVal;
186 }
187
191 Timer& operator+=(const Timer& other)
192 {
193 realTimeElapsed_ += other.realTimeElapsed();
194 cpuTimeElapsed_ += other.cpuTimeElapsed();
195
196 return *this;
197 }
198
199private:
200 // measure the current time and put it into the object passed via
201 // the argument.
202 static void measure_(TimeData& timeData)
203 {
204 // Note: On Linux -- or rather fully POSIX compliant systems -- using
205 // clock_gettime() would be more accurate for the CPU time.
206 timeData.realtimeData = std::chrono::high_resolution_clock::now();
207 timeData.cputimeData = std::clock();
208 }
209
210 bool isStopped_;
211 double cpuTimeElapsed_;
212 double realTimeElapsed_;
213 TimeData startTime_;
214};
215} // namespace Opm
216
217#endif
Provides an encapsulation to measure the system time.
Definition: timer.hh:49
void reset()
Make the current point in time t=0 but do not change the status of the timer.
Definition: timer.hh:109
void start()
Start counting the time resources used by the simulation.
Definition: timer.hh:62
double elapsed() const
This is an alias for realTimeElapsed()
Definition: timer.hh:143
void halt()
Stop the measurement reset all timing values.
Definition: timer.hh:99
double realTimeElapsed() const
Return the real time [s] elapsed during the periods the timer was active since the last reset.
Definition: timer.hh:121
Timer()
Definition: timer.hh:56
double globalCpuTimeElapsed() const
Return the CPU time [s] used by all threads of the all processes of program.
Definition: timer.hh:170
Timer & operator+=(const Timer &other)
Adds the time of another timer to the current one.
Definition: timer.hh:191
double stop()
Stop counting the time resources.
Definition: timer.hh:73
double cpuTimeElapsed() const
Return the CPU time [s] used by all threads of the local process for the periods the timer was active...
Definition: timer.hh:150
Definition: blackoilboundaryratevector.hh:37