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.
fini.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 finalize
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, finalize, 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 finalizeright 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/fini.hpp
27 * \brief Definition for global and thread-local finalization functions for a component
28 */
29
30#pragma once
31
35
36namespace tim
37{
38namespace operation
39{
40//
41//--------------------------------------------------------------------------------------//
42//
43///
44/// \struct tim::operation::fini
45/// \brief This operation class is used for invoking the static initializer and
46/// thread-local initializer of a component.
47///
48//
49//--------------------------------------------------------------------------------------//
50//
51template <typename Tp>
52struct fini
53{
54 using type = Tp;
55
56 fini() = default;
57 ~fini() = default;
58
60
61 template <typename Up = Tp, int StateT,
62 enable_if_t<trait::is_available<Up>::value, char> = 0>
63 explicit TIMEMORY_COLD fini(mode_constant<StateT>)
64 {
65 auto _storage = storage<Tp>::noninit_instance();
66 if(_storage)
67 sfinae<type, StateT>(_storage, 0, 0);
68 }
69
70 template <typename Up = Tp, int StateT, typename StorageT,
72 explicit TIMEMORY_COLD fini(StorageT _storage, mode_constant<StateT>)
73 {
74 sfinae<type, StateT>(_storage, 0, 0);
75 }
76
77 template <typename Up = Tp, int StateT, typename StorageT,
79 explicit TIMEMORY_COLD fini(StorageT, mode_constant<StateT>)
80 {}
81
82 template <typename Up = Tp, int StateT,
84 TIMEMORY_COLD auto operator()(mode_constant<StateT>)
85 {
86 auto _storage = storage<Tp>::noninit_instance();
87 if(_storage)
88 return sfinae<type, StateT>(_storage, 0, 0);
89 return false;
90 }
91
92 template <typename Up = Tp, int StateT,
94 TIMEMORY_COLD auto operator()(mode_constant<StateT>)
95 {
96 return false;
97 }
98
99 /// returns whether the finalization function has been executed (will return false
100 /// if the component does not support any overload of `global_finalize(...)` or
101 /// `thread_finalize(...)`
102 template <int StateT>
103 static bool get_executed()
104 {
105 return was_executed<StateT>();
106 }
107
108private:
109 template <typename Up, int StateT, typename StorageT,
111 TIMEMORY_COLD auto sfinae(StorageT _storage, int, int)
112 -> decltype(std::declval<Up>().global_finalize(_storage), bool())
113 {
114 if(was_executed<StateT>())
115 return false;
116 type::global_finalize(_storage);
117 was_executed<StateT>() = true;
118 return true;
119 }
120
121 template <typename Up, int StateT, typename StorageT,
123 TIMEMORY_COLD auto sfinae(StorageT, int, long)
124 -> decltype(std::declval<Up>().global_finalize(), bool())
125 {
126 if(was_executed<StateT>())
127 return false;
128 type::global_finalize();
129 was_executed<StateT>() = true;
130 return true;
131 }
132
133 template <typename Up, int StateT, typename StorageT,
135 TIMEMORY_COLD auto sfinae(StorageT _storage, int, int)
136 -> decltype(std::declval<Up>().thread_finalize(_storage), bool())
137 {
138 if(was_executed<StateT>())
139 return false;
140 type::thread_finalize(_storage);
141 was_executed<StateT>() = true;
142 return true;
143 }
144
145 template <typename Up, int StateT, typename StorageT,
147 TIMEMORY_COLD auto sfinae(StorageT, int, long)
148 -> decltype(std::declval<Up>().thread_finalize(), bool())
149 {
150 if(was_executed<StateT>())
151 return false;
152 type::thread_finalize();
153 was_executed<StateT>() = true;
154 return true;
155 }
156
157 template <typename Up, int StateT, typename StorageT>
158 bool sfinae(StorageT, long, long)
159 {
160 return false;
161 }
162
163private:
164 template <int StateT, enable_if_t<StateT == fini_mode::global, char> = 0>
165 static bool& was_executed()
166 {
167 static bool _instance = false;
168 return _instance;
169 }
170
171 template <int StateT, enable_if_t<StateT == fini_mode::thread, char> = 0>
172 static bool& was_executed()
173 {
174 static thread_local bool _instance = false;
175 return _instance;
176 }
177};
178//
179//--------------------------------------------------------------------------------------//
180//
181} // namespace operation
182} // namespace tim
std::integral_constant< int, ModeV > mode_constant
Definition: types.hpp:240
Definition: kokkosp.cpp:39
typename std::enable_if< B, T >::type enable_if_t
Alias template for enable_if.
Definition: types.hpp:190
The declaration for the types for operations without definitions.
Include the macros for operations.
Declare the operations types.
This operation class is used for invoking the static initializer and thread-local initializer of a co...
Definition: fini.hpp:53
int int int StateT
Definition: fini.hpp:77
int int int int int StateT
Definition: fini.hpp:92
int int StateT
Definition: fini.hpp:70
int int int int int int int int int StateT
Definition: fini.hpp:145
int int int int StateT
Definition: fini.hpp:82
int int int int int int int int StateT
Definition: fini.hpp:133
TIMEMORY_DELETE_COPY_MOVE_OBJECT(fini) template< typename Up
int int int int int int int int int int StorageT bool sfinae(StorageT, long, long)
Definition: fini.hpp:158
int int int int int int StateT
Definition: fini.hpp:109
int int StorageT
Definition: fini.hpp:70
int int int int int int int StateT
Definition: fini.hpp:121
typename typename typename
Definition: types.hpp:226