www.cemf.ir
Timer.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 #ifndef __Timerr_hpp__
22 #define __Timerr_hpp__
23 
24 #include <chrono>
25 #include "types.hpp"
26 
27 namespace pFlow
28 {
29 
30 // forward
31 class Timers;
32 
33 class Timer
34 {
35 private:
36 
37  using timer = std::chrono::high_resolution_clock;
38 
40  timer::time_point start_;
41 
44 
46  real accTime_ = 0.0;
47 
49  real lastTime_ = 0.0;
50 
54 
56  word name_ = "noNameTimer";
57 
59  Timers* parrent_ = nullptr;
60 
61 protected:
62 
63  real averageTimeMax()const;
64 
65  real accTimersTotalMax()const;
66 
67 public:
68 
69  TypeInfo("Timer");
70 
71  Timer() = default;
72 
73  explicit Timer(const word& name)
74  : name_(name)
75  {
76  }
77 
78  Timer(const word& name, Timers* parrent);
79 
80  const word& name() const
81  {
82  return name_;
83  }
84 
85  virtual ~Timer();
86 
87  virtual void removeParrent()
88  {
89  parrent_ = nullptr;
90  }
91 
92  virtual int32 level() const;
93 
94  virtual bool master() const
95  {
96  return false;
97  }
98 
99  Timers* parrent()const
100  {
101  return parrent_;
102  }
103 
104  inline
105  void start()
106  {
107  start_ = timer::now();
108  stepAccTime_ = 0;
109  }
110 
111  inline
112  void pause()
113  {
114  auto end = timer::now();
115  stepAccTime_ +=
116  std::chrono::duration_cast<std::chrono::duration<real> >(
117  end - start_
118  )
119  .count();
120  }
121 
122  inline
123  void resume()
124  {
125  start_ = timer::now();
126  }
127 
128  inline
129  void end()
130  {
131  pause();
133 
134  numIteration_++;
135  accTime_ += lastTime_;
136  }
137 
138  inline
139  bool timerActive() const
140  {
141  return numIteration_ != 0;
142  }
143 
144  inline
145  real lastTime() const
146  {
147  return lastTime_;
148  }
149 
150  inline
151  real totalTime() const
152  {
153  return accTime_;
154  }
155 
156  inline
158  {
159  return accTime_ / max(numIteration_, 1);
160  }
161 
162  virtual
164  {
165  return totalTime();
166  }
167 
169 
170  virtual bool write(iOstream& os, bool subTree) const;
171 
172  virtual bool read(iIstream& is)
173  {
174  return true;
175  }
176 };
177 
178 inline iOstream& operator<<(iOstream& os, const Timer& t)
179 {
180  t.write(os, false);
181  return os;
182 }
183 
185 {
186  return is;
187 }
188 
189 } // namespace pFlow
190 
191 #endif //__Timer_hpp__
pFlow::Timer::pause
void pause()
Definition: Timer.hpp:112
pFlow::Timer::averageTime
real averageTime() const
Definition: Timer.hpp:157
pFlow::real
float real
Definition: builtinTypes.hpp:45
pFlow::Timer::start
void start()
Definition: Timer.hpp:105
pFlow::Timer::accTimersTotalMax
real accTimersTotalMax() const
Definition: Timer.cpp:30
pFlow::Timer::name_
word name_
name for the timer
Definition: Timer.hpp:56
types.hpp
pFlow::Timer::totalTime
real totalTime() const
Definition: Timer.hpp:151
pFlow::Timer::removeParrent
virtual void removeParrent()
Definition: Timer.hpp:87
pFlow::Timer::numIteration_
int32 numIteration_
number of times start() and end() are called
Definition: Timer.hpp:43
pFlow::Timer::write
virtual bool write(iOstream &os, bool subTree) const
Definition: Timer.cpp:59
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::max
T max(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:79
pFlow::Timer::stepAccTime_
real stepAccTime_
Accumulative duration for multiple steps between start() and end()
Definition: Timer.hpp:53
pFlow::Timers
Definition: Timers.hpp:32
pFlow::Timer::start_
timer::time_point start_
start time
Definition: Timer.hpp:40
pFlow
Definition: demGeometry.hpp:27
pFlow::Timer::lastTime
real lastTime() const
Definition: Timer.hpp:145
pFlow::Timer::parrent_
Timers * parrent_
parrent of timer
Definition: Timer.hpp:59
pFlow::Timer::parrent
Timers * parrent() const
Definition: Timer.hpp:99
pFlow::Timer::end
void end()
Definition: Timer.hpp:129
pFlow::Timer::~Timer
virtual ~Timer()
Definition: Timer.cpp:43
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::Timer::Timer
Timer()=default
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
pFlow::Timer::lastTime_
real lastTime_
last time duration
Definition: Timer.hpp:49
pFlow::Timer
Definition: Timer.hpp:33
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::count
auto count(const Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:26
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::Timer::level
virtual int32 level() const
Definition: Timer.cpp:51
pFlow::Timer::resume
void resume()
Definition: Timer.hpp:123
pFlow::Timer::accTime_
real accTime_
sum of time duratios (in seconds) between all start() and end() calls
Definition: Timer.hpp:46
pFlow::Timer::name
const word & name() const
Definition: Timer.hpp:80
pFlow::Timer::master
virtual bool master() const
Definition: Timer.hpp:94
pFlow::Timer::accTimersTotal
virtual real accTimersTotal() const
Definition: Timer.hpp:163
pFlow::Timer::Timer
Timer(const word &name)
Definition: Timer.hpp:73
pFlow::Timer::read
virtual bool read(iIstream &is)
Definition: Timer.hpp:172
pFlow::Timer::timer
std::chrono::high_resolution_clock timer
Definition: Timer.hpp:37
pFlow::Timer::TypeInfo
TypeInfo("Timer")
pFlow::Timer::averageTimeMax
real averageTimeMax() const
Definition: Timer.cpp:25
pFlow::Timer::timerActive
bool timerActive() const
Definition: Timer.hpp:139
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59