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.
set.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
30
31#include <utility>
32
33namespace tim
34{
35//
36namespace component
37{
38struct base_state;
39}
40//
41namespace operation
42{
43//
44//--------------------------------------------------------------------------------------//
45//
46//
47//
48//--------------------------------------------------------------------------------------//
49//
50/// \struct tim::operation::set_prefix
51/// \tparam Tp Component type
52///
53/// \brief Call the set_prefix member function. These instantiations are always inlined
54/// because of the use of string_view. Without inlining, you will get undefined symbols
55/// in C++14 code when timemory was compiled with C++17.
56///
57template <typename Tp>
59{
60 using type = Tp;
62
63 TIMEMORY_DEFAULT_OBJECT(set_prefix)
64
65 TIMEMORY_INLINE set_prefix(type& obj, const string_t& _prefix);
66 TIMEMORY_INLINE set_prefix(type& obj, hash_value_t _nhash, const string_t& _prefix);
67
68 TIMEMORY_INLINE auto operator()(type& obj, const string_t& _prefix) const
69 {
70 return sfinae_str(obj, 0, 0, 0, _prefix);
71 }
72
73 TIMEMORY_HOT auto operator()(type& obj, hash_value_t _nhash) const
74 {
75 return sfinae_hash(obj, 0, _nhash);
76 }
77
78private:
79 // If the component has a set_prefix(const string_t&) member function
80 template <typename U>
81 TIMEMORY_INLINE auto sfinae_str(U& obj, int, int, int, const string_t& _prefix) const
82 -> decltype(obj.set_prefix(_prefix))
83 {
84 return obj.set_prefix(_prefix);
85 }
86
87 template <typename U, typename S>
88 TIMEMORY_HOT auto sfinae_str(U& obj, int, int, long, const S& _prefix) const
89 -> decltype(obj.set_prefix(_prefix.c_str()))
90 {
91 return obj.set_prefix(_prefix.c_str());
92 }
93
94 template <typename U, typename S>
95 TIMEMORY_HOT auto sfinae_str(U& obj, int, long, long, const S& _prefix) const
96 -> decltype(obj.set_prefix(_prefix.data()))
97 {
98 return obj.set_prefix(_prefix.data());
99 }
100
101 // If the component does not have a set_prefix(const string_t&) member function
102 template <typename U>
103 TIMEMORY_INLINE void sfinae_str(U&, long, long, long, const string_t&) const
104 {}
105
106private:
107 // If the component has a set_prefix(hash_value_t) member function
108 template <typename U>
109 TIMEMORY_HOT auto sfinae_hash(U& obj, int, hash_value_t _nhash) const
110 -> decltype(obj.set_prefix(_nhash))
111 {
112 return obj.set_prefix(_nhash);
113 }
114
115 // If the component does not have a set_prefix(hash_value_t) member function
116 template <typename U>
117 TIMEMORY_INLINE void sfinae_hash(U&, long, hash_value_t) const
118 {}
119};
120//
121//--------------------------------------------------------------------------------------//
122//
123template <typename Tp>
125{
126 sfinae_str(obj, 0, 0, 0, _prefix);
127}
128//
129//--------------------------------------------------------------------------------------//
130//
131template <typename Tp>
133{
134 sfinae_hash(obj, 0, _nhash);
135 sfinae_str(obj, 0, 0, 0, _prefix);
136}
137//
138//--------------------------------------------------------------------------------------//
139//
140//
141//
142//--------------------------------------------------------------------------------------//
143//
144template <typename Tp>
146{
147 using type = Tp;
148
149 TIMEMORY_DEFAULT_OBJECT(set_scope)
150
151 TIMEMORY_HOT set_scope(type& obj, scope::config _data);
152
153 TIMEMORY_HOT auto operator()(type& obj, scope::config _data) const
154 {
155 return sfinae(obj, 0, _data);
156 }
157
158private:
159 // If the component has a set_scope(...) member function
160 template <typename T>
161 TIMEMORY_HOT auto sfinae(T& obj, int, scope::config _data) const
162 -> decltype(obj.set_scope(_data))
163 {
164 return obj.set_scope(_data);
165 }
166
167 // If the component does not have a set_scope(...) member function
168 template <typename T>
169 TIMEMORY_INLINE void sfinae(T&, long, scope::config) const
170 {}
171};
172//
173//--------------------------------------------------------------------------------------//
174//
175template <typename Tp>
177{
178 sfinae(obj, 0, _data);
179}
180//
181//--------------------------------------------------------------------------------------//
182//
183//
184//
185//--------------------------------------------------------------------------------------//
186//
187template <typename Tp>
189{
190 using type = Tp;
191
192 TIMEMORY_DEFAULT_OBJECT(set_state)
193
194 TIMEMORY_HOT set_state(type& obj, component::base_state* _data);
195
196 TIMEMORY_HOT auto operator()(type& obj, component::base_state* _data)
197 {
198 return sfinae(obj, 0, _data);
199 }
200
201private:
202 // If the component has a set_scope(...) member function
203 template <typename T>
204 TIMEMORY_HOT auto sfinae(T& obj, int, component::base_state* _data)
205 -> decltype(obj.set_state(_data))
206 {
207 return obj.set_state(_data);
208 }
209
210 // If the component does not have a set_scope(...) member function
211 template <typename T>
212 TIMEMORY_INLINE void sfinae(T&, long, component::base_state*)
213 {}
214};
215//
216//--------------------------------------------------------------------------------------//
217//
218template <typename Tp>
220{
221 sfinae(obj, 0, _data);
222}
223//
224//--------------------------------------------------------------------------------------//
225//
226template <typename Tp>
228{
229 TIMEMORY_DEFAULT_OBJECT(set_depth_change)
230
231 template <typename Up>
232 TIMEMORY_HOT auto operator()(Up& obj, bool v) const
233 {
234 static_assert(!std::is_pointer<Up>::value,
235 "SFINAE tests will always fail with pointer types");
236 return sfinae(obj, 0, v);
237 }
238
239 constexpr auto operator()() const { return sfinae<Tp>(0); }
240
241private:
242 template <typename Up>
243 static TIMEMORY_HOT auto sfinae(Up& obj, int, bool v)
244 -> decltype(obj.set_depth_change(v))
245 {
246 return obj.set_depth_change(v);
247 }
248
249 template <typename Up>
250 static TIMEMORY_INLINE auto sfinae(Up&, long, bool)
251 {}
252
253 template <typename Up>
254 constexpr auto sfinae(int) const
255 -> decltype(std::declval<Up>().set_depth_change(std::declval<bool>()), bool())
256 {
257 return true;
258 }
259
260 template <typename Up>
261 constexpr bool sfinae(long) const
262 {
263 return false;
264 }
265};
266//
267//--------------------------------------------------------------------------------------//
268//
269template <typename Tp>
270struct set_is_flat
271{
272 TIMEMORY_DEFAULT_OBJECT(set_is_flat)
273
274 template <typename Up>
275 TIMEMORY_HOT auto operator()(Up& obj, bool v) const
276 {
277 static_assert(!std::is_pointer<Up>::value,
278 "SFINAE tests will always fail with pointer types");
279 return sfinae(obj, 0, v);
280 }
281
282 constexpr auto operator()() const { return sfinae<Tp>(0); }
283
284private:
285 template <typename Up>
286 static TIMEMORY_HOT auto sfinae(Up& obj, int, bool v) -> decltype(obj.set_is_flat(v))
287 {
288 return obj.set_is_flat(v);
289 }
290
291 template <typename Up>
292 static TIMEMORY_INLINE auto sfinae(Up&, long, bool)
293 {}
294
295 template <typename Up>
296 constexpr auto sfinae(int) const
297 -> decltype(std::declval<Up>().set_is_flat(std::declval<bool>()), bool())
298 {
299 return true;
300 }
301
302 template <typename Up>
303 constexpr bool sfinae(long) const
304 {
305 return false;
306 }
307};
308//
309//--------------------------------------------------------------------------------------//
310//
311template <typename Tp>
312struct set_is_on_stack
313{
314 TIMEMORY_DEFAULT_OBJECT(set_is_on_stack)
315
316 template <typename Up>
317 TIMEMORY_HOT auto operator()(Up& obj, bool v) const
318 {
319 static_assert(!std::is_pointer<Up>::value,
320 "SFINAE tests will always fail with pointer types");
321 return sfinae(obj, 0, v);
322 }
323
324private:
325 template <typename Up>
326 static TIMEMORY_HOT auto sfinae(Up& obj, int, bool v)
327 -> decltype(obj.set_is_on_stack(v))
328 {
329 return obj.set_is_on_stack(v);
330 }
331
332 template <typename Up>
333 static TIMEMORY_INLINE auto sfinae(Up&, long, bool)
334 {}
335};
336//
337//--------------------------------------------------------------------------------------//
338//
339template <typename Tp>
340struct set_is_invalid
341{
342 TIMEMORY_DEFAULT_OBJECT(set_is_invalid)
343
344 template <typename Up>
345 TIMEMORY_HOT auto operator()(Up& obj, bool v) const
346 {
347 static_assert(!std::is_pointer<Up>::value,
348 "SFINAE tests will always fail with pointer types");
349 return sfinae(obj, 0, v);
350 }
351
352 constexpr auto operator()() const { return sfinae<Tp>(0); }
353
354private:
355 template <typename Up>
356 static TIMEMORY_HOT auto sfinae(Up& obj, int, bool v)
357 -> decltype(obj.set_is_invalid(v))
358 {
359 return obj.set_is_invalid(v);
360 }
361
362 template <typename Up>
363 static TIMEMORY_INLINE auto sfinae(Up&, long, bool)
364 {}
365
366 template <typename Up>
367 constexpr auto sfinae(int) const
368 -> decltype(std::declval<Up>().set_is_invalid(std::declval<bool>()), bool())
369 {
370 return true;
371 }
372
373 template <typename Up>
374 constexpr bool sfinae(long) const
375 {
376 return false;
377 }
378};
379//
380//--------------------------------------------------------------------------------------//
381//
382template <typename Tp>
383struct set_iterator
384{
385 TIMEMORY_DEFAULT_OBJECT(set_iterator)
386
387 template <typename Up>
388 TIMEMORY_HOT auto operator()(Tp& obj, Up&& v)
389 {
390 return sfinae(obj, 0, std::forward<Up>(v));
391 }
392
393private:
394 template <typename ObjT, typename ItrT>
395 TIMEMORY_HOT auto sfinae(ObjT& obj, int, ItrT&& v)
396 -> decltype(std::declval<ObjT>().set_iterator(std::forward<ItrT>(v)))
397 {
398 return obj.set_iterator(std::forward<ItrT>(v));
399 }
400
401 template <typename ObjT, typename ItrT>
402 TIMEMORY_INLINE auto sfinae(ObjT&, long, ItrT&&)
403 {}
404};
405//
406//--------------------------------------------------------------------------------------//
407//
408template <typename Tp>
409struct set_is_running
410{
411 TIMEMORY_DEFAULT_OBJECT(set_is_running)
412
413 template <typename Up>
414 TIMEMORY_HOT auto operator()(Up& obj, bool v) const
415 {
416 static_assert(!std::is_pointer<Up>::value,
417 "SFINAE tests will always fail with pointer types");
418 return sfinae(obj, 0, v);
419 }
420
421private:
422 template <typename Up>
423 static TIMEMORY_HOT auto sfinae(Up& obj, int, bool v)
424 -> decltype(obj.set_is_running(v))
425 {
426 return obj.set_is_running(v);
427 }
428
429 template <typename Up>
430 static TIMEMORY_INLINE auto sfinae(Up&, long, bool)
431 {}
432};
433//
434//--------------------------------------------------------------------------------------//
435//
436} // namespace operation
437} // namespace tim
std::string string_t
Definition: library.cpp:57
size_t hash_value_t
Definition: types.hpp:84
Definition: kokkosp.cpp:39
char const std::string & _prefix
Definition: config.cpp:55
std::string string_view_t
Definition: language.hpp:102
std::string string_t
Definition: utility.hpp:98
The declaration for the types for operations without definitions.
Include the macros for operations.
Declare the operations types.
Provide state configuration options for a component instance. The current states are:
Definition: data.hpp:446
This operation attempts to call a member function which the component provides to internally store wh...
Definition: set.hpp:228
auto operator()(Up &obj, bool v) const
Definition: set.hpp:232
constexpr auto operator()() const
Definition: set.hpp:239
auto operator()(Up &obj, bool v) const
Definition: set.hpp:275
constexpr auto operator()() const
Definition: set.hpp:282
constexpr auto operator()() const
Definition: set.hpp:352
auto operator()(Up &obj, bool v) const
Definition: set.hpp:345
auto operator()(Up &obj, bool v) const
Definition: set.hpp:317
auto operator()(Up &obj, bool v) const
Definition: set.hpp:414
auto operator()(Tp &obj, Up &&v)
Definition: set.hpp:388
Call the set_prefix member function. These instantiations are always inlined because of the use of st...
Definition: set.hpp:59
string_view_t string_t
Definition: set.hpp:61
set_prefix(type &obj, const string_t &_prefix)
Definition: set.hpp:124
auto operator()(type &obj, const string_t &_prefix) const
Definition: set.hpp:68
auto operator()(type &obj, hash_value_t _nhash) const
Definition: set.hpp:73
set_scope(type &obj, scope::config _data)
Definition: set.hpp:176
auto operator()(type &obj, scope::config _data) const
Definition: set.hpp:153
set_state(type &obj, component::base_state *_data)
Definition: set.hpp:219
auto operator()(type &obj, component::base_state *_data)
Definition: set.hpp:196
this data type encodes the options of storage scope. The default is hierarchical (tree) scope....
Definition: types.hpp:453