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 
62  void PrintStatistics(StatMap const& statistics) const;
63 
64  public:
65  Monitor() { self_timer_.Start(); }
66  /*\brief Print statistics info during destruction.
67  *
68  * Please note that this may not work, as with distributed frameworks like Dask, the
69  * model is pickled to other workers, and the global parameters like `global_verbosity_`
70  * are not included in the pickle.
71  */
73  this->Print();
74  self_timer_.Stop();
75  }
76 
78  void Print() const;
79 
80  void Init(std::string label) { this->label_ = label; }
81  void Start(const std::string &name);
82  void Stop(const std::string &name);
83 };
84 } // namespace common
85 } // namespace xgboost
xgboost::common::Timer::Start
void Start()
Definition: timer.h:29
xgboost::common::Timer::DurationT
std::chrono::high_resolution_clock::duration DurationT
Definition: timer.h:19
xgboost::common::Monitor::Stop
void Stop(const std::string &name)
xgboost::common::Timer::ElapsedSeconds
double ElapsedSeconds() const
Definition: timer.h:31
xgboost::common::Timer::Reset
void Reset()
Definition: timer.h:25
xgboost::common::Timer
Definition: timer.h:16
xgboost::common::Monitor::Start
void Start(const std::string &name)
xgboost::common::Monitor
Timing utility used to measure total method execution time over the lifetime of the containing object...
Definition: timer.h:47
xgboost::common::Timer::Stop
void Stop()
Definition: timer.h:30
xgboost::common::Timer::Timer
Timer()
Definition: timer.h:24
xgboost::common::Timer::ClockT
std::chrono::high_resolution_clock ClockT
Definition: timer.h:17
xgboost::common::Monitor::Monitor
Monitor()
Definition: timer.h:65
xgboost::common::Monitor::~Monitor
~Monitor()
Definition: timer.h:72
xgboost::common::Monitor::Init
void Init(std::string label)
Definition: timer.h:80
xgboost::common::Monitor::Print
void Print() const
Print all the statistics.
xgboost::common::Timer::PrintElapsed
void PrintElapsed(std::string label)
Definition: timer.h:32
xgboost::common::Timer::elapsed
DurationT elapsed
Definition: timer.h:23
xgboost::common::Timer::SecondsT
std::chrono::duration< double > SecondsT
Definition: timer.h:20
xgboost::common::Timer::start
TimePointT start
Definition: timer.h:22
xgboost::common::Timer::TimePointT
std::chrono::high_resolution_clock::time_point TimePointT
Definition: timer.h:18
xgboost
namespace of xgboost
Definition: base.h:110