timemory  3.2.1
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.
auto_base_bundle.cpp
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 #ifndef TIMEMORY_VARIADIC_AUTO_BASE_BUNDLE_CPP_
27 #define TIMEMORY_VARIADIC_AUTO_BASE_BUNDLE_CPP_ 1
28 
30 
31 namespace tim
32 {
33 //--------------------------------------------------------------------------------------//
34 
35 template <typename Tag, typename CompT, typename BundleT>
36 template <typename... T>
38  quirk::config<T...> _config,
39  transient_func_t init_func)
40 : m_enabled(settings::enabled())
41 , m_report_at_exit(quirk_config<quirk::exit_report, T...>::value)
42 , m_reference_object(nullptr)
43 , m_temporary(key, m_enabled, _config)
44 {
45  if(m_enabled)
46  {
47  internal_init(init_func);
49  {
50  m_temporary.start();
51  }
52  }
53 }
54 
55 //--------------------------------------------------------------------------------------//
56 
57 template <typename Tag, typename CompT, typename BundleT>
58 template <typename... T>
60  quirk::config<T...> _config,
61  transient_func_t init_func)
62 : m_enabled(settings::enabled())
63 , m_report_at_exit(quirk_config<quirk::exit_report, T...>::value)
64 , m_reference_object(nullptr)
65 , m_temporary(loc, m_enabled, _config)
66 {
67  if(m_enabled)
68  {
69  internal_init(init_func);
71  {
72  m_temporary.start();
73  }
74  }
75 }
76 
77 //--------------------------------------------------------------------------------------//
78 
79 template <typename Tag, typename CompT, typename BundleT>
81  scope::config _scope,
82  bool report_at_exit,
83  transient_func_t init_func)
84 : m_enabled(settings::enabled())
85 , m_report_at_exit(report_at_exit || quirk_config<quirk::exit_report>::value)
86 , m_reference_object(nullptr)
87 , m_temporary(key, m_enabled, _scope)
88 {
89  if(m_enabled)
90  {
91  internal_init(init_func);
93  }
94 }
95 
96 //--------------------------------------------------------------------------------------//
97 
98 template <typename Tag, typename CompT, typename BundleT>
100  scope::config _scope,
101  bool report_at_exit,
102  transient_func_t init_func)
103 : m_enabled(settings::enabled())
104 , m_report_at_exit(report_at_exit || quirk_config<quirk::exit_report>::value)
105 , m_reference_object(nullptr)
106 , m_temporary(loc, m_enabled, _scope)
107 {
108  if(m_enabled)
109  {
110  internal_init(init_func);
112  }
113 }
114 
115 //--------------------------------------------------------------------------------------//
116 
117 template <typename Tag, typename CompT, typename BundleT>
119  bool report_at_exit,
120  transient_func_t init_func)
121 : m_enabled(settings::enabled())
122 , m_report_at_exit(report_at_exit || quirk_config<quirk::exit_report>::value)
123 , m_reference_object(nullptr)
124 , m_temporary(hash, m_enabled, _scope)
125 {
126  if(m_enabled)
127  {
128  internal_init(init_func);
130  }
131 }
132 
133 //--------------------------------------------------------------------------------------//
134 
135 template <typename Tag, typename CompT, typename BundleT>
137  scope::config _scope,
138  bool report_at_exit)
139 : m_report_at_exit(report_at_exit || quirk_config<quirk::exit_report>::value)
140 , m_reference_object(&tmp)
141 , m_temporary(component_type(tmp.clone(true, _scope)))
142 {
143  if(m_enabled)
144  {
146  }
147 }
148 
149 //--------------------------------------------------------------------------------------//
150 
151 template <typename Tag, typename CompT, typename BundleT>
152 template <typename Arg, typename... Args>
154  bool store, scope::config _scope,
155  transient_func_t init_func,
156  Arg&& arg, Args&&... args)
157 : m_enabled(store && settings::enabled())
158 , m_report_at_exit(settings::destructor_report() ||
159  quirk_config<quirk::exit_report>::value)
160 , m_reference_object(nullptr)
161 , m_temporary(key, m_enabled, _scope)
162 {
163  if(m_enabled)
164  {
165  internal_init(init_func, std::forward<Arg>(arg), std::forward<Args>(args)...);
167  }
168 }
169 
170 //--------------------------------------------------------------------------------------//
171 
172 template <typename Tag, typename CompT, typename BundleT>
173 template <typename Arg, typename... Args>
175  bool store, scope::config _scope,
176  transient_func_t init_func,
177  Arg&& arg, Args&&... args)
178 : m_enabled(store && settings::enabled())
179 , m_report_at_exit(settings::destructor_report() ||
180  quirk_config<quirk::exit_report>::value)
181 , m_reference_object(nullptr)
182 , m_temporary(loc, m_enabled, _scope)
183 {
184  if(m_enabled)
185  {
186  internal_init(init_func, std::forward<Arg>(arg), std::forward<Args>(args)...);
188  }
189 }
190 
191 //--------------------------------------------------------------------------------------//
192 
193 template <typename Tag, typename CompT, typename BundleT>
194 template <typename Arg, typename... Args>
196  scope::config _scope,
197  transient_func_t init_func,
198  Arg&& arg, Args&&... args)
199 : m_enabled(store && settings::enabled())
200 , m_report_at_exit(settings::destructor_report() ||
201  quirk_config<quirk::exit_report>::value)
202 , m_reference_object(nullptr)
203 , m_temporary(hash, m_enabled, _scope)
204 {
205  if(m_enabled)
206  {
207  internal_init(init_func, std::forward<Arg>(arg), std::forward<Args>(args)...);
209  }
210 }
211 
212 //--------------------------------------------------------------------------------------//
213 
214 template <typename Tag, typename CompT, typename BundleT>
216 {
218  {
219  if(m_enabled)
220  {
221  // stop the timer
222  m_temporary.stop();
223 
224  // report timer at exit
225  if(m_report_at_exit)
226  {
227  std::stringstream ss;
228  ss << m_temporary;
229  if(ss.str().length() > 0)
230  std::cout << ss.str() << std::endl;
231  }
232 
233  if(m_reference_object)
234  {
235  *m_reference_object += m_temporary;
236  }
237  }
238  }
239 }
240 
241 //
242 template <typename Tag, typename CompT, typename BundleT>
243 BundleT&
245 {
246  m_temporary.push();
247  return static_cast<this_type&>(*this);
248 }
249 
250 template <typename Tag, typename CompT, typename BundleT>
251 BundleT&
253 {
254  m_temporary.pop();
255  return static_cast<this_type&>(*this);
256 }
257 
258 template <typename Tag, typename CompT, typename BundleT>
259 template <typename... Args>
260 BundleT&
262 {
263  m_temporary.measure(std::forward<Args>(args)...);
264  return static_cast<this_type&>(*this);
265 }
266 
267 template <typename Tag, typename CompT, typename BundleT>
268 template <typename... Args>
269 BundleT&
271 {
272  m_temporary.record(std::forward<Args>(args)...);
273  return static_cast<this_type&>(*this);
274 }
275 
276 template <typename Tag, typename CompT, typename BundleT>
277 template <typename... Args>
278 BundleT&
280 {
281  m_temporary.sample(std::forward<Args>(args)...);
282  return static_cast<this_type&>(*this);
283 }
284 
285 template <typename Tag, typename CompT, typename BundleT>
286 template <typename... Args>
287 BundleT&
289 {
290  m_temporary.start(std::forward<Args>(args)...);
291  return static_cast<this_type&>(*this);
292 }
293 
294 template <typename Tag, typename CompT, typename BundleT>
295 template <typename... Args>
296 BundleT&
298 {
299  m_temporary.stop(std::forward<Args>(args)...);
300  return static_cast<this_type&>(*this);
301 }
302 
303 template <typename Tag, typename CompT, typename BundleT>
304 template <typename... Args>
305 BundleT&
307 {
308  m_temporary.assemble(std::forward<Args>(args)...);
309  return static_cast<this_type&>(*this);
310 }
311 
312 template <typename Tag, typename CompT, typename BundleT>
313 template <typename... Args>
314 BundleT&
316 {
317  m_temporary.derive(std::forward<Args>(args)...);
318  return static_cast<this_type&>(*this);
319 }
320 
321 template <typename Tag, typename CompT, typename BundleT>
322 template <typename... Args>
323 BundleT&
325 {
326  m_temporary.mark(std::forward<Args>(args)...);
327  return static_cast<this_type&>(*this);
328 }
329 
330 template <typename Tag, typename CompT, typename BundleT>
331 template <typename... Args>
332 BundleT&
334 {
335  m_temporary.mark_begin(std::forward<Args>(args)...);
336  return static_cast<this_type&>(*this);
337 }
338 
339 template <typename Tag, typename CompT, typename BundleT>
340 template <typename... Args>
341 BundleT&
343 {
344  m_temporary.mark_end(std::forward<Args>(args)...);
345  return static_cast<this_type&>(*this);
346 }
347 
348 template <typename Tag, typename CompT, typename BundleT>
349 template <typename... Args>
350 BundleT&
352 {
353  m_temporary.store(std::forward<Args>(args)...);
354  return static_cast<this_type&>(*this);
355 }
356 
357 template <typename Tag, typename CompT, typename BundleT>
358 template <typename... Args>
359 BundleT&
361 {
362  m_temporary.audit(std::forward<Args>(args)...);
363  return static_cast<this_type&>(*this);
364 }
365 
366 template <typename Tag, typename CompT, typename BundleT>
367 template <typename... Args>
368 BundleT&
370 {
371  m_temporary.add_secondary(std::forward<Args>(args)...);
372  return static_cast<this_type&>(*this);
373 }
374 
375 template <typename Tag, typename CompT, typename BundleT>
376 template <typename... Args>
377 BundleT&
379 {
380  m_temporary.reset(std::forward<Args>(args)...);
381  return static_cast<this_type&>(*this);
382 }
383 
384 template <typename Tag, typename CompT, typename BundleT>
385 template <typename... Args>
386 BundleT&
388 {
389  m_temporary.set_scope(std::forward<Args>(args)...);
390  return static_cast<this_type&>(*this);
391 }
392 
393 template <typename Tag, typename CompT, typename BundleT>
394 template <typename... Args>
395 BundleT&
397 {
398  m_temporary.set_prefix(std::forward<Args>(args)...);
399  return static_cast<this_type&>(*this);
400 }
401 
402 template <typename Tag, typename CompT, typename BundleT>
403 template <template <typename> class OpT, typename... Args>
404 BundleT&
406 {
407  m_temporary.template invoke<OpT>(std::forward<Args>(_args)...);
408  return static_cast<this_type&>(*this);
409 }
410 
411 template <typename Tag, typename CompT, typename BundleT>
412 bool
414 {
415  return m_enabled;
416 }
417 
418 template <typename Tag, typename CompT, typename BundleT>
419 bool
421 {
422  return m_report_at_exit;
423 }
424 
425 template <typename Tag, typename CompT, typename BundleT>
426 bool
428 {
429  return m_temporary.store();
430 }
431 
432 template <typename Tag, typename CompT, typename BundleT>
433 int64_t
435 {
436  return m_temporary.laps();
437 }
438 
439 template <typename Tag, typename CompT, typename BundleT>
440 uint64_t
442 {
443  return m_temporary.hash();
444 }
445 
446 template <typename Tag, typename CompT, typename BundleT>
449 {
450  return m_temporary.key();
451 }
452 
453 template <typename Tag, typename CompT, typename BundleT>
456 {
457  return m_temporary.data();
458 }
459 
460 template <typename Tag, typename CompT, typename BundleT>
463 {
464  return m_temporary.data();
465 }
466 
467 template <typename Tag, typename CompT, typename BundleT>
468 void
470 {
471  m_report_at_exit = val;
472 }
473 
474 template <typename Tag, typename CompT, typename BundleT>
475 void
477 {
478  m_temporary.rekey(_key);
479 }
480 
481 template <typename Tag, typename CompT, typename BundleT>
482 BundleT&
484 {
485  m_temporary.rekey(_loc);
486  return static_cast<this_type&>(*this);
487 }
488 
489 template <typename Tag, typename CompT, typename BundleT>
490 BundleT&
492 {
493  m_temporary.rekey(_hash);
494  return static_cast<this_type&>(*this);
495 }
496 
497 template <typename Tag, typename CompT, typename BundleT>
500 {
501  return scope::transient_destructor{ [&]() { this->stop(); } };
502 }
503 
504 template <typename Tag, typename CompT, typename BundleT>
508 {
509  return scope::transient_destructor{ [&, _func]() {
510  _func(static_cast<this_type&>(*this));
511  } };
512 }
513 
514 //
515 template <typename Tag, typename CompT, typename BundleT>
516 template <typename... Tail>
517 void
519 {
520  m_temporary.template disable<Tail...>();
521 }
522 
523 template <typename Tag, typename CompT, typename BundleT>
524 void
526 {
527  if(m_enabled)
528  _init(static_cast<this_type&>(*this));
529 }
530 
531 template <typename Tag, typename CompT, typename BundleT>
532 template <typename Arg, typename... Args>
533 void
535  Args&&... _args)
536 {
537  if(m_enabled)
538  {
539  _init(static_cast<this_type&>(*this));
540  m_temporary.construct(std::forward<Arg>(_arg), std::forward<Args>(_args)...);
541  }
542 }
543 
544 } // namespace tim
545 
546 #endif
typename base_type::component_type component_type
tim::variadic::impl::quirk_config< T, convert_t< BundleT, type_list<> >, U... > quirk_config
typename component_type::captured_location_t captured_location_t
#define IF_CONSTEXPR(...)
Definition: language.hpp:72
void set_scope(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:792
void assemble(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:850
void mark_begin(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:474
void set_prefix(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:756
void audit(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:921
void store(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:546
void record(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:616
void derive(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:886
void pop(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:721
void stop(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:368
void measure(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:651
void push(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:686
void mark_end(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:510
void mark(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:439
void reset(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:581
void start(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:298
Ret invoke(string_view_t &&label, Func &&func, Args &&... args)
Definition: invoker.hpp:39
A light-weight alternative to std::function. Pass any callback - including capturing lambdas - cheapl...
Definition: kokkosp.cpp:38
std::string string_view_t
Definition: language.hpp:100
add_secondary
Definition: settings.cpp:1371
tim::mpl::apply< std::string > string
Definition: macros.hpp:52
destructor_report
Definition: settings.cpp:1475
Static polymorphic base class for automatic start/stop bundlers.
Definition: types.hpp:72
a variadic type which holds zero or more quirks that are passed to the constructor of a component bun...
Definition: quirks.hpp:39
this data type encodes the options of storage scope. The default is hierarchical (tree) scope....
Definition: types.hpp:446