kokkos-utils 0.0.1
 
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_Core.hpp"
5
7
18
28
29using execution_space = Kokkos::DefaultExecutionSpace;
30
32{
33
34using namespace Kokkos::utils::callbacks;
35
37using named_event_type_list_t = Kokkos::Impl::type_list<
47>;
48
51{
52 static_assert(Matcher <EventRegexMatcher>);
53 static_assert(MatcherFor<EventRegexMatcher, named_event_type_list_t>);
54 static_assert(std::movable<EventRegexMatcher>);
55
56 static_assert( ! MatcherFor<EventRegexMatcher, BeginDeepCopyEvent>);
57}
58
60TEST(EventRegexMatcher, regex_matcher)
61{
62 const EventRegexMatcher matcher{{std::regex("buried-[a-z]+-to-time")}};
63
64 ASSERT_TRUE( matcher(BeginParallelForEvent{.name = "buried-kernel-to-time", .event_id = 2}));
65 ASSERT_FALSE(matcher(BeginParallelForEvent{.name = "not-this-other-kernel", .event_id = 2}));
66
67 ASSERT_TRUE( matcher(AllocateDataEvent{.alloc = {.name = "buried-allocation-to-time"}}));
68 ASSERT_FALSE(matcher(AllocateDataEvent{.alloc = {.name = "not-this-other-allocation"}}));
69}
70
73{
74 static_assert(Matcher <EventNameMatcher>);
75 static_assert(MatcherFor <EventNameMatcher, named_event_type_list_t>);
76 static_assert(std::movable<EventNameMatcher>);
77}
78
81{
82 const EventNameMatcher matcher{.name = "named-as-I-like-it"};
83
84 ASSERT_FALSE(matcher(AllocateDataEvent{.alloc = {.name = "named-deep-copy"}}));
85 ASSERT_TRUE (matcher(AllocateDataEvent{.alloc = {.name = "named-as-I-like-it"}}));
86}
87
90{
92
93 static_assert(Matcher <matcher_t>);
94 static_assert(MatcherFor <matcher_t, EventTypeList>);
95 static_assert(std::movable<matcher_t>);
96}
97
99TEST(EventInProfileSectionMatcher, in_profile_section_matcher)
100{
101 constexpr uint32_t section_id = 2;
102
103 EventInProfileSectionMatcher<EventRegexMatcher> matcher{{{std::regex("buried-profile-section")}}};
104
105 ASSERT_FALSE(matcher(CreateProfileSectionEvent{.name = "buried-profile-section", .section_id = section_id}));
106
107 ASSERT_FALSE(matcher(AllocateDataEvent{})) << "Expecting not to record the event before starting the section.";
108
109 ASSERT_FALSE(matcher(StartProfileSectionEvent{.section_id = section_id}));
110
111 ASSERT_TRUE(matcher(AllocateDataEvent{})) << "Expecting to record the event inside the section.";
112
113 ASSERT_FALSE(matcher(StopProfileSectionEvent{.section_id = section_id}));
114
115 ASSERT_FALSE(matcher(AllocateDataEvent{})) << "Expecting to record the event after stopping the section.";
116
117 ASSERT_FALSE(matcher(DestroyProfileSectionEvent{.section_id = section_id}));
118}
119
122{
124
125 static_assert(Matcher <matcher_t>);
126 static_assert(MatcherFor <matcher_t, EventTypeList>);
127 static_assert(std::movable<matcher_t>);
128}
129
131TEST(EventRegionMatcher, within_region_matcher)
132{
133 EventRegionMatcher<EventRegexMatcher> matcher{{{std::regex("buried-region")}}};
134
135 ASSERT_FALSE(matcher(PushRegionEvent{.name = "not-the-one-we-want"}));
136
137 ASSERT_FALSE(matcher(AllocateDataEvent{}));
138
139 ASSERT_FALSE(matcher(PushRegionEvent{.name = "buried-region"}));
140
141 ASSERT_TRUE(matcher(AllocateDataEvent{}));
142
143 ASSERT_TRUE(matcher(PushRegionEvent{.name = "nested-region"}));
144
145 ASSERT_TRUE(matcher(AllocateDataEvent{}));
146
147 ASSERT_TRUE(matcher(PopRegionEvent{}));
148
149 ASSERT_FALSE(matcher(PopRegionEvent{}));
150}
151
153TEST(EventRegionMatcher, boundary_region_matcher)
154{
155 EventRegionMatcher<EventNameMatcher, true> matcher{{"buried-region"}};
156
157 ASSERT_FALSE(matcher(PushRegionEvent{.name = "not-the-one-we-want"}));
158
159 ASSERT_FALSE(matcher(AllocateDataEvent{}));
160
161 ASSERT_TRUE(matcher(PushRegionEvent{.name = "buried-region"}));
162
163 ASSERT_FALSE(matcher(AllocateDataEvent{}));
164
165 ASSERT_FALSE(matcher(PushRegionEvent{.name = "nested-region"}));
166
167 ASSERT_FALSE(matcher(AllocateDataEvent{}));
168
169 ASSERT_FALSE(matcher(PopRegionEvent{}));
170
171 ASSERT_TRUE(matcher(PopRegionEvent{}));
172
173 ASSERT_FALSE(matcher(BeginFenceEvent{}));
174}
175
178{
179 static_assert(Matcher <AnyEventMatcher>);
180 static_assert(MatcherFor <AnyEventMatcher, EventTypeList>);
181 static_assert(std::movable<AnyEventMatcher>);
182}
183
185TEST(AnyEventMatcher, operator_parentheses)
186{
187 AnyEventMatcher matcher;
188
189 Kokkos::utils::impl::for_each<EventTypeList>([&] <Event EventType>() {
190 static_assert(matcher(EventType{}));
191 });
192}
193
196{
198
199 static_assert(Matcher <matcher_t>);
200 static_assert(MatcherFor <matcher_t, BeginFenceEvent, PushRegionEvent>);
201 static_assert(std::same_as<matcher_event_type_list_t<matcher_t>, Kokkos::Impl::type_list<BeginFenceEvent, PushRegionEvent>>);
202 static_assert(std::movable<matcher_t>);
203}
204
206TEST(EventTypeMatcher, operator_parentheses)
207{
209
210 ASSERT_TRUE(matcher(PushRegionEvent{}));
211 ASSERT_TRUE(matcher(BeginFenceEvent{}));
212}
213
216{
218
219 static_assert(Matcher <matcher_t>);
220 static_assert(MatcherFor <matcher_t, BeginParallelForEvent, EndParallelForEvent>);
221 static_assert(std::same_as<matcher_event_type_list_t<matcher_t>, Kokkos::Impl::type_list<BeginParallelForEvent, EndParallelForEvent>>);
222 static_assert(std::movable<matcher_t>);
223}
224
226TEST(EventBeginEndIdMatcher, operator_parentheses)
227{
229
230 ASSERT_FALSE(matcher(BeginParallelForEvent {.name = "does-not-match"}));
231 ASSERT_FALSE(matcher(EndParallelForEvent {.event_id = 42}));
232 ASSERT_TRUE (matcher(BeginParallelForEvent {.name = "example-pfor", .event_id = 666}));
233 ASSERT_FALSE(matcher(EndParallelForEvent {.event_id = 42}));
234 ASSERT_TRUE (matcher(EndParallelForEvent {.event_id = 666}));
235}
236
239{
240 using matcher_t = EventQueueMatcher<execution_space>;
241
242 using enqueued_event_type_list_t = Kokkos::Impl::type_list<
247 >;
248
249 static_assert(Matcher <matcher_t>);
250 static_assert(std::same_as<matcher_event_type_list_t<matcher_t>, enqueued_event_type_list_t>);
251 static_assert(std::movable<matcher_t>);
252}
253
255TEST(EventQueueMatcher, operator_parentheses)
256{
257 const auto execs = Kokkos::Experimental::partition_space(execution_space{}, 1, 1);
258
259 const auto dev_id_0 = Kokkos::Tools::Experimental::device_id(execs.at(0));
260 const auto dev_id_1 = Kokkos::Tools::Experimental::device_id(execs.at(1));
261
262 const EventQueueMatcher<execution_space> matcher {.exec = execs.at(0)};
263
264 ASSERT_EQ (matcher(BeginParallelForEvent{.name = "", .dev_id = dev_id_1}), dev_id_0 == dev_id_1);
265 ASSERT_TRUE(matcher(BeginParallelForEvent{.name = "", .dev_id = dev_id_0}));
266}
267
270{
271 using matcher_t = EventIdMatcher;
272
273 using indexed_event_type_list_t = Kokkos::Impl::type_list<
278 >;
279
280 static_assert(Matcher <matcher_t>);
281 static_assert(std::same_as<matcher_event_type_list_t<matcher_t>, indexed_event_type_list_t>);
282 static_assert(std::movable<matcher_t>);
283}
284
286TEST(EventIdMatcher, operator_parentheses)
287{
288 const EventIdMatcher matcher{.event_id = 42};
289
290 ASSERT_TRUE (matcher(BeginParallelForEvent{.event_id = 42}));
291 ASSERT_FALSE(matcher(BeginParallelForEvent{.event_id = 666}));
292}
293
296{
297 using matcher_t = ConjunctionMatcher<
300 >;
301
302 static_assert(Matcher <matcher_t>);
303 static_assert(MatcherFor <matcher_t, EventTypeList>);
304 static_assert(std::movable<matcher_t>);
305}
306
308TEST(ConjunctionMatcher, everyone_agrees)
309{
310 ConjunctionMatcher matcher{
311 EventRegionMatcher<EventRegexMatcher>{{{std::regex("buried-region-one")}}},
312 EventRegionMatcher<EventRegexMatcher>{{{std::regex("buried-region-two")}}}
313 };
314
315 ASSERT_FALSE(matcher(PushRegionEvent{.name = "not-the-one-we-want"}));
316
317 ASSERT_FALSE(matcher(AllocateDataEvent{}));
318
319 ASSERT_FALSE(matcher(PushRegionEvent{.name = "buried-region-one"}));
320
321 ASSERT_FALSE(matcher(AllocateDataEvent{}));
322
323 ASSERT_FALSE(matcher(PushRegionEvent{.name = "buried-region-two"}));
324
325 ASSERT_TRUE(matcher(AllocateDataEvent{}));
326
327 ASSERT_FALSE(matcher(PopRegionEvent{}));
328}
329
334TEST(ConjunctionMatcher, different_event_type_sets)
335{
336 ConjunctionMatcher matcher{
337 EventRegexMatcher{{std::regex("my-triggering-event")}},
339 };
340
341 static_assert(std::same_as<
343 matcher_event_type_list_t<decltype(matcher)>
344 >);
345
346 ASSERT_FALSE(matcher(BeginFenceEvent{.name = "not-this-one"}));
347 ASSERT_TRUE (matcher(BeginFenceEvent{.name = "my-triggering-event"}));
348}
349
351template <Matcher MatcherType>
353{
354 bool operator()(const BeginFenceEvent& event)
355 {
356 encountered.push_back(event);
357 return matcher(event);
358 }
359
360 MatcherType matcher;
361 std::vector<BeginFenceEvent> encountered {};
362};
363
368TEST(ConjunctionMatcher, order_of_evaluation)
369{
370 using matcher_t = RecordingMatcher<EventNameMatcher>;
371
372 ConjunctionMatcher matcher{
373 matcher_t{{"matcher-a"}},
374 matcher_t{{"matcher-a"}},
375 matcher_t{{"matcher-b"}},
376 matcher_t{{"matcher-c"}}
377 };
378
379 ASSERT_FALSE(matcher(BeginFenceEvent{.name = "matcher-a", .event_id = 0}));
380 ASSERT_FALSE(matcher(BeginFenceEvent{.name = "matcher-a", .event_id = 1}));
381 ASSERT_FALSE(matcher(BeginFenceEvent{.name = "matcher-b", .event_id = 2}));
382 ASSERT_FALSE(matcher(BeginFenceEvent{.name = "matcher-c", .event_id = 3}));
383
385 ASSERT_THAT(std::get<0>(matcher.matchers).encountered, ::testing::ElementsAre(
386 BeginFenceEvent{.name = "matcher-a", .event_id = 0},
387 BeginFenceEvent{.name = "matcher-a", .event_id = 1},
388 BeginFenceEvent{.name = "matcher-b", .event_id = 2},
389 BeginFenceEvent{.name = "matcher-c", .event_id = 3}
390 ));
391
393 ASSERT_THAT(std::get<1>(matcher.matchers).encountered, ::testing::ElementsAre(
394 BeginFenceEvent{.name = "matcher-a", .event_id = 0},
395 BeginFenceEvent{.name = "matcher-a", .event_id = 1}
396 ));
397
399 ASSERT_THAT(std::get<2>(matcher.matchers).encountered, ::testing::ElementsAre(
400 BeginFenceEvent{.name = "matcher-a", .event_id = 0},
401 BeginFenceEvent{.name = "matcher-a", .event_id = 1}
402 ));
403
405 ASSERT_THAT(std::get<3>(matcher.matchers).encountered, ::testing::IsEmpty());
406}
407
408} // 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...
TEST(EventTest, BeginEvent)
Kokkos::Impl::type_list< BeginParallelForEvent, BeginParallelReduceEvent, BeginParallelScanEvent, BeginFenceEvent, AllocateDataEvent, DeallocateDataEvent, CreateProfileSectionEvent, PushRegionEvent, ProfileEvent > named_event_type_list_t
List of event types that have a name.
Allocate-data event associated with Kokkos::Tools::Experimental::EventSet::allocate_data.
Definition Events.hpp:113
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:84
Begin-parallel-reduce event associated with Kokkos::Tools::Experimental::EventSet::begin_parallel_red...
Definition Events.hpp:48
Begin-parallel-scan event associated with Kokkos::Tools::Experimental::EventSet::begin_parallel_scan.
Definition Events.hpp:66
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:144
Deallocate-data event associated with Kokkos::Tools::Experimental::EventSet::deallocate_data.
Definition Events.hpp:121
Destroy-profile-section event associated with Kokkos::Tools::Experimental::EventSet::destroy_profile_...
Definition Events.hpp:169
End-fence event associated with Kokkos::Tools::Experimental::EventSet::end_fence.
Definition Events.hpp:94
End-parallel-for event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_for.
Definition Events.hpp:40
End-parallel-reduce event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_reduce.
Definition Events.hpp:58
End-parallel-scan event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_scan.
Definition Events.hpp:76
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:185
Profile event associated with Kokkos::Tools::Experimental::EventSet::profile_event.
Definition Events.hpp:191
Push-region event associated with Kokkos::Tools::Experimental::EventSet::push_region.
Definition Events.hpp:177
Start-profile-section event associated with Kokkos::Tools::Experimental::EventSet::start_profile_sect...
Definition Events.hpp:153
Stop-profile-section event associated with Kokkos::Tools::Experimental::EventSet::stop_profile_sectio...
Definition Events.hpp:161
Helper matcher that records in encountered all the events passed to it.
bool operator()(const BeginFenceEvent &event)
Kokkos::DefaultExecutionSpace execution_space