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.
compute.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#pragma once
26
28#include "timemory/math/abs.hpp"
31#include "timemory/math/fwd.hpp"
32#include "timemory/math/max.hpp"
33#include "timemory/math/min.hpp"
38#include "timemory/math/pow.hpp"
39#include "timemory/math/sqr.hpp"
41
42namespace tim
43{
44namespace math
45{
46/// \struct tim::math::compute
47/// \brief Struct for performing math operations on complex data structures without using
48/// globally overload operators (e.g. `lhs += rhs`) and generic functions (`lhs =
49/// abs(rhs)`)
50///
51template <typename Tp, typename Up = Tp>
52struct compute
53{
55 using type = Tp;
56 using value_type = Up;
57
58 static TIMEMORY_INLINE decltype(auto) abs(const type& _v)
59 {
60 return this_type::abs(_v, 0);
61 }
62
63 static TIMEMORY_INLINE decltype(auto) sqr(const type& _v)
64 {
65 return this_type::sqr(_v, 0);
66 }
67
68 static TIMEMORY_INLINE decltype(auto) sqrt(const type& _v)
69 {
70 return this_type::sqrt(_v, 0);
71 }
72
73 template <typename V>
74 static TIMEMORY_INLINE decltype(auto) min(const type& _l, const V& _r)
75 {
76 return this_type::min(_l, _r, 0);
77 }
78
79 template <typename V>
80 static TIMEMORY_INLINE decltype(auto) max(const type& _l, const V& _r)
81 {
82 return this_type::max(_l, _r, 0);
83 }
84
85 template <typename V>
86 static TIMEMORY_INLINE decltype(auto) percent_diff(const type& _l, const V& _r)
87 {
88 return this_type::percent_diff(_l, _r, 0);
89 }
90
91 // reference
92 template <typename V>
93 static TIMEMORY_INLINE decltype(auto) plus(type& _l, const V& _r)
94 {
95 return this_type::plus(_l, _r, 0);
96 }
97
98 template <typename V>
99 static TIMEMORY_INLINE decltype(auto) minus(type& _l, const V& _r)
100 {
101 return this_type::minus(_l, _r, 0);
102 }
103
104 template <typename V>
105 static TIMEMORY_INLINE decltype(auto) multiply(type& _l, const V& _r)
106 {
107 return this_type::multiply(_l, _r, 0);
108 }
109
110 template <typename V>
111 static TIMEMORY_INLINE decltype(auto) divide(type& _l, const V& _r)
112 {
113 return this_type::divide(_l, _r, 0);
114 }
115
116 // const ref
117 template <typename V>
118 static TIMEMORY_INLINE auto plus(const type& _l, const V& _r)
119 {
120 type _t{ _l };
121 return this_type::plus(_t, _r, 0);
122 }
123
124 template <typename V>
125 static TIMEMORY_INLINE auto minus(const type& _l, const V& _r)
126 {
127 type _t{ _l };
128 return this_type::minus(_t, _r, 0);
129 }
130
131 template <typename V>
132 static TIMEMORY_INLINE auto multiply(const type& _l, const V& _r)
133 {
134 type _t{ _l };
135 return this_type::multiply(_t, _r, 0);
136 }
137
138 template <typename V>
139 static TIMEMORY_INLINE auto divide(const type& _l, const V& _r)
140 {
141 type _t{ _l };
142 return this_type::divide(_t, _r, 0);
143 }
144
145private:
146 //----------------------------------------------------------------------------------//
147 // tim::math overload available
148 //
149 template <typename V>
150 static TIMEMORY_INLINE auto abs(const V& _v, int) -> decltype(::tim::math::abs(_v))
151 {
153 }
154
155 template <typename V>
156 static TIMEMORY_INLINE auto sqr(const V& _v, int) -> decltype(::tim::math::sqr(_v))
157 {
159 }
160
161 template <typename V>
162 static TIMEMORY_INLINE auto sqrt(const V& _v, int) -> decltype(::tim::math::sqrt(_v))
163 {
165 }
166
167 template <typename V>
168 static TIMEMORY_INLINE auto min(const type& _l, const V& _r, int)
170 ::tim::math::min(_l, _r))
171 {
173 }
174
175 template <typename V>
176 static TIMEMORY_INLINE auto max(const type& _l, const V& _r, int)
178 ::tim::math::max(_l, _r))
179 {
181 }
182
183 template <typename V>
184 static TIMEMORY_INLINE auto percent_diff(const type& _l, const V& _r, int)
187 {
189 }
190
191 template <typename V>
192 static TIMEMORY_INLINE auto plus(type& _l, const V& _r, int)
194 std::declval<type&>())
195 {
197 }
198
199 template <typename V>
200 static TIMEMORY_INLINE auto minus(type& _l, const V& _r, int)
202 std::declval<type&>())
203 {
205 }
206
207 template <typename V>
208 static TIMEMORY_INLINE auto multiply(type& _l, const V& _r, int)
210 std::declval<type&>())
211 {
213 }
214
215 template <typename V, typename U = void>
216 static TIMEMORY_INLINE auto divide(type& _l, const V& _r, int)
218 std::declval<type&>())
219 {
221 }
222
223 //----------------------------------------------------------------------------------//
224 // no tim::math overload available
225 //
226 template <typename V>
227 static TIMEMORY_INLINE auto abs(const V& _v, long)
228 {
229 return _v;
230 }
231
232 template <typename V>
233 static TIMEMORY_INLINE auto sqr(const V& _v, long)
234 {
235 return _v;
236 }
237
238 template <typename V>
239 static TIMEMORY_INLINE auto sqrt(const V& _v, long)
240 {
241 return _v;
242 }
243
244 template <typename V>
245 static TIMEMORY_INLINE auto min(const type& _l, const V&, long)
246 {
247 return _l;
248 }
249
250 template <typename V>
251 static TIMEMORY_INLINE auto max(const type& _l, const V&, long)
252 {
253 return _l;
254 }
255
256 template <typename V>
257 static TIMEMORY_INLINE auto percent_diff(const type& _l, const V&, long)
258 {
259 return _l;
260 }
261
262 template <typename V>
263 static TIMEMORY_INLINE auto& plus(type& _l, const V&, long)
264 {
265 return _l;
266 }
267
268 template <typename V>
269 static TIMEMORY_INLINE auto& minus(type& _l, const V&, long)
270 {
271 return _l;
272 }
273
274 template <typename V>
275 static TIMEMORY_INLINE auto& multiply(type& _l, const V&, long)
276 {
277 return _l;
278 }
279
280 template <typename V, typename U = void>
281 static TIMEMORY_INLINE auto& divide(type& _l, const V&, long)
282 {
283 return _l;
284 }
285};
286
287//--------------------------------------------------------------------------------------//
288
289} // namespace math
290} // namespace tim
291
292#define TIMEMORY_MATH_NULL_TYPE_COMPUTE(TYPE) \
293 namespace tim \
294 { \
295 namespace math \
296 { \
297 template <> \
298 struct compute<TYPE, TYPE> \
299 { \
300 using type = TYPE; \
301 static type abs(const type&) { return type{}; } \
302 static type sqr(const type&) { return type{}; } \
303 static type sqrt(const type&) { return type{}; } \
304 static type max(const type&, const type&) { return type{}; } \
305 static type min(const type&, const type&) { return type{}; } \
306 static type percent_diff(const type&, const type&) { return type{}; } \
307 \
308 template <typename Vp> \
309 static decltype(auto) plus(type& lhs, const Vp&) \
310 { \
311 return lhs; \
312 } \
313 template <typename Vp> \
314 static decltype(auto) minus(type& lhs, const Vp&) \
315 { \
316 return lhs; \
317 } \
318 template <typename Vp> \
319 static decltype(auto) multiply(type& lhs, const Vp&) \
320 { \
321 return lhs; \
322 } \
323 template <typename Vp> \
324 static decltype(auto) divide(type& lhs, const Vp&) \
325 { \
326 return lhs; \
327 } \
328 }; \
329 } \
330 }
331
#define TIMEMORY_MATH_NULL_TYPE_COMPUTE(TYPE)
Definition: compute.hpp:292
::tim::statistics< Tp > max(::tim::statistics< Tp > lhs, const Tp &rhs)
Definition: statistics.hpp:320
::tim::statistics< Tp > min(::tim::statistics< Tp > lhs, const Tp &rhs)
Definition: statistics.hpp:329
type_list abs(type_list<>)
Definition: fwd.hpp:405
Tp & plus(Tp &, const Up &)
Definition: plus.hpp:106
type_list sqr(type_list<>)
Definition: fwd.hpp:405
auto divide(Tp &_lhs, Up _rhs, type_list<>,...) -> decltype(_lhs/=_rhs, void())
Definition: divide.hpp:43
Tp & minus(Tp &, const Up &)
Definition: minus.hpp:98
Tp & multiply(Tp &, const Up &)
Definition: multiply.hpp:140
Tp percent_diff(const Tp &, const Tp &)
type_list sqrt(type_list<>)
Definition: fwd.hpp:405
Definition: kokkosp.cpp:39
lightweight tuple-alternative for meta-programming logic
Definition: types.hpp:233
static constexpr auto value
Definition: types.hpp:818
Struct for performing math operations on complex data structures without using globally overload oper...
Definition: compute.hpp:53
static auto multiply(const type &_l, const V &_r)
Definition: compute.hpp:132
static auto divide(const type &_l, const V &_r)
Definition: compute.hpp:139
static decltype(auto) sqr(const type &_v)
Definition: compute.hpp:63
static decltype(auto) abs(const type &_v)
Definition: compute.hpp:58
static decltype(auto) sqrt(const type &_v)
Definition: compute.hpp:68
static decltype(auto) max(const type &_l, const V &_r)
Definition: compute.hpp:80
static decltype(auto) min(const type &_l, const V &_r)
Definition: compute.hpp:74
static decltype(auto) minus(type &_l, const V &_r)
Definition: compute.hpp:99
static auto minus(const type &_l, const V &_r)
Definition: compute.hpp:125
static decltype(auto) percent_diff(const type &_l, const V &_r)
Definition: compute.hpp:86
static decltype(auto) plus(type &_l, const V &_r)
Definition: compute.hpp:93
static decltype(auto) multiply(type &_l, const V &_r)
Definition: compute.hpp:105
static decltype(auto) divide(type &_l, const V &_r)
Definition: compute.hpp:111
static auto plus(const type &_l, const V &_r)
Definition: compute.hpp:118
this is a placeholder type for optional type-traits. It is used as the default type for the type-trai...
Definition: types.hpp:225