LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
timer_map.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.
26 #ifndef LBANN_UTILS_TIMER_MAP_HPP_INCLUDED
27 #define LBANN_UTILS_TIMER_MAP_HPP_INCLUDED
28 
30 
31 #include <algorithm>
32 #include <iomanip>
33 #include <iostream>
34 #include <list>
35 #include <numeric>
36 #include <string>
37 
38 namespace lbann {
39 
51 class TimerMap
52 {
53 public:
54  TimerMap(std::string const& key);
55 
56  std::string const& key() const noexcept;
57  AccumulatingTimer& timer() noexcept;
58  AccumulatingTimer const& timer() const noexcept;
59 
60  TimerMap& scope(std::string const& key);
61  TimerMap const& scope(std::string const& key) const;
62 
63  void print(std::ostream& os) const;
64 
65 private:
66  void print_impl(std::ostream& os, unsigned width, unsigned indent) const;
67 
68 private:
69  std::string m_key;
71  std::list<TimerMap> m_subscopes;
72 
73 }; // class TimerMap
74 
76 {
78 
79 public:
80  ScopeTimer(TimerMap& timer, std::string const& scope_name);
81  ScopeTimer(ScopeTimer& timer, std::string const& scope_name);
82  ~ScopeTimer() noexcept;
83 }; // class ScopeTimer
84 
85 template <typename TimerT>
86 auto time_scope(TimerT& timer, std::string const& scope_name)
87 {
88  return ScopeTimer{timer, scope_name};
89 }
90 
91 // Implementation
92 
93 inline TimerMap::TimerMap(std::string const& key) : m_key{key} {}
94 
95 inline std::string const& TimerMap::key() const noexcept { return m_key; }
96 inline AccumulatingTimer& TimerMap::timer() noexcept { return m_timer; }
97 inline AccumulatingTimer const& TimerMap::timer() const noexcept
98 {
99  return m_timer;
100 }
101 
102 inline auto TimerMap::scope(std::string const& key) -> TimerMap&
103 {
104  auto iter = std::find_if(begin(m_subscopes),
105  end(m_subscopes),
106  [&key](auto& t) { return t.key() == key; });
107  if (iter == end(m_subscopes))
108  return m_subscopes.emplace_back(key);
109  else
110  return *iter;
111 }
112 
113 inline ScopeTimer::ScopeTimer(TimerMap& timer, std::string const& scope_name)
114  : m_timer{&(timer.scope(scope_name))}
115 {
116  m_timer->timer().start();
117 }
118 
119 inline ScopeTimer::ScopeTimer(ScopeTimer& timer, std::string const& scope_name)
120  : ScopeTimer{*(timer.m_timer), scope_name}
121 {}
122 
123 inline ScopeTimer::~ScopeTimer() noexcept
124 {
125  m_timer->timer().stop();
126  m_timer = nullptr;
127 }
128 
129 } // namespace lbann
130 #endif // LBANN_UTILS_TIMER_MAP_HPP_INCLUDED
TimerMap(std::string const &key)
Definition: timer_map.hpp:93
Timer that accumulates mean and variance of timer durations as it goes.
std::list< TimerMap > m_subscopes
Definition: timer_map.hpp:71
TimerMap & scope(std::string const &key)
Definition: timer_map.hpp:102
double stop() noexcept
Get the elapsed time for this duration in seconds.
void start() noexcept
Start counting time for this duration sample.
std::string m_key
Definition: timer_map.hpp:69
A nesting inclusive-timer.
Definition: timer_map.hpp:51
auto time_scope(TimerT &timer, std::string const &scope_name)
Definition: timer_map.hpp:86
void print(std::ostream &os) const
void print_impl(std::ostream &os, unsigned width, unsigned indent) const
TimerMap * m_timer
Definition: timer_map.hpp:77
AccumulatingTimer & timer() noexcept
Definition: timer_map.hpp:96
ScopeTimer(TimerMap &timer, std::string const &scope_name)
Definition: timer_map.hpp:113
~ScopeTimer() noexcept
Definition: timer_map.hpp:123
std::string const & key() const noexcept
Definition: timer_map.hpp:95
AccumulatingTimer m_timer
Definition: timer_map.hpp:70