KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
timer.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Pooyan Dadvand
11 // Riccardo Rossi
12 // Vicente Mataix Ferrandiz
13 //
14 //
15 
16 #if !defined(KRATOS_TIMER_H_INCLUDED )
17 #define KRATOS_TIMER_H_INCLUDED
18 
19 // System includes
20 #include <string>
21 #include <iostream>
22 #include <fstream>
23 #include <map>
24 #include <unordered_map>
25 #include <chrono>
26 
27 // External includes
28 
29 // Project includes
30 #include "includes/define.h"
31 
32 namespace Kratos
33 {
34 
37 
41 
45 
49 
53 
62 class KRATOS_API(KRATOS_CORE) Timer
63 {
71  class TimerData
72  {
73  int mRepeatNumber;
74  double mStartTime;
75  double mTotalElapsedTime;
76  double mMaximumTime;
77  double mMinimumTime;
78  public:
79  TimerData() : mRepeatNumber(int()), mStartTime(double()), mTotalElapsedTime(double()), mMaximumTime(double()), mMinimumTime(double()) {}
80  double GetStartTime()
81  {
82  return mStartTime;
83  }
84  void SetStartTime(double StartTime)
85  {
86  mStartTime = StartTime;
87  }
88  double GetTotalElapsedTime()
89  {
90  return mTotalElapsedTime;
91  }
92  void SetTotalElapsedTime(double TotalElapsedTime)
93  {
94  mTotalElapsedTime = TotalElapsedTime;
95  }
96  void Update(double StopTime)
97  {
98  double elapsed = StopTime - mStartTime;
99  if(mRepeatNumber == 0)
100  mMinimumTime = elapsed;
101  mTotalElapsedTime += elapsed;
102  if(mMaximumTime < elapsed)
103  mMaximumTime = elapsed;
104 
105  if((mMinimumTime > elapsed))
106  mMinimumTime = elapsed;
107 
108  mRepeatNumber++;
109 
110  }
112  void PrintData(std::ostream& rOStream, double GlobalElapsedTime = -1.00) const;
113  };
114 
115 public:
118 
121 
123  typedef double TimeType;
124 
126  typedef std::map<std::string, TimerData> ContainerType;
127 
129  typedef std::unordered_map<std::string, std::string> InternalNameDatabaseType;
130 
132  static constexpr std::size_t NumberOfZeros = 3;
133 
137 
139  Timer();
140 
142  virtual ~Timer()
143  {
144 
145  }
146 
150 
154 
159  static void Start(std::string const& rIntervalName);
160 
165  static void Stop(std::string const& rIntervalName);
166 
170  static inline double GetTime()
171  {
172  const auto current_time = std::chrono::steady_clock::now();
173  return std::chrono::duration_cast<std::chrono::duration<double>>(current_time.time_since_epoch()).count();
174  }
175 
179  static inline double ElapsedSeconds(const std::chrono::steady_clock::time_point StartTime)
180  {
181  const auto current_time = std::chrono::steady_clock::now();
182  return std::chrono::duration_cast<std::chrono::duration<double>>(current_time - StartTime).count();
183  }
184 
188 
193  static int SetOuputFile(std::string const& rOutputFileName);
194 
198  static int CloseOuputFile();
199 
204  static bool GetPrintOnScreen();
205 
210  static void SetPrintOnScreen(bool const PrintOnScreen);
211 
216  static bool GetPrintIntervalInformation();
217 
222  static void SetPrintIntervalInformation(bool const PrintIntervalInformation);
223 
227 
231 
239  static void PrintIntervalInformation(
240  std::ostream& rOStream,
241  std::string const& rIntervalName,
242  const double StartTime,
243  const double StopTime
244  );
245 
252  static void PrintIntervalInformation(
253  std::string const& rIntervalName,
254  const double StartTime,
255  const double StopTime
256  );
257 
261  static void PrintTimingInformation();
262 
267  static void PrintTimingInformation(std::ostream& rOStream);
268 
270  virtual std::string Info() const
271  {
272  return "Timer";
273  }
274 
276  virtual void PrintInfo(std::ostream& rOStream) const
277  {
278  }
279 
281  virtual void PrintData(std::ostream& rOStream) const
282  {
283  PrintTimingInformation(rOStream);
284  }
285 
289 
291 
292 protected:
295 
299 
303 
307 
311 
315 
319 
321 private:
322 
325 
326  static InternalNameDatabaseType msInternalNameDatabase;
327 
328  static ContainerType msTimeTable;
329 
330  static std::ofstream msOutputFile;
331 
332  static std::size_t msCounter;
333 
334  static bool msPrintOnScreen;
335 
336  static bool msPrintIntervalInformation;
337 
338  static const std::chrono::steady_clock::time_point mStartTime;
339 
343 
347 
353  static std::string GetInternalName(const std::string& rName);
354 
355  static std::vector<std::string>& GetLabelsStackInstance()
356  {
357  static std::vector<std::string> instance;
358  return instance;
359  }
360 
361  static std::string CreateFullLabel(){
362  const auto& r_labels_stack = GetLabelsStackInstance();
363  std::string result;
364  for(const auto& r_label : r_labels_stack){
365  result += "/" + r_label;
366  }
367  return result;
368  }
369 
373 
377 
381 
385 
387  Timer& operator=(Timer const& rOther)
388  {
389  return *this;
390  }
391 
393  /* Timer(Timer const& rOther); */
394 
396 
397 }; // Class Timer
398 
400 
403 
407 
408 
410 // inline std::istream& operator >> (std::istream& rIStream,
411 // Timer& rThis){}
412 
414 inline std::ostream& operator << (std::ostream& rOStream,
415  const Timer& rThis)
416 {
417  rThis.PrintInfo(rOStream);
418  rOStream << std::endl;
419  rThis.PrintData(rOStream);
420 
421  return rOStream;
422 }
424 
425 } // namespace Kratos.
426 
427 #endif // KRATOS_TIMER_H_INCLUDED defined
428 
429 
PeriodicInterfaceProcess & operator=(const PeriodicInterfaceProcess &)=delete
This utility can be used to compute the time employed on computations.
Definition: timer.h:63
static double GetTime()
This method returns the resulting time.
Definition: timer.h:170
virtual ~Timer()
Destructor.
Definition: timer.h:142
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: timer.h:281
virtual std::string Info() const
Turn back information as a string.
Definition: timer.h:270
std::map< std::string, TimerData > ContainerType
The timer data container type (map)
Definition: timer.h:126
KRATOS_CLASS_POINTER_DEFINITION(Timer)
Pointer definition of Timer.
double TimeType
The type of float used to store the time.
Definition: timer.h:123
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: timer.h:276
std::unordered_map< std::string, std::string > InternalNameDatabaseType
This is used to know the internal name used for the time table.
Definition: timer.h:129
static double ElapsedSeconds(const std::chrono::steady_clock::time_point StartTime)
This method returns the resulting time.
Definition: timer.h:179
void PrintTimingInformation(Timer &rTimer)
Definition: add_other_utilities_to_python.cpp:105
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
int count
Definition: generate_frictional_mortar_condition.py:212
Configure::ContainerType ContainerType
Definition: transfer_utility.h:247