kokkos-utils 0.0.4
 
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_Core.hpp"
5
13
16
25
26using execution_space = Kokkos::DefaultExecutionSpace;
27
29{
30
31using namespace Kokkos::utils::callbacks;
32
35
36class RecorderListenerTest : public ::testing::Test,
38 public scoped::ExecutionSpace<execution_space>
39{};
40
43{
45 static_assert(std::same_as<
48 >);
49
50 static_assert(Listener <event_in_profile_section_recorder_t>);
51 static_assert(ListenerFor<event_in_profile_section_recorder_t, EventTypeList>);
52 static_assert(ListenerFor<event_in_profile_section_recorder_t, PushRegionEvent, PopRegionEvent>);
53}
54
55template <Event EvenType>
56class RecorderListenerSingleEventTypeTest : public ::testing::Test,
58
60
63{
64 using recorder_listener_t = RecorderListener<TypeParam>;
65 static_assert(std::same_as<typename recorder_listener_t::event_type_list_t, Kokkos::Impl::type_list<TypeParam>>);
66 static_assert(std::same_as<typename recorder_listener_t::matcher_t, AnyEventMatcher>);
67
68 const auto recorder = std::make_shared<recorder_listener_t>();
69
71 Manager::unregister_listener(recorder.get());
72
73 ASSERT_EQ(recorder->recorded_events.size(), 0);
74}
75
78{
79 EventInProfileSectionMatcher matcher{.matcher = EventRegexMatcher{.regex = std::regex("profile section")}};
80 const auto recorder = std::make_shared<event_in_profile_section_recorder_t>(std::move(matcher));
81
82 using recorder_t = RecorderListener<EventTypeList>;
83
84 const auto any_event_recorder = std::make_shared<recorder_t>();
85
88
90
92 ASSERT_THAT(
93 recorder->recorded_events,
94 ::testing::Contains(ABeginParallelForEventWithName(::testing::StrEq("computation - level 0 - pfor")))
95 );
96
97 ASSERT_THAT(
98 recorder->recorded_events,
100 APushRegionEventWithName(::testing::StrEq("computation - level 0")),
101 ABeginParallelForEventWithName(::testing::StrEq("computation - level 0 - pfor")),
103 ABeginFenceEventWithName(::testing::StrEq("computation - level 0 - fence after pfor")),
105 AProfileEventWithName(::testing::StrEq("buried marker")),
106 APushRegionEventWithName(::testing::StrEq("computation - level 1")),
107 ABeginParallelReduceEventWithName(::testing::StrEq("computation - level 1 - preduce on default exec")),
109 ABeginFenceEventWithName(::testing::StrEq("Kokkos::parallel_reduce: fence due to result being value, not view")),
113 )
114 );
115
116 ASSERT_GT(any_event_recorder->recorded_events.size(), recorder->recorded_events.size());
117
118 const auto fence_event_outside_profile_section = ABeginFenceEventWithName(::testing::StrEq("other fence after stopping the profile section"));
119
120 ASSERT_THAT(
121 recorder->recorded_events,
122 ::testing::Not(::testing::Contains(fence_event_outside_profile_section))
123 );
124
125 ASSERT_THAT(
126 any_event_recorder->recorded_events,
127 ::testing::Contains(fence_event_outside_profile_section)
128 );
129
130 const auto output = [&recorder] () -> std::string {
131 std::ostringstream oss;
132 recorder->report(oss);
133 return oss.str();
134 }();
135
136 ASSERT_THAT(output, ::testing::HasSubstr("computation - level 0 - pfor"));
137 ASSERT_THAT(output, ::testing::HasSubstr("computation - level 0 - fence after pfor"));
138
141}
142
145{
146 ASSERT_THAT(
148 ::testing::Contains(
150 ::testing::Field(&BeginParallelForEvent::name, ::testing::StrEq("computation - level 0 - pfor")),
151 ::testing::Field(&BeginParallelForEvent::dev_id, ::testing::Eq(Kokkos::Tools::Experimental::device_id(this->exec)))
152 )
153 )
154 );
155
157 ASSERT_THAT(
159 ::testing::Contains(
161 ::testing::FieldsAre(::testing::StrEq("computation - level 0 - pfor"), ::testing::Eq(Kokkos::Tools::Experimental::device_id(this->exec)), ::testing::_)
162 )
163 )
164 );
165}
166
169
171TEST(FenceFinder, traits)
172{
174 static_assert(std::same_as<
176 Kokkos::Impl::type_list<BeginFenceEvent>
177 >);
178
179 static_assert(Listener<fence_finder_t>);
180}
181
183TEST_F_WITH_CB_MGR(FenceFinderTest, recorded_events)
184{
185 const auto exec = Kokkos::Experimental::partition_space(execution_space{}, 1)[0];
186
187 const auto fence_finder = std::make_shared<fence_finder_t>(EventRegexMatcher(std::regex("computation - level 0 - fence after pfor")));
188
190
192
194 ASSERT_THAT(
195 fence_finder->recorded_events,
196 ::testing::Contains(
198 ::testing::Field(&BeginFenceEvent::name, ::testing::StrEq("computation - level 0 - fence after pfor")),
199 ::testing::Field(&BeginFenceEvent::dev_id, ::testing::Eq(Kokkos::Tools::Experimental::device_id(exec)))
200 )
201 )
202 );
203
205}
206
212
213inline std::ostream& operator<<(std::ostream& out, const CustomEvent& event) {
214 return out << "CustomEvent: " << "{event_id = " << event.event_id << "}";
215}
216
223
224inline std::ostream& operator<<(std::ostream& out, const CustomEventWithPayload& event) {
225 return out << "CustomEventWithPayload: " << "{payload = " << event.payload << ", event_id = " << event.event_id << "}";
226}
227
230
233
234
235TEST_F(RecorderListenerTest, record_mix_kokkos_and_custom_events) {
237
238 static_assert(std::same_as<
240 Kokkos::Impl::type_list<BeginParallelForEvent, CustomEvent, CustomEventWithPayload>
241 >);
242
247
248 ASSERT_THAT(
249 (recorder_listener_t::record([&exec = this->exec]{
252 .payload = "My taylor is rich.",
254 });
257 });
258 })),
261 ::testing::Field(&BeginParallelForEvent::name, ::testing::StrEq("computation - level 0 - pfor")),
262 ::testing::Field(&BeginParallelForEvent::dev_id, ::testing::Eq(Kokkos::Tools::Experimental::device_id(this->exec))),
263 ::testing::Field(&BeginParallelForEvent::event_id, ::testing::Eq(0))
264 ),
265 ACustomEventWithPayload(
266 ::testing::Field(&CustomEventWithPayload::payload, ::testing::StrEq("My taylor is rich.")),
267 ::testing::Field(&CustomEventWithPayload::event_id, ::testing::Eq(1))
268 ),
269 ACustomEvent(
270 ::testing::Field(&CustomEvent::event_id, ::testing::Eq(2))
271 )
272 )
273 );
274}
275
277TEST_F(RecorderListenerTest, record_custom_event) {
278 using recorder_listener_t = RecorderListener<CustomEventWithPayload>;
279
280 static_assert(std::same_as<
282 Kokkos::Impl::type_list<CustomEventWithPayload>
283 >);
284
290
291 ASSERT_THAT(
292 (recorder_listener_t::record([]{
294 .payload = "My taylor is rich.",
296 });
297 })),
298 ::testing::ElementsAre(
299 ACustomEventWithPayload(
300 ::testing::Field(&CustomEventWithPayload::payload, ::testing::StrEq("My taylor is rich.")),
301 ::testing::Field(&CustomEventWithPayload::event_id, ::testing::Eq(0))
302 )
303 )
304 );
305}
306
307} // 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
#define TEST_F_WITH_CB_MGR(__test_fixture_name__, __test_name__)
Definition Helpers.hpp:11
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:327
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
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
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:206
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:176
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)
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:17
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