1#include "gtest/gtest.h"
26#if !defined(KOKKOS_EXECUTION_ENABLE_EVENT_DISPATCH)
27# error "This is not supported."
32using namespace Kokkos::utils::callbacks;
48template <Kokkos::ExecutionSpace Exec>
57TEST(RecordEvent, description) {
60 std::ostringstream oss;
63 ASSERT_EQ(oss.str(),
"RecordEvent: {dev_id = 42, event_id = 1337}");
67TEST(WaitEvent, description) {
70 std::ostringstream oss;
73 ASSERT_EQ(oss.str(),
"WaitEvent: {dev_id = 0, event_id = 1337}");
78 const auto recorded_events = recorder_listener_t::record([
this]() {
84 ASSERT_THAT(recorded_events, testing::SizeIs(2));
91 const auto recorded_events = recorder_listener_t::record([
this]() {
96 exec.fence(
"some-label");
99 ASSERT_THAT(recorded_events, testing::SizeIs(2));
106 const auto recorded_events = recorder_listener_t::record([
this]() {
116 ASSERT_THAT(recorded_events, ::testing::SizeIs(6));
127 const auto recorded_events = recorder_listener_t::record([
this]() {
135 ASSERT_THAT(recorded_events, ::testing::SizeIs(4));
146 const auto recorded_events = recorder_listener_t::record([
this]() {
152 exec.fence(
"some-label");
155 ASSERT_THAT(recorded_events, ::testing::SizeIs(3));
162 std::get<Kokkos::Execution::Impl::RecordEvent>(recorded_events.at(0)).event_id,
163 std::get<Kokkos::Execution::Impl::RecordEvent>(recorded_events.at(1)).event_id);
170 const TEST_EXECUTION_SPACE default_exec{};
172 const auto recorded_events = recorder_listener_t::record([&default_exec]() {
179 ASSERT_THAT(recorded_events, ::testing::SizeIs(2));
186 const auto [exec_A, exec_B] = Kokkos::Experimental::partition_space(
exec, 1, 1);
188 const auto recorded_events = recorder_listener_t::record([&exec_A, &exec_B]() {
194 ASSERT_THAT(recorded_events, ::testing::SizeIs(2));
201 if constexpr (std::same_as<TEST_EXECUTION_SPACE, Kokkos::DefaultHostExecutionSpace>) {
202 GTEST_SKIP() <<
"The default host execution space is the same type as the test execution space.";
205 const Kokkos::DefaultHostExecutionSpace exec_h;
207 const auto recorded_events = recorder_listener_t::record([
this, &exec_h]() {
213 ASSERT_THAT(recorded_events, ::testing::SizeIs(2));
225 constexpr size_t size = 128;
227 const auto [exec_A, exec_B, exec_C] = Kokkos::Experimental::partition_space(
exec, 1, 1, 1);
229 const Kokkos::View<int, TEST_EXECUTION_SPACE> data(Kokkos::view_alloc(exec_A,
"data"));
230 const auto data_h = Kokkos::create_mirror_view(Kokkos::WithoutInitializing, data);
232 std::optional<Kokkos::Execution::Impl::Event<TEST_EXECUTION_SPACE>> event_A;
234 stdexec::run_loop loop;
235 std::thread consumer([&] { loop.run(); });
237 Kokkos::parallel_for(
238 Kokkos::RangePolicy(exec_A, 0, size), KOKKOS_LAMBDA(
const auto idx) { Kokkos::atomic_add(&data(), idx); });
242 bool upon_error =
false;
244 auto opstate = stdexec::connect(
245 stdexec::schedule(loop.get_scheduler()) | stdexec::then([&] {
248 Kokkos::parallel_for(
249 Kokkos::RangePolicy(exec_B, 0, size),
250 KOKKOS_LAMBDA(const auto idx) { Kokkos::atomic_add(&data(), idx); });
254 Kokkos::parallel_for(
255 Kokkos::RangePolicy(exec_C, 0, 1),
256 KOKKOS_LAMBDA(
const auto) { Kokkos::atomic_compare_exchange(&data(), size * (size - 1), 1); });
257 Kokkos::deep_copy(exec_C, data_h, data);
258 exec_C.fence(
"wait for the deep copy to complete");
260 }) | stdexec::upon_error([&](
const auto& eptr) {
263 std::rethrow_exception(eptr);
264 }
catch (
const std::exception& exc) {
265 Kokkos::printf(
"%s\n", exc.what());
269 Tests::Utils::SinkReceiver{});
275 ASSERT_FALSE(event_A.has_value());
278#if defined(KOKKOS_ENABLE_THREADS)
279 if constexpr (std::same_as<TEST_EXECUTION_SPACE, Kokkos::Threads>) {
280 ASSERT_EQ(data_h(), size / 2 * (size - 1));
281 ASSERT_TRUE(upon_error);
285 ASSERT_EQ(data_h(), 1);
286 ASSERT_FALSE(upon_error);
#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.
void record(Event< Exec > &event, const Exec &exec)
Record event on exec.
void wait(const Event< Exec > &event)
Wait for event to complete.
consteval bool test_models_event()
Matcher to filter out events that are just noise for tests.
An event that can be recorded on an execution space instance.
Event to be sent to Kokkos::utils::callbacks::dispatch when calling record.
Event to be sent to Kokkos::utils::callbacks::dispatch when calling wait.