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
22 TimePointT start;
23 DurationT elapsed;
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 */
74 ~Monitor() {
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
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:67
xgboost::common::Monitor::~Monitor
~Monitor()
Definition: timer.h:74
xgboost::common::Monitor::Init
void Init(std::string label)
Definition: timer.h:82
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