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.
init.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 init
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, init, 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 initright 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/init.hpp
27 * \brief Definition for global and thread-local initialzation 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::init
45/// \brief This operation class is used for invoking the static initializer and
46/// thread-local initializer of a component
47///
48//
49template <typename Tp>
50struct init
51{
52 using type = Tp;
53
54 init() = default;
55 ~init() = default;
56
58
59 template <typename Up = Tp, int StateT,
60 enable_if_t<trait::is_available<Up>::value, char> = 0>
61 explicit init(mode_constant<StateT>)
62 {
63 auto _storage = storage<Tp>::instance();
64 if(_storage)
65 sfinae<type, StateT>(_storage, 0, 0);
66 }
67
68 template <typename Up = Tp, int StateT, typename StorageT,
71 {
72 sfinae<type, StateT>(_storage, 0, 0);
73 }
74
75 template <typename Up = Tp, int StateT, typename StorageT,
78 {}
79
80 template <typename Up = Tp, int StateT,
82 auto operator()(mode_constant<StateT>) const
83 {
84 auto _storage = storage<Tp>::instance();
85 if(_storage)
86 return sfinae<type, StateT>(_storage, 0, 0);
87 return false;
88 }
89
90 template <typename Up = Tp, int StateT,
92 auto operator()(mode_constant<StateT>) const
93 {
94 return false;
95 }
96
97 /// returns whether the finalization function has been executed (will return false
98 /// if the component does not support any overload of `global_init(...)` or
99 /// `thread_init(...)`
100 template <int StateT>
101 static bool get_executed()
102 {
103 return was_executed<StateT>();
104 }
105
106private:
107 template <typename Up, int StateT, typename StorageT,
109 auto sfinae(StorageT _storage, int, int) const
110 -> decltype(std::declval<Up>().global_init(_storage), bool())
111 {
112 if(was_executed<StateT>())
113 return false;
114 CONDITIONAL_PRINT_HERE(tim::settings::debug(), "global init for %s",
115 demangle<Tp>().c_str());
116 type::global_init(_storage);
117 was_executed<StateT>() = true;
118 return true;
119 }
120
121 template <typename Up, int StateT, typename StorageT,
123 auto sfinae(StorageT, int, long) const
124 -> decltype(std::declval<Up>().global_init(), bool())
125 {
126 if(was_executed<StateT>())
127 return false;
128 CONDITIONAL_PRINT_HERE(tim::settings::debug(), "global init for %s",
129 demangle<Tp>().c_str());
130 type::global_init();
131 was_executed<StateT>() = true;
132 return true;
133 }
134
135 template <typename Up, int StateT, typename StorageT,
137 auto sfinae(StorageT _storage, int, int) const
138 -> decltype(std::declval<Up>().thread_init(_storage), bool())
139 {
140 if(was_executed<StateT>())
141 return false;
142 CONDITIONAL_PRINT_HERE(tim::settings::debug(), "thread init for %s",
143 demangle<Tp>().c_str());
144 type::thread_init(_storage);
145 was_executed<StateT>() = true;
146 return true;
147 }
148
149 template <typename Up, int StateT, typename StorageT,
151 auto sfinae(StorageT, int, long) const
152 -> decltype(std::declval<Up>().thread_init(), bool())
153 {
154 if(was_executed<StateT>())
155 return false;
156 CONDITIONAL_PRINT_HERE(tim::settings::debug(), "thread init for %s",
157 demangle<Tp>().c_str());
158 type::thread_init();
159 was_executed<StateT>() = true;
160 return true;
161 }
162
163 template <typename Up, int StateT, typename StorageT>
164 bool sfinae(StorageT, long, long) const
165 {
166 return false;
167 }
168
169private:
170 template <int StateT, enable_if_t<StateT == init_mode::global, char> = 0>
171 static bool& was_executed()
172 {
173 static bool _instance = false;
174 return _instance;
175 }
176
177 template <int StateT, enable_if_t<StateT == init_mode::thread, char> = 0>
178 static bool& was_executed()
179 {
180 static thread_local bool _instance = false;
181 return _instance;
182 }
183};
184//
185//--------------------------------------------------------------------------------------//
186//
187} // namespace operation
188} // 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: init.hpp:51
int int int int int int StateT
Definition: init.hpp:107
int int int int int int int int int int StorageT bool sfinae(StorageT, long, long) const
Definition: init.hpp:164
int int int int int int int int StateT
Definition: init.hpp:135
int int int int StateT
Definition: init.hpp:80
int int int StateT
Definition: init.hpp:75
int int int int int StateT
Definition: init.hpp:90
int int int int int int int StateT
Definition: init.hpp:121
int int StateT
Definition: init.hpp:68
int int int int int int int int int StateT
Definition: init.hpp:149
int int StorageT
Definition: init.hpp:68
TIMEMORY_DELETE_COPY_MOVE_OBJECT(init) template< typename Up
#define CONDITIONAL_PRINT_HERE(CONDITION,...)
Definition: macros.hpp:183
typename typename typename
Definition: types.hpp:226