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.
stop.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
31
32namespace tim
33{
34namespace operation
35{
36//
37//--------------------------------------------------------------------------------------//
38//
39//
40//
41//--------------------------------------------------------------------------------------//
42//
43template <typename Tp>
44struct stop
45{
46 using type = Tp;
47
48 template <typename U>
49 using base_t = typename U::base_type;
50
51 TIMEMORY_DEFAULT_OBJECT(stop)
52
53 TIMEMORY_HOT explicit stop(type& obj) { impl(obj); }
54 TIMEMORY_HOT explicit stop(type& obj, quirk::unsafe&&) { impl(obj, quirk::unsafe{}); }
55
56 template <typename Arg, typename... Args>
57 TIMEMORY_HOT stop(type& obj, Arg&& arg, Args&&... args)
58 {
59 impl(obj, std::forward<Arg>(arg), std::forward<Args>(args)...);
60 }
61
62 template <typename... Args>
63 TIMEMORY_HOT auto operator()(type& obj, Args&&... args) const
64 {
65 using RetT = decltype(sfinae(obj, 0, 0, std::forward<Args>(args)...));
66 if(is_running<Tp, true>{}(obj))
67 {
68 return sfinae(obj, 0, 0, std::forward<Args>(args)...);
69 }
70 return get_return<RetT>();
71 }
72
73 template <typename... Args>
74 TIMEMORY_HOT auto operator()(type& obj, quirk::unsafe&&, Args&&... args) const
75 {
76 return sfinae(obj, 0, 0, std::forward<Args>(args)...);
77 }
78
79private:
80 template <typename T>
81 auto get_return(enable_if_t<std::is_void<T>::value, int> = 0) const
82 {}
83
84 template <typename T>
85 auto get_return(enable_if_t<!std::is_void<T>::value, long> = 0) const
86 {
87 static_assert(std::is_default_constructible<T>::value,
88 "Error! start() returns a type that is not default constructible! "
89 "You must specialize operation::start<T> struct");
90 return T{};
91 }
92
93 template <typename... Args>
94 TIMEMORY_HOT void impl(type& obj, Args&&... args) const;
95
96 // resolution #1 (best)
97 template <typename Up, typename... Args>
98 TIMEMORY_HOT auto sfinae(Up& obj, int, int, Args&&... args) const
99 -> decltype(obj.stop(std::forward<Args>(args)...))
100 {
101 set_stopped<Tp>{}(obj);
102 return obj.stop(std::forward<Args>(args)...);
103 }
104
105 // resolution #2
106 template <typename Up, typename... Args>
107 TIMEMORY_HOT auto sfinae(Up& obj, int, long, Args&&...) const -> decltype(obj.stop())
108 {
109 set_stopped<Tp>{}(obj);
110 return obj.stop();
111 }
112
113 // resolution #3 (worst) - no member function
114 template <typename Up, typename... Args>
115 void sfinae(Up&, long, long, Args&&...) const
116 {
118 }
119};
120//
121//--------------------------------------------------------------------------------------//
122//
123//
124//
125//--------------------------------------------------------------------------------------//
126//
127template <typename Tp>
129{
130 using type = Tp;
131
133
134 template <typename... Args>
135 TIMEMORY_HOT explicit priority_stop(type& obj, Args&&... args);
136
137private:
138 // satisfies mpl condition
139 template <typename Up, typename... Args>
140 TIMEMORY_HOT auto sfinae(Up& obj, true_type&&, Args&&... args)
141 {
142 stop<Tp> _tmp(obj, std::forward<Args>(args)...);
143 }
144
145 // does not satisfy mpl condition
146 template <typename Up, typename... Args>
147 TIMEMORY_INLINE void sfinae(Up&, false_type&&, Args&&...)
148 {}
149};
150//
151//--------------------------------------------------------------------------------------//
152//
153//
154//
155//--------------------------------------------------------------------------------------//
156//
157template <typename Tp>
159{
160 using type = Tp;
161
163
164 template <typename... Args>
165 TIMEMORY_HOT explicit standard_stop(type& obj, Args&&... args);
166
167private:
168 // satisfies mpl condition
169 template <typename Up, typename... Args>
170 TIMEMORY_HOT auto sfinae(Up& obj, true_type&&, Args&&... args)
171 {
172 stop<Tp> _tmp(obj, std::forward<Args>(args)...);
173 }
174
175 // does not satisfy mpl condition
176 template <typename Up, typename... Args>
177 TIMEMORY_INLINE void sfinae(Up&, false_type&&, Args&&...)
178 {}
179};
180//
181//--------------------------------------------------------------------------------------//
182//
183//
184//
185//--------------------------------------------------------------------------------------//
186//
187template <typename Tp>
189{
190 using type = Tp;
191
193
194 template <typename... Args>
195 TIMEMORY_HOT explicit delayed_stop(type& obj, Args&&... args);
196
197private:
198 // satisfies mpl condition
199 template <typename Up, typename... Args>
200 TIMEMORY_HOT auto sfinae(Up& obj, true_type&&, Args&&... args)
201 {
202 stop<Tp> _tmp(obj, std::forward<Args>(args)...);
203 }
204
205 // does not satisfy mpl condition
206 template <typename Up, typename... Args>
207 TIMEMORY_INLINE void sfinae(Up&, false_type&&, Args&&...)
208 {}
209};
210//
211//--------------------------------------------------------------------------------------//
212//
213template <typename Tp>
214template <typename... Args>
215void
216stop<Tp>::impl(type& obj, Args&&... args) const
217{
218 if(is_running<Tp, true>{}(obj))
219 {
220 set_stopped<Tp>{}(obj);
221 sfinae(obj, 0, 0, std::forward<Args>(args)...);
222 }
223}
224//
225//--------------------------------------------------------------------------------------//
226//
227template <typename Tp>
228template <typename... Args>
229priority_stop<Tp>::priority_stop(type& obj, Args&&... args)
230{
231 using sfinae_type =
232 conditional_t<(trait::stop_priority<Tp>::value < 0), true_type, false_type>;
233 sfinae(obj, sfinae_type{}, std::forward<Args>(args)...);
234}
235//
236//--------------------------------------------------------------------------------------//
237//
238template <typename Tp>
239template <typename... Args>
240standard_stop<Tp>::standard_stop(type& obj, Args&&... args)
241{
242 using sfinae_type =
243 conditional_t<(trait::stop_priority<Tp>::value == 0), true_type, false_type>;
244 sfinae(obj, sfinae_type{}, std::forward<Args>(args)...);
245}
246//
247//--------------------------------------------------------------------------------------//
248//
249template <typename Tp>
250template <typename... Args>
251delayed_stop<Tp>::delayed_stop(type& obj, Args&&... args)
252{
253 using sfinae_type =
254 conditional_t<(trait::stop_priority<Tp>::value > 0), true_type, false_type>;
255 sfinae(obj, sfinae_type{}, std::forward<Args>(args)...);
256}
257//
258//--------------------------------------------------------------------------------------//
259//
260} // namespace operation
261} // namespace tim
Definition: kokkosp.cpp:39
typename std::enable_if< B, T >::type enable_if_t
Alias template for enable_if.
Definition: types.hpp:190
typename std::conditional< B, Lhs, Rhs >::type conditional_t
Definition: types.hpp:197
The declaration for the types for operations without definitions.
#define SFINAE_WARNING(...)
Definition: declaration.hpp:70
Include the macros for operations.
Declare the operations types.
TIMEMORY_DELETED_OBJECT(delayed_stop) template< typename... Args > explicit delayed_stop(type &obj
This operation attempts to call a member function which provides whether or not the component current...
Definition: types.hpp:597
TIMEMORY_DELETED_OBJECT(priority_stop) template< typename... Args > explicit priority_stop(type &obj
TIMEMORY_DELETED_OBJECT(standard_stop) template< typename... Args > explicit standard_stop(type &obj
typename U::base_type base_t
Definition: stop.hpp:49
stop(type &obj)
Definition: stop.hpp:53
stop(type &obj, quirk::unsafe &&)
Definition: stop.hpp:54
stop(type &obj, Arg &&arg, Args &&... args)
Definition: stop.hpp:57
auto operator()(type &obj, quirk::unsafe &&, Args &&... args) const
Definition: stop.hpp:74
auto operator()(type &obj, Args &&... args) const
Definition: stop.hpp:63
When present, this argument instructs to skip any safety checks. Example checks include: checking whe...
Definition: quirks.hpp:270