kokkos-execution 0.0.1
Loading...
Searching...
No Matches
test_event.cpp
Go to the documentation of this file.
1#include "gtest/gtest.h"
2
5
7
11
22
23#if !defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
24# error "This is not supported."
25#endif
26
27namespace Tests::Impl {
28
29using namespace Kokkos::utils::callbacks;
30
33 public:
41 template <Kokkos::ExecutionSpace Exec>
42 static constexpr bool has_support = []() {
43#if defined(KOKKOS_ENABLE_CUDA)
44 if constexpr (std::same_as<Exec, Kokkos::Cuda>)
45 return true;
46#endif
47#if defined(KOKKOS_ENABLE_HIP)
48 if constexpr (std::same_as<Exec, Kokkos::HIP>)
49 return true;
50#endif
51#if defined(KOKKOS_ENABLE_HPX)
52 if constexpr (std::same_as<Exec, Kokkos::Experimental::HPX>)
53 return true;
54#endif
55#if defined(KOKKOS_ENABLE_SYCL)
56 if constexpr (std::same_as<Exec, Kokkos::SYCL>)
57 return true;
58#endif
59 return false;
60 }();
61};
62
74
76template <Kokkos::ExecutionSpace Exec>
77consteval bool test_models_event() {
78 if constexpr (EventTest::has_support<Exec>) {
80 }
81 return true;
82}
84
86template <Kokkos::ExecutionSpace Exec>
91
92#define KOKKOS_EXECUTION_TESTS_IMPL_EVENT(_fixture_, _name_, _statement_) \
93 TEST_F(_fixture_, _name_) { \
94 if constexpr (EventTest::has_support<TEST_EXECUTION_SPACE>) { \
95 test_event_##_name_ _statement_; \
96 } else { \
97 GTEST_SKIP() << Kokkos::Impl::TypeInfo<TEST_EXECUTION_SPACE>::name() << " does not support events."; \
98 } \
99 }
100
102template <Kokkos::ExecutionSpace Exec>
103void test_event_record_and_wait(const Exec& exec) {
104 const auto recorded_events = EventTest::recorder_listener_t::record([&exec]() {
106 event.record(exec);
107 event.wait();
108 });
109
110 ASSERT_EQ(recorded_events.size(), 2);
111 ASSERT_THAT(recorded_events.at(0), MATCHER_FOR_RECORD_EVENT(exec));
112 ASSERT_THAT(recorded_events.at(1), MATCHER_FOR_WAIT_EVENT(recorded_events.at(0)));
113}
114
116
117
118template <Kokkos::ExecutionSpace Exec>
119void test_event_record_but_dont_wait(const Exec& exec) {
120 const auto recorded_events = EventTest::recorder_listener_t::record([&exec]() {
122 Kokkos::parallel_for(Kokkos::RangePolicy(exec, 0, 1), Tests::Utils::Functors::NoOp{});
123 event.record(exec);
124
125 exec.fence();
126 });
127
128 ASSERT_EQ(recorded_events.size(), 1);
129 ASSERT_THAT(recorded_events.at(0), MATCHER_FOR_RECORD_EVENT(exec));
130}
131
133
134
135template <Kokkos::ExecutionSpace Exec>
137 const auto recorded_events = EventTest::recorder_listener_t::record([&exec]() {
139 event.record(exec);
140 event.wait();
141 event.wait();
142 event.wait();
143 event.wait();
144 event.wait();
145 });
146
147 ASSERT_THAT(recorded_events, ::testing::SizeIs(6));
148 ASSERT_THAT(recorded_events.at(0), MATCHER_FOR_RECORD_EVENT(exec));
149 ASSERT_THAT(recorded_events.at(1), MATCHER_FOR_WAIT_EVENT(recorded_events.at(0)));
150 ASSERT_THAT(recorded_events.at(2), MATCHER_FOR_WAIT_EVENT(recorded_events.at(0)));
151 ASSERT_THAT(recorded_events.at(3), MATCHER_FOR_WAIT_EVENT(recorded_events.at(0)));
152 ASSERT_THAT(recorded_events.at(4), MATCHER_FOR_WAIT_EVENT(recorded_events.at(0)));
153 ASSERT_THAT(recorded_events.at(5), MATCHER_FOR_WAIT_EVENT(recorded_events.at(0)));
154}
155
156KOKKOS_EXECUTION_TESTS_IMPL_EVENT(EventTest, record_and_wait_many_times, (exec))
157
158
159template <Kokkos::ExecutionSpace Exec>
161 const auto recorded_events = EventTest::recorder_listener_t::record([&exec]() {
163 event.record(exec);
164 event.wait();
165 event.record(exec);
166 event.wait();
167 });
168
169 ASSERT_THAT(recorded_events, ::testing::SizeIs(4));
170
171 ASSERT_THAT(recorded_events.at(0), MATCHER_FOR_RECORD_EVENT(exec));
172 ASSERT_THAT(recorded_events.at(1), MATCHER_FOR_WAIT_EVENT(recorded_events.at(0)));
173
174 ASSERT_THAT(recorded_events.at(2), MATCHER_FOR_RECORD_EVENT(exec));
175 ASSERT_THAT(recorded_events.at(3), MATCHER_FOR_WAIT_EVENT(recorded_events.at(2)));
176}
177
178KOKKOS_EXECUTION_TESTS_IMPL_EVENT(EventTest, record_and_wait_and_record_and_wait, (exec))
179
180
181template <Kokkos::ExecutionSpace Exec>
183 ASSERT_THAT(Kokkos::utils::callbacks::Manager::is_initialized(), ::testing::IsFalse());
184
186 event.record(exec);
187 event.wait();
188}
189
191
192
193template <Kokkos::ExecutionSpace Exec>
194void test_event_uniqueness(const Exec& exec) {
195 const auto recorded_events = EventTest::recorder_listener_t::record([&exec]() {
196 Kokkos::Execution::Impl::Event<Exec> event_before, event_after;
197 event_before.record(exec);
198 Kokkos::parallel_for(Kokkos::RangePolicy(exec, 0, 1), Tests::Utils::Functors::NoOp{});
199 event_after.record(exec);
200
201 exec.fence();
202 });
203
204 ASSERT_THAT(recorded_events, ::testing::SizeIs(2));
205
206 ASSERT_THAT(recorded_events.at(0), MATCHER_FOR_RECORD_EVENT(exec));
207 ASSERT_THAT(recorded_events.at(1), MATCHER_FOR_RECORD_EVENT(exec));
208
209 ASSERT_NE(
210 std::get<Kokkos::Execution::Impl::RecordEvent>(recorded_events.at(0)).event_id,
211 std::get<Kokkos::Execution::Impl::RecordEvent>(recorded_events.at(1)).event_id);
212}
213
215
216
217template <Kokkos::ExecutionSpace Exec>
219 using recorder_listener_with_fence_t = RecorderListener<
224 >;
225
227
228 const Exec default_exec{};
229
230 const auto recorded_events = recorder_listener_with_fence_t::record([&default_exec]() {
231 Kokkos::parallel_for(Kokkos::RangePolicy(default_exec, 0, 1), Tests::Utils::Functors::NoOp{});
232 const Kokkos::Execution::Impl::Event<Exec> event{default_exec};
233 event.wait();
234 });
235
236 ASSERT_THAT(recorded_events, ::testing::SizeIs(2));
237 ASSERT_THAT(recorded_events.at(0), MATCHER_FOR_RECORD_EVENT(default_exec));
238 ASSERT_THAT(recorded_events.at(1), MATCHER_FOR_WAIT_EVENT(recorded_events.at(0)));
239}
240
241KOKKOS_EXECUTION_TESTS_IMPL_EVENT(EventTest, default_instance, <TEST_EXECUTION_SPACE>())
242
243} // namespace Tests::Impl
#define MATCHER_FOR_WAIT_EVENT(_record_event_variant_)
#define MATCHER_FOR_RECORD_EVENT(_exec_)
Fixture that does not set any Kokkos::utils::callbacks::Manager callback.
static constexpr bool has_support
Fixture that enables callbacks with Kokkos::utils::tests::scoped::callbacks::Manager.
RecorderListener< EventDiscardMatcher< TEST_EXECUTION_SPACE >, Kokkos::Execution::Impl::RecordEvent, Kokkos::Execution::Impl::WaitEvent > recorder_listener_t
Constrain an EventType type to be a valid event type for Exec execution space type.
Definition event.hpp:24
void test_event_record_and_wait_no_check(const Exec &exec)
consteval bool test_models_event()
void test_event_record_and_wait(const Exec &exec)
void test_event_default_instance()
void test_event_record_and_wait_many_times(const Exec &exec)
void test_event_uniqueness(const Exec &exec)
void test_event_record_and_wait_and_record_and_wait(const Exec &exec)
consteval bool test_support_events()
void test_event_record_but_dont_wait(const Exec &exec)
Matcher to filter out events that are just noise for tests.
An event that can be recorded on an execution space instance.
Definition event.hpp:34
Event to be sent to Kokkos::utils::callbacks::dispatch when an event is recorded on an execution spac...
Definition event.hpp:46
Event to be sent to Kokkos::utils::callbacks::dispatch when an event is being waited for.
Definition event.hpp:63
#define KOKKOS_EXECUTION_TESTS_IMPL_EVENT(_fixture_, _name_, _statement_)