chombo-discharge
CD_Timer.H
Go to the documentation of this file.
1 /* chombo-discharge
2  * Copyright © 2021 SINTEF Energy Research.
3  * Please refer to Copyright.txt and LICENSE in the chombo-discharge root directory.
4  */
5 
13 #ifndef CD_Timer_H
14 #define CD_Timer_H
15 
16 // Std includes
17 #include <chrono>
18 #include <map>
19 #include <tuple>
20 
21 // Chombo includes
22 #include <REAL.H>
23 
24 // Our includes
25 #include <CD_NamespaceHeader.H>
26 
30 class Timer
31 {
32 public:
36  using Clock = std::chrono::steady_clock;
37 
41  using TimePoint = std::chrono::steady_clock::time_point;
42 
46  using Duration = std::chrono::duration<Real>;
47 
51  inline static Real
52  wallClock();
53 
57  inline Timer() = default;
58 
62  inline Timer(const std::string a_process);
63 
67  inline ~Timer();
68 
73  Timer(const Timer& a_other) = default;
74 
79  Timer&
80  operator=(const Timer& a_other) = default;
81 
87  inline void
88  startEvent(const std::string a_event) noexcept;
89 
95  inline void
96  stopEvent(const std::string a_event) noexcept;
97 
104  inline void
105  eventReport(std::ostream& a_outputStream, const bool a_localReportOnly = false) const noexcept;
106 
112  inline void
113  writeReportToFile(const std::string a_fileName) const noexcept;
114 
118  inline void
119  clear() noexcept;
120 
121 protected:
126  {
127  StoppedEvent = 0,
128  StartClock = 1,
129  ElapsedTime = 2
130  };
131 
135  std::string m_processName;
136 
141  std::map<std::string, std::tuple<bool, TimePoint, Duration>> m_events;
142 
146  void
147  printReportHeader(std::ostream& a_outputStream) const noexcept;
148 
152  void
153  printReportTail(std::ostream& a_outputStream, const std::pair<Real, Real> a_elapsedTime) const noexcept;
154 
159  std::pair<Real, Real>
160  computeTotalElapsedTime(const bool a_localReportOnly) const noexcept;
161 };
162 
163 #include <CD_NamespaceFooter.H>
164 
165 #include <CD_TimerImplem.H>
166 
167 #endif
Implementation of CD_Timer.H.
Class which is used for run-time monitoring of events.
Definition: CD_Timer.H:31
void stopEvent(const std::string a_event) noexcept
Stop an event.
Definition: CD_TimerImplem.H:88
std::chrono::duration< Real > Duration
Duration in seconds.
Definition: CD_Timer.H:46
~Timer()
Destructor.
Definition: CD_TimerImplem.H:47
void startEvent(const std::string a_event) noexcept
Start an event.
Definition: CD_TimerImplem.H:59
EventFields
Enum for indexing tuple. A rare case of where unscoped enums are useful.
Definition: CD_Timer.H:126
std::chrono::steady_clock Clock
Clock implements.
Definition: CD_Timer.H:36
std::string m_processName
Process name. Used for input/output.
Definition: CD_Timer.H:135
void writeReportToFile(const std::string a_fileName) const noexcept
Print all timed events to a file.
Definition: CD_TimerImplem.H:342
std::chrono::steady_clock::time_point TimePoint
Point in time.
Definition: CD_Timer.H:41
void printReportHeader(std::ostream &a_outputStream) const noexcept
Print report header.
Definition: CD_TimerImplem.H:113
std::map< std::string, std::tuple< bool, TimePoint, Duration > > m_events
Timer events. First entry is the name of the event.
Definition: CD_Timer.H:141
Timer()=default
Default constructor. This creates a timer without any timer events and without a name.
Timer(const Timer &a_other)=default
Copy construction.
void printReportTail(std::ostream &a_outputStream, const std::pair< Real, Real > a_elapsedTime) const noexcept
Print report tail.
Definition: CD_TimerImplem.H:149
static Real wallClock()
Static function which returns the time (in seconds) between now and an arbitrary time in the past.
Definition: CD_TimerImplem.H:31
Timer & operator=(const Timer &a_other)=default
Copy assignement.
std::pair< Real, Real > computeTotalElapsedTime(const bool a_localReportOnly) const noexcept
Compute the total time for all finished events.
Definition: CD_TimerImplem.H:310
void clear() noexcept
Clear all events.
Definition: CD_TimerImplem.H:53
void eventReport(std::ostream &a_outputStream, const bool a_localReportOnly=false) const noexcept
Print all timed events to cout.
Definition: CD_TimerImplem.H:173