timemory 3.3.0
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
35#include "timemory/tpls/cereal/cereal.hpp"
36
37namespace tim
38{
39namespace component
40{
41//
42template <typename Tp>
43using graph_iterator_t = typename graph<node::graph<Tp>>::iterator;
44//
45template <typename Tp>
46using graph_const_iterator_t = typename graph<node::graph<Tp>>::const_iterator;
47//
48/// \struct tim::component::empty_storage
49/// \brief A very lightweight storage class which provides nothing
51{
52 static constexpr empty_storage* noninit_instance() { return nullptr; }
53 constexpr bool empty() const { return true; }
54 constexpr size_t size() const { return 0; }
55 constexpr size_t true_size() const { return 0; }
56 constexpr void reset() const {}
57 constexpr void print() const {}
58 template <typename ArchiveT>
59 constexpr void do_serialize(ArchiveT&)
60 {}
61};
62/// \struct tim::component::empty_base
63/// \brief A very lightweight base which provides no storage
65{
67 using base_type = void;
68
69 void get() const {}
70
71 template <typename... Args>
72 static opaque get_opaque(Args&&...)
73 {
74 return opaque{};
75 }
76};
77//
78//======================================================================================//
79//
80// static polymorphism base
81// base component class for all components with non-void types
82//
83//======================================================================================//
84//
85/// \struct tim::component::base<Tp, Value>
86/// \tparam Tp the component type
87/// \tparam Value the value type of the component (overrides tim::data<T>)
88///
89/// \brief A helper static polymorphic base class for components. It is not required to be
90/// used but generally recommended for ease of implementation.
91///
92template <typename Tp, typename Value>
93struct base
94: public trait::dynamic_base<Tp>::type
95, private base_state
96, private base_data_t<Tp, Value>
98{
99 using EmptyT = std::tuple<>;
100 template <typename U>
101 using vector_t = std::vector<U>;
102
103public:
104 static constexpr bool is_component = true;
105 using Type = Tp;
106 using value_type = Value;
108 using accum_type = typename data_type::accum_type;
109 using last_type = typename data_type::last_type;
112
113 using this_type = Tp;
120 using fmtflags = std::ios_base::fmtflags;
121
122private:
123 friend struct node::graph<Tp>;
124 friend struct operation::init_storage<Tp>;
125 friend struct operation::fini_storage<Tp>;
126 friend struct operation::cache<Tp>;
127 friend struct operation::construct<Tp>;
128 friend struct operation::set_prefix<Tp>;
129 friend struct operation::push_node<Tp>;
130 friend struct operation::pop_node<Tp>;
131 friend struct operation::record<Tp>;
132 friend struct operation::reset<Tp>;
133 friend struct operation::measure<Tp>;
134 friend struct operation::start<Tp>;
135 friend struct operation::stop<Tp>;
136 friend struct operation::set_started<Tp>;
137 friend struct operation::set_stopped<Tp>;
138 friend struct operation::minus<Tp>;
139 friend struct operation::plus<Tp>;
140 friend struct operation::multiply<Tp>;
141 friend struct operation::divide<Tp>;
142 friend struct operation::base_printer<Tp>;
143 friend struct operation::print<Tp>;
144 friend struct operation::print_storage<Tp>;
145 friend struct operation::copy<Tp>;
146 friend struct operation::sample<Tp>;
147 friend struct operation::serialization<Tp>;
148 friend struct operation::finalize::get<Tp, true>;
149 friend struct operation::finalize::get<Tp, false>;
150 friend struct operation::finalize::merge<Tp, true>;
151 friend struct operation::finalize::merge<Tp, false>;
152 friend struct operation::finalize::print<Tp, true>;
153 friend struct operation::finalize::print<Tp, false>;
154
155 template <typename Ret, typename Lhs, typename Rhs>
156 friend struct operation::compose;
157
158 static_assert(std::is_pointer<Tp>::value == false, "Error pointer base type");
159
160public:
161 TIMEMORY_DEFAULT_OBJECT(base)
163
164public:
165 template <typename... Args>
166 static void configure(Args&&...)
167 {}
168
169public:
170 /// get the opaque binding for user-bundle
172
173 /// store that start has been called
175
176 /// store that stop has been called
178
179 /// reset the values
180 void reset();
181
182 /// assign type to a pointer
183 void get(void*& ptr, size_t _typeid_hash) const;
184
185 /// retrieve the current measurement value in the units for the type
186 auto get() const { return this->load(); }
187
188 /// retrieve the current measurement value in the units for the type in a format
189 /// that can be piped to the output stream operator ('<<')
190 auto get_display() const { return this->load(); }
191
192 Type& operator+=(const Type& rhs) { return plus_oper(rhs); }
193 Type& operator-=(const Type& rhs) { return minus_oper(rhs); }
194 Type& operator*=(const Type& rhs) { return multiply_oper(rhs); }
195 Type& operator/=(const Type& rhs) { return divide_oper(rhs); }
196
197 Type& operator+=(const Value& rhs) { return plus_oper(rhs); }
198 Type& operator-=(const Value& rhs) { return minus_oper(rhs); }
199 Type& operator*=(const Value& rhs) { return multiply_oper(rhs); }
200 Type& operator/=(const Value& rhs) { return divide_oper(rhs); }
201
202 template <typename Up = Tp>
203 void print(std::ostream&,
205
206 template <typename Up = Tp>
207 void print(std::ostream&,
209
210 friend std::ostream& operator<<(std::ostream& os, const base_type& obj)
211 {
212 obj.print(os);
213 return os;
214 }
215
216 /// serialization load (input)
217 template <typename Archive, typename Up = Type,
219 void load(Archive& ar, unsigned int);
220
221 /// serialization store (output)
222 template <typename Archive, typename Up = Type,
224 void save(Archive& ar, unsigned int version) const;
225
226 template <typename Vp, typename Up = Tp,
228 static void add_sample(Vp&&); /// add a sample
229
230 /// get number of measurement
231 TIMEMORY_INLINE int64_t get_laps() const { return laps; }
232 TIMEMORY_INLINE auto get_iterator() const { return graph_itr; }
233 TIMEMORY_INLINE void set_laps(int64_t v) { laps = v; }
234 TIMEMORY_INLINE void set_iterator(graph_iterator itr) { graph_itr = itr; }
235
242 using data_type::get_accum;
243 using data_type::get_last;
244 using data_type::get_value;
245
252 using data_type::set_accum;
253 using data_type::set_last;
254 using data_type::set_value;
255
256 decltype(auto) load() { return data_type::load(get_is_transient()); }
257 decltype(auto) load() const { return data_type::load(get_is_transient()); }
258
260
261protected:
262 Type& plus_oper(const Type& rhs);
263 Type& minus_oper(const Type& rhs);
264 Type& multiply_oper(const Type& rhs);
265 Type& divide_oper(const Type& rhs);
266
267 Type& plus_oper(const Value& rhs);
268 Type& minus_oper(const Value& rhs);
269 Type& multiply_oper(const Value& rhs);
270 Type& divide_oper(const Value& rhs);
271
272 TIMEMORY_INLINE void plus(const base_type& rhs)
273 {
274 laps += rhs.laps;
275 if(rhs.get_is_transient())
276 set_is_transient(rhs.get_is_transient());
277 }
278
279 TIMEMORY_INLINE void minus(const base_type& rhs)
280 {
281 laps -= rhs.laps;
282 if(rhs.get_is_transient())
283 set_is_transient(rhs.get_is_transient());
284 }
285
286public:
287 TIMEMORY_INLINE auto plus(crtp::base, const base_type& rhs) { this->plus(rhs); }
288 TIMEMORY_INLINE auto minus(crtp::base, const base_type& rhs) { this->minus(rhs); }
289
290protected:
291 int64_t laps = 0;
293
294 using data_type::accum;
295 using data_type::last;
296 using data_type::value;
297
298public:
304 static constexpr auto ios_fixed = std::ios_base::fixed;
305 static constexpr auto ios_decimal = std::ios_base::dec;
306 static constexpr auto ios_showpoint = std::ios_base::showpoint;
308
309#if !defined(DOXYGEN_SHOULD_SKIP_THIS)
310 static const short precision = percent_units_v ? 1 : 3;
311 static const short width = percent_units_v ? 6 : 8;
312#endif
313
314 template <typename Up = Type, typename UnitT = typename trait::units<Up>::type,
316 static int64_t unit();
317
318 template <typename Up = Type,
319 typename UnitT = typename trait::units<Up>::display_type,
322
323 template <typename Up = Type, typename UnitT = typename trait::units<Up>::type,
325 static int64_t get_unit();
326
327 template <typename Up = Type,
328 typename UnitT = typename trait::units<Up>::display_type,
331
332 static short get_width();
333 static short get_precision();
339};
340//
341//======================================================================================//
342//
343// static polymorphic base
344// base component class for all components with void types
345//
346//======================================================================================//
347//
348template <typename Tp>
349struct base<Tp, void>
350: public trait::dynamic_base<Tp>::type
351, private base_state
352, public concepts::component
353{
354 using EmptyT = std::tuple<>;
355
356public:
357 static constexpr bool is_component = true;
358 using Type = Tp;
359 using value_type = void;
360 using accum_type = void;
361 using last_type = void;
363 using cache_type = typename trait::cache<Tp>::type;
364
365 using this_type = Tp;
368
369private:
370 friend struct node::graph<Tp>;
371 friend struct operation::init_storage<Tp>;
372 friend struct operation::fini_storage<Tp>;
373 friend struct operation::cache<Tp>;
374 friend struct operation::construct<Tp>;
375 friend struct operation::set_prefix<Tp>;
376 friend struct operation::push_node<Tp>;
377 friend struct operation::pop_node<Tp>;
378 friend struct operation::record<Tp>;
379 friend struct operation::reset<Tp>;
380 friend struct operation::measure<Tp>;
381 friend struct operation::start<Tp>;
382 friend struct operation::stop<Tp>;
383 friend struct operation::set_started<Tp>;
384 friend struct operation::set_stopped<Tp>;
385 friend struct operation::minus<Tp>;
386 friend struct operation::plus<Tp>;
387 friend struct operation::multiply<Tp>;
388 friend struct operation::divide<Tp>;
389 friend struct operation::print<Tp>;
390 friend struct operation::print_storage<Tp>;
391 friend struct operation::copy<Tp>;
392 friend struct operation::serialization<Tp>;
393
394 template <typename Ret, typename Lhs, typename Rhs>
395 friend struct operation::compose;
396
397public:
398 TIMEMORY_DEFAULT_OBJECT(base)
399
400public:
401 template <typename... Args>
402 static void configure(Args&&...)
403 {}
404
405public:
406 /// get the opaque binding for user-bundle
407 static opaque get_opaque(scope::config);
408
409 void set_started();
410 void set_stopped();
411 void reset();
412 TIMEMORY_INLINE int64_t get_laps() const { return 0; }
413 TIMEMORY_INLINE void* get_iterator() const { return nullptr; }
414
415 TIMEMORY_INLINE void set_laps(int64_t) {}
416 TIMEMORY_INLINE void set_iterator(void*) {}
417
418 friend std::ostream& operator<<(std::ostream& os, const base_type&) { return os; }
419
420 TIMEMORY_INLINE void get() {}
421 void get(void*& ptr, size_t _typeid_hash) const;
422
423 void operator+=(const base_type&) {}
424 void operator-=(const base_type&) {}
425
426 void operator+=(const Type&) {}
427 void operator-=(const Type&) {}
428
435
442
443protected:
444 TIMEMORY_INLINE void plus(const base_type& rhs)
445 {
446 if(rhs.get_is_transient())
447 set_is_transient(rhs.get_is_transient());
448 }
449
450 TIMEMORY_INLINE void minus(const base_type& rhs)
451 {
452 if(rhs.get_is_transient())
453 set_is_transient(rhs.get_is_transient());
454 }
455
456public:
457 TIMEMORY_INLINE auto plus(crtp::base, const base_type& rhs) { this->plus(rhs); }
458 TIMEMORY_INLINE auto minus(crtp::base, const base_type& rhs) { this->minus(rhs); }
459
460public:
461 //
462 // components with void data types do not use label()/get_label()
463 // to generate an output filename so provide a default one from
464 // (potentially demangled) typeid(Type).name() and strip out
465 // namespace and any template parameters + replace any spaces
466 // with underscores
467 //
468 static std::string label();
469 static std::string description();
470 static std::string get_label();
472};
473//
474} // namespace component
475} // namespace tim
476
#define TIMEMORY_HOST_DEVICE_FUNCTION
Definition: attributes.hpp:183
Arbitrary Graph / Tree (i.e. binary-tree but not binary). It is unlikely that this class will interac...
Definition: graph.hpp:115
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:46
typename graph< node::graph< Tp > >::iterator graph_iterator_t
Definition: declaration.hpp:43
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
return false
Definition: definition.hpp:326
std::string display_type
Definition: kokkosp.cpp:39
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
const std::string std::ostream * os
precision
Definition: settings.cpp:1643
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
static short get_precision()
Type & divide_oper(const Value &rhs)
Type & plus_oper(const Value &rhs)
friend std::ostream & operator<<(std::ostream &os, const base_type &obj)
void set_is_transient(bool v)
Definition: data.hpp:464
Type & multiply_oper(const Type &rhs)
Type & operator-=(const Value &rhs)
decltype(auto) load()
graph_iterator graph_itr
static base_storage_type * get_storage()
base_data_t< Tp, Value > data_type
auto get_iterator() const
void reset()
reset the values
graph_iterator_t< Tp > graph_iterator
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
Type & minus_oper(const Type &rhs)
static int64_t get_unit()
void save(Archive &ar, unsigned int version) const
serialization store (output)
static constexpr auto ios_showpoint
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)
Type & operator+=(const Type &rhs)
Type & multiply_oper(const Value &rhs)
base< Tp, Value > base_type
std::tuple<> EmptyT
Definition: declaration.hpp:99
Type & operator*=(const Type &rhs)
int64_t get_laps() const
add a sample
Type & operator-=(const Type &rhs)
auto plus(crtp::base, const base_type &rhs)
Type & plus_oper(const Type &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()
Type & operator/=(const Value &rhs)
static std::string display_unit()
static std::string get_display_unit()
static const fmtflags format_flags
typename data_type::accum_type accum_type
static short get_width()
static constexpr auto ios_fixed
void load(Archive &ar, unsigned int)
serialization load (input)
static void add_sample(Vp &&)
typename data_type::last_type last_type
static void configure(Args &&...)
static fmtflags get_format_flags()
Type & operator/=(const Type &rhs)
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
static constexpr bool timing_units_v
Type & operator+=(const Value &rhs)
Type & divide_oper(const Type &rhs)
void minus(const base_type &rhs)
static constexpr bool percent_units_v
static constexpr bool memory_units_v
Type & minus_oper(const Value &rhs)
static constexpr bool is_component
auto get() const
retrieve the current measurement value in the units for the type
std::vector< U > vector_t
Type & operator*=(const Value &rhs)
void get(void *&ptr, size_t _typeid_hash) const
assign type to a pointer
decltype(auto) load() const
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
typename trait::cache< Tp >::type cache_type
static constexpr auto ios_decimal
std::ios_base::fmtflags fmtflags
A very lightweight base which provides no storage.
Definition: declaration.hpp:65
static opaque get_opaque(Args &&...)
Definition: declaration.hpp:72
A very lightweight storage class which provides nothing.
Definition: declaration.hpp:51
constexpr bool empty() const
Definition: declaration.hpp:53
constexpr void reset() const
Definition: declaration.hpp:56
constexpr size_t true_size() const
Definition: declaration.hpp:55
static constexpr empty_storage * noninit_instance()
Definition: declaration.hpp:52
constexpr void print() const
Definition: declaration.hpp:57
constexpr void do_serialize(ArchiveT &)
Definition: declaration.hpp:59
constexpr size_t size() const
Definition: declaration.hpp:54
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 copying the object generically.
Definition: copy.hpp:55
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:59
This operation attempts to call a member function which the component provides to internally store wh...
Definition: types.hpp:469
This operation attempts to call a member function which the component provides to internally store wh...
Definition: types.hpp:502
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:453
trait that designates the type the static polymorphic base class (tim::component::base) inherit from.
Definition: types.hpp:115
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.
typename typename typename
Definition: types.hpp:226