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>
57template <Kokkos::ExecutionSpace Exec>
59#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || defined(KOKKOS_ENABLE_SYCL)
60 if constexpr (std::same_as<Exec, Kokkos::DefaultExecutionSpace>) {
65#if defined(KOKKOS_ENABLE_HPX)
66 if constexpr (std::same_as<Exec, Kokkos::Experimental::HPX>) {
79TEST(RecordEvent, description) {
82 std::ostringstream oss;
85 ASSERT_EQ(oss.str(),
"RecordEvent: {dev_id = 42, event_id = 1337}");
89TEST(WaitEvent, description) {
92 std::ostringstream oss;
95 ASSERT_EQ(oss.str(),
"WaitEvent: {dev_id = 0, event_id = 1337}");
100 const auto recorded_events = recorder_listener_t::record([
this]() {
106 ASSERT_THAT(recorded_events, testing::SizeIs(2));
113 const auto recorded_events = recorder_listener_t::record([
this]() {
121 ASSERT_THAT(recorded_events, ::testing::SizeIs(4));
132 const auto recorded_events = recorder_listener_t::record([
this]() {
142 ASSERT_THAT(recorded_events, ::testing::SizeIs(4));
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);
158 const TEST_EXECUTION_SPACE default_exec{};
160 const auto recorded_events = recorder_listener_t::record([&default_exec]() {
167 ASSERT_THAT(recorded_events, ::testing::SizeIs(2));
174 const auto [exec_A, exec_B] = Kokkos::Experimental::partition_space(
exec, 1, 1);
176 const auto recorded_events = recorder_listener_t::record([&exec_A, &exec_B]() {
182 exec_B.fence(
"some-label");
185 ASSERT_THAT(recorded_events, ::testing::SizeIs(3));
193 const auto [exec_A, exec_B] = Kokkos::Experimental::partition_space(
exec, 1, 1);
195 const auto recorded_events = recorder_listener_t::record([&exec_A, &exec_B]() {
203 exec_B.fence(
"some-label");
206 ASSERT_THAT(recorded_events, ::testing::SizeIs(7));
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.";
222 const Kokkos::DefaultHostExecutionSpace exec_h;
224 const auto recorded_events = recorder_listener_t::record([
this, &exec_h]() {
230 exec_h.fence(
"some-label");
233 ASSERT_THAT(recorded_events, ::testing::SizeIs(3));
246 constexpr size_t size = 128;
248 const auto [exec_A, exec_B, exec_C] = Kokkos::Experimental::partition_space(
exec, 1, 1, 1);
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);
253 std::optional<Kokkos::Execution::Impl::Event<TEST_EXECUTION_SPACE>> event_A;
255 stdexec::run_loop loop;
256 std::thread consumer([&] { loop.run(); });
258 Kokkos::parallel_for(
259 Kokkos::RangePolicy(exec_A, 0, size), KOKKOS_LAMBDA(
const auto idx) { Kokkos::atomic_add(&data(), idx); });
263 bool upon_error =
false;
265 auto op_state = stdexec::connect(
266 stdexec::schedule(loop.get_scheduler()) | stdexec::then([&] {
269 Kokkos::parallel_for(
270 Kokkos::RangePolicy(exec_B, 0, size),
271 KOKKOS_LAMBDA(const auto idx) { Kokkos::atomic_add(&data(), idx); });
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");
281 }) | stdexec::upon_error([&](
const auto& eptr) {
284 std::rethrow_exception(eptr);
285 }
catch (
const std::exception& exc) {
286 Kokkos::printf(
"%s\n", exc.what());
290 Tests::Utils::SinkReceiver{});
296 ASSERT_FALSE(event_A.has_value());
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);
306 ASSERT_EQ(data_h(), 1);
307 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.
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.
void wait(const Event< Exec > &... events)
Wait for events to complete.
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.
Event to be sent to Kokkos::utils::callbacks::dispatch when calling record.
Event to be sent to Kokkos::utils::callbacks::dispatch when calling wait.