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.
math.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/math.hpp
27 * \brief Definition for various functions for math in operations
28 */
29
30#pragma once
31
35
36namespace tim
37{
38namespace operation
39{
40//
41//--------------------------------------------------------------------------------------//
42//
43///
44/// \struct tim::operation::plus
45/// \brief Define addition operations
46///
47//
48//--------------------------------------------------------------------------------------//
49//
50template <typename Tp>
51struct plus
52{
53 using type = Tp;
54
55 template <typename U>
56 using base_t = typename U::base_type;
57
59
60 template <typename Up = Tp, enable_if_t<has_data<Up>::value, char> = 0>
61 plus(type& obj, const type& rhs)
62 {
63 obj += rhs;
64 // ensures update to laps
65 sfinae(obj, 0, 0, rhs);
66 }
67
68 template <typename Vt, typename Up = Tp, enable_if_t<!has_data<Up>::value, char> = 0>
69 plus(type&, const Vt&)
70 {}
71
72private:
73 template <typename Up>
74 auto sfinae(Up& obj, int, int, const Up& rhs)
75 -> decltype(static_cast<base_t<Up>&>(obj).plus(crtp::base{},
76 static_cast<base_t<Up>&>(rhs)),
77 void())
78 {
79 static_cast<base_t<Up>&>(obj).plus(crtp::base{}, static_cast<base_t<Up>&>(rhs));
80 }
81
82 template <typename U>
83 auto sfinae(U& obj, int, long, const U& rhs) -> decltype(obj.plus(rhs), void())
84 {
85 obj.plus(rhs);
86 }
87
88 template <typename U>
89 auto sfinae(U&, long, long, const U&)
90 {}
91};
92//
93//--------------------------------------------------------------------------------------//
94//
95///
96/// \struct tim::operation::minus
97/// \brief Define subtraction operations
98///
99//
100//--------------------------------------------------------------------------------------//
101//
102template <typename Tp>
103struct minus
104{
105 using type = Tp;
106
107 template <typename U>
108 using base_t = typename U::base_type;
109
111
112 template <typename Up = Tp, enable_if_t<has_data<Up>::value, char> = 0>
113 minus(type& obj, const type& rhs)
114 {
115 obj -= rhs;
116 // ensures update to laps
117 sfinae(obj, 0, 0, rhs);
118 }
119
120 template <typename Vt, typename Up = Tp, enable_if_t<!has_data<Up>::value, char> = 0>
121 minus(type&, const Vt&)
122 {}
123
124private:
125 template <typename Up>
126 auto sfinae(Up& obj, int, int, const Up& rhs)
127 -> decltype(static_cast<base_t<Up>&>(obj).minus(crtp::base{},
128 static_cast<base_t<Up>&>(rhs)),
129 void())
130 {
131 static_cast<base_t<Up>&>(obj).minus(crtp::base{}, static_cast<base_t<Up>&>(rhs));
132 }
133
134 template <typename U>
135 auto sfinae(U& obj, int, long, const U& rhs) -> decltype(obj.minus(rhs), void())
136 {
137 obj.minus(rhs);
138 }
139
140 template <typename U>
141 auto sfinae(U&, long, long, const U&)
142 {}
143};
144//
145//--------------------------------------------------------------------------------------//
146//
147///
148/// \struct tim::operation::multiply
149/// \brief This operation class is used for multiplication of a component
150///
151//
152//--------------------------------------------------------------------------------------//
153//
154template <typename Tp>
156{
157 using type = Tp;
158
160
161 template <typename Up = Tp, enable_if_t<has_data<Up>::value, char> = 0>
162 multiply(type& obj, int64_t rhs)
163 {
164 using namespace tim::stl;
165 obj *= rhs;
166 }
167
168 template <typename Up = Tp, enable_if_t<has_data<Up>::value, char> = 0>
169 multiply(type& obj, const type& rhs)
170 {
171 using namespace tim::stl;
172 obj *= rhs;
173 }
174
175 template <typename Vt, typename Up = Tp, enable_if_t<!has_data<Up>::value, char> = 0>
176 multiply(type&, const Vt&)
177 {}
178};
179//
180//--------------------------------------------------------------------------------------//
181//
182///
183/// \struct tim::operation::divide
184/// \brief This operation class is used for division of a component
185///
186//
187//--------------------------------------------------------------------------------------//
188//
189template <typename Tp>
190struct divide
191{
192 using type = Tp;
193
195
196 template <typename Up = Tp, enable_if_t<has_data<Up>::value, char> = 0>
197 divide(type& obj, int64_t rhs)
198 {
199 using namespace tim::stl;
200 obj /= rhs;
201 }
202
203 template <typename Up = Tp, enable_if_t<has_data<Up>::value, char> = 0>
204 divide(type& obj, const type& rhs)
205 {
206 using namespace tim::stl;
207 obj /= rhs;
208 }
209
210 template <typename Vt, typename Up = Tp, enable_if_t<!has_data<Up>::value, char> = 0>
211 divide(type&, const Vt&)
212 {}
213};
214//
215//--------------------------------------------------------------------------------------//
216//
217} // namespace operation
218} // namespace tim
a generic type for prioritizing a function call to the base class over derived functions,...
Definition: types.hpp:311
Tp & plus(Tp &, const Up &)
Definition: plus.hpp:106
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
the namespace is provided to hide stl overload from global namespace but provide a method of using th...
Definition: fwd.hpp:105
Definition: kokkosp.cpp:39
The declaration for the types for operations without definitions.
Include the macros for operations.
Declare the operations types.
This operation class is used for division of a component.
Definition: math.hpp:191
TIMEMORY_DELETED_OBJECT(divide) template< typename Up
Define subtraction operations.
Definition: math.hpp:104
TIMEMORY_DELETED_OBJECT(minus) template< typename Up
typename U::base_type base_t
Definition: math.hpp:108
This operation class is used for multiplication of a component.
Definition: math.hpp:156
TIMEMORY_DELETED_OBJECT(multiply) template< typename Up
Define addition operations.
Definition: math.hpp:52
TIMEMORY_DELETED_OBJECT(plus) template< typename Up
typename U::base_type base_t
Definition: math.hpp:56