kokkos-utils 0.0.7
Loading...
Searching...
No Matches
Manager.hpp
Go to the documentation of this file.
1#ifndef KOKKOS_UTILS_CALLBACKS_MANAGER_HPP
2#define KOKKOS_UTILS_CALLBACKS_MANAGER_HPP
3
4#include <atomic>
5#include <list>
6#include <ranges>
7
10
12{
13
14namespace impl
15{
16
29{
30 virtual ~ListenerConceptBase() = default;
31};
32
33template <Event EventType>
35{
36 virtual ~ListenerConceptCallOperator() = default;
37
38 virtual void operator()(const EventType& event) const = 0;
39};
40
41template <typename...>
43
44template <typename Callable>
46{
47 template <typename T> requires std::same_as<std::remove_cvref_t<T>, Callable>
48 explicit ListenerModelBase(T&& callable_) : callable(std::forward<T>(callable_)) {}
49
50 Callable callable;
51};
52
53template <Event EventType, typename ListenerModel>
55{
56 void operator()(const EventType& event) const override
57 {
58 // Distinguish between a callable object that is a function object and one that is a shared pointer.
59 if constexpr (std::is_invocable_v<decltype(static_cast<const ListenerModel*>(this)->callable), const EventType&>) {
60#if (defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC <= 1350)
61 static_cast<const ListenerModel*>(this)->callable(event);
62#else
63 static_cast<const ListenerModel*>(this)->callable.operator()(event);
64#endif
65 } else {
66#if (defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC <= 1350)
67 (*static_cast<const ListenerModel*>(this)->callable)(event);
68#else
69 static_cast<const ListenerModel*>(this)->callable->operator()(event);
70#endif
71 }
72 };
73};
74
75template <typename Callable, Event... EventTypes>
76struct ListenerModel<Callable, EventTypes...>
77 : public ListenerModelCallOperator<EventTypes, ListenerModel<Callable, EventTypes...>>...,
78 public ListenerModelBase<Callable>
79{
80 using event_type_list_t = Kokkos::Impl::type_list<EventTypes...>;
81
83};
84
85template <typename Callable, Event... EventTypes>
86struct ListenerModel<Callable, Kokkos::Impl::type_list<EventTypes...>>
87 : public ListenerModel<Callable, EventTypes...>
88{
89 using event_type_list_t = Kokkos::Impl::type_list<EventTypes...>;
90
91 using ListenerModel<Callable, EventTypes...>::ListenerModel;
92};
93
95template <Event EventType>
97{
98 using type = std::list<const ListenerConceptCallOperator<EventType>*>;
99};
100
107>;
109
110} // namespace impl
111
149{
150public:
151 using listener_list_t = std::list<std::unique_ptr<impl::ListenerConceptBase>>;
152 using listener_list_const_iter_t = typename listener_list_t::const_iterator;
154
155public:
156 Manager(const Manager&) = delete;
157 Manager& operator=(const Manager&) = delete;
158
160
161protected:
166 Manager() : context_callbacks(Kokkos::Tools::Experimental::get_callbacks()) {}
167
168public:
169 static void initialize() { if(singleton == nullptr) singleton = new Manager(); }
170 static void finalize() { if(singleton != nullptr) { delete singleton; singleton = nullptr; }}
171
172 static bool is_initialized() { return singleton != nullptr; }
173
174 static Manager& get_instance() { return *singleton; }
175
181 template <Listener Callable>
182 static listener_list_const_iter_t register_listener(std::shared_ptr<Callable> callable)
183 {
184 using event_type_list_t = listener_event_type_list_t<Callable>;
185 using listener_model_t = impl::ListenerModel<std::shared_ptr<Callable>, event_type_list_t>;
186 return register_listener_impl<listener_model_t>(std::move(callable));
187 }
188
195 template <typename T> requires Listener<std::remove_cvref_t<T>>
197 {
198 using event_type_list_t = listener_event_type_list_t<std::remove_cvref_t<T>>;
199 using listener_model_t = impl::ListenerModel<std::remove_cvref_t<T>, event_type_list_t>;
200 return register_listener_impl<listener_model_t>(std::forward<T>(callable));
201 }
202
204 template <Listener Callable>
205 static void unregister_listener(const Callable* const callable)
206 {
207 using event_type_list_t = listener_event_type_list_t<Callable>;
208
209 using listener_model_t = impl::ListenerModel<std::shared_ptr<Callable>, event_type_list_t>;
210
212
213 const auto iter = std::ranges::find_if(
214 listeners,
215 [&] (const std::unique_ptr<impl::ListenerConceptBase>& listener)
216 {
217 const auto* const listener_ptr = dynamic_cast<listener_model_t*>(listener.get());
218 return listener_ptr == nullptr ? false : listener_ptr->callable.get() == callable;
219 }
220 );
221
224 get<Kokkos::utils::impl::type_list_index_v<EventType, EventTypeList>>(get_instance().registered_callbacks).remove(
225 dynamic_cast<const impl::ListenerConceptCallOperator<EventType>*>(iter->get())
226 );
227 }
228 });
229
230 listeners.erase(iter);
231
233 }
234
236 {
238 get<Kokkos::utils::impl::type_list_index_v<EventType, EventTypeList>>(get_instance().registered_callbacks).remove(
239 dynamic_cast<const impl::ListenerConceptCallOperator<EventType>*>(iter->get())
240 );
241 });
242
243 get_instance().listeners.erase(iter);
244
246 }
247
249 template <Event EventType>
250 void dispatch(const EventType& event) const {
251 for (const auto& listener: this->listeners) {
252 if (const auto* const callable = dynamic_cast<const impl::ListenerConceptCallOperator<EventType>*>(listener.get()); callable != nullptr) {
253#if (defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC <= 1350)
254 (*callable)(event);
255#else
256 callable->operator()(event);
257#endif
258 }
259 }
260 }
261
263 auto get_next_event_id() noexcept {
264 return next_event_id.fetch_add(1, std::memory_order_relaxed);
265 }
266
268 auto get_next_section_id() noexcept {
269 return next_section_id.fetch_add(1, std::memory_order_relaxed);
270 }
271
272private:
278 Kokkos::Tools::Experimental::set_callbacks(context_callbacks);
279 }
280
281 template <typename ListenerModelType, typename CallableType>
283 {
284 using event_type_list_t = typename ListenerModelType::event_type_list_t;
285
287
289 const auto iter = listeners.insert(
290 listeners.end(), std::make_unique<ListenerModelType>(std::forward<CallableType>(callable))
291 );
292
297 get<Kokkos::utils::impl::type_list_index_v<EventType, EventTypeList>>(get_instance().registered_callbacks).push_back(
298 dynamic_cast<const impl::ListenerConceptCallOperator<EventType>*>(iter->get())
299 );
300 }
301 });
302
304
305 return iter;
306 }
307
319
320 template <Event EventType>
322 {
324 get<Kokkos::utils::impl::type_list_index_v<EventType, EventTypeList>>(registered_callbacks).empty()
327 );
328 }
329
332 template <Event EventType>
334 {
335 return [] <typename... Args>(Args... args) -> void {
336 EventType event{args...};
337 get_instance().dispatch(event);
338 };
339 }
340
341 template <BeginEvent EventType>
343 {
344 return [] (const char* name, const uint32_t dev_id, EventTraits::event_id_t* event_id) -> void
345 {
346 EventType event{ .name = name, .dev_id = dev_id, .event_id = *event_id };
347 get_instance().dispatch(event);
348 *event_id = event.event_id;
349 };
350 }
351
352 template <DataEvent EventType>
354 {
355 return [] (Kokkos::Tools::SpaceHandle kpsh, const char* name, const void* ptr, uint64_t size) -> void
356 {
357 EventType event{ .alloc = { .kpsh = kpsh, .name = name, .ptr = ptr, .size = size } };
358 get_instance().dispatch(event);
359 };
360 }
361
362 template <Event EventType> requires std::same_as<EventType, BeginDeepCopyEvent>
364 {
365 return [] (Kokkos::Tools::SpaceHandle dst_kpsh, const char* dst_name, const void* dst_ptr,
366 Kokkos::Tools::SpaceHandle src_kpsh, const char* src_name, const void* src_ptr, uint64_t size) -> void
367 {
368 EventType event{
369 .dst = { .kpsh = dst_kpsh, .name = dst_name, .ptr = dst_ptr, .size = size },
370 .src = { .kpsh = src_kpsh, .name = src_name, .ptr = src_ptr, .size = size }
371 };
372 get_instance().dispatch(event);
373 };
374 }
375
376 template <Event EventType> requires std::same_as<EventType, CreateProfileSectionEvent>
378 {
379 return [] (const char* name, uint32_t* section_id) -> void
380 {
381 EventType event{ .name = name, .section_id = *section_id };
382 get_instance().dispatch(event);
383 *section_id = event.section_id;
384 };
385 }
386
391 template <Event EventType>
392 void dispatch(EventType& event)
393 {
395
397 }
398
400 template <Event EventType>
401 void dispatch_context_callback(EventType& event)
402 {
404
405 if (context_callback) {
406 dispatch_context_callback_for_event_type_impl(context_callback, event);
407 } else {
409 }
410 }
411
412 template <typename ContextCallbackType, BeginEvent EventType>
413 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback, EventType& event) {
414 context_callback(event.name.c_str(), event.dev_id, &event.event_id);
415 }
416
417 template <typename ContextCallbackType, EndEvent EventType>
418 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback, const EventType& event) {
419 context_callback(event.event_id);
420 }
421
422 template <typename ContextCallbackType, DataEvent EventType>
423 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback, const EventType& event) {
424 context_callback(event.alloc.kpsh, event.alloc.name.c_str(), event.alloc.ptr, event.alloc.size);
425 }
426
427 template <typename ContextCallbackType>
428 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback, const BeginDeepCopyEvent& event) {
429 context_callback(event.dst.kpsh, event.dst.name.c_str(), event.dst.ptr,
430 event.src.kpsh, event.src.name.c_str(), event.src.ptr, event.src.size);
431 }
432
433 template <typename ContextCallbackType>
434 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback, const EndDeepCopyEvent& /* event */) {
435 context_callback();
436 }
437
438 template <typename ContextCallbackType>
439 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback, CreateProfileSectionEvent& event) {
440 context_callback(event.name.c_str(), &event.section_id);
441 }
442
443 template <typename ContextCallbackType, ProfileSectionManipulationEvent EventType>
444 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback, const EventType& event) {
445 context_callback(event.section_id);
446 }
447
448 template <typename ContextCallbackType>
449 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback, const PushRegionEvent& event) {
450 context_callback(event.name.c_str());
451 }
452
453 template <typename ContextCallbackType>
454 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback, const PopRegionEvent& /* event */) {
455 context_callback();
456 }
457
458 template <typename ContextCallbackType>
459 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback, const ProfileEvent& event) {
460 context_callback(event.name.c_str());
461 }
462
463 template <Event EventType>
464 void increment_id_if_needed_for_event_type_impl(EventType& /* event */) {}
465
466 template <BeginEvent EventType>
468 event.event_id = this->get_next_event_id();
469 }
470
474
475 template <Event EventType>
476 void dispatch_registered_callbacks(const EventType& event) const
477 {
479#if (defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC <= 1350)
480 (*listener)(event);
481#else
482 listener->operator()(event);
483#endif
484 }
485 }
486
487private:
489
491
492 static inline Manager* singleton = nullptr;
493
494 Kokkos::Tools::Experimental::EventSet context_callbacks {};
495
496 std::atomic<EventTraits::event_id_t> next_event_id{0};
497 std::atomic<uint32_t> next_section_id{0};
498};
499
501template <Event EventType>
502void dispatch(const EventType& event) {
505}
506
511
512inline auto get_next_section_id() noexcept {
514}
515
516} // namespace Kokkos::utils::callbacks
517
518#endif // KOKKOS_UTILS_CALLBACKS_MANAGER_HPP
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, const EndDeepCopyEvent &)
Definition Manager.hpp:434
void set_dispatching_callback_impl() const
Definition Manager.hpp:321
std::list< std::unique_ptr< impl::ListenerConceptBase > > listener_list_t
Definition Manager.hpp:151
static void unregister_listener(const Callable *const callable)
Unregister a callable object as a listener.
Definition Manager.hpp:205
static Manager & get_instance()
Definition Manager.hpp:174
void dispatch_registered_callbacks(const EventType &event) const
Definition Manager.hpp:476
static auto create_dispatching_callback_for_event_type_impl()
Definition Manager.hpp:377
void reset_context_callbacks() const
Uses Kokkos::Tools::Experimental::set_callbacks to restore the Kokkos callback function pointers to t...
Definition Manager.hpp:277
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, const ProfileEvent &event)
Definition Manager.hpp:459
Manager(const Manager &)=delete
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, const PushRegionEvent &event)
Definition Manager.hpp:449
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, CreateProfileSectionEvent &event)
Definition Manager.hpp:439
void dispatch_context_callback(EventType &event)
Dispatch the event to the context callback, if any is set in Kokkos.
Definition Manager.hpp:401
static listener_list_const_iter_t register_listener(T &&callable)
Definition Manager.hpp:196
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, EventType &event)
Definition Manager.hpp:413
listener_call_opr_list_tuple_t registered_callbacks
Definition Manager.hpp:488
auto get_next_event_id() noexcept
Get the next available event ID.
Definition Manager.hpp:263
void dispatch(const EventType &event) const
Dispatch the event to all registered listeners that can handle it.
Definition Manager.hpp:250
std::atomic< uint32_t > next_section_id
Definition Manager.hpp:497
Manager()
Constructor. Retrieves and stores the Kokkos::Tools::Experimental::EventSet containing the Kokkos cal...
Definition Manager.hpp:166
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, const BeginDeepCopyEvent &event)
Definition Manager.hpp:428
void set_dispatching_callbacks() const
Uses the setters Kokkos::Tools::Experimental::set_<event_type_name>_callback to set the Kokkos callba...
Definition Manager.hpp:313
std::atomic< EventTraits::event_id_t > next_event_id
Definition Manager.hpp:496
void dispatch(EventType &event)
Dispatch first the context callback, if any is set in Kokkos, and then sequentially the registered ca...
Definition Manager.hpp:392
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, const PopRegionEvent &)
Definition Manager.hpp:454
void increment_id_if_needed_for_event_type_impl(CreateProfileSectionEvent &event)
Definition Manager.hpp:471
impl::listener_call_opr_list_tuple_t listener_call_opr_list_tuple_t
Definition Manager.hpp:153
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, const EventType &event)
Definition Manager.hpp:418
static auto create_dispatching_callback_for_event_type_impl()
Definition Manager.hpp:333
void increment_id_if_needed_for_event_type_impl(EventType &event)
Definition Manager.hpp:467
typename listener_list_t::const_iterator listener_list_const_iter_t
Definition Manager.hpp:152
Manager & operator=(const Manager &)=delete
static void unregister_listener(const listener_list_const_iter_t &iter)
Definition Manager.hpp:235
void increment_id_if_needed_for_event_type_impl(EventType &)
Definition Manager.hpp:464
static listener_list_const_iter_t register_listener_impl(CallableType &&callable)
Definition Manager.hpp:282
Kokkos::Tools::Experimental::EventSet context_callbacks
Definition Manager.hpp:494
static auto create_dispatching_callback_for_event_type_impl()
Definition Manager.hpp:363
auto get_next_section_id() noexcept
Get the next available section ID.
Definition Manager.hpp:268
static listener_list_const_iter_t register_listener(std::shared_ptr< Callable > callable)
Register a callable object, passed as a shared pointer, as a listener.
Definition Manager.hpp:182
Kokkos::utils::impl::type_list_to_tuple_t< Kokkos::utils::impl::transform_type_list_t< ListOfListenerCallOprPerEventTypeTransformer, EventTypeList > > listener_call_opr_list_tuple_t
Type of container used by Kokkos::utils::callbacks::Manager to store pointers to the registered liste...
Definition Manager.hpp:105
auto get_next_event_id() noexcept
If the Manager is initialized, return Manager::get_next_event_id. Otherwise, return EventTraits::inva...
Definition Manager.hpp:508
typename impl::ListenerEventTypeList< Callable >::type listener_event_type_list_t
Type list holding the event types that Callable can be a listener for.
Definition Listener.hpp:72
auto get_next_section_id() noexcept
Definition Manager.hpp:512
void dispatch(const EventType &event)
If the Manager is initialized, call Manager::dispatch with event.
Definition Manager.hpp:502
auto get_callback_from_eventset(const Kokkos::Tools::Experimental::EventSet &event_set)
auto get_callback_setter()
Get the setter function of a Kokkos profiling callback corresponding to EventType.
constexpr bool type_list_contains_v
Definition type_list.hpp:73
constexpr void for_each(Callable callable)
Calls the instantiation of the call operator of a callable object for each type in a Kokkos::Impl::ty...
typename TransformTypeList< TransformerType, T >::type transform_type_list_t
typename TypeListToTuple< T >::type type_list_to_tuple_t
constexpr size_t type_list_index_v
Kokkos::Tools::SpaceHandle kpsh
Definition Events.hpp:136
Begin-deep-copy event associated with Kokkos::Tools::Experimental::EventSet::begin_deep_copy.
Definition Events.hpp:162
Create-profile-section event associated with Kokkos::Tools::Experimental::EventSet::create_profile_se...
Definition Events.hpp:177
End-deep-copy event associated with Kokkos::Tools::Experimental::EventSet::end_deep_copy.
Definition Events.hpp:171
static constexpr event_id_t invalid_event_id
Definition Events.hpp:25
Pop-region event associated with Kokkos::Tools::Experimental::EventSet::pop_region.
Definition Events.hpp:218
Profile event associated with Kokkos::Tools::Experimental::EventSet::profile_event.
Definition Events.hpp:224
Push-region event associated with Kokkos::Tools::Experimental::EventSet::push_region.
Definition Events.hpp:210
Helper struct needed for the implementation of Kokkos::utils::callbacks::impl::listener_call_opr_list...
Definition Manager.hpp:97
std::list< const ListenerConceptCallOperator< EventType > * > type
Definition Manager.hpp:98
Helper structures used by Kokkos::utils::callbacks::Manager to store and call registered listeners.
Definition Manager.hpp:29
virtual void operator()(const EventType &event) const =0
void operator()(const EventType &event) const override
Definition Manager.hpp:56