xgboost
timer.h
Go to the documentation of this file.
1 
4 #pragma once
5 #include <xgboost/logging.h>
6 #include <chrono>
7 #include <iostream>
8 #include <map>
9 #include <string>
10 #include <utility>
11 #include <vector>
12 
13 namespace xgboost {
14 namespace common {
15 
16 struct Timer {
17  using ClockT = std::chrono::high_resolution_clock;
18  using TimePointT = std::chrono::high_resolution_clock::time_point;
19  using DurationT = std::chrono::high_resolution_clock::duration;
20  using SecondsT = std::chrono::duration<double>;
21 
24  Timer() { Reset(); }
25  void Reset() {
26  elapsed = DurationT::zero();
27  Start();
28  }
29  void Start() { start = ClockT::now(); }
30  void Stop() { elapsed += ClockT::now() - start; }
31  double ElapsedSeconds() const { return SecondsT(elapsed).count(); }
32  void PrintElapsed(std::string label) {
33  char buffer[255];
34  snprintf(buffer, sizeof(buffer), "%s:\t %fs", label.c_str(),
35  SecondsT(elapsed).count());
36  LOG(CONSOLE) << buffer;
37  Reset();
38  }
39 };
40 
47 struct Monitor {
48  private:
49  struct Statistics {
50  Timer timer;
51  size_t count{0};
52  uint64_t nvtx_id;
53  };
54 
55  // from left to right, <name <count, elapsed>>
56  using StatMap = std::map<std::string, std::pair<size_t, size_t>>;
57 
58  std::string label_ = "";
59  std::map<std::string, Statistics> statistics_map_;
60  Timer self_timer_;
61 
63  std::vector<StatMap> CollectFromOtherRanks() const;
64  void PrintStatistics(StatMap const& statistics) const;
65 
66  public:
67  Monitor() { self_timer_.Start(); }
68  /*\brief Print statistics info during destruction.
69  *
70  * Please note that this may not work, as with distributed frameworks like Dask, the
71  * model is pickled to other workers, and the global parameters like `global_verbosity_`
72  * are not included in the pickle.
73  */
75  this->Print();
76  self_timer_.Stop();
77  }
78 
80  void Print() const;
81 
82  void Init(std::string label) { this->label_ = label; }
83  void Start(const std::string &name);
84  void Stop(const std::string &name);
85 };
86 } // namespace common
87 } // namespace xgboost
Definition: timer.h:16
std::chrono::duration< double > SecondsT
Definition: timer.h:20
void Reset()
Definition: timer.h:25
void Stop()
Definition: timer.h:30
std::chrono::high_resolution_clock::duration DurationT
Definition: timer.h:19
~Monitor()
Definition: timer.h:74
std::chrono::high_resolution_clock ClockT
Definition: timer.h:17
void Init(std::string label)
Definition: timer.h:82
Timer()
Definition: timer.h:24
double ElapsedSeconds() const
Definition: timer.h:31
namespace of xgboost
Definition: base.h:102
DurationT elapsed
Definition: timer.h:23
Timing utility used to measure total method execution time over the lifetime of the containing object...
Definition: timer.h:47
void Start()
Definition: timer.h:29
TimePointT start
Definition: timer.h:22
Monitor()
Definition: timer.h:67
std::chrono::high_resolution_clock::time_point TimePointT
Definition: timer.h:18
void PrintElapsed(std::string label)
Definition: timer.h:32