kokkos-utils 0.0.7
Loading...
Searching...
No Matches
test_Matchers.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
12
23
33
34using execution_space = Kokkos::DefaultExecutionSpace;
35
37{
38
39using namespace Kokkos::utils::callbacks;
40
42using named_event_type_list_t = Kokkos::Impl::type_list<
53>;
54
57{
58 static_assert(Matcher <EventRegexMatcher>);
59 static_assert(MatcherFor<EventRegexMatcher, named_event_type_list_t>);
60 static_assert(std::movable<EventRegexMatcher>);
61
62 static_assert( ! MatcherFor<EventRegexMatcher, BeginDeepCopyEvent>);
63}
64
66TEST(EventRegexMatcher, regex_matcher)
67{
68 const EventRegexMatcher matcher{{std::regex("buried-[a-z]+-to-time")}};
69
70 ASSERT_TRUE( matcher(BeginParallelForEvent{.name = "buried-kernel-to-time", .event_id = 2}));
71 ASSERT_FALSE(matcher(BeginParallelForEvent{.name = "not-this-other-kernel", .event_id = 2}));
72
73 ASSERT_TRUE( matcher(AllocateDataEvent{.alloc = {.name = "buried-allocation-to-time"}}));
74 ASSERT_FALSE(matcher(AllocateDataEvent{.alloc = {.name = "not-this-other-allocation"}}));
75}
76
79{
80 static_assert(Matcher <EventNameMatcher>);
81 static_assert(MatcherFor <EventNameMatcher, named_event_type_list_t>);
82 static_assert(std::movable<EventNameMatcher>);
83}
84
87{
88 const EventNameMatcher matcher{.name = "named-as-I-like-it"};
89
90 ASSERT_FALSE(matcher(AllocateDataEvent{.alloc = {.name = "named-deep-copy"}}));
91 ASSERT_TRUE (matcher(AllocateDataEvent{.alloc = {.name = "named-as-I-like-it"}}));
92}
93
96{
98
99 static_assert(Matcher <matcher_t>);
100 static_assert(MatcherFor <matcher_t, EventTypeList>);
101 static_assert(std::movable<matcher_t>);
102}
103
105TEST(EventInProfileSectionMatcher, in_profile_section_matcher)
106{
107 constexpr uint32_t section_id = 2;
108
109 EventInProfileSectionMatcher<EventRegexMatcher> matcher{{{std::regex("buried-profile-section")}}};
110
111 ASSERT_FALSE(matcher(CreateProfileSectionEvent{.name = "buried-profile-section", .section_id = section_id}));
112
113 ASSERT_FALSE(matcher(AllocateDataEvent{})) << "Expecting not to record the event before starting the section.";
114
115 ASSERT_FALSE(matcher(StartProfileSectionEvent{.section_id = section_id}));
116
117 ASSERT_TRUE(matcher(AllocateDataEvent{})) << "Expecting to record the event inside the section.";
118
119 ASSERT_FALSE(matcher(StopProfileSectionEvent{.section_id = section_id}));
120
121 ASSERT_FALSE(matcher(AllocateDataEvent{})) << "Expecting to record the event after stopping the section.";
122
123 ASSERT_FALSE(matcher(DestroyProfileSectionEvent{.section_id = section_id}));
124}
125
128{
130
131 static_assert(Matcher <matcher_t>);
132 static_assert(MatcherFor <matcher_t, EventTypeList>);
133 static_assert(std::movable<matcher_t>);
134}
135
137TEST(EventRegionMatcher, within_region_matcher)
138{
139 EventRegionMatcher<EventRegexMatcher> matcher{{{std::regex("buried-region")}}};
140
141 ASSERT_FALSE(matcher(PushRegionEvent{.name = "not-the-one-we-want"}));
142
143 ASSERT_FALSE(matcher(AllocateDataEvent{}));
144
145 ASSERT_FALSE(matcher(PushRegionEvent{.name = "buried-region"}));
146
147 ASSERT_TRUE(matcher(AllocateDataEvent{}));
148
149 ASSERT_TRUE(matcher(PushRegionEvent{.name = "nested-region"}));
150
151 ASSERT_TRUE(matcher(AllocateDataEvent{}));
152
153 ASSERT_TRUE(matcher(PopRegionEvent{}));
154
155 ASSERT_FALSE(matcher(PopRegionEvent{}));
156}
157
159TEST(EventRegionMatcher, boundary_region_matcher)
160{
161 EventRegionMatcher<EventNameMatcher, true> matcher{{"buried-region"}};
162
163 ASSERT_FALSE(matcher(PushRegionEvent{.name = "not-the-one-we-want"}));
164
165 ASSERT_FALSE(matcher(AllocateDataEvent{}));
166
167 ASSERT_TRUE(matcher(PushRegionEvent{.name = "buried-region"}));
168
169 ASSERT_FALSE(matcher(AllocateDataEvent{}));
170
171 ASSERT_FALSE(matcher(PushRegionEvent{.name = "nested-region"}));
172
173 ASSERT_FALSE(matcher(AllocateDataEvent{}));
174
175 ASSERT_FALSE(matcher(PopRegionEvent{}));
176
177 ASSERT_TRUE(matcher(PopRegionEvent{}));
178
179 ASSERT_FALSE(matcher(BeginFenceEvent{}));
180}
181
184{
185 static_assert(Matcher <AnyEventMatcher>);
186 static_assert(MatcherFor <AnyEventMatcher, EventTypeList>);
187 static_assert(std::movable<AnyEventMatcher>);
188}
189
191TEST(AnyEventMatcher, operator_parentheses)
192{
193 AnyEventMatcher matcher;
194
195 Kokkos::utils::impl::for_each<EventTypeList>([&] <Event EventType>() {
196 static_assert(matcher(EventType{}));
197 });
198}
199
202{
204
205 static_assert(Matcher <matcher_t>);
206 static_assert(MatcherFor <matcher_t, BeginFenceEvent, PushRegionEvent>);
207 static_assert(std::same_as<matcher_event_type_list_t<matcher_t>, Kokkos::Impl::type_list<BeginFenceEvent, PushRegionEvent>>);
208 static_assert(std::movable<matcher_t>);
209}
210
212TEST(EventTypeMatcher, operator_parentheses)
213{
215
216 ASSERT_TRUE(matcher(PushRegionEvent{}));
217 ASSERT_TRUE(matcher(BeginFenceEvent{}));
218}
219
222{
224
225 static_assert(Matcher <matcher_t>);
226 static_assert(MatcherFor <matcher_t, BeginParallelForEvent, EndParallelForEvent>);
227 static_assert(std::same_as<matcher_event_type_list_t<matcher_t>, Kokkos::Impl::type_list<BeginParallelForEvent, EndParallelForEvent>>);
228 static_assert(std::movable<matcher_t>);
229}
230
232TEST(EventBeginEndIdMatcher, operator_parentheses)
233{
235
236 ASSERT_FALSE(matcher(BeginParallelForEvent {.name = "does-not-match"}));
237 ASSERT_FALSE(matcher(EndParallelForEvent {.event_id = 42}));
238 ASSERT_TRUE (matcher(BeginParallelForEvent {.name = "example-pfor", .event_id = 666}));
239 ASSERT_FALSE(matcher(EndParallelForEvent {.event_id = 42}));
240 ASSERT_TRUE (matcher(EndParallelForEvent {.event_id = 666}));
241}
242
245{
246 using matcher_t = EventQueueMatcher<execution_space>;
247
248 using enqueued_event_type_list_t = Kokkos::Impl::type_list<
254 >;
255
256 static_assert(Matcher <matcher_t>);
257 static_assert(std::same_as<matcher_event_type_list_t<matcher_t>, enqueued_event_type_list_t>);
258 static_assert(std::movable<matcher_t>);
259}
260
262TEST(EventQueueMatcher, operator_parentheses)
263{
264 const auto execs = Kokkos::Experimental::partition_space(execution_space{}, 1, 1);
265
266 const auto dev_id_0 = Kokkos::Tools::Experimental::device_id(execs.at(0));
267 const auto dev_id_1 = Kokkos::Tools::Experimental::device_id(execs.at(1));
268
269 const EventQueueMatcher<execution_space> matcher {.exec = execs.at(0)};
270
271 ASSERT_EQ (matcher(BeginParallelForEvent{.name = "", .dev_id = dev_id_1}), dev_id_0 == dev_id_1);
272 ASSERT_TRUE(matcher(BeginParallelForEvent{.name = "", .dev_id = dev_id_0}));
273}
274
277{
278 using matcher_t = EventIdMatcher;
279
280 using indexed_event_type_list_t = Kokkos::Impl::type_list<
286 >;
287
288 static_assert(Matcher <matcher_t>);
289 static_assert(std::same_as<matcher_event_type_list_t<matcher_t>, indexed_event_type_list_t>);
290 static_assert(std::movable<matcher_t>);
291}
292
294TEST(EventIdMatcher, operator_parentheses)
295{
296 const EventIdMatcher matcher{.event_id = 42};
297
298 ASSERT_TRUE (matcher(BeginParallelForEvent{.event_id = 42}));
299 ASSERT_FALSE(matcher(BeginParallelForEvent{.event_id = 666}));
300}
301
304{
305 using matcher_t = ConjunctionMatcher<
308 >;
309
310 static_assert(Matcher <matcher_t>);
311 static_assert(MatcherFor <matcher_t, EventTypeList>);
312 static_assert(std::movable<matcher_t>);
313}
314
316TEST(ConjunctionMatcher, everyone_agrees)
317{
318 ConjunctionMatcher matcher{
319 EventRegionMatcher<EventRegexMatcher>{{{std::regex("buried-region-one")}}},
320 EventRegionMatcher<EventRegexMatcher>{{{std::regex("buried-region-two")}}}
321 };
322
323 ASSERT_FALSE(matcher(PushRegionEvent{.name = "not-the-one-we-want"}));
324
325 ASSERT_FALSE(matcher(AllocateDataEvent{}));
326
327 ASSERT_FALSE(matcher(PushRegionEvent{.name = "buried-region-one"}));
328
329 ASSERT_FALSE(matcher(AllocateDataEvent{}));
330
331 ASSERT_FALSE(matcher(PushRegionEvent{.name = "buried-region-two"}));
332
333 ASSERT_TRUE(matcher(AllocateDataEvent{}));
334
335 ASSERT_FALSE(matcher(PopRegionEvent{}));
336}
337
342TEST(ConjunctionMatcher, different_event_type_sets)
343{
344 ConjunctionMatcher matcher{
345 EventRegexMatcher{{std::regex("my-triggering-event")}},
347 };
348
349 static_assert(std::same_as<
351 matcher_event_type_list_t<decltype(matcher)>
352 >);
353
354 ASSERT_FALSE(matcher(BeginFenceEvent{.name = "not-this-one"}));
355 ASSERT_TRUE (matcher(BeginFenceEvent{.name = "my-triggering-event"}));
356}
357
359TEST(ConjunctionMatcher, default_constructed)
360{
362
363 static_assert(std::default_initializable<matcher_t>);
364}
365
367template <Matcher MatcherType>
369{
370 bool operator()(const BeginFenceEvent& event)
371 {
372 encountered.push_back(event);
373 return matcher(event);
374 }
375
376 MatcherType matcher;
377 std::vector<BeginFenceEvent> encountered {};
378};
379
384TEST(ConjunctionMatcher, order_of_evaluation)
385{
386 using matcher_t = RecordingMatcher<EventNameMatcher>;
387
388 ConjunctionMatcher matcher{
389 matcher_t{{"matcher-a"}},
390 matcher_t{{"matcher-a"}},
391 matcher_t{{"matcher-b"}},
392 matcher_t{{"matcher-c"}}
393 };
394
395 ASSERT_FALSE(matcher(BeginFenceEvent{.name = "matcher-a", .event_id = 0}));
396 ASSERT_FALSE(matcher(BeginFenceEvent{.name = "matcher-a", .event_id = 1}));
397 ASSERT_FALSE(matcher(BeginFenceEvent{.name = "matcher-b", .event_id = 2}));
398 ASSERT_FALSE(matcher(BeginFenceEvent{.name = "matcher-c", .event_id = 3}));
399
401 ASSERT_THAT(std::get<0>(matcher.matchers).encountered, ::testing::ElementsAre(
402 BeginFenceEvent{.name = "matcher-a", .event_id = 0},
403 BeginFenceEvent{.name = "matcher-a", .event_id = 1},
404 BeginFenceEvent{.name = "matcher-b", .event_id = 2},
405 BeginFenceEvent{.name = "matcher-c", .event_id = 3}
406 ));
407
409 ASSERT_THAT(std::get<1>(matcher.matchers).encountered, ::testing::ElementsAre(
410 BeginFenceEvent{.name = "matcher-a", .event_id = 0},
411 BeginFenceEvent{.name = "matcher-a", .event_id = 1}
412 ));
413
415 ASSERT_THAT(std::get<2>(matcher.matchers).encountered, ::testing::ElementsAre(
416 BeginFenceEvent{.name = "matcher-a", .event_id = 0},
417 BeginFenceEvent{.name = "matcher-a", .event_id = 1}
418 ));
419
421 ASSERT_THAT(std::get<3>(matcher.matchers).encountered, ::testing::IsEmpty());
422}
423
424} // namespace Kokkos::utils::tests::callbacks
Kokkos::Impl::filter_type_list_t< impl::IsMatcherFor< Callable >::template type, EventTypeList > matcher_event_type_list_t
Type list holding the event types that Callable can be a matcher for.
Definition Matcher.hpp:39
constexpr void for_each(Callable callable)
Calls the instantiation of the call operator of a callable object for each type in a Kokkos::Impl::ty...
Kokkos::Impl::type_list< BeginParallelForEvent, BeginParallelReduceEvent, BeginParallelScanEvent, BeginSingleEvent, BeginFenceEvent, AllocateDataEvent, DeallocateDataEvent, CreateProfileSectionEvent, PushRegionEvent, ProfileEvent > named_event_type_list_t
List of event types that have a name.
TEST(EventTest, BeginEvent)
Allocate-data event associated with Kokkos::Tools::Experimental::EventSet::allocate_data.
Definition Events.hpp:146
Matcher that returns true for any event.
Definition Matcher.hpp:43
Begin-fence event associated with Kokkos::Tools::Experimental::EventSet::begin_fence.
Definition Events.hpp:117
Begin-parallel-reduce event associated with Kokkos::Tools::Experimental::EventSet::begin_parallel_red...
Definition Events.hpp:63
Begin-parallel-scan event associated with Kokkos::Tools::Experimental::EventSet::begin_parallel_scan.
Definition Events.hpp:81
Begin-single event associated with Kokkos::Tools::Experimental::EventSet::begin_single.
Definition Events.hpp:99
Conjunction of matchers that is true only if all matchers agree.
Create-profile-section event associated with Kokkos::Tools::Experimental::EventSet::create_profile_se...
Definition Events.hpp:177
Deallocate-data event associated with Kokkos::Tools::Experimental::EventSet::deallocate_data.
Definition Events.hpp:154
Destroy-profile-section event associated with Kokkos::Tools::Experimental::EventSet::destroy_profile_...
Definition Events.hpp:202
End-fence event associated with Kokkos::Tools::Experimental::EventSet::end_fence.
Definition Events.hpp:127
End-parallel-for event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_for.
Definition Events.hpp:55
End-parallel-reduce event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_reduce.
Definition Events.hpp:73
End-parallel-scan event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_scan.
Definition Events.hpp:91
End-single event associated with Kokkos::Tools::Experimental::EventSet::end_single.
Definition Events.hpp:109
Match a begin event and its corresponding end event based on their event_id.
Match an event whose event_id is event_id.
Matcher to select events that occur within a profile section.
Matcher to select events whose name matches name.
Match an event whose dev_id is the same as the one of exec.
Matcher to select events whose name matches a regular expression.
Matcher to select events that occur within a region.
Restrict the event types that can be matched by matcher to the subset EventTypes.
Pop-region event associated with Kokkos::Tools::Experimental::EventSet::pop_region.
Definition Events.hpp:218
Profile event associated with Kokkos::Tools::Experimental::EventSet::profile_event.
Definition Events.hpp:224
Push-region event associated with Kokkos::Tools::Experimental::EventSet::push_region.
Definition Events.hpp:210
Start-profile-section event associated with Kokkos::Tools::Experimental::EventSet::start_profile_sect...
Definition Events.hpp:186
Stop-profile-section event associated with Kokkos::Tools::Experimental::EventSet::stop_profile_sectio...
Definition Events.hpp:194
Helper matcher that records in encountered all the events passed to it.
bool operator()(const BeginFenceEvent &event)
Kokkos::DefaultExecutionSpace execution_space