kokkos-utils 0.0.4
 
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 <functional>
6#include <mutex>
7#include <variant>
8
12
14{
15
16template <typename...>
18
26template <typename MatcherType, Event... EventTypes> requires (sizeof...(EventTypes) > 0 && MatcherFor<MatcherType, EventTypes...>)
28{
29public:
30 using matcher_t = MatcherType;
31 using event_type_list_t = Kokkos::Impl::type_list<EventTypes...>;
32 using event_variant_t = std::variant<EventTypes...>;
33
34public:
35 RecorderListener() = default;
36
37 template <typename T> requires std::same_as<std::remove_cvref_t<T>, MatcherType>
38 explicit RecorderListener(T&& matcher_)
39 : matcher(std::forward<T>(matcher_)) {}
40
45
47 template <EventOneOf<EventTypes...> EventType>
48 void operator()(const EventType& event) {
49 if (matcher(event)) {
50 const std::lock_guard lock(mtx);
51 recorded_events.push_back(event);
52 }
53 }
54
63 void report(std::ostream& out) const
64 {
65 out << "Number of events recorded: " << recorded_events.size() << std::endl;
66 for (const auto& recorded_event : recorded_events) {
67 std::visit([&out] (const auto& arg) { out << "- " << arg << std::endl; }, recorded_event);
68 }
69 }
70
72 template <typename Callable>
73 static auto record(Callable&& callable)
74 {
75 const auto recorder_listener = std::make_shared<RecorderListener>();
76
77 Manager::register_listener(recorder_listener);
78
79 std::invoke(std::forward<Callable>(callable));
80
81 Manager::unregister_listener(recorder_listener.get());
82
83 return std::move(recorder_listener->recorded_events);
84 }
85
86public:
91 std::deque<event_variant_t> recorded_events {};
92
93private:
94 MatcherType matcher {};
95 mutable std::mutex mtx;
96};
97
98template <typename MatcherType, Event... EventTypes>
99class RecorderListener<MatcherType, Kokkos::Impl::type_list<EventTypes...>> : public RecorderListener<MatcherType, EventTypes...> {
100 using RecorderListener<MatcherType, EventTypes...>::RecorderListener;
101};
102
103template <Event... EventTypes>
104class RecorderListener<EventTypes...> : public RecorderListener<AnyEventMatcher, EventTypes...> {};
105
106template <Event... EventTypes>
107class RecorderListener<Kokkos::Impl::type_list<EventTypes...>> : public RecorderListener<AnyEventMatcher, EventTypes...> {};
108
109} // namespace Kokkos::utils::callbacks
110
111#endif // KOKKOS_UTILS_CALLBACKS_RECORDERLISTENER_HPP
static void unregister_listener(const Callable *const callable)
Unregister a callable object as a listener.
Definition Manager.hpp:197
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:174
static auto record(Callable &&callable)
Returns the events of the types EventTypes... that occur during the execution of callable.
RecorderListener & operator=(RecorderListener &&)=delete
RecorderListener & operator=(const RecorderListener &)=delete
void operator()(const EventType &event)
Ensure atomic growth of recorded_events with mtx.
void report(std::ostream &out) const
Report the recorded events.
Concept to constrain any event type that is one of the given event types.
Definition Events.hpp:288
Check that Callable is a matcher for each event in EventTypes.
Definition Matcher.hpp:35