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.
templates.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
29#include "timemory/mpl/math.hpp"
33#include "timemory/units.hpp"
34
35#include <cassert>
36
37namespace tim
38{
39namespace component
40{
41//
42//======================================================================================//
43//
44// NON-VOID BASE
45//
46//======================================================================================//
47//
48template <typename Tp, typename Value>
49template <typename Vp, typename Up, enable_if_t<trait::sampler<Up>::value, int>>
50void
52{
53 auto _storage = static_cast<storage_type*>(get_storage());
54 assert(_storage != nullptr);
55 if(_storage)
56 _storage->add_sample(std::forward<Vp>(_obj));
57}
58//
59//--------------------------------------------------------------------------------------//
60//
61// Units, display units, width, precision, labels, display labels
62//
63//--------------------------------------------------------------------------------------//
64//
65template <typename Tp, typename Value>
66template <typename Up, typename Unit,
67 enable_if_t<std::is_same<Unit, int64_t>::value, int>>
68int64_t
70{
71 IF_CONSTEXPR(timing_units_v) { return units::sec; }
72 IF_CONSTEXPR(memory_units_v) { return units::megabyte; }
73 IF_CONSTEXPR(percent_units_v) { return 1; }
74
75 return 1;
76}
77//
78//--------------------------------------------------------------------------------------//
79//
80template <typename Tp, typename Value>
81template <typename Up, typename Unit,
82 enable_if_t<std::is_same<Unit, std::string>::value, int>>
85{
86 IF_CONSTEXPR(timing_units_v) { return units::time_repr(unit()); }
87 IF_CONSTEXPR(memory_units_v) { return units::mem_repr(unit()); }
88 IF_CONSTEXPR(percent_units_v) { return "%"; }
89 return "";
90}
91//
92//--------------------------------------------------------------------------------------//
93//
94template <typename Tp, typename Value>
95template <typename Up, typename Unit,
96 enable_if_t<std::is_same<Unit, int64_t>::value, int>>
97int64_t
99{
100 static int64_t _instance = []() {
101 auto _value = Type::unit();
102 IF_CONSTEXPR(timing_units_v)
103 {
104 if(!settings::timing_units().empty())
105 {
106 _value = std::get<1>(units::get_timing_unit(settings::timing_units()));
107 }
108 }
109 IF_CONSTEXPR(memory_units_v)
110 {
111 if(!settings::memory_units().empty())
112 {
113 _value = std::get<1>(units::get_memory_unit(settings::memory_units()));
114 }
115 }
116 return _value;
117 }();
118 return _instance;
119}
120//
121//--------------------------------------------------------------------------------------//
122//
123template <typename Tp, typename Value>
124template <typename Up, typename Unit,
125 enable_if_t<std::is_same<Unit, std::string>::value, int>>
128{
129 static std::string _instance = Type::display_unit();
130
131 IF_CONSTEXPR(timing_units_v)
132 {
133 // _instance = std::get<0>(units::get_timing_unit(Type::unit()));
134 auto&& _unit_setting = settings::timing_units();
135 if(!_unit_setting.empty())
136 {
137 _instance = std::get<0>(units::get_timing_unit(_unit_setting));
138 }
139 }
140
141 IF_CONSTEXPR(memory_units_v)
142 {
143 // _instance = std::get<0>(units::get_memory_unit(Type::unit()));
144 auto&& _unit_setting = settings::memory_units();
145 if(!_unit_setting.empty())
146 {
147 _instance = std::get<0>(units::get_memory_unit(_unit_setting));
148 }
149 }
150
151 return _instance;
152}
153//
154//--------------------------------------------------------------------------------------//
155//
156template <typename Tp, typename Value>
157template <typename Up>
158void
160 std::ostream&, enable_if_t<!trait::uses_value_storage<Up, Value>::value, long>) const
161{}
162//
163//--------------------------------------------------------------------------------------//
164//
165} // namespace component
166} // namespace tim
167//
168
171#include "timemory/tpls/cereal/cereal.hpp"
172
173namespace tim
174{
175namespace component
176{
177//
178template <typename Tp, typename Value>
179template <typename Up>
180void
183{
184 operation::base_printer<Up>(os, static_cast<const Up&>(*this));
185}
186//
187//--------------------------------------------------------------------------------------//
188//
189template <typename Tp, typename Value>
190template <typename Archive, typename Up,
191 enable_if_t<!trait::custom_serialization<Up>::value, int>>
192void
193base<Tp, Value>::save(Archive& ar, const unsigned int version) const
194{
195 operation::serialization<Type>(static_cast<const Type&>(*this), ar, version);
196}
197//
198template <typename Tp, typename Value>
199template <typename Archive, typename Up,
200 enable_if_t<!trait::custom_serialization<Up>::value, int>>
201void
202base<Tp, Value>::load(Archive& ar, const unsigned int version)
203{
204 auto try_catch = [](Archive& arch, const char* key, auto& val) {
205 try
206 {
207 arch(cereal::make_nvp(key, val));
208 } catch(cereal::Exception& e)
209 {
210 if(settings::debug() || settings::verbose() > -1)
211 fprintf(stderr, "Warning! '%s' threw exception: %s\n", key, e.what());
212 }
213 };
214
215 // bool _transient = get_is_transient();
216 // try_catch(ar, "is_transient", _transient);
217 try_catch(ar, "laps", laps);
218 data_type::serialize(ar, version);
219 set_is_transient(true); // assume always transient
220}
221//
222} // namespace component
223} // namespace tim
Definition for various functions for base_printer in operations.
Declare the base component types.
#define IF_CONSTEXPR(...)
Definition: language.hpp:72
typename std::enable_if< B, T >::type enable_if_t
Definition: types.hpp:58
void serialize(std::string fname, exec_data< Counter > &obj)
Definition: counter.hpp:325
std::tuple< std::string, int64_t > get_timing_unit(std::string _unit)
Definition: units.hpp:229
std::string mem_repr(int64_t _unit)
Definition: units.hpp:164
std::string time_repr(int64_t _unit)
Definition: units.hpp:145
std::tuple< std::string, int64_t > get_memory_unit(std::string _unit)
Definition: units.hpp:188
Definition: kokkosp.cpp:39
memory_units
Definition: settings.cpp:1657
timing_units
Definition: settings.cpp:1650
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
const std::string std::ostream * os
The declaration for the types for storage without definitions.
decltype(auto) load()
static int64_t get_unit()
void save(Archive &ar, unsigned int version) const
serialization store (output)
static int64_t unit()
static std::string display_unit()
static std::string get_display_unit()
static void add_sample(Vp &&)
void print(std::ostream &, enable_if_t< trait::uses_value_storage< Up, Value >::value, int >=0) const
static constexpr bool value