QMCPACK
TimerManager.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2020 QMCPACK developers.
6 //
7 // File developed by: Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
9 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
10 //
11 // File created by: Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
12 //////////////////////////////////////////////////////////////////////////////////////
13 
14 
15 /** @file TimerManager.h
16  * @brief timer_manager class.
17  */
18 #ifndef QMCPLUSPLUS_TIMER_MANAGER_H
19 #define QMCPLUSPLUS_TIMER_MANAGER_H
20 
21 #include <vector>
22 #include <string>
23 #include <mutex>
24 #include <map>
25 #include <memory>
26 #include <type_traits>
27 #include "NewTimer.h"
28 #include "config.h"
29 #include "OhmmsData/Libxml2Doc.h"
30 
31 #ifdef USE_VTUNE_TASKS
32 #include <ittnotify.h>
33 #endif
34 
35 class Communicate;
36 
37 namespace qmcplusplus
38 {
39 /** Manager creates timers and handle reports
40  * @tparam TIMER regular or fake timer
41  *
42  * TimerManager is generally not thread-safe.
43  * Thread-safe functions are noted below.
44  */
45 template<class TIMER>
46 class TimerManager
47 {
48 private:
49  /// All the timers created by this manager
50  std::vector<std::unique_ptr<TIMER>> timer_storage_;
51  /// mutex for TimerList
52  std::mutex timer_list_lock_;
53  /// The stack of nested active timers
54  std::vector<TIMER*> CurrentTimerStack;
55  /// The threshold for active timers
57  /// The current maximal timer id
59  /// status of maxmal timer id reached
61  /// timer id to name mapping
62  std::map<timer_id_t, std::string> timer_id_name;
63  /// name to timer id mapping
64  std::map<std::string, timer_id_t> timer_name_to_id;
65 
66  void initializeTimer(TIMER& t);
67 
70 
71 public:
72 #ifdef USE_VTUNE_TASKS
73  __itt_domain* task_domain;
74 #endif
75 
77  {
78 #ifdef USE_VTUNE_TASKS
79  task_domain = __itt_domain_create("QMCPACK");
80 #endif
81  }
82 
83  /// Create a new timer object registred in this manager. This call is thread-safe.
84  TIMER* createTimer(const std::string& myname, timer_levels mytimer = timer_level_fine);
85 
86  void push_timer(TIMER* t);
87 
88  void pop_timer(TIMER* t);
89 
90  TIMER* current_timer()
91  {
92  TIMER* current = nullptr;
93  if (CurrentTimerStack.size() > 0)
94  current = CurrentTimerStack.back();
95 
96  return current;
97  }
98 
99  void set_timer_threshold(const timer_levels threshold);
100  void set_timer_threshold(const std::string& threshold);
101  std::string get_timer_threshold_string() const;
102 
104 
105  void reset();
106  void print(Communicate* comm);
107 
108  using nameList_t = std::map<std::string, int>;
109  using timeList_t = std::vector<double>;
110  using callList_t = std::vector<long>;
111  using names_t = std::vector<std::string>;
112 
114  {
118  };
119 
121  {
127  };
128 
130 
132 
133  void output_timing(Communicate* comm, Libxml2Document& doc, xmlNodePtr root);
134 
135  void get_stack_name_from_id(const StackKey& key, std::string& name);
136 };
137 
138 extern template class TimerManager<NewTimer>;
139 extern template class TimerManager<FakeTimer>;
140 
142 
143 NewTimer& createGlobalTimer(const std::string& myname, timer_levels mylevel = timer_level_fine);
144 
145 // Helpers to make it easier to define a set of timers
146 // See tests/test_timer.cpp for an example
147 
148 template<class T>
150 {
151  T id;
152  const std::string name;
153 };
154 
155 template<class T>
156 using TimerNameList_t = std::vector<TimerIDName_t<T>>;
157 
158 template<class TIMER>
159 class TimerList : public std::vector<std::reference_wrapper<TIMER>>
160 {
161 public:
162  template<class T>
164  const TimerNameList_t<T>& timer_list,
165  timer_levels timer_level = timer_level_fine)
166  {
167  this->reserve(timer_list.size());
168  for (std::size_t i = 0; i < timer_list.size(); i++)
169  {
170  if (i != static_cast<std::underlying_type_t<T>>(timer_list[i].id))
171  throw std::runtime_error("Mismatch between index and enumeration");
172  this->push_back(*manager.createTimer(timer_list[i].name, timer_level));
173  }
174  }
175 };
176 
178 
179 } // namespace qmcplusplus
180 #endif
class that handles xmlDoc
Definition: Libxml2Doc.h:76
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
void output_timing(Communicate *comm, Libxml2Document &doc, xmlNodePtr root)
std::vector< TimerIDName_t< T > > TimerNameList_t
Definition: TimerManager.h:156
std::map< timer_id_t, std::string > timer_id_name
timer id to name mapping
Definition: TimerManager.h:62
void collate_stack_profile(Communicate *comm, StackProfileData &p)
timer_levels timer_threshold
The threshold for active timers.
Definition: TimerManager.h:56
void print_flat(Communicate *comm)
const std::string name
Definition: TimerManager.h:152
void initializeTimer(TIMER &t)
bool max_timers_exceeded
status of maxmal timer id reached
Definition: TimerManager.h:60
Timer accumulates time and call counts.
Definition: NewTimer.h:135
Manager creates timers and handle reports.
Definition: NewTimer.h:40
void print(Communicate *comm)
unsigned char timer_id_t
Definition: NewTimer.h:56
Wrapping information on parallelism.
Definition: Communicate.h:68
std::vector< std::unique_ptr< TIMER > > timer_storage_
All the timers created by this manager.
Definition: TimerManager.h:50
std::vector< TIMER * > CurrentTimerStack
The stack of nested active timers.
Definition: TimerManager.h:54
TimerList(TimerManager< TIMER > &manager, const TimerNameList_t< T > &timer_list, timer_levels timer_level=timer_level_fine)
Definition: TimerManager.h:163
NewTimer & createGlobalTimer(const std::string &myname, timer_levels mylevel)
void collate_flat_profile(Communicate *comm, FlatProfileData &p)
TIMER * createTimer(const std::string &myname, timer_levels mytimer=timer_level_fine)
Create a new timer object registred in this manager. This call is thread-safe.
std::mutex timer_list_lock_
mutex for TimerList
Definition: TimerManager.h:52
bool maximum_number_of_timers_exceeded() const
Definition: TimerManager.h:103
void get_stack_name_from_id(const StackKey &key, std::string &name)
void set_timer_threshold(const timer_levels threshold)
TimerManager< NewTimer > & getGlobalTimerManager()
void print_stack(Communicate *comm)
std::string get_timer_threshold_string() const
NewTimer class various high-resolution timers.
timer_id_t max_timer_id
The current maximal timer id.
Definition: TimerManager.h:58
std::map< std::string, timer_id_t > timer_name_to_id
name to timer id mapping
Definition: TimerManager.h:64