profiler  0.0
Profiler.h
1 #ifndef PROFILER_SRC_MOLPRO_PROFILER_PROFILER_H
2 #define PROFILER_SRC_MOLPRO_PROFILER_PROFILER_H
3 #ifdef MOLPRO_PROFILER_MPI
4 // #include <mpi.h>
5 #endif
6 #include <molpro/mpi.h>
7 
8 #include <molpro/profiler/SortBy.h>
9 
10 #include <fstream>
11 #include <memory>
12 #include <ostream>
13 #include <stdexcept>
14 #include <string>
15 #include <vector>
16 
17 namespace molpro {
18 namespace profiler {
19 
20 static int hot_default[3] = {255, 0, 0};
21 static int cool_default[3] = {0, 0, 255};
22 static std::vector<std::pair<double, double>> default_heat_adjust = {{}};
23 
24 class Counter;
25 template <class CounterT>
26 class Node;
27 
42 class Profiler {
43 protected:
44  std::string m_description;
45  std::string m_root_name = "All";
47  int m_current_depth = 0;
48 public:
49  std::shared_ptr<profiler::Node<profiler::Counter>> root;
50  std::shared_ptr<profiler::Node<profiler::Counter>> active_node;
57  explicit Profiler(std::string description_, bool with_wall = true, bool with_cpu = false);
58  Profiler(Profiler&&) = default;
59  Profiler& operator=(Profiler&&) = default;
60  ~Profiler();
61 
62  Profiler() = delete;
63  Profiler(const Profiler&) = delete;
64  Profiler& operator=(const Profiler&) = delete;
65 
66  const std::string& description() const { return m_description; }
67 
70  static std::shared_ptr<Profiler> single(const std::string& description_, bool with_wall = true,
71  bool with_cpu = false);
73  static std::shared_ptr<Profiler> single();
75  static void erase(const std::string& description);
76 
85  int get_max_depth() const;
87  void set_max_depth(int new_max_depth);
90  int get_current_depth() const;
91 
97  Profiler& start(const std::string& name);
98 
100  Profiler& stop(const std::string& name = "");
101 
103  Profiler& stop_all();
104 
107 
108  // FIXME Why is this even needed?
110  Profiler& reset(const std::string& name);
111 
116  void operator+=(size_t operations);
117 
121  void operator++();
122 
123  size_t operator++(int);
124 
125 protected:
127  struct Proxy {
129 
130  Proxy(Profiler& prof, const std::string& name) : prof(prof) { prof.start(name); }
131  ~Proxy() { prof.stop(); }
132 
134  Proxy push(const std::string& name) { return Proxy(prof, name); }
135 
140  void operator+=(size_t operations);
141 
145  void operator++();
146 
147  size_t operator++(int);
148  };
149 
150 public:
152  Proxy push(const std::string& name) { return Proxy(*this, name); }
153 
156  std::string str(bool cumulative = true, profiler::SortBy sort_by = profiler::SortBy::wall) const;
157 
174  std::string dotgraph(std::string path, double threshold = 0.01, bool cumulative = true, int hot[3] = hot_default,
175  int cool[3] = cool_default, SortBy sort_by = profiler::SortBy::none,
176  std::vector<std::pair<double, double>> heat_adjust = default_heat_adjust,
177  bool get_percentage_time = false);
178 
179 #ifdef MOLPRO_PROFILER_MPI
180  std::string str(MPI_Comm communicator, bool cumulative = true,
181  profiler::SortBy sort_by = profiler::SortBy::wall) const;
182 
201  std::string dotgraph(std::string path, MPI_Comm communicator, int root_process, double threshold = 0.01,
202  bool cumulative = true, int hot[3] = hot_default, int cool[3] = cool_default,
203  SortBy sort_by = profiler::SortBy::none,
204  std::vector<std::pair<double, double>> heat_adjust = default_heat_adjust,
205  bool get_percentage_time = false);
206 #endif
207 
208  friend std::ostream& operator<<(std::ostream& os, const Profiler& obj);
209 };
210 
211 } // namespace profiler
212 } // namespace molpro
213 #endif // PROFILER_SRC_MOLPRO_PROFILER_PROFILER_H
Resource counter used for storing operation count, call count, timing information.
Definition: Counter.h:15
A node in a parameter tree storing a Counter object aliased by a name.
Definition: Node.h:20
Instrumental profiler for timing sections of code.
Definition: Profiler.h:42
std::shared_ptr< profiler::Node< profiler::Counter > > root
root node of the profiler call tree
Definition: Profiler.h:49
void operator++()
Advance the counter holding the notional number of operations executed in the code segment.
Definition: Profiler.cpp:77
std::string dotgraph(std::string path, double threshold=0.01, bool cumulative=true, int hot[3]=hot_default, int cool[3]=cool_default, SortBy sort_by=profiler::SortBy::none, std::vector< std::pair< double, double >> heat_adjust=default_heat_adjust, bool get_percentage_time=false)
Get a graphviz .dot markup file for a profile.
Definition: Profiler.cpp:115
profiler::Counter & counter()
Access counter at the top of the call stack.
Definition: Profiler.cpp:73
static void erase(const std::string &description)
Remove Profiler with specified description from the singleton register.
Definition: Profiler.cpp:32
int m_max_depth
max depth level of profiler tree counting root as 0. Defaults to largest possible value.
Definition: Profiler.h:46
std::shared_ptr< profiler::Node< profiler::Counter > > active_node
the most recent active node.
Definition: Profiler.h:50
std::string m_description
name of the root node
Definition: Profiler.h:44
Profiler & stop_all()
Stop all nodes and traverse up to the root.
Definition: Profiler.cpp:66
std::string m_root_name
name of the root node
Definition: Profiler.h:45
void set_max_depth(int new_max_depth)
Set the maximum depth the profiler tree is allowed to reach.
Definition: Profiler.cpp:35
Profiler & operator=(Profiler &&)=default
void operator+=(size_t operations)
Advance the counter holding the notional number of operations executed in the code segment.
Definition: Profiler.cpp:75
int get_max_depth() const
Get the maximum depth the profiler tree is allowed to reach.
Definition: Profiler.cpp:34
Profiler & start(const std::string &name)
Traverse down to a child node and start timing.
Definition: Profiler.cpp:38
std::string str(bool cumulative=true, profiler::SortBy sort_by=profiler::SortBy::wall) const
Definition: Profiler.cpp:95
const std::string & description() const
Definition: Profiler.h:66
Profiler & operator=(const Profiler &)=delete
~Profiler()
Definition: Profiler.cpp:15
friend std::ostream & operator<<(std::ostream &os, const Profiler &obj)
Definition: Profiler.cpp:150
Profiler & stop(const std::string &name="")
Stop the active node and traverse up to its parent.
Definition: Profiler.cpp:54
int m_current_depth
current depth of the active node
Definition: Profiler.h:47
Proxy push(const std::string &name)
Returns a proxy object which will start() on construction and stop on destruction.
Definition: Profiler.h:152
Profiler & reset(const std::string &name)
Erases all data and starts from root again.
Definition: Profiler.cpp:154
int get_current_depth() const
Definition: Profiler.cpp:36
Profiler(const Profiler &)=delete
static std::shared_ptr< Profiler > single()
Access the last registered Profiler.
Definition: Profiler.cpp:30
Profiler(Profiler &&)=default
SortBy
Definition: SortBy.h:7
Definition: Profiler.h:5
Proxy object that calls start() on creation and stop() on destruction.
Definition: Profiler.h:127
Profiler & prof
reference to the underlying profiler
Definition: Profiler.h:128
~Proxy()
Definition: Profiler.h:131
void operator+=(size_t operations)
Advance the counter holding the notional number of operations executed in the code segment.
Definition: Profiler.cpp:85
Proxy(Profiler &prof, const std::string &name)
Definition: Profiler.h:130
void operator++()
Advance the counter holding the notional number of operations executed in the code segment.
Definition: Profiler.cpp:87
Proxy push(const std::string &name)
Push a new proxy.
Definition: Profiler.h:134