kokkos-execution 0.0.1
Loading...
Searching...
No Matches
test_let_value.cpp
Go to the documentation of this file.
2PRAGMA_DIAGNOSTIC_PUSH
4#include "exec/single_thread_context.hpp"
5PRAGMA_DIAGNOSTIC_POP
6
10
12
17
29
31
32using namespace Kokkos::utils::callbacks;
33
49
51TEST_F(LetValueTest, scoped_allocation) {
52 const view_s_t data(Kokkos::view_alloc(exec, "data - shared space"));
53
54 const context_t esc{exec};
55
56 experimental::execution::single_thread_context stc{};
57
58 using view_of_5_t = Kokkos::View<value_t[5], typename TEST_EXECUTION_SPACE::memory_space>;
59
61 auto allocate = stdexec::schedule(esc.get_scheduler())
62 | stdexec::let_value([this]() noexcept -> stdexec::sender auto {
63 return stdexec::just(
64 view_of_5_t{Kokkos::view_alloc(exec, "scratch", Kokkos::WithoutInitializing)});
65 });
66
67 static_assert(stdexec::dependent_sender<decltype(allocate)>);
68
71 decltype(allocate),
72 stdexec::__mset<stdexec::set_value_t(view_of_5_t)>,
73 stdexec::env<>
74 >);
75
77 auto run = std::move(allocate) // NOLINT(performance-move-const-arg)
86 | stdexec::let_value([&esc, &data, &stc](auto&& scratch) noexcept -> stdexec::sender auto {
87 static_assert(std::same_as<decltype(scratch), view_of_5_t&>);
88 EXPECT_EQ(scratch.use_count(), 1);
89 return stdexec::schedule(esc.get_scheduler())
90 | stdexec::bulk(
91 stdexec::par,
92 5,
93 KOKKOS_LAMBDA<std::integral T>(const T index) {
100 KOKKOS_IF_ON_HOST(EXPECT_EQ(scratch.use_count(), 2);)
101 scratch(index) = index;
102 Kokkos::atomic_add(data.data(), scratch(index));
103 })
104 | stdexec::continues_on(stc.get_scheduler())
105 | stdexec::then([&scratch]() mutable -> Kokkos::utils::concepts::ViewOfRank<1> auto {
107 EXPECT_EQ(scratch.use_count(), 2);
108 return std::move(scratch);
109 });
110 });
111
113 decltype(run),
114 stdexec::__mset<stdexec::set_value_t(view_of_5_t), stdexec::set_error_t(std::exception_ptr)>,
115 stdexec::env<>
116 >);
117
119 auto check = std::move(run) // NOLINT(performance-move-const-arg)
120 | stdexec::let_value([&esc, &data](auto&& scratch) noexcept -> stdexec::sender auto {
121 static_assert(std::same_as<decltype(scratch), view_of_5_t&>);
136 EXPECT_EQ(scratch.use_count(), 2);
137 return stdexec::schedule(esc.get_scheduler())
138 | stdexec::bulk(
139 stdexec::par, 5, KOKKOS_LAMBDA<std::integral T>(const T index) {
140 Kokkos::atomic_add(data.data(), scratch(index));
147 KOKKOS_IF_ON_HOST(EXPECT_EQ(scratch.use_count(), 2);)
148 });
149 });
150
152 decltype(check),
153 stdexec::__mset<stdexec::set_value_t(), stdexec::set_error_t(std::exception_ptr)>,
154 stdexec::env<>
155 >);
156
157 ASSERT_EQ(data(), 0) << "Eager execution is not allowed.";
158
160
161 const auto recorded_events = Tests::Utils::record_sync_wait<recorder_listener_t>(
162 std::move(check)); // NOLINT(performance-move-const-arg)
163
164 ASSERT_THAT(recorded_events, [&]() {
168 testing::Field(
170 testing::Field(&Kokkos::utils::callbacks::AllocDescriptor::name, testing::StrEq("scratch")))),
175 MATCHER_FOR_WAIT_EVENT(recorded_events.at(4)),
177 testing::Field(
179 testing::Field(&Kokkos::utils::callbacks::AllocDescriptor::name, testing::StrEq("scratch")))));
180 } else {
183 testing::Field(
185 testing::Field(&Kokkos::utils::callbacks::AllocDescriptor::name, testing::StrEq("scratch")))),
189 MATCHER_FOR_BEGIN_FENCE(exec, dispatch_label(exec, "after dispatch")),
191 testing::Field(
193 testing::Field(&Kokkos::utils::callbacks::AllocDescriptor::name, testing::StrEq("scratch")))));
194 }
195 }());
196
197 ASSERT_EQ(data(), 2 * (0 + 1 + 2 + 3 + 4));
198}
199
200} // namespace Tests::ExecutionSpaceImpl
constexpr std::string dispatch_label(const Exec &, Label &&label)
Get the dispatch label from Exec and label.
#define MATCHER_FOR_WAIT_EVENT(_record_event_variant_)
#define MATCHER_FOR_BEGIN_PFOR(_exec_, _label_)
#define MATCHER_FOR_RECORD_EVENT(_exec_)
#define MATCHER_FOR_BEGIN_FENCE(_exec_, _label_)
typename recorder_listener_t::event_variant_t variant_t
RecorderListener< EventDiscardMatcher< TEST_EXECUTION_SPACE >, BeginFenceEvent, BeginParallelForEvent, AllocateDataEvent, DeallocateDataEvent, Kokkos::Execution::Impl::RecordEvent, Kokkos::Execution::Impl::WaitEvent > recorder_listener_t
#define KOKKOS_EXECUTION_THREADS_THROWS_ON_SYNC_WAIT_ASSERT_AND_SKIP(_sndr_)
Definition context.hpp:69
#define KOKKOS_EXECUTION_STDEXEC_PRAGMA_DIAGNOSTIC_IGNORED
Basic list of ignored diagnostics when including anything from stdexec.
auto AAllocateDataEvent(Matchers &&... matchers)
auto ADeallocateDataEvent(Matchers &&... matchers)
auto ContainsInOrder(Matchers &&... matchers)
auto record_sync_wait(Sndr &&sndr)
Definition sync_wait.hpp:14
Matcher to filter out events that are just noise for tests.
auto get_scheduler() const noexcept -> ExecutionSpaceImpl::Scheduler< Exec >
Event to be sent to Kokkos::utils::callbacks::dispatch when calling record.
Definition event.hpp:52
Event to be sent to Kokkos::utils::callbacks::dispatch when calling wait.
Definition event.hpp:73
Kokkos::Execution::ExecutionSpaceContext< Exec > context_t
Definition context.hpp:27