kokkos-execution 0.0.1
Loading...
Searching...
No Matches
sync_wait.hpp
Go to the documentation of this file.
1#ifndef KOKKOS_EXECUTION_EXECUTION_SPACE_SYNC_WAIT_HPP
2#define KOKKOS_EXECUTION_EXECUTION_SPACE_SYNC_WAIT_HPP
3
4#include "stdexec/execution.hpp"
5
7
11
13
15template <Kokkos::ExecutionSpace Exec, typename... Values>
17 using receiver_concept = stdexec::receiver_t;
18
21 std::optional<std::tuple<Values...>>* result;
22
23 template <typename... Args>
24 void set_value(Args&&... args) && noexcept {
25 state->exec.fence(std::format("{}: sync_wait", Kokkos::Impl::TypeInfo<Exec>::name()));
26 result->emplace(std::forward<Args>(args)...);
27 runloop_state->loop.finish();
28 }
29
30 template <typename Error>
31 void set_error(Error&& err) && noexcept {
32 runloop_state->error = std::forward<Error>(err);
33 state->exec.fence(std::format("{}: sync_wait", Kokkos::Impl::TypeInfo<Exec>::name()));
34 runloop_state->loop.finish();
35 }
36
37 void set_stopped() noexcept {
38 state->exec.fence(std::format("{}: sync_wait", Kokkos::Impl::TypeInfo<Exec>::name()));
39 runloop_state->loop.finish();
40 }
41
43 [[nodiscard]]
44 constexpr auto get_env() const noexcept
45 -> stdexec::__join_env_t<stdexec::prop<get_exec_t, ExecutionSpaceRef<Exec>>, Kokkos::Execution::Impl::env> {
46 return stdexec::__env::__join(
47 stdexec::prop{get_exec, ExecutionSpaceRef{state->exec}},
48 Kokkos::Execution::Impl::env{runloop_state->loop.get_scheduler()});
49 }
50};
51
52struct SyncWait {
59 template <stdexec::sender Sndr>
60 auto operator()(Sndr&& sndr) const noexcept(false)
61 -> std::optional<stdexec::__sync_wait::__value_tuple_for_t<Sndr>> {
63
64 auto schd = stdexec::get_completion_scheduler<stdexec::set_value_t>(stdexec::get_env(sndr), stdexec::env{});
65
66 using result_t = std::optional<stdexec::__sync_wait::__value_tuple_for_t<Sndr>>;
67
68 result_t result{};
69
70 auto op_state = stdexec::connect(
71 std::forward<Sndr>(sndr),
73 .state = std::move(schd.state),
74 .runloop_state = std::addressof(runloop_state),
75 .result = std::addressof(result)});
76
77 stdexec::start(op_state);
78
79 runloop_state.loop.run();
80
81 if (runloop_state.error)
82 std::rethrow_exception(std::move(runloop_state.error));
83
84 return result;
85 }
86};
87
96template <>
97struct ApplySenderFor<stdexec::sync_wait_t> {
98 template <execution_space_completing_sender Sndr>
99 auto operator()(Sndr&& sndr) && noexcept(std::is_nothrow_invocable_v<SyncWait, Sndr&&>) {
100 return SyncWait{}(std::forward<Sndr>(sndr));
101 }
102};
103
104} // namespace Kokkos::Execution::ExecutionSpaceImpl
105
106#endif // KOKKOS_EXECUTION_EXECUTION_SPACE_SYNC_WAIT_HPP
auto operator()(Sndr &&sndr) &&noexcept(std::is_nothrow_invocable_v< SyncWait, Sndr && >)
Definition sync_wait.hpp:99
Wrap a Kokkos execution space to make it cheap to copy/move in new environments.
Definition get_exec.hpp:31
std::optional< std::tuple< Values... > > * result
Definition sync_wait.hpp:21
constexpr auto get_env() const noexcept -> stdexec::__join_env_t< stdexec::prop< get_exec_t, ExecutionSpaceRef< Exec > >, Kokkos::Execution::Impl::env >
Make others aware of which execution space instance it will synchronize.
Definition sync_wait.hpp:44
auto operator()(Sndr &&sndr) const noexcept(false) -> std::optional< stdexec::__sync_wait::__value_tuple_for_t< Sndr > >
Definition sync_wait.hpp:60
Inspired by https://github.com/NVIDIA/stdexec/blob/16076a81efa4477513e6ede9c2741fd034ecef99/include/s...
Definition sync_wait.hpp:24
Inspired by https://github.com/NVIDIA/stdexec/blob/16076a81efa4477513e6ede9c2741fd034ecef99/include/s...
Definition sync_wait.hpp:9