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.
components.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
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/components/ompt/components.hpp
27 * \brief Implementation of the ompt component(s)
28 */
29
30#pragma once
31
35//
37#include "timemory/components/ompt/backends.hpp"
39//
44//
45//======================================================================================//
46//
47namespace tim
48{
49//
50//--------------------------------------------------------------------------------------//
51//
52namespace component
53{
54//
55//--------------------------------------------------------------------------------------//
56//
57template <typename Api>
59: public base<ompt_handle<Api>, void>
60, private policy::instance_tracker<ompt_handle<Api>>
61{
62 using api_type = Api;
64 using value_type = void;
69
70 static std::string label() { return "ompt_handle"; }
72 {
73 return std::string(
74 "Control switch for enabling/disabling OpenMP tools defined by the ") +
75 demangle<api_type>() + " tag";
76 }
77
78 static auto& get_initializer()
79 {
80 static std::function<void()> _instance = []() {};
81 return _instance;
82 }
83
84 static void configure()
85 {
86 static int32_t _once = 0;
87 if(_once++ == 0)
89 }
90
91 static void preinit()
92 {
93 static thread_local auto _data_tracker =
94 tim::storage_initializer::get<ompt_data_tracker<api_type>>();
95 consume_parameters(_data_tracker);
96 }
97
98 static void global_init()
99 {
100 // if handle gets initialized (i.e. used), it indicates we want to disable
102 configure();
103 }
104
106
107 void start()
108 {
109#if defined(TIMEMORY_USE_OMPT)
111 if(m_tot == 0)
113#endif
114 }
115
116 void stop()
117 {
118#if defined(TIMEMORY_USE_OMPT)
120 if(m_tot == 0)
122#endif
123 }
124
125private:
126 using tracker_type::m_tot;
127
128public:
130 {
131 if(_prefix.empty())
132 return;
133 tim::auto_lock_t lk(get_persistent_data().m_mutex);
134 get_persistent_data().m_prefix = _prefix + "/";
135 }
136
138 {
139 tim::auto_lock_t lk(get_persistent_data().m_mutex);
140 return get_persistent_data().m_prefix;
141 }
142
143private:
144 struct persistent_data
145 {
146 std::string m_prefix;
147 mutex_t m_mutex;
148 };
149
150 static persistent_data& get_persistent_data()
151 {
152 static persistent_data _instance;
153 return _instance;
154 }
155};
156//
157//--------------------------------------------------------------------------------------//
158//
159template <typename Api>
160struct ompt_data_tracker : public base<ompt_data_tracker<Api>, void>
161{
162 using api_type = Api;
164 using value_type = void;
167
168 using tracker_t = ompt_data_tracker_t;
169
170 static std::string label() { return "ompt_data_tracker"; }
172 {
173 return std::string("OpenMP tools data tracker ") + demangle<api_type>();
174 }
175
176 static void preinit()
177 {
178 static thread_local auto _tracker_storage = storage_initializer::get<tracker_t>();
179 consume_parameters(_tracker_storage);
180 }
181
182 static void global_init()
183 {
184 preinit();
185 tracker_t::label() = "ompt_data_tracker";
186 }
187
188 void start() {}
189 void stop() {}
190
191 template <typename... Args>
192 void store(Args&&...)
193 {
194 apply_store<tracker_t>(std::plus<size_t>{}, 1);
195 }
196
197 void store(ompt_id_t target_id, ompt_id_t host_op_id, ompt_target_data_op_t optype,
198 void* host_addr, void* device_addr, size_t bytes)
199 {
200 apply_store<tracker_t>(std::plus<size_t>{}, bytes);
201 consume_parameters(target_id, host_op_id, optype, host_addr, device_addr);
202 }
203
204 void store(ompt_id_t target_id, unsigned int nitems, void** host_addr,
205 void** device_addr, const size_t* bytes, unsigned int* mapping_flags)
206 {
207 size_t _tot = 0;
208 for(unsigned int i = 0; i < nitems; ++i)
209 _tot += bytes[i];
210 apply_store<tracker_t>(std::plus<size_t>{}, _tot);
211 consume_parameters(target_id, host_addr, device_addr, mapping_flags);
212 // auto _prefix = tim::get_hash_identifier(m_prefix_hash);
213 }
214
215public:
216 void set_prefix(uint64_t _prefix_hash) { m_prefix_hash = _prefix_hash; }
217 void set_scope(scope::config _scope) { m_scope_config = _scope; }
218
219private:
220 template <typename Tp, typename... Args>
221 void apply_store(Tp& _obj, Args&&... args)
222 {
223 operation::push_node<Tp>(_obj, m_scope_config, m_prefix_hash);
224 operation::start<Tp> _start(_obj);
225 operation::store<Tp>(_obj, std::forward<Args>(args)...);
226 operation::stop<Tp> _stop(_obj);
227 operation::pop_node<Tp> _pop(_obj);
228 }
229
230 template <typename Tp, typename... Args>
231 void apply_store(Args&&... args)
232 {
233 Tp _obj;
234 apply_store(_obj, std::forward<Args>(args)...);
235 }
236
237private:
238 uint64_t m_prefix_hash = 0;
239 scope::config m_scope_config = scope::get_default();
240};
241//
242//--------------------------------------------------------------------------------------//
243//
244
245} // namespace component
246} // namespace tim
247//
248//======================================================================================//
Implementation of the data_tracker component(s)
void stop(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:386
void start(TupleT< Tp... > &obj, Args &&... args)
Definition: functional.cpp:316
Inherit from this policy to add reference counting support. Useful if you want to turn a global setti...
Definition: types.hpp:406
Definition: kokkosp.cpp:39
char const std::string & _prefix
Definition: config.cpp:55
std::unique_lock< mutex_t > auto_lock_t
Unique lock type around mutex_t.
Definition: locking.hpp:42
std::recursive_mutex mutex_t
Recursive mutex is used for convenience since the performance penalty vs. a regular mutex is not real...
Definition: locking.hpp:38
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
void consume_parameters(ArgsT &&...)
Definition: types.hpp:285
Definition for various functions for store in operations.
storage< Tp, Value > storage_type
A very lightweight storage class which provides nothing.
Definition: declaration.hpp:51
void set_scope(scope::config _scope)
Definition: components.hpp:217
void store(ompt_id_t target_id, unsigned int nitems, void **host_addr, void **device_addr, const size_t *bytes, unsigned int *mapping_flags)
Definition: components.hpp:204
void set_prefix(uint64_t _prefix_hash)
Definition: components.hpp:216
void store(ompt_id_t target_id, ompt_id_t host_op_id, ompt_target_data_op_t optype, void *host_addr, void *device_addr, size_t bytes)
Definition: components.hpp:197
static std::string description()
Definition: components.hpp:171
static std::string description()
Definition: components.hpp:71
void set_prefix(const std::string &_prefix)
Definition: components.hpp:129
static auto & get_initializer()
Definition: components.hpp:78
static std::string label()
Definition: components.hpp:70
static std::string get_prefix()
Definition: components.hpp:137
typename trait::ompt_handle< api_type >::type toolset_type
Definition: components.hpp:67
this data type encodes the options of storage scope. The default is hierarchical (tree) scope....
Definition: types.hpp:453
static bool set(bool val, enable_if_t< is_available< U >::value &&get_value< U >(), int >=0)
SET specialization if component is available.