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.
add_statistics.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/or sell
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/**
26 * \file timemory/operations/types/add_statistics.hpp
27 * \brief Definition for various functions for add_statistics in operations
28 */
29
30#pragma once
31
36
37namespace tim
38{
39//
40//--------------------------------------------------------------------------------------//
41//
42namespace policy
43{
44//
45//--------------------------------------------------------------------------------------//
46//
47// Configure the default method for policy::record_statistics
48//
49//--------------------------------------------------------------------------------------//
50//
51template <typename CompT, typename Tp>
52inline void
54 bool _last)
55{
56 using component_type = std::remove_pointer_t<decay_t<CompT>>;
57 using result_type = decltype(std::declval<CompT>().get());
58 static_assert(std::is_same<result_type, Tp>::value,
59 "Error! The default implementation of "
60 "'policy::record_statistics<Component, T>::operator()' requires 'T' "
61 "to be the same type as the return type from 'Component::get()'");
62
63 if(!_last)
64 {
65 if(_obj.get_laps() < 2)
66 {
67 _stats += _obj.get();
68 }
69 else
70 {
72 "Updating statistics<%s> skipped for %s. Laps: %lu > "
73 "1",
74 demangle<Tp>().c_str(),
75 demangle<component_type>().c_str(),
76 (unsigned long) _obj.get_laps());
77 }
78 }
79 else
80 {
81 constexpr bool has_accum = trait::base_has_accum<component_type>::value;
82 // make a copy and replace the accumulation with last measurement
83 auto _tmp = _obj;
84 IF_CONSTEXPR(has_accum) { _tmp.set_accum(_tmp.get_last()); }
85 else { _tmp.set_value(_tmp.get_last()); }
86 _stats += _tmp.get();
87 }
88}
89//
90//--------------------------------------------------------------------------------------//
91//
92} // namespace policy
93//
94//--------------------------------------------------------------------------------------//
95//
96namespace operation
97{
98//
99//--------------------------------------------------------------------------------------//
100//
101///
102/// \struct tim::operation::add_statistics
103/// \brief
104/// Enabling statistics in timemory has two parts:
105/// 1. tim::trait::record_statistics must be set to true for component
106/// 2. tim::trait::statistics must set the data type of the statistics
107/// - this is usually set to the data type returned from get()
108/// - tuple<> is the default and will fully disable statistics unless changed
109///
110//
111//--------------------------------------------------------------------------------------//
112//
113template <typename T>
115{
116 using type = T;
117
118 TIMEMORY_DEFAULT_OBJECT(add_statistics)
119
120 //----------------------------------------------------------------------------------//
121 // if statistics is not enabled
122 //
123 template <typename StatsT>
124 TIMEMORY_INLINE add_statistics(const type& _obj, StatsT& _stats, bool _last = false)
125 {
126 (*this)(_obj, _stats, _last);
127 }
128
129 //----------------------------------------------------------------------------------//
130 // if statistics is not enabled
131 //
132 TIMEMORY_INLINE add_statistics(const type& _obj, bool _last = false)
133 {
134 (*this)(_obj, _last);
135 }
136
137 //----------------------------------------------------------------------------------//
138 // generic operator
139 //
140 template <typename U>
141 TIMEMORY_INLINE auto operator()(const U& rhs, bool _last = false) const
142 {
143 return sfinae(rhs, 0, 0, _last);
144 }
145
146 //----------------------------------------------------------------------------------//
147 // if statistics is enabled
148 //
149 template <typename StatsT, typename U = type>
150 TIMEMORY_INLINE void operator()(
151 const U& rhs, StatsT& stats, bool _last = false,
153
154 //----------------------------------------------------------------------------------//
155 // if statistics is not enabled
156 //
157 template <typename StatsT, typename U = type>
158 TIMEMORY_INLINE void operator()(
159 const U&, StatsT&, bool = true,
161 {}
162
163private:
164 template <typename U>
165 TIMEMORY_INLINE auto sfinae(const U& rhs, int, int, bool _last) const
166 -> decltype(rhs.update_statistics(_last))
167 {
168 return rhs.update_statistics(_last);
169 }
170
171 template <typename U>
172 TIMEMORY_INLINE auto sfinae(const U& rhs, int, long, bool _last) const
173 -> decltype(rhs.get_iterator()->stats(),
174 decay_t<decltype(rhs.get_iterator()->stats())>{})
175 {
176 using stats_type = decay_t<decltype(rhs.get_iterator()->stats())>;
177 auto itr = rhs.get_iterator();
178 if(itr)
179 {
180 stats_type& _stats = itr->stats();
181 (*this)(rhs, _stats, _last);
182 return _stats;
183 }
184 return stats_type{};
185 }
186
187 template <typename U>
188 TIMEMORY_INLINE void sfinae(const U&, long, long, bool) const
189 {}
190};
191//
192template <typename T>
193template <typename StatsT, typename U>
194void
196 const U& rhs, StatsT& stats, bool _last,
198{
199 // for type comparison
200 using incoming_t = decay_t<typename StatsT::value_type>;
202 // check the incomming stat type against declared stat type
203 // but allow for permissive_statistics when there is an acceptable
204 // implicit conversion
206 std::is_same<incoming_t, expected_t>::value,
207 "add_statistics was passed a data type different than declared "
208 "trait::statistics type. To disable this error, e.g. permit "
209 "implicit conversion, set trait::permissive_statistics "
210 "to true_type for component");
211 using stats_policy_type = policy::record_statistics<U>;
212 DEBUG_PRINT_HERE("%s :: updating %s (accum: %s)", demangle<U>().c_str(),
213 demangle<StatsT>().c_str(), (_last) ? "y" : "n");
214 stats_policy_type{}(stats, rhs, _last);
215}
216//
217} // namespace operation
218} // namespace tim
#define IF_CONSTEXPR(...)
Definition: language.hpp:72
Definition: kokkosp.cpp:39
typename std::decay< T >::type decay_t
Alias template for decay.
Definition: types.hpp:194
typename std::enable_if< B, T >::type enable_if_t
Alias template for enable_if.
Definition: types.hpp:190
The declaration for the types for operations without definitions.
Include the macros for operations.
Declare the operations types.
Enabling statistics in timemory has two parts:
add_statistics(const type &_obj, StatsT &_stats, bool _last=false)
add_statistics(const type &_obj, bool _last=false)
auto operator()(const U &rhs, bool _last=false) const
void operator()(const U &, StatsT &, bool=true, enable_if_t<!enabled_statistics< U, StatsT >::value, int >=0) const
Specification of how to accumulate statistics. This will not be used unless tim::trait::statistics ha...
Definition: policy.hpp:48
void operator()(statistics< type > &, const CompT &, bool _accum=true)
A generic class for statistical accumulation. It uses the timemory math overloads to enable statistic...
Definition: statistics.hpp:83
trait that signifies that a component has an accumulation value. In general, most components implemen...
Definition: type_traits.hpp:81
trait that will suppress compilation error in operation::add_statistics<Component> if the data type p...
#define CONDITIONAL_PRINT_HERE(CONDITION,...)
Definition: macros.hpp:183
#define DEBUG_PRINT_HERE(...)
Definition: macros.hpp:168