kokkos-utils 0.0.6
Loading...
Searching...
No Matches
test_RecorderListener.cpp
Go to the documentation of this file.
1#include "gmock/gmock.h"
2#include "gtest/gtest.h"
3
4#include "Kokkos_Macros.hpp"
5#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
6import kokkos.core;
7#else
8#include "Kokkos_Core.hpp"
9#endif
10
18
21
30
31using execution_space = Kokkos::DefaultExecutionSpace;
32
34{
35
36using namespace Kokkos::utils::callbacks;
37
40
41class RecorderListenerTest : public ::testing::Test,
43 public scoped::ExecutionSpace<execution_space>
44{};
45
48{
50 static_assert(std::same_as<
53 >);
54
55 static_assert(Listener <event_in_profile_section_recorder_t>);
56 static_assert(ListenerFor<event_in_profile_section_recorder_t, EventTypeList>);
57 static_assert(ListenerFor<event_in_profile_section_recorder_t, PushRegionEvent, PopRegionEvent>);
58}
59
60template <Event EvenType>
61class RecorderListenerSingleEventTypeTest : public ::testing::Test,
63
65
68{
69 using recorder_listener_t = RecorderListener<TypeParam>;
70 static_assert(std::same_as<typename recorder_listener_t::event_type_list_t, Kokkos::Impl::type_list<TypeParam>>);
71 static_assert(std::same_as<typename recorder_listener_t::matcher_t, AnyEventMatcher>);
72
73 const auto recorder = std::make_shared<recorder_listener_t>();
74
76 Manager::unregister_listener(recorder.get());
77
78 ASSERT_EQ(recorder->recorded_events.size(), 0);
79}
80
83{
84 EventInProfileSectionMatcher matcher{.matcher = EventRegexMatcher{.regex = std::regex("profile section")}};
85 const auto recorder = std::make_shared<event_in_profile_section_recorder_t>(std::move(matcher));
86
87 using recorder_t = RecorderListener<EventTypeList>;
88
89 const auto any_event_recorder = std::make_shared<recorder_t>();
90
93
95
97 ASSERT_THAT(
98 recorder->recorded_events,
99 ::testing::Contains(ABeginParallelForEventWithName(::testing::StrEq("computation - level 0 - pfor")))
100 );
101
102 ASSERT_THAT(
103 recorder->recorded_events,
105 APushRegionEventWithName(::testing::StrEq("computation - level 0")),
106 ABeginParallelForEventWithName(::testing::StrEq("computation - level 0 - pfor")),
108 ABeginFenceEventWithName(::testing::StrEq("computation - level 0 - fence after pfor")),
110 AProfileEventWithName(::testing::StrEq("buried marker")),
111 APushRegionEventWithName(::testing::StrEq("computation - level 1")),
112 ABeginParallelReduceEventWithName(::testing::StrEq("computation - level 1 - preduce on default exec")),
114 ABeginFenceEventWithName(::testing::StrEq("Kokkos::parallel_reduce: fence due to result being value, not view")),
118 )
119 );
120
121 ASSERT_GT(any_event_recorder->recorded_events.size(), recorder->recorded_events.size());
122
123 const auto fence_event_outside_profile_section = ABeginFenceEventWithName(::testing::StrEq("other fence after stopping the profile section"));
124
125 ASSERT_THAT(
126 recorder->recorded_events,
127 ::testing::Not(::testing::Contains(fence_event_outside_profile_section))
128 );
129
130 ASSERT_THAT(
131 any_event_recorder->recorded_events,
132 ::testing::Contains(fence_event_outside_profile_section)
133 );
134
135 const auto output = [&recorder] () -> std::string {
136 std::ostringstream oss;
137 recorder->report(oss);
138 return oss.str();
139 }();
140
141 ASSERT_THAT(output, ::testing::HasSubstr("computation - level 0 - pfor"));
142 ASSERT_THAT(output, ::testing::HasSubstr("computation - level 0 - fence after pfor"));
143
146}
147
150{
151 ASSERT_THAT(
153 ::testing::Contains(
155 ::testing::Field(&BeginParallelForEvent::name, ::testing::StrEq("computation - level 0 - pfor")),
156 ::testing::Field(&BeginParallelForEvent::dev_id, ::testing::Eq(Kokkos::Tools::Experimental::device_id(this->exec)))
157 )
158 )
159 );
160
162 ASSERT_THAT(
164 ::testing::Contains(
166 ::testing::FieldsAre(::testing::StrEq("computation - level 0 - pfor"), ::testing::Eq(Kokkos::Tools::Experimental::device_id(this->exec)), ::testing::_)
167 )
168 )
169 );
170}
171
174
176TEST(FenceFinder, traits)
177{
179 static_assert(std::same_as<
181 Kokkos::Impl::type_list<BeginFenceEvent>
182 >);
183
184 static_assert(Listener<fence_finder_t>);
185}
186
188TEST_F_WITH_CB_MGR(FenceFinderTest, recorded_events)
189{
190 const auto exec = Kokkos::Experimental::partition_space(execution_space{}, 1)[0];
191
192 const auto fence_finder = std::make_shared<fence_finder_t>(EventRegexMatcher(std::regex("computation - level 0 - fence after pfor")));
193
195
197
199 ASSERT_THAT(
200 fence_finder->recorded_events,
201 ::testing::Contains(
203 ::testing::Field(&BeginFenceEvent::name, ::testing::StrEq("computation - level 0 - fence after pfor")),
204 ::testing::Field(&BeginFenceEvent::dev_id, ::testing::Eq(Kokkos::Tools::Experimental::device_id(exec)))
205 )
206 )
207 );
208
210}
211
217
218inline std::ostream& operator<<(std::ostream& out, const CustomEvent& event) {
219 return out << "CustomEvent: " << "{event_id = " << event.event_id << "}";
220}
221
228
229inline std::ostream& operator<<(std::ostream& out, const CustomEventWithPayload& event) {
230 return out << "CustomEventWithPayload: " << "{payload = " << event.payload << ", event_id = " << event.event_id << "}";
231}
232
235
238
239
240TEST_F(RecorderListenerTest, record_mix_kokkos_and_custom_events) {
242
243 static_assert(std::same_as<
245 Kokkos::Impl::type_list<BeginParallelForEvent, CustomEvent, CustomEventWithPayload>
246 >);
247
252
253 ASSERT_THAT(
254 (recorder_listener_t::record([&exec = this->exec]{
257 .payload = "My taylor is rich.",
259 });
262 });
263 })),
266 ::testing::Field(&BeginParallelForEvent::name, ::testing::StrEq("computation - level 0 - pfor")),
267 ::testing::Field(&BeginParallelForEvent::dev_id, ::testing::Eq(Kokkos::Tools::Experimental::device_id(this->exec))),
268 ::testing::Field(&BeginParallelForEvent::event_id, ::testing::Eq(0))
269 ),
270 ACustomEventWithPayload(
271 ::testing::Field(&CustomEventWithPayload::payload, ::testing::StrEq("My taylor is rich.")),
272 ::testing::Field(&CustomEventWithPayload::event_id, ::testing::Eq(1))
273 ),
274 ACustomEvent(
275 ::testing::Field(&CustomEvent::event_id, ::testing::Eq(2))
276 )
277 )
278 );
279}
280
282TEST_F(RecorderListenerTest, record_custom_event) {
283 using recorder_listener_t = RecorderListener<CustomEventWithPayload>;
284
285 static_assert(std::same_as<
287 Kokkos::Impl::type_list<CustomEventWithPayload>
288 >);
289
295
296 ASSERT_THAT(
297 (recorder_listener_t::record([]{
299 .payload = "My taylor is rich.",
301 });
302 })),
303 ::testing::ElementsAre(
304 ACustomEventWithPayload(
305 ::testing::Field(&CustomEventWithPayload::payload, ::testing::StrEq("My taylor is rich.")),
306 ::testing::Field(&CustomEventWithPayload::event_id, ::testing::Eq(0))
307 )
308 )
309 );
310}
311
312} // namespace Kokkos::utils::tests::callbacks
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
Check that Callable is a listener for each event in EventTypes.
Definition Listener.hpp:68
#define DEFINE_EVENT_MATCHER_IN(__namespace__, __eventtype__)
Definition Helpers.hpp:16
auto APopRegionEvent(Matchers &&... matchers)
Definition Helpers.hpp:46
auto AEndParallelReduceEvent(Matchers &&... matchers)
Definition Helpers.hpp:32
auto ABeginParallelForEvent(Matchers &&... matchers)
Definition Helpers.hpp:29
std::ostream & operator<<(std::ostream &out, const EventType &event)
Definition Events.hpp:335
auto get_next_event_id() noexcept
If the Manager is initialized, return Manager::get_next_event_id. Otherwise, return EventTraits::inva...
Definition Manager.hpp:492
typename impl::ListenerEventTypeList< Callable >::type listener_event_type_list_t
Type list holding the event types that Callable can be a listener for.
Definition Listener.hpp:72
auto ABeginParallelForEventWithName(Matcher &&matcher)
Definition Helpers.hpp:59
auto APushRegionEventWithName(Matcher &&matcher)
Definition Helpers.hpp:64
auto AEndFenceEvent(Matchers &&... matchers)
Definition Helpers.hpp:36
Kokkos::Impl::type_list< BeginParallelForEvent, EndParallelForEvent, BeginParallelReduceEvent, EndParallelReduceEvent, BeginParallelScanEvent, EndParallelScanEvent, BeginFenceEvent, EndFenceEvent, AllocateDataEvent, DeallocateDataEvent, BeginDeepCopyEvent, EndDeepCopyEvent, CreateProfileSectionEvent, DestroyProfileSectionEvent, StartProfileSectionEvent, StopProfileSectionEvent, PushRegionEvent, PopRegionEvent, ProfileEvent > EventTypeList
Type list holding all event types.
Definition Events.hpp:214
void dispatch(const EventType &event)
If the Manager is initialized, call Manager::dispatch with event.
Definition Manager.hpp:486
auto ABeginParallelReduceEventWithName(Matcher &&matcher)
Definition Helpers.hpp:60
auto AProfileEventWithName(Matcher &&matcher)
Definition Helpers.hpp:65
auto ABeginFenceEventWithName(Matcher &&matcher)
Definition Helpers.hpp:62
auto ABeginFenceEvent(Matchers &&... matchers)
Definition Helpers.hpp:35
auto ContainsInOrder(Matchers &&... matchers)
Definition Helpers.hpp:180
auto AEndParallelForEvent(Matchers &&... matchers)
Definition Helpers.hpp:30
typename impl::EventTestTypes< Kokkos::utils::callbacks::EventTypeList >::type EventTestTypes
Useful type for defining a typed test suite over all types in Kokkos::utils::callbacks::EventTypeList...
Definition Helpers.hpp:21
TEST_F(EnqueuedEventTimerTest, duration)
TYPED_TEST(TimerTest, start_aborts_for_wrongly_enqueued_event)
TEST_F_WITH_CB_MGR(FenceFinderTest, recorded_events)
RecorderListener< EventInProfileSectionMatcher< EventRegexMatcher >, EventTypeList > event_in_profile_section_recorder_t
Listener to record events that occur in a profile section.
TYPED_TEST_SUITE(TimerTest, TimerTypes)
RecorderListener< EventRegexMatcher, BeginFenceEvent > fence_finder_t
Listener to record fence events.
TEST(EventTest, BeginEvent)
Matcher to select events that occur within a profile section.
Matcher to select events whose name matches a regular expression.
static constexpr event_id_t invalid_event_id
Definition Events.hpp:25
bool operator==(const CustomEventWithPayload &) const =default
bool operator==(const CustomEvent &) const =default
Create a new execution space instance with RAII semantics.
Initializing and finalizing Kokkos::utils::callbacks::Manager in a RAII manner.
Definition Manager.hpp:10
Kokkos::DefaultExecutionSpace execution_space