1#ifndef KOKKOS_UTILS_CALLBACKS_MANAGER_HPP
2#define KOKKOS_UTILS_CALLBACKS_MANAGER_HPP
32template <Event EventType>
37 virtual void operator()(
const EventType& event)
const = 0;
43template <
typename Callable>
46 template <
typename T>
requires std::same_as<std::remove_cvref_t<T>, Callable>
52template <Event EventType,
typename ListenerModel>
58 if constexpr (std::is_invocable_v<decltype(static_cast<const ListenerModel*>(
this)->callable),
const EventType&>) {
59 static_cast<const ListenerModel*
>(
this)->callable.operator()(event);
61 static_cast<const ListenerModel*
>(
this)->callable->operator()(event);
66template <
typename Callable,
Event... EventTypes>
76template <
typename Callable,
Event... EventTypes>
86template <Event EventType>
89 using type = std::list<const ListenerConceptCallOperator<EventType>*>;
170 template <Listener Callable>
193 template <Listener Callable>
202 const auto iter = std::ranges::find_if(
204 [&] (
const std::unique_ptr<impl::ListenerConceptBase>& listener)
206 auto*
const listener_ptr =
dynamic_cast<listener_model_t*
>(listener.get());
207 return listener_ptr ==
nullptr ? false : listener_ptr->callable.get() == callable;
244 template <
typename ListenerModelType,
typename CallableType>
247 using event_type_list_t =
typename ListenerModelType::event_type_list_t;
253 listeners.end(), std::make_unique<ListenerModelType>(std::forward<CallableType>(callable))
281 template <Event EventType>
285 get<Kokkos::utils::impl::type_list_index_v<EventType, EventTypeList>>(
registered_callbacks).empty()
293 template <Event EventType>
296 return [] <
typename... Args>(Args... args) ->
void {
297 EventType
event{args...};
302 template <BeginEvent EventType>
305 return [] (
const char* name,
const uint32_t dev_id, uint64_t* event_id) ->
void
307 EventType
event{ .name = name, .dev_id = dev_id, .event_id = *event_id };
309 *event_id =
event.event_id;
313 template <DataEvent EventType>
316 return [] (Kokkos_Profiling_SpaceHandle kpsh,
const char* name,
const void* ptr, uint64_t size) ->
void
318 EventType
event{ .alloc = { .kpsh = kpsh, .name = name, .ptr = ptr, .size = size } };
323 template <Event EventType>
requires std::same_as<EventType, BeginDeepCopyEvent>
326 return [] (Kokkos_Profiling_SpaceHandle dst_kpsh,
const char* dst_name,
const void* dst_ptr,
327 Kokkos_Profiling_SpaceHandle src_kpsh,
const char* src_name,
const void* src_ptr, uint64_t size) ->
void
330 .dst = { .kpsh = dst_kpsh, .name = dst_name, .ptr = dst_ptr, .size = size },
331 .src = { .kpsh = src_kpsh, .name = src_name, .ptr = src_ptr, .size = size }
337 template <Event EventType>
requires std::same_as<EventType, CreateProfileSectionEvent>
340 return [] (
const char* name, uint32_t* section_id) ->
void
342 EventType
event{ .name = name, .section_id = *section_id };
344 *section_id =
event.section_id;
352 template <Event EventType>
361 template <Event EventType>
366 if (context_callback) {
373 template <
typename ContextCallbackType, BeginEvent EventType>
375 context_callback(event.name.c_str(), event.dev_id, &event.event_id);
378 template <
typename ContextCallbackType, EndEvent EventType>
380 context_callback(event.event_id);
383 template <
typename ContextCallbackType, DataEvent EventType>
385 context_callback(event.alloc.kpsh, event.alloc.name.c_str(), event.alloc.ptr, event.alloc.size);
388 template <
typename ContextCallbackType>
394 template <
typename ContextCallbackType>
399 template <
typename ContextCallbackType>
404 template <
typename ContextCallbackType, ProfileSectionManipulationEvent EventType>
406 context_callback(event.section_id);
409 template <
typename ContextCallbackType>
411 context_callback(event.
name.c_str());
414 template <
typename ContextCallbackType>
419 template <
typename ContextCallbackType>
421 context_callback(event.
name.c_str());
424 template <Event EventType>
427 template <BeginEvent EventType>
436 template <Event EventType>
440 listener->operator()(event);
449 static inline std::unique_ptr<Manager>
singleton =
nullptr;
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)
static std::unique_ptr< Manager > singleton
listener_call_opr_list_tuple_t registered_callbacks
listener_list_t listeners
Manager()
Constructor. Retrieves and stores the Kokkos::Tools::Experimental::EventSet containing the Kokkos cal...
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...
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 &)
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()
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.
Callable is a listener if it is invocable with at least one event type from Kokkos::utils::callbacks:...
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...
Kokkos::Impl::filter_type_list_t< impl::IsListenerFor< Callable >::template type, EventTypeList > listener_event_type_list_t
Type list holding the event types that Callable can be a listener for.
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 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.
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