LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
utils/timer.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
25 //
26 // lbann_timer .hpp - Wrapper around time functionality
28 
29 #ifndef LBANN_UTILS_TIMER_HPP_INCLUDED
30 #define LBANN_UTILS_TIMER_HPP_INCLUDED
31 
32 #include <chrono>
33 
34 namespace lbann {
35 
37 inline double get_time()
38 {
39  using namespace std::chrono;
40  return duration_cast<duration<double>>(steady_clock::now().time_since_epoch())
41  .count();
42 }
43 
52 class Timer
53 {
54 public:
61  using ClockType = std::chrono::steady_clock;
63  using TimePoint = typename ClockType::time_point;
64 
65 public:
66  Timer() = default;
67  ~Timer() noexcept = default;
68  Timer(Timer const&) = delete;
69  Timer& operator=(Timer const&) = delete;
70  Timer(Timer&&) = default;
71  Timer& operator=(Timer&&) = default;
72 
80  void start() noexcept { m_start = ClockType::now(); }
81 
89  double stop() noexcept
90  {
91  auto elapsed_time = this->check();
92  this->reset();
93  return elapsed_time;
94  }
95 
97  double check() const noexcept
98  {
99  using seconds_type = std::chrono::duration<double>;
100  return running() ? seconds_type(ClockType::now() - m_start).count() : 0.;
101  }
102 
106  void reset() noexcept { m_start = TimePoint{}; }
107 
109  bool running() const noexcept { return m_start != TimePoint{}; }
110 
111 private:
113 
114 }; // class Timer
115 
116 } // namespace lbann
117 
118 #endif // LBANN_UTILS_TIMER_HPP_INCLUDED
void reset() noexcept
Clear any internal state in the timer.
double check() const noexcept
Get the current elapsed time (seconds) without stopping.
Definition: utils/timer.hpp:97
bool running() const noexcept
Check if the timer is running.
TimePoint m_start
double get_time()
Return time in fractional seconds since an epoch.
Definition: utils/timer.hpp:37
typename ClockType::time_point TimePoint
Simplifying typedef.
Definition: utils/timer.hpp:63
An exceedingly simple duration calculator.
Definition: utils/timer.hpp:52
double stop() noexcept
Get the total elapsed time in seconds.
Definition: utils/timer.hpp:89
std::chrono::steady_clock ClockType
The clock used for counting.
Definition: utils/timer.hpp:61