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 
11 #if defined(XGBOOST_USE_NVTX) && defined(__CUDACC__)
12 #include <nvToolsExt.h>
13 #endif
14 
15 namespace xgboost {
16 namespace common {
17 struct Timer {
18  using ClockT = std::chrono::high_resolution_clock;
19  using TimePointT = std::chrono::high_resolution_clock::time_point;
20  using DurationT = std::chrono::high_resolution_clock::duration;
21  using SecondsT = std::chrono::duration<double>;
22 
25  Timer() { Reset(); }
26  void Reset() {
27  elapsed = DurationT::zero();
28  Start();
29  }
30  void Start() { start = ClockT::now(); }
31  void Stop() { elapsed += ClockT::now() - start; }
32  double ElapsedSeconds() const { return SecondsT(elapsed).count(); }
33  void PrintElapsed(std::string label) {
34  char buffer[255];
35  snprintf(buffer, sizeof(buffer), "%s:\t %fs", label.c_str(),
36  SecondsT(elapsed).count());
37  LOG(CONSOLE) << buffer;
38  Reset();
39  }
40 };
41 
49 struct Monitor {
50  private:
51  struct Statistics {
52  Timer timer;
53  size_t count{0};
54  uint64_t nvtx_id;
55  };
56  std::string label = "";
57  std::map<std::string, Statistics> statistics_map;
58  Timer self_timer;
59 
60  public:
61  Monitor() { self_timer.Start(); }
62 
64  if (!ConsoleLogger::ShouldLog(ConsoleLogger::LV::kDebug)) return;
65 
66  LOG(CONSOLE) << "======== Monitor: " << label << " ========";
67  for (auto &kv : statistics_map) {
68  if (kv.second.count == 0) {
69  LOG(WARNING) <<
70  "Timer for " << kv.first << " did not get stopped properly.";
71  continue;
72  }
73  LOG(CONSOLE) << kv.first << ": " << kv.second.timer.ElapsedSeconds()
74  << "s, " << kv.second.count << " calls @ "
75  << std::chrono::duration_cast<std::chrono::microseconds>(
76  kv.second.timer.elapsed / kv.second.count)
77  .count()
78  << "us";
79  }
80  self_timer.Stop();
81  }
82  void Init(std::string label) { this->label = label; }
83  void Start(const std::string &name) {
84  if (ConsoleLogger::ShouldLog(ConsoleLogger::LV::kDebug)) {
85  statistics_map[name].timer.Start();
86  }
87  }
88  void Stop(const std::string &name) {
89  if (ConsoleLogger::ShouldLog(ConsoleLogger::LV::kDebug)) {
90  auto &stats = statistics_map[name];
91  stats.timer.Stop();
92  stats.count++;
93  }
94  }
95  void StartCuda(const std::string &name) {
96  if (ConsoleLogger::ShouldLog(ConsoleLogger::LV::kDebug)) {
97  auto &stats = statistics_map[name];
98  stats.timer.Start();
99 #if defined(XGBOOST_USE_NVTX) && defined(__CUDACC__)
100  stats.nvtx_id = nvtxRangeStartA(name.c_str());
101 #endif
102  }
103  }
104  void StopCuda(const std::string &name) {
105  if (ConsoleLogger::ShouldLog(ConsoleLogger::LV::kDebug)) {
106  auto &stats = statistics_map[name];
107  stats.timer.Stop();
108  stats.count++;
109 #if defined(XGBOOST_USE_NVTX) && defined(__CUDACC__)
110  nvtxRangeEnd(stats.nvtx_id);
111 #endif
112  }
113  }
114 };
115 } // namespace common
116 } // namespace xgboost
Definition: timer.h:17
std::chrono::duration< double > SecondsT
Definition: timer.h:21
void Reset()
Definition: timer.h:26
void Stop()
Definition: timer.h:31
std::chrono::high_resolution_clock::duration DurationT
Definition: timer.h:20
~Monitor()
Definition: timer.h:63
std::chrono::high_resolution_clock ClockT
Definition: timer.h:18
void Stop(const std::string &name)
Definition: timer.h:88
void StopCuda(const std::string &name)
Definition: timer.h:104
void Init(std::string label)
Definition: timer.h:82
void Start(const std::string &name)
Definition: timer.h:83
Timer()
Definition: timer.h:25
double ElapsedSeconds() const
Definition: timer.h:32
namespace of xgboost
Definition: base.h:79
DurationT elapsed
Definition: timer.h:24
Timing utility used to measure total method execution time over the lifetime of the containing object...
Definition: timer.h:49
void Start()
Definition: timer.h:30
void StartCuda(const std::string &name)
Definition: timer.h:95
TimePointT start
Definition: timer.h:23
Monitor()
Definition: timer.h:61
std::chrono::high_resolution_clock::time_point TimePointT
Definition: timer.h:19
void PrintElapsed(std::string label)
Definition: timer.h:33