C++ mpi module for stochmagnet_main Package
CORE_Profiler.h
1 #ifndef CORE_Profiler_H
2 #define CORE_Profiler_H
3 
4 #include "types.h"
5 #include "functions_type.h"
6 
7 //list headers
8 #include <map>
9 #include <vector>
10 #include <tuple>
11 
12 //std::cout header
13 #include <iostream>
14 
15 //type of routine
16 typedef std::tuple<tInteger,tReal,std::vector<tWallTime>> tRoutineCall;
17 
18 
26 class CORE_Profiler {
27 
28  // ATTRIBUTES
29 
30 public:
34 
35 private:
36 
37 #ifdef USE_PROFILER
38  //routine name -> (n,total called time,vector<recursive call timers>)
39  static std::map<tString,tRoutineCall> mProfiledRoutines;
40 #endif
41 
42 public:
43  // CONSTRUCTORS
47  }
48 
49  // DESTRUCTORS
52  virtual ~CORE_Profiler() {
53  }
54 
55 
56 
57 
61  inline static void StartCallRoutine(const tString& routineName) {
62 #ifdef USE_PROFILER
63  auto iter=mProfiledRoutines.find(routineName);
64  if (iter==mProfiledRoutines.end()) {
65  tRoutineCall call;
66  //number of call
67  std::get<0>(call)=1;
68  //timer
69  std::get<1>(call)=0;
70  //start time of the call
71  std::get<2>(call).push_back(std::chrono::high_resolution_clock::now());
73  std::cout<<routineName<<" start called :"<<std::get<0>(call)<<" calls \n";
74  }
75  mProfiledRoutines[routineName]=call;
76  } else {
77  tRoutineCall &call=iter->second;
78  std::get<0>(call)++;
79  //start time of the call
80  std::get<2>(call).push_back(std::chrono::high_resolution_clock::now());
82  std::cout<<routineName<<" start called :"<<std::get<0>(call)<<" calls \n";
83  }
84  }
85 #endif
86  }//end StartCallRoutine(routineName) method
87 
91  inline static void EndCallRoutine(const tString& routineName) {
92 #ifdef USE_PROFILER
93  auto iter=mProfiledRoutines.find(routineName);
94  if (iter!=mProfiledRoutines.end()) {
95  tRoutineCall &call=iter->second;
96  //time of the end call
97  tWallTime timer=std::chrono::high_resolution_clock::now();
98  tWallTime& start=std::get<2>(call).back();
99  //duration of the call
100  std::chrono::duration<tReal,std::milli> duration=(timer-start);
101  //add the duration
102  std::get<1>(call)+=duration.count();
103  //remove the last calls
104  std::get<2>(call).pop_back();
106  std::cout<<routineName<<" end called :"<<std::get<0>(call)<<" calls, duration:"<<(std::get<1>(call)/1000.0L)<<" (s) \n";
107  }
108  }
109 
110 #endif
111  }//end EndCallRoutine(routineName) method
112 
115  inline static void PrintCallRoutines(std::ostream& out) {
116 #ifdef USE_PROFILER
117 
118  out<<"Called profiles Routines : \n";
119  for(const auto& routines: mProfiledRoutines) {
120  out<<"\t"<<routines.first<<" : ";
121  const tRoutineCall &call=routines.second;
122  out<<"\t nCalls:"<<std::get<0>(call);
123  out<<"\t time:"<<(std::get<1>(call)/1000.0L)<<" (s)";
124  out<<"\t number of not ended calls:"<<(std::get<2>(call).size())<<"\n";
125  }
126 
127 #endif
128  }//end PrintCallRoutine(out)
129 
130 };
131 #endif
abstract base class for most classes.
Definition: CORE_Profiler.h:26
virtual ~CORE_Profiler()
destroy the instance of object std
Definition: CORE_Profiler.h:52
static void PrintCallRoutines(std::ostream &out)
print the routine called
Definition: CORE_Profiler.h:115
static void EndCallRoutine(const tString &routineName)
end calling routine
Definition: CORE_Profiler.h:91
CORE_Profiler()
build an instance of the object
Definition: CORE_Profiler.h:46
static tBoolean IS_PROFILING_ROUTINE_PRINTED
indicates if the enter & leaving of method is printed on screen
Definition: CORE_Profiler.h:33
static void StartCallRoutine(const tString &routineName)
start calling routine
Definition: CORE_Profiler.h:61