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.
handler.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/** \file timemory/data/handler.hpp
26 * \headerfile timemory/data/handler.hpp "timemory/data/handler.hpp"
27 * Defines the data_tracker handler component interface
28 *
29 */
30
31#pragma once
32
33#include "timemory/api.hpp"
35#include "timemory/mpl/math.hpp"
38
39#include <sstream>
40#include <string>
41
42namespace tim
43{
44namespace data
45{
46/// \struct tim::data::handler<V, Tag>
47/// \brief This class is used to provide the implementation for tracking data
48/// via the data_tracker component. It is written such that it can be
49/// defined for a specific data type (e.g. int) with a tag (e.g. ConvergenceIterations)
50/// and shared among multiple template types of data_tracker<T>
51///
52template <typename V, typename Tag>
53struct handler
54{
55 using value_type = V;
56
57public:
58 /// this function is returns the current value
59 template <typename T>
60 static decltype(auto) get(const T& obj)
61 {
62 return handler::get(obj, 0);
63 }
64
65 /// this function is returns the current value in a form suitable for display.
66 /// It may be necessary to specialize this function downstream.
67 template <typename T>
68 static decltype(auto) get_display(const T& obj)
69 {
70 return handler::get(obj);
71 }
72
73public:
74 //----------------------------------------------------------------------------------//
75 // const ref semantics
76 //----------------------------------------------------------------------------------//
77 /// this function is used to store a value
78 template <typename T>
79 static void store(T& obj, const value_type& v)
80 {
81 obj.set_value(v);
82 }
83
84 /// \tparam FuncT Should be binary operation which takes two inputs and returns one
85 /// this function is used to store the current value after performing some operation.
86 template <typename T, typename FuncT>
87 static auto store(T& obj, FuncT&& f, const value_type& v)
88 -> decltype(obj.set_value(std::forward<FuncT>(f)(obj.get_value(), v)), void())
89 {
90 obj.set_value(std::forward<FuncT>(f)(obj.get_value(), v));
91 }
92
93 /// this function sets the value of the temporary in `mark_begin`.
94 static void begin(value_type& obj, const value_type& v) { obj = v; }
95
96 /// this function sets the value of the temporary in `mark_begin`.
97 template <typename FuncT>
98 static void begin(value_type& obj, FuncT&& f, const value_type& v)
99 {
100 obj = std::forward<FuncT>(f)(obj, v);
101 }
102
103 /// this function computes the difference between the provided value and the
104 /// temporary from `mark_begin` and then updates the current value
105 template <typename T>
106 static void end(T& obj, const value_type& v)
107 {
108 auto _v = v;
109 _v = math::minus(_v, obj.get_temporary());
110 store(obj, std::move(_v));
111 }
112
113 /// this function computes the difference between the provided value and the
114 /// temporary from `mark_begin` and then updates the current value
115 template <typename T, typename FuncT>
116 static void end(T& obj, FuncT&& f, const value_type& v)
117 {
118 auto _v = v;
119 _v = math::minus(_v, obj.get_temporary());
120 store(obj, std::forward<FuncT>(f), std::move(_v));
121 }
122
123public:
124 //----------------------------------------------------------------------------------//
125 // move semantics
126 //----------------------------------------------------------------------------------//
127 /// overload with move semantics
128 template <typename T>
129 static void store(T& obj, value_type&& v)
130 {
131 obj.set_value(std::move(v));
132 }
133
134 /// overload with move semantics
135 template <typename T, typename FuncT>
136 static auto store(T& obj, FuncT&& f, value_type&& v)
137 -> decltype(obj.set_value(std::forward<FuncT>(f)(obj.get_value(), std::move(v))),
138 void())
139 {
140 obj.set_value(std::forward<FuncT>(f)(obj.get_value(), std::move(v)));
141 }
142
143 /// overload with move semantics
144 static void begin(value_type& obj, value_type&& v) { obj = v; }
145
146 /// overload with move semantics
147 template <typename T, typename FuncT>
148 static auto begin(value_type& obj, FuncT&& f, value_type&& v)
149 {
150 obj = std::forward<FuncT>(f)(obj, v);
151 }
152
153 /// overload with move semantics
154 template <typename T>
155 static void end(T& obj, value_type&& v)
156 {
157 auto&& _v = std::move(v);
158 math::minus(_v, obj.get_temporary());
159 obj.set_value(std::move(_v));
160 }
161
162 /// overload with move semantics
163 template <typename T, typename FuncT>
164 static void end(T& obj, FuncT&& f, value_type&& v)
165 {
166 auto&& _v = std::move(v);
167 _v = math::minus(_v, obj.get_temporary());
168 store(obj, std::forward<FuncT>(f), std::move(_v));
169 }
170
171private:
172 // prefer the get_value version since value is updated by default
173 template <typename T>
174 static auto get(const T& obj, int) -> decltype(obj.get_value())
175 {
176 return obj.get_value();
177 }
178
179 template <typename T>
180 static decltype(auto) get(const T& obj, long)
181 {
182 return obj.load();
183 }
184};
185} // namespace data
186} // namespace tim
Tp & minus(Tp &, const Up &)
Definition: minus.hpp:98
Definition: kokkosp.cpp:39
static auto store(T &obj, FuncT &&f, const value_type &v) -> decltype(obj.set_value(std::forward< FuncT >(f)(obj.get_value(), v)), void())
Definition: handler.hpp:87
static void begin(value_type &obj, value_type &&v)
overload with move semantics
Definition: handler.hpp:144
static auto begin(value_type &obj, FuncT &&f, value_type &&v)
overload with move semantics
Definition: handler.hpp:148
static auto store(T &obj, FuncT &&f, value_type &&v) -> decltype(obj.set_value(std::forward< FuncT >(f)(obj.get_value(), std::move(v))), void())
overload with move semantics
Definition: handler.hpp:136
static void end(T &obj, value_type &&v)
overload with move semantics
Definition: handler.hpp:155
static void end(T &obj, FuncT &&f, value_type &&v)
overload with move semantics
Definition: handler.hpp:164
static void end(T &obj, FuncT &&f, const value_type &v)
this function computes the difference between the provided value and the temporary from mark_begin an...
Definition: handler.hpp:116
static decltype(auto) get_display(const T &obj)
this function is returns the current value in a form suitable for display. It may be necessary to spe...
Definition: handler.hpp:68
static void end(T &obj, const value_type &v)
this function computes the difference between the provided value and the temporary from mark_begin an...
Definition: handler.hpp:106
static void store(T &obj, value_type &&v)
overload with move semantics
Definition: handler.hpp:129
static void begin(value_type &obj, const value_type &v)
this function sets the value of the temporary in mark_begin.
Definition: handler.hpp:94
static void store(T &obj, const value_type &v)
this function is used to store a value
Definition: handler.hpp:79
static decltype(auto) get(const T &obj)
this function is returns the current value
Definition: handler.hpp:60
static void begin(value_type &obj, FuncT &&f, const value_type &v)
this function sets the value of the temporary in mark_begin.
Definition: handler.hpp:98