kokkos-utils 0.0.1
 
Loading...
Searching...
No Matches
RecorderListener.hpp
Go to the documentation of this file.
1#ifndef KOKKOS_UTILS_CALLBACKS_RECORDERLISTENER_HPP
2#define KOKKOS_UTILS_CALLBACKS_RECORDERLISTENER_HPP
3
4#include <deque>
5#include <variant>
6
10
12{
13
14template <typename...>
16
24template <typename MatcherType, Event... EventTypes>
25requires Matcher<MatcherType, Kokkos::Impl::type_list<EventTypes...>>
26class RecorderListener<MatcherType, EventTypes...>
27{
28public:
29 using matcher_t = MatcherType;
30 using event_type_list_t = Kokkos::Impl::type_list<EventTypes...>;
31
32public:
33 RecorderListener() = default;
34
35 template <typename T> requires std::same_as<std::remove_cvref_t<T>, MatcherType>
36 explicit RecorderListener(T&& matcher_)
37 : matcher(std::forward<T>(matcher_)) {}
38
39 template <EventOneOf<event_type_list_t> EventType>
40 void operator()(const EventType& event) {
41 if (matcher(event)) this->recorded_events.push_back(event);
42 }
43
52 void report(std::ostream& out = std::cout) const
53 {
54 out << "Number of events recorded: " << recorded_events.size() << std::endl;
55 for (const auto& recorded_event : recorded_events) {
56 std::visit([&out] (const auto& arg) { out << "- " << arg << std::endl; }, recorded_event);
57 }
58 }
59
61 template <typename Callable>
62 static auto record(Callable&& callable)
63 {
64 const auto recorder_listener = std::make_shared<RecorderListener>();
65
66 Manager::register_listener(recorder_listener);
67
68 std::invoke(std::forward<Callable>(callable));
69
70 Manager::unregister_listener(recorder_listener.get());
71
72 return std::move(recorder_listener->recorded_events);
73 }
74
75public:
80 std::deque<std::variant<EventTypes...>> recorded_events {};
81
82private:
83 MatcherType matcher {};
84};
85
86template <typename MatcherType, Event... EventTypes>
87class RecorderListener<MatcherType, Kokkos::Impl::type_list<EventTypes...>> : public RecorderListener<MatcherType, EventTypes...> {
88 using RecorderListener<MatcherType, EventTypes...>::RecorderListener;
89};
90
91template <Event... EventTypes>
92class RecorderListener<EventTypes...> : public RecorderListener<AnyEventMatcher, EventTypes...> {};
93
94template <Event... EventTypes>
95class RecorderListener<Kokkos::Impl::type_list<EventTypes...>> : public RecorderListener<EventTypes...> {};
96
97} // namespace Kokkos::utils::callbacks
98
99#endif // KOKKOS_UTILS_CALLBACKS_RECORDERLISTENER_HPP
static void unregister_listener(const Callable *const callable)
Unregister a callable object as a listener.
Definition Manager.hpp:194
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:171
static auto record(Callable &&callable)
Returns the events of the types EventTypes... that occur during the execution of callable.
void report(std::ostream &out=std::cout) const
Report the recorded events.
Concept that models that a callable object is a matcher for the event types in EventTypeSubList if it...
Definition Matcher.hpp:29