timemory  3.2.1
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
declaration.hpp
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2020, The Regents of the University of California,
4 // through Lawrence Berkeley National Laboratory (subject to receipt of any
5 // required approvals from the U.S. Dept. of Energy). All rights reserved.
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 
25 #pragma once
26 
30 #include "timemory/mpl/types.hpp"
34 #include "timemory/tpls/cereal/cereal.hpp"
35 
36 namespace tim
37 {
38 namespace component
39 {
40 //
41 template <typename Tp>
42 using graph_iterator_t = typename graph<node::graph<Tp>>::iterator;
43 //
44 template <typename Tp>
45 using graph_const_iterator_t = typename graph<node::graph<Tp>>::const_iterator;
46 //
47 /// \struct tim::component::empty_base
48 /// \brief The default base class for timemory components.
49 struct empty_base
50 {};
51 //
52 //======================================================================================//
53 //
54 // static polymorphism base
55 // base component class for all components with non-void types
56 //
57 //======================================================================================//
58 //
59 /// \struct tim::component::base<Tp, Value>
60 /// \tparam Tp the component type
61 /// \tparam Value the value type of the component (overrides tim::data<T>)
62 ///
63 /// \brief A helper static polymorphic base class for components. It is not required to be
64 /// used but generally recommended for ease of implementation.
65 ///
66 template <typename Tp, typename Value>
67 struct base
68 : public trait::dynamic_base<Tp>::type
69 , private base_state
70 , private base_data_t<Tp, Value>
71 , public concepts::component
72 {
73  using EmptyT = std::tuple<>;
74  template <typename U>
75  using vector_t = std::vector<U>;
76 
77 public:
78  static constexpr bool is_component = true;
79  using Type = Tp;
80  using value_type = Value;
82  using accum_type = typename data_type::accum_type;
83  using last_type = typename data_type::last_type;
86 
87  using this_type = Tp;
94  using fmtflags = std::ios_base::fmtflags;
95 
96 private:
97  friend struct node::graph<Tp>;
98  friend struct operation::init_storage<Tp>;
99  friend struct operation::fini_storage<Tp>;
100  friend struct operation::cache<Tp>;
101  friend struct operation::construct<Tp>;
102  friend struct operation::set_prefix<Tp>;
103  friend struct operation::push_node<Tp>;
104  friend struct operation::pop_node<Tp>;
105  friend struct operation::record<Tp>;
106  friend struct operation::reset<Tp>;
107  friend struct operation::measure<Tp>;
108  friend struct operation::start<Tp>;
109  friend struct operation::stop<Tp>;
110  friend struct operation::set_started<Tp>;
111  friend struct operation::set_stopped<Tp>;
112  friend struct operation::minus<Tp>;
113  friend struct operation::plus<Tp>;
114  friend struct operation::multiply<Tp>;
115  friend struct operation::divide<Tp>;
116  friend struct operation::base_printer<Tp>;
117  friend struct operation::print<Tp>;
118  friend struct operation::print_storage<Tp>;
119  friend struct operation::copy<Tp>;
120  friend struct operation::sample<Tp>;
121  friend struct operation::serialization<Tp>;
122  friend struct operation::finalize::get<Tp, true>;
123  friend struct operation::finalize::get<Tp, false>;
124  friend struct operation::finalize::merge<Tp, true>;
125  friend struct operation::finalize::merge<Tp, false>;
126  friend struct operation::finalize::print<Tp, true>;
127  friend struct operation::finalize::print<Tp, false>;
128 
129  template <typename Ret, typename Lhs, typename Rhs>
130  friend struct operation::compose;
131 
132  static_assert(std::is_pointer<Tp>::value == false, "Error pointer base type");
133 
134 public:
135  TIMEMORY_DEFAULT_OBJECT(base)
136 
137 public:
138  template <typename... Args>
139  static void configure(Args&&...)
140  {}
141 
142 public:
143  /// get the opaque binding for user-bundle
145 
146  /// store that start has been called
147  void set_started();
148 
149  /// store that stop has been called
150  void set_stopped();
151 
152  /// reset the values
153  void reset();
154 
155  /// assign type to a pointer
156  void get(void*& ptr, size_t _typeid_hash) const;
157 
158  /// retrieve the current measurement value in the units for the type
159  TIMEMORY_NODISCARD auto get() const { return this->load(); }
160 
161  /// retrieve the current measurement value in the units for the type in a format
162  /// that can be piped to the output stream operator ('<<')
163  TIMEMORY_NODISCARD auto get_display() const { return this->load(); }
164 
165  Type& operator+=(const Type& rhs) { return plus_oper(rhs); }
166  Type& operator-=(const Type& rhs) { return minus_oper(rhs); }
167  Type& operator*=(const Type& rhs) { return multiply_oper(rhs); }
168  Type& operator/=(const Type& rhs) { return divide_oper(rhs); }
169 
170  Type& operator+=(const Value& rhs) { return plus_oper(rhs); }
171  Type& operator-=(const Value& rhs) { return minus_oper(rhs); }
172  Type& operator*=(const Value& rhs) { return multiply_oper(rhs); }
173  Type& operator/=(const Value& rhs) { return divide_oper(rhs); }
174 
175  template <typename Up = Tp>
176  void print(std::ostream&,
178 
179  template <typename Up = Tp>
180  void print(std::ostream&,
182 
183  friend std::ostream& operator<<(std::ostream& os, const base_type& obj)
184  {
185  obj.print(os);
186  return os;
187  }
188 
189  /// serialization load (input)
190  template <typename Archive, typename Up = Type,
192  void load(Archive& ar, unsigned int);
193 
194  /// serialization store (output)
195  template <typename Archive, typename Up = Type,
197  void save(Archive& ar, unsigned int version) const;
198 
199  template <typename Vp, typename Up = Tp,
201  static void add_sample(Vp&&); /// add a sample
202 
203  /// get number of measurement
204  TIMEMORY_NODISCARD TIMEMORY_INLINE int64_t get_laps() const { return laps; }
205  TIMEMORY_NODISCARD TIMEMORY_INLINE auto get_iterator() const { return graph_itr; }
206  TIMEMORY_INLINE void set_laps(int64_t v) { laps = v; }
207  TIMEMORY_INLINE void set_iterator(graph_iterator itr) { graph_itr = itr; }
208 
215  using data_type::get_accum;
216  using data_type::get_last;
217  using data_type::get_value;
218 
225  using data_type::set_accum;
226  using data_type::set_last;
227  using data_type::set_value;
228 
229  decltype(auto) load() { return data_type::load(get_is_transient()); }
230  TIMEMORY_NODISCARD decltype(auto) load() const
231  {
233  }
234 
236 
237 protected:
238  Type& plus_oper(const Type& rhs);
239  Type& minus_oper(const Type& rhs);
240  Type& multiply_oper(const Type& rhs);
241  Type& divide_oper(const Type& rhs);
242 
243  Type& plus_oper(const Value& rhs);
244  Type& minus_oper(const Value& rhs);
245  Type& multiply_oper(const Value& rhs);
246  Type& divide_oper(const Value& rhs);
247 
248  TIMEMORY_INLINE void plus(const base_type& rhs)
249  {
250  laps += rhs.laps;
251  if(rhs.get_is_transient())
252  set_is_transient(rhs.get_is_transient());
253  }
254 
255  TIMEMORY_INLINE void minus(const base_type& rhs)
256  {
257  laps -= rhs.laps;
258  if(rhs.get_is_transient())
259  set_is_transient(rhs.get_is_transient());
260  }
261 
262 public:
263  TIMEMORY_INLINE auto plus(crtp::base, const base_type& rhs) { this->plus(rhs); }
264  TIMEMORY_INLINE auto minus(crtp::base, const base_type& rhs) { this->minus(rhs); }
265 
266 protected:
267  int64_t laps = 0;
269 
270  using data_type::accum;
271  using data_type::last;
272  using data_type::value;
273 
274 public:
280  static constexpr auto ios_fixed = std::ios_base::fixed;
281  static constexpr auto ios_decimal = std::ios_base::dec;
282  static constexpr auto ios_showpoint = std::ios_base::showpoint;
284 
285 #if !defined(DOXYGEN_SHOULD_SKIP_THIS)
286  static const short precision = percent_units_v ? 1 : 3;
287  static const short width = percent_units_v ? 6 : 8;
288 #endif
289 
290  template <typename Up = Type, typename UnitT = typename trait::units<Up>::type,
292  static int64_t unit();
293 
294  template <typename Up = Type,
295  typename UnitT = typename trait::units<Up>::display_type,
298 
299  template <typename Up = Type, typename UnitT = typename trait::units<Up>::type,
301  static int64_t get_unit();
302 
303  template <typename Up = Type,
304  typename UnitT = typename trait::units<Up>::display_type,
307 
308  static short get_width();
309  static short get_precision();
311  static std::string label();
315 };
316 //
317 //======================================================================================//
318 //
319 // static polymorphic base
320 // base component class for all components with void types
321 //
322 //======================================================================================//
323 //
324 template <typename Tp>
325 struct base<Tp, void>
326 : public trait::dynamic_base<Tp>::type
327 , private base_state
328 , public concepts::component
329 {
330  using EmptyT = std::tuple<>;
331 
332 public:
333  static constexpr bool is_component = true;
334  using Type = Tp;
335  using value_type = void;
336  using accum_type = void;
337  using last_type = void;
339  using cache_type = typename trait::cache<Tp>::type;
340 
341  using this_type = Tp;
344 
345 private:
346  friend struct node::graph<Tp>;
347  friend struct operation::init_storage<Tp>;
348  friend struct operation::fini_storage<Tp>;
349  friend struct operation::cache<Tp>;
350  friend struct operation::construct<Tp>;
351  friend struct operation::set_prefix<Tp>;
352  friend struct operation::push_node<Tp>;
353  friend struct operation::pop_node<Tp>;
354  friend struct operation::record<Tp>;
355  friend struct operation::reset<Tp>;
356  friend struct operation::measure<Tp>;
357  friend struct operation::start<Tp>;
358  friend struct operation::stop<Tp>;
359  friend struct operation::set_started<Tp>;
360  friend struct operation::set_stopped<Tp>;
361  friend struct operation::minus<Tp>;
362  friend struct operation::plus<Tp>;
363  friend struct operation::multiply<Tp>;
364  friend struct operation::divide<Tp>;
365  friend struct operation::print<Tp>;
366  friend struct operation::print_storage<Tp>;
367  friend struct operation::copy<Tp>;
368  friend struct operation::serialization<Tp>;
369 
370  template <typename Ret, typename Lhs, typename Rhs>
371  friend struct operation::compose;
372 
373 public:
374  TIMEMORY_DEFAULT_OBJECT(base)
375 
376 public:
377  template <typename... Args>
378  static void configure(Args&&...)
379  {}
380 
381 public:
382  /// get the opaque binding for user-bundle
383  static opaque get_opaque(scope::config);
384 
385  void set_started();
386  void set_stopped();
387  void reset();
388  TIMEMORY_NODISCARD TIMEMORY_INLINE int64_t get_laps() const { return 0; }
389  TIMEMORY_NODISCARD TIMEMORY_INLINE void* get_iterator() const { return nullptr; }
390 
391  TIMEMORY_INLINE void set_laps(int64_t) {}
392  TIMEMORY_INLINE void set_iterator(void*) {}
393 
394  friend std::ostream& operator<<(std::ostream& os, const base_type&) { return os; }
395 
396  TIMEMORY_INLINE void get() {}
397  TIMEMORY_INLINE void get(void*& ptr, size_t _typeid_hash) const;
398 
399  void operator+=(const base_type&) {}
400  void operator-=(const base_type&) {}
401 
402  void operator+=(const Type&) {}
403  void operator-=(const Type&) {}
404 
411 
418 
419 protected:
420  TIMEMORY_INLINE void plus(const base_type& rhs)
421  {
422  if(rhs.get_is_transient())
423  set_is_transient(rhs.get_is_transient());
424  }
425 
426  TIMEMORY_INLINE void minus(const base_type& rhs)
427  {
428  if(rhs.get_is_transient())
429  set_is_transient(rhs.get_is_transient());
430  }
431 
432 public:
433  TIMEMORY_INLINE auto plus(crtp::base, const base_type& rhs) { this->plus(rhs); }
434  TIMEMORY_INLINE auto minus(crtp::base, const base_type& rhs) { this->minus(rhs); }
435 
436 public:
437  //
438  // components with void data types do not use label()/get_label()
439  // to generate an output filename so provide a default one from
440  // (potentially demangled) typeid(Type).name() and strip out
441  // namespace and any template parameters + replace any spaces
442  // with underscores
443  //
444  static std::string label();
445  static std::string description();
446  static std::string get_label();
447  static std::string get_description();
448 };
449 //
450 } // namespace component
451 } // namespace tim
452 
Arbitrary Graph / Tree (i.e. binary-tree but not binary). It is unlikely that this class will interac...
Definition: graph.hpp:336
Declare the base component types.
void load(Archive &ar, tim::node::graph< Tp > &d)
Definition: node.hpp:520
typename internal::base_data< Tp, ValueT >::type base_data_t
Definition: data.hpp:519
typename graph< node::graph< Tp > >::const_iterator graph_const_iterator_t
Definition: declaration.hpp:45
typename graph< node::graph< Tp > >::iterator graph_iterator_t
Definition: declaration.hpp:42
typename std::enable_if< B, T >::type enable_if_t
Definition: types.hpp:58
a generic type for prioritizing a function call to the base class over derived functions,...
Definition: types.hpp:311
This operation class is used for copying the object generically.
Definition: copy.hpp:52
std::string display_type
Definition: kokkosp.cpp:38
tim::mpl::apply< std::string > string
Definition: macros.hpp:52
precision
Definition: settings.cpp:1337
Declare the operations types.
Declare the storage types.
Provide state configuration options for a component instance. The current states are:
Definition: data.hpp:446
void set_is_transient(bool v)
Definition: data.hpp:464
bool get_is_flat() const
Definition: data.hpp:458
void set_is_on_stack(bool v)
Definition: data.hpp:463
bool get_depth_change() const
Definition: data.hpp:459
void set_is_running(bool v)
Definition: data.hpp:462
bool get_is_on_stack() const
Definition: data.hpp:456
bool get_is_transient() const
Definition: data.hpp:457
void set_depth_change(bool v)
Definition: data.hpp:466
bool get_is_running() const
Definition: data.hpp:455
bool get_is_invalid() const
Definition: data.hpp:460
void set_is_invalid(bool v)
Definition: data.hpp:467
void set_is_flat(bool v)
Definition: data.hpp:465
Type & operator-=(const Type &rhs)
static short get_precision()
Type & operator/=(const Value &rhs)
void set_is_transient(bool v)
Definition: data.hpp:464
Type & minus_oper(const Type &rhs)
decltype(auto) load()
graph_iterator graph_itr
base_data_t< Tp, Value > data_type
Definition: declaration.hpp:81
auto get_iterator() const
static base_storage_type * get_storage()
Type & operator*=(const Type &rhs)
void reset()
reset the values
Type & minus_oper(const Value &rhs)
graph_iterator_t< Tp > graph_iterator
Definition: declaration.hpp:91
auto get_display() const
retrieve the current measurement value in the units for the type in a format that can be piped to the...
static constexpr bool memory_category_v
static int64_t get_unit()
Type & operator*=(const Value &rhs)
void save(Archive &ar, unsigned int version) const
serialization store (output)
static constexpr auto ios_showpoint
friend std::ostream & operator<<(std::ostream &os, const base_type &obj)
void print(std::ostream &, enable_if_t<!trait::uses_value_storage< Up, Value >::value, long >=0) const
static constexpr bool timing_category_v
static std::string get_label()
void set_iterator(graph_iterator itr)
base< Tp, Value > base_type
Definition: declaration.hpp:88
std::tuple<> EmptyT
Definition: declaration.hpp:73
int64_t get_laps() const
add a sample
Type & multiply_oper(const Value &rhs)
Type & multiply_oper(const Type &rhs)
auto plus(crtp::base, const base_type &rhs)
Type & plus_oper(const Type &rhs)
Type & operator+=(const Value &rhs)
auto minus(crtp::base, const base_type &rhs)
void set_stopped()
store that stop has been called
bool get_is_transient() const
Definition: data.hpp:457
static int64_t unit()
static std::string display_unit()
Type & operator+=(const Type &rhs)
static std::string get_display_unit()
Type & operator/=(const Type &rhs)
static const fmtflags format_flags
typename data_type::accum_type accum_type
Definition: declaration.hpp:82
Type & divide_oper(const Value &rhs)
static short get_width()
static constexpr auto ios_fixed
void load(Archive &ar, unsigned int)
serialization load (input)
Type & plus_oper(const Value &rhs)
static void add_sample(Vp &&)
typename data_type::last_type last_type
Definition: declaration.hpp:83
static void configure(Args &&...)
static fmtflags get_format_flags()
static opaque get_opaque(scope::config)
get the opaque binding for user-bundle
static std::string label()
void print(std::ostream &, enable_if_t< trait::uses_value_storage< Up, Value >::value, int >=0) const
storage< Tp, Value > storage_type
Definition: declaration.hpp:90
static constexpr bool timing_units_v
Type & divide_oper(const Type &rhs)
void minus(const base_type &rhs)
static constexpr bool percent_units_v
static constexpr bool memory_units_v
static constexpr bool is_component
Definition: declaration.hpp:78
auto get() const
retrieve the current measurement value in the units for the type
std::vector< U > vector_t
Definition: declaration.hpp:75
Type & operator-=(const Value &rhs)
void get(void *&ptr, size_t _typeid_hash) const
assign type to a pointer
void plus(const base_type &rhs)
void set_started()
store that start has been called
static std::string get_description()
void set_laps(int64_t v)
static std::string description()
typename trait::dynamic_base< Tp >::type dynamic_type
Definition: declaration.hpp:84
typename trait::cache< Tp >::type cache_type
Definition: declaration.hpp:85
static constexpr auto ios_decimal
std::ios_base::fmtflags fmtflags
Definition: declaration.hpp:94
The default base class for timemory components.
Definition: declaration.hpp:50
This is the compact representation of a measurement in the call-graph.
Definition: node.hpp:139
this is a placeholder type for optional type-traits. It is used as the default type for the type-trai...
Definition: types.hpp:225
invoked from the base class to provide default printing behavior
The purpose of this operation class is operating on two components to compose a result,...
Definition: compose.hpp:53
The purpose of this operation class is construct an object with specific args.
Definition: construct.hpp:52
This operation class is used for division of a component.
Definition: math.hpp:191
This operation class is used for a single measurement for a component.
Definition: measure.hpp:52
Define subtraction operations.
Definition: math.hpp:104
This operation class is used for multiplication of a component.
Definition: math.hpp:156
Define addition operations.
Definition: math.hpp:52
Print the storage for a component.
print routines for individual components
Definition: print.hpp:59
This operation class is used for sampling.
Definition: sample.hpp:52
Call the set_prefix member function. These instantiations are always inlined because of the use of st...
Definition: set.hpp:57
This operation attempts to call a member function which the component provides to internally store wh...
Definition: types.hpp:472
This operation attempts to call a member function which the component provides to internally store wh...
Definition: types.hpp:505
Specification of how to accumulate statistics. This will not be used unless tim::trait::statistics ha...
Definition: policy.hpp:48
this data type encodes the options of storage scope. The default is hierarchical (tree) scope....
Definition: types.hpp:446
trait that designates the type the static polymorphic base class (tim::component::base) inherit from.
Definition: types.hpp:113
trait that designates the width and precision should follow formatting settings related to memory mea...
trait that designates the width and precision should follow formatting settings related to timing mea...
trait that designates the units should follow unit settings related to memory measurements
trait that designates the units are a percentage
trait that designates the units should follow unit settings related to timing measurements
This trait is used to determine whether the (expensive) instantiation of the storage class happens.