1#ifndef KOKKOS_UTILS_CALLBACKS_MANAGER_HPP
2#define KOKKOS_UTILS_CALLBACKS_MANAGER_HPP
33template <Event EventType>
38 virtual void operator()(
const EventType& event)
const = 0;
44template <
typename Callable>
47 template <
typename T>
requires std::same_as<std::remove_cvref_t<T>, Callable>
53template <Event EventType,
typename ListenerModel>
59 if constexpr (std::is_invocable_v<decltype(static_cast<const ListenerModel*>(
this)->callable),
const EventType&>) {
60 static_cast<const ListenerModel*
>(
this)->callable.operator()(event);
62 static_cast<const ListenerModel*
>(
this)->callable->operator()(event);
67template <
typename Callable,
Event... EventTypes>
77template <
typename Callable,
Event... EventTypes>
87template <Event EventType>
90 using type = std::list<const ListenerConceptCallOperator<EventType>*>;
173 template <Listener Callable>
196 template <Listener Callable>
205 const auto iter = std::ranges::find_if(
207 [&] (
const std::unique_ptr<impl::ListenerConceptBase>& listener)
209 auto*
const listener_ptr =
dynamic_cast<listener_model_t*
>(listener.get());
210 return listener_ptr ==
nullptr ? false : listener_ptr->callable.get() == callable;
241 template <Event EventType>
243 for (
const auto& listener: this->
listeners) {
245 callable->operator()(event);
252 return next_event_id.fetch_add(1, std::memory_order_relaxed);
269 template <
typename ListenerModelType,
typename CallableType>
272 using event_type_list_t =
typename ListenerModelType::event_type_list_t;
278 listeners.end(), std::make_unique<ListenerModelType>(std::forward<CallableType>(callable))
308 template <Event EventType>
312 get<Kokkos::utils::impl::type_list_index_v<EventType, EventTypeList>>(
registered_callbacks).empty()
320 template <Event EventType>
323 return [] <
typename... Args>(Args... args) ->
void {
324 EventType
event{args...};
329 template <BeginEvent EventType>
334 EventType
event{ .name = name, .dev_id = dev_id, .event_id = *event_id };
336 *event_id =
event.event_id;
340 template <DataEvent EventType>
343 return [] (Kokkos_Profiling_SpaceHandle kpsh,
const char* name,
const void* ptr, uint64_t size) ->
void
345 EventType
event{ .alloc = { .kpsh = kpsh, .name = name, .ptr = ptr, .size = size } };
350 template <Event EventType>
requires std::same_as<EventType, BeginDeepCopyEvent>
353 return [] (Kokkos_Profiling_SpaceHandle dst_kpsh,
const char* dst_name,
const void* dst_ptr,
354 Kokkos_Profiling_SpaceHandle src_kpsh,
const char* src_name,
const void* src_ptr, uint64_t size) ->
void
357 .dst = { .kpsh = dst_kpsh, .name = dst_name, .ptr = dst_ptr, .size = size },
358 .src = { .kpsh = src_kpsh, .name = src_name, .ptr = src_ptr, .size = size }
364 template <Event EventType>
requires std::same_as<EventType, CreateProfileSectionEvent>
367 return [] (
const char* name, uint32_t* section_id) ->
void
369 EventType
event{ .name = name, .section_id = *section_id };
371 *section_id =
event.section_id;
379 template <Event EventType>
388 template <Event EventType>
393 if (context_callback) {
400 template <
typename ContextCallbackType, BeginEvent EventType>
402 context_callback(event.name.c_str(), event.dev_id, &event.event_id);
405 template <
typename ContextCallbackType, EndEvent EventType>
407 context_callback(event.event_id);
410 template <
typename ContextCallbackType, DataEvent EventType>
412 context_callback(event.alloc.kpsh, event.alloc.name.c_str(), event.alloc.ptr, event.alloc.size);
415 template <
typename ContextCallbackType>
421 template <
typename ContextCallbackType>
426 template <
typename ContextCallbackType>
431 template <
typename ContextCallbackType, ProfileSectionManipulationEvent EventType>
433 context_callback(event.section_id);
436 template <
typename ContextCallbackType>
438 context_callback(event.
name.c_str());
441 template <
typename ContextCallbackType>
446 template <
typename ContextCallbackType>
448 context_callback(event.
name.c_str());
451 template <Event EventType>
454 template <BeginEvent EventType>
463 template <Event EventType>
467 listener->operator()(event);
485template <Event EventType>
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, const EndDeepCopyEvent &)
void set_dispatching_callback_impl() const
std::list< std::unique_ptr< impl::ListenerConceptBase > > listener_list_t
static void unregister_listener(const Callable *const callable)
Unregister a callable object as a listener.
static Manager & get_instance()
void dispatch_registered_callbacks(const EventType &event) const
static auto create_dispatching_callback_for_event_type_impl()
void reset_context_callbacks() const
Uses Kokkos::Tools::Experimental::set_callbacks to restore the Kokkos callback function pointers to t...
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, const ProfileEvent &event)
Manager(const Manager &)=delete
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, const PushRegionEvent &event)
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, CreateProfileSectionEvent &event)
void dispatch_context_callback(EventType &event)
Dispatch the event to the context callback, if any is set in Kokkos.
static listener_list_const_iter_t register_listener(T &&callable)
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, EventType &event)
listener_call_opr_list_tuple_t registered_callbacks
auto get_next_event_id() noexcept
Get the next available event ID.
listener_list_t listeners
void dispatch(const EventType &event) const
Dispatch the event to all registered listeners that can handle it.
std::atomic< uint32_t > next_section_id
Manager()
Constructor. Retrieves and stores the Kokkos::Tools::Experimental::EventSet containing the Kokkos cal...
static Manager * singleton
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, const BeginDeepCopyEvent &event)
void set_dispatching_callbacks() const
Uses the setters Kokkos::Tools::Experimental::set_<event_type_name>_callback to set the Kokkos callba...
std::atomic< EventTraits::event_id_t > next_event_id
void dispatch(EventType &event)
Dispatch first the context callback, if any is set in Kokkos, and then sequentially the registered ca...
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, const PopRegionEvent &)
static bool is_initialized()
void increment_id_if_needed_for_event_type_impl(CreateProfileSectionEvent &event)
impl::listener_call_opr_list_tuple_t listener_call_opr_list_tuple_t
static void dispatch_context_callback_for_event_type_impl(ContextCallbackType *context_callback, const EventType &event)
static auto create_dispatching_callback_for_event_type_impl()
void increment_id_if_needed_for_event_type_impl(EventType &event)
typename listener_list_t::const_iterator listener_list_const_iter_t
Manager & operator=(const Manager &)=delete
static void unregister_listener(const listener_list_const_iter_t &iter)
void increment_id_if_needed_for_event_type_impl(EventType &)
static listener_list_const_iter_t register_listener_impl(CallableType &&callable)
Kokkos::Tools::Experimental::EventSet context_callbacks
static auto create_dispatching_callback_for_event_type_impl()
auto get_next_section_id() noexcept
Get the next available section ID.
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.
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...
auto get_next_event_id() noexcept
If the Manager is initialized, return Manager::get_next_event_id. Otherwise, return EventTraits::inva...
typename impl::ListenerEventTypeList< Callable >::type listener_event_type_list_t
Type list holding the event types that Callable can be a listener for.
auto get_next_section_id() noexcept
void dispatch(const EventType &event)
If the Manager is initialized, call Manager::dispatch with event.
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
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_Profiling_SpaceHandle kpsh
Begin-deep-copy event associated with Kokkos::Tools::Experimental::EventSet::begin_deep_copy.
Create-profile-section event associated with Kokkos::Tools::Experimental::EventSet::create_profile_se...
End-deep-copy event associated with Kokkos::Tools::Experimental::EventSet::end_deep_copy.
static constexpr event_id_t invalid_event_id
Pop-region event associated with Kokkos::Tools::Experimental::EventSet::pop_region.
Profile event associated with Kokkos::Tools::Experimental::EventSet::profile_event.
Push-region event associated with Kokkos::Tools::Experimental::EventSet::push_region.
Helper structures used by Kokkos::utils::callbacks::Manager to store and call registered listeners.
virtual ~ListenerConceptBase()=default
virtual ~ListenerConceptCallOperator()=default
virtual void operator()(const EventType &event) const =0
ListenerModelBase(T &&callable_)
void operator()(const EventType &event) const override
Kokkos::Impl::type_list< EventTypes... > event_type_list_t
Kokkos::Impl::type_list< EventTypes... > event_type_list_t