157 Manager() : context_callbacks(
Kokkos::Tools::Experimental::get_callbacks()) {}
170 template <Listener Callable>
175 return register_listener_impl<listener_model_t>(std::move(callable));
189 return register_listener_impl<listener_model_t>(std::forward<T>(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;
212 get<Kokkos::utils::impl::type_list_index_v<EventType, EventTypeList>>(
get_instance().registered_callbacks).remove(
217 listeners.erase(iter);
225 get<Kokkos::utils::impl::type_list_index_v<EventType, EventTypeList>>(
get_instance().registered_callbacks).remove(
240 void reset_context_callbacks()
const {
241 Kokkos::Tools::Experimental::set_callbacks(context_callbacks);
244 template <
typename ListenerModelType,
typename CallableType>
247 using event_type_list_t =
typename ListenerModelType::event_type_list_t;
252 const auto iter = listeners.insert(
253 listeners.end(), std::make_unique<ListenerModelType>(std::forward<CallableType>(callable))
259 get<Kokkos::utils::impl::type_list_index_v<EventType, EventTypeList>>(
get_instance().registered_callbacks).push_back(
260 dynamic_cast<const impl::ListenerConceptCallOperator<EventType>*
>(iter->get())
274 void set_dispatching_callbacks()
const
277 set_dispatching_callback_impl<EventType>();
281 template <Event EventType>
282 void set_dispatching_callback_impl()
const
285 get<Kokkos::utils::impl::type_list_index_v<EventType, EventTypeList>>(registered_callbacks).empty()
287 : create_dispatching_callback_for_event_type_impl<EventType>()
293 template <Event EventType>
294 static auto create_dispatching_callback_for_event_type_impl()
296 return [] <
typename... Args>(Args... args) ->
void {
297 EventType
event{args...};
302 template <BeginEvent EventType>
303 static auto create_dispatching_callback_for_event_type_impl()
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>
314 static auto create_dispatching_callback_for_event_type_impl()
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>
324 static auto create_dispatching_callback_for_event_type_impl()
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>
338 static auto create_dispatching_callback_for_event_type_impl()
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>
353 void dispatch(EventType& event)
355 dispatch_context_callback(event);
357 dispatch_registered_callbacks(event);
361 template <Event EventType>
362 void dispatch_context_callback(EventType& event)
366 if (context_callback) {
367 dispatch_context_callback_for_event_type_impl(context_callback, event);
369 increment_id_if_needed_for_event_type_impl(event);
373 template <
typename ContextCallbackType, BeginEvent EventType>
374 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback, EventType& event) {
375 context_callback(event.name.c_str(), event.dev_id, &event.event_id);
378 template <
typename ContextCallbackType, EndEvent EventType>
379 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback,
const EventType& event) {
380 context_callback(event.event_id);
383 template <
typename ContextCallbackType, DataEvent EventType>
384 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback,
const EventType& event) {
385 context_callback(event.alloc.kpsh, event.alloc.name.c_str(), event.alloc.ptr, event.alloc.size);
388 template <
typename ContextCallbackType>
389 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback,
const BeginDeepCopyEvent& event) {
390 context_callback(event.dst.kpsh, event.dst.name.c_str(), event.dst.ptr,
391 event.src.kpsh, event.src.name.c_str(), event.src.ptr, event.src.size);
394 template <
typename ContextCallbackType>
395 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback,
const EndDeepCopyEvent& ) {
399 template <
typename ContextCallbackType>
400 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback, CreateProfileSectionEvent& event) {
401 context_callback(event.name.c_str(), &event.section_id);
404 template <
typename ContextCallbackType, ProfileSectionManipulationEvent EventType>
405 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback,
const EventType& event) {
406 context_callback(event.section_id);
409 template <
typename ContextCallbackType>
410 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback,
const PushRegionEvent& event) {
411 context_callback(event.name.c_str());
414 template <
typename ContextCallbackType>
415 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback,
const PopRegionEvent& ) {
419 template <
typename ContextCallbackType>
420 static void dispatch_context_callback_for_event_type_impl(ContextCallbackType* context_callback,
const ProfileEvent& event) {
421 context_callback(event.name.c_str());
424 template <Event EventType>
425 void increment_id_if_needed_for_event_type_impl(EventType& ) {}
427 template <BeginEvent EventType>
428 void increment_id_if_needed_for_event_type_impl(EventType& event) {
429 event.event_id = next_event_id++;
432 void increment_id_if_needed_for_event_type_impl(CreateProfileSectionEvent& event) {
433 event.section_id = next_section_id++;
436 template <Event EventType>
437 void dispatch_registered_callbacks(
const EventType& event)
const
440 listener->operator()(event);
449 static inline std::unique_ptr<Manager> singleton =
nullptr;
451 Kokkos::Tools::Experimental::EventSet context_callbacks {};
453 uint64_t next_event_id = 0;
454 uint32_t next_section_id = 0;