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
9
14
25
26#if !defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
27# error "This is not supported."
28#endif
29
30namespace Tests::Impl {
31
32using namespace Kokkos::utils::callbacks;
33
46
48template <Kokkos::ExecutionSpace Exec>
49consteval bool test_models_event() {
51
52 return true;
53}
55
57template <Kokkos::ExecutionSpace Exec>
58consteval bool test_has_exec_wait_event() {
59#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || defined(KOKKOS_ENABLE_SYCL)
60 if constexpr (std::same_as<Exec, Kokkos::DefaultExecutionSpace>) {
62 return true;
63 } else
64#endif
65#if defined(KOKKOS_ENABLE_HPX)
66 if constexpr (std::same_as<Exec, Kokkos::Experimental::HPX>) {
68 return true;
69 } else
70#endif
71 {
73 return true;
74 }
75}
77
79TEST(RecordEvent, description) {
80 const Kokkos::Execution::Impl::RecordEvent event{.dev_id = 42, .event_id = 1337};
81
82 std::ostringstream oss;
83 oss << event;
84
85 ASSERT_EQ(oss.str(), "RecordEvent: {dev_id = 42, event_id = 1337}");
86}
87
89TEST(WaitEvent, description) {
90 const Kokkos::Execution::Impl::WaitEvent event{.event_id = 1337};
91
92 std::ostringstream oss;
93 oss << event;
94
95 ASSERT_EQ(oss.str(), "WaitEvent: {dev_id = 0, event_id = 1337}");
96}
97
99TEST_F(EventTest, record_and_wait) {
100 const auto recorded_events = recorder_listener_t::record([this]() {
104 });
105
106 ASSERT_THAT(recorded_events, testing::SizeIs(2));
107 ASSERT_THAT(recorded_events.at(0), MATCHER_FOR_RECORD_EVENT(exec));
108 ASSERT_THAT(recorded_events.at(1), MATCHER_FOR_WAIT_EVENT(recorded_events.at(0)));
109}
110
112TEST_F(EventTest, record_and_wait_and_record_and_wait) {
113 const auto recorded_events = recorder_listener_t::record([this]() {
119 });
120
121 ASSERT_THAT(recorded_events, ::testing::SizeIs(4));
122
123 ASSERT_THAT(recorded_events.at(0), MATCHER_FOR_RECORD_EVENT(exec));
124 ASSERT_THAT(recorded_events.at(1), MATCHER_FOR_WAIT_EVENT(recorded_events.at(0)));
125
126 ASSERT_THAT(recorded_events.at(2), MATCHER_FOR_RECORD_EVENT(exec));
127 ASSERT_THAT(recorded_events.at(3), MATCHER_FOR_WAIT_EVENT(recorded_events.at(2)));
128}
129
131TEST_F(EventTest, uniqueness) {
132 const auto recorded_events = recorder_listener_t::record([this]() {
135 Kokkos::parallel_for(Kokkos::RangePolicy(exec, 0, 1), Tests::Utils::Functors::NoOp{});
137
138 Kokkos::Execution::Impl::wait(event_before);
140 });
141
142 ASSERT_THAT(recorded_events, ::testing::SizeIs(4));
143
144 ASSERT_THAT(recorded_events.at(0), MATCHER_FOR_RECORD_EVENT(exec));
145
146 ASSERT_THAT(recorded_events.at(1), MATCHER_FOR_RECORD_EVENT(exec));
147
148 ASSERT_NE(
149 std::get<Kokkos::Execution::Impl::RecordEvent>(recorded_events.at(0)).event_id,
150 std::get<Kokkos::Execution::Impl::RecordEvent>(recorded_events.at(1)).event_id);
151
152 ASSERT_THAT(recorded_events.at(2), MATCHER_FOR_WAIT_EVENT(recorded_events.at(0)));
153 ASSERT_THAT(recorded_events.at(3), MATCHER_FOR_WAIT_EVENT(recorded_events.at(1)));
154}
155
157TEST_F(EventTest, default_instance) {
158 const TEST_EXECUTION_SPACE default_exec{};
159
160 const auto recorded_events = recorder_listener_t::record([&default_exec]() {
161 Kokkos::parallel_for(Kokkos::RangePolicy(default_exec, 0, 1), Tests::Utils::Functors::NoOp{});
163 Kokkos::Execution::Impl::record(event, default_exec);
165 });
166
167 ASSERT_THAT(recorded_events, ::testing::SizeIs(2));
168 ASSERT_THAT(recorded_events.at(0), MATCHER_FOR_RECORD_EVENT(default_exec));
169 ASSERT_THAT(recorded_events.at(1), MATCHER_FOR_WAIT_EVENT(recorded_events.at(0)));
170}
171
173TEST_F(EventTest, wait_exec_event_for_same_type) {
174 const auto [exec_A, exec_B] = Kokkos::Experimental::partition_space(exec, 1, 1);
175
176 const auto recorded_events = recorder_listener_t::record([&exec_A, &exec_B]() {
178 Kokkos::Execution::Impl::record(event_A, exec_A);
179 Kokkos::Execution::Impl::wait(exec_B, event_A);
180
182 exec_B.fence("some-label");
183 });
184
185 ASSERT_THAT(recorded_events, ::testing::SizeIs(3));
186 ASSERT_THAT(recorded_events.at(0), MATCHER_FOR_RECORD_EVENT(exec_A));
187 ASSERT_THAT(recorded_events.at(1), MATCHER_FOR_WAIT_EXEC_EVENT(exec_B, recorded_events.at(0)));
188 ASSERT_THAT(recorded_events.at(2), MATCHER_FOR_BEGIN_FENCE(exec_B, "some-label"));
189}
190
192TEST_F(EventTest, wait_exec_events_for_same_type) {
193 const auto [exec_A, exec_B] = Kokkos::Experimental::partition_space(exec, 1, 1);
194
195 const auto recorded_events = recorder_listener_t::record([&exec_A, &exec_B]() {
196 Kokkos::Execution::Impl::Event<TEST_EXECUTION_SPACE> event_A_0, event_A_1, event_A_2;
197 Kokkos::Execution::Impl::record(event_A_0, exec_A);
198 Kokkos::Execution::Impl::record(event_A_1, exec_A);
199 Kokkos::Execution::Impl::record(event_A_2, exec_A);
200 Kokkos::Execution::Impl::wait(exec_B, event_A_0, event_A_1, event_A_2);
201
203 exec_B.fence("some-label");
204 });
205
206 ASSERT_THAT(recorded_events, ::testing::SizeIs(7));
207 ASSERT_THAT(recorded_events.at(0), MATCHER_FOR_RECORD_EVENT(exec_A));
208 ASSERT_THAT(recorded_events.at(1), MATCHER_FOR_RECORD_EVENT(exec_A));
209 ASSERT_THAT(recorded_events.at(2), MATCHER_FOR_RECORD_EVENT(exec_A));
210 ASSERT_THAT(recorded_events.at(3), MATCHER_FOR_WAIT_EXEC_EVENT(exec_B, recorded_events.at(0)));
211 ASSERT_THAT(recorded_events.at(4), MATCHER_FOR_WAIT_EXEC_EVENT(exec_B, recorded_events.at(1)));
212 ASSERT_THAT(recorded_events.at(5), MATCHER_FOR_WAIT_EXEC_EVENT(exec_B, recorded_events.at(2)));
213 ASSERT_THAT(recorded_events.at(6), MATCHER_FOR_BEGIN_FENCE(exec_B, "some-label"));
214}
215
217TEST_F(EventTest, wait_exec_event_different_type) {
218 if constexpr (std::same_as<TEST_EXECUTION_SPACE, Kokkos::DefaultHostExecutionSpace>) {
219 GTEST_SKIP() << "The default host execution space is the same type as the test execution space.";
220 }
221
222 const Kokkos::DefaultHostExecutionSpace exec_h;
223
224 const auto recorded_events = recorder_listener_t::record([this, &exec_h]() {
227 Kokkos::Execution::Impl::wait(exec_h, event_d);
228
230 exec_h.fence("some-label");
231 });
232
233 ASSERT_THAT(recorded_events, ::testing::SizeIs(3));
234 ASSERT_THAT(recorded_events.at(0), MATCHER_FOR_RECORD_EVENT(this->exec));
235 ASSERT_THAT(recorded_events.at(1), MATCHER_FOR_WAIT_EXEC_EVENT(exec_h, recorded_events.at(0)));
236 ASSERT_THAT(recorded_events.at(2), MATCHER_FOR_BEGIN_FENCE(exec_h, "some-label"));
237}
238
245TEST_F(EventTest, works_intended_usage_pattern) {
246 constexpr size_t size = 128;
247
248 const auto [exec_A, exec_B, exec_C] = Kokkos::Experimental::partition_space(exec, 1, 1, 1);
249
250 const Kokkos::View<int, TEST_EXECUTION_SPACE> data(Kokkos::view_alloc(exec_A, "data"));
251 const auto data_h = Kokkos::create_mirror_view(Kokkos::WithoutInitializing, data);
252
253 std::optional<Kokkos::Execution::Impl::Event<TEST_EXECUTION_SPACE>> event_A;
254
255 stdexec::run_loop loop;
256 std::thread consumer([&] { loop.run(); });
257
258 Kokkos::parallel_for(
259 Kokkos::RangePolicy(exec_A, 0, size), KOKKOS_LAMBDA(const auto idx) { Kokkos::atomic_add(&data(), idx); });
260 event_A.emplace();
261 Kokkos::Execution::Impl::record(*event_A, exec_A);
262
263 bool upon_error = false;
264
265 auto op_state = stdexec::connect(
266 stdexec::schedule(loop.get_scheduler()) | stdexec::then([&] {
267 event_A->wait();
268 event_A.reset();
269 Kokkos::parallel_for(
270 Kokkos::RangePolicy(exec_B, 0, size),
271 KOKKOS_LAMBDA(const auto idx) { Kokkos::atomic_add(&data(), idx); });
273 Kokkos::Execution::Impl::record(event_B, exec_B);
274 Kokkos::Execution::Impl::wait(exec_C, event_B);
275 Kokkos::parallel_for(
276 Kokkos::RangePolicy(exec_C, 0, 1),
277 KOKKOS_LAMBDA(const auto) { Kokkos::atomic_compare_exchange(&data(), size * (size - 1), 1); });
278 Kokkos::deep_copy(exec_C, data_h, data);
279 exec_C.fence("wait for the deep copy to complete");
280 loop.finish();
281 }) | stdexec::upon_error([&](const auto& eptr) {
282 upon_error = true;
283 try {
284 std::rethrow_exception(eptr);
285 } catch (const std::exception& exc) {
286 Kokkos::printf("%s\n", exc.what()); // NOLINT(modernize-use-std-print)
287 }
288 loop.finish();
289 }),
290 Tests::Utils::SinkReceiver{});
291
292 op_state.start();
293
294 consumer.join();
295
296 ASSERT_FALSE(event_A.has_value());
297
299#if defined(KOKKOS_ENABLE_THREADS)
300 if constexpr (std::same_as<TEST_EXECUTION_SPACE, Kokkos::Threads>) {
301 ASSERT_EQ(data_h(), size / 2 * (size - 1));
302 ASSERT_TRUE(upon_error);
303 } else
304#endif
305 {
306 ASSERT_EQ(data_h(), 1);
307 ASSERT_FALSE(upon_error);
308 }
309}
310
311} // namespace Tests::Impl
#define MATCHER_FOR_WAIT_EVENT(_record_event_variant_)
#define MATCHER_FOR_WAIT_EXEC_EVENT(_exec_, _record_event_variant_)
#define MATCHER_FOR_RECORD_EVENT(_exec_)
#define MATCHER_FOR_BEGIN_FENCE(_exec_, _label_)
Fixture that enables callbacks with Kokkos::utils::tests::scoped::callbacks::Manager.
RecorderListener< EventDiscardMatcher< TEST_EXECUTION_SPACE >, BeginFenceEvent, 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:26
Determine if the Kokkos backend can enqueue a wait for an event into an execution space instance.
void record(Event< Exec > &event, const Exec &exec)
Record event on exec.
Definition event.hpp:140
void wait(const Event< Exec > &... events)
Wait for events to complete.
Definition event.hpp:152
consteval bool test_models_event()
consteval bool test_has_exec_wait_event()
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:110
Event to be sent to Kokkos::utils::callbacks::dispatch when calling record.
Definition event.hpp:54
Event to be sent to Kokkos::utils::callbacks::dispatch when calling wait.
Definition event.hpp:75