kokkos-execution 0.0.1
Loading...
Searching...
No Matches
operation_state.hpp
Go to the documentation of this file.
1#ifndef KOKKOS_EXECUTION_EXECUTION_SPACE_OPERATION_STATE_HPP
2#define KOKKOS_EXECUTION_EXECUTION_SPACE_OPERATION_STATE_HPP
3
5
15
17
18template <typename Clsr>
19concept Closure = requires(const Clsr& clsr) {
20 typename Clsr::execution_space;
21
22 { clsr.submit() } -> std::same_as<void>;
23
24 { clsr.get_policy() };
25 requires Kokkos::ExecutionPolicy<std::remove_cvref_t<decltype(clsr.get_policy())>>;
26
27 requires std::same_as<std::remove_cvref_t<decltype(clsr.get_policy().space())>, typename Clsr::execution_space>;
28};
29
30template <typename Exec, typename Rcvr>
31consteval auto select_sync_policy() {
32 if constexpr (
33 stdexec::__is_instance_of<Rcvr, Impl::SyncWait::Receiver>
34 || stdexec::__is_instance_of<Rcvr, ScheduleFromReceiver>) {
38 } else if constexpr (
40 && stdexec::__queryable_with<stdexec::env_of_t<Rcvr>, stdexec::get_delegation_scheduler_t>) {
42 } else {
44 }
45}
46
47template <typename Exec, typename Rcvr>
49
50template <stdexec::receiver Rcvr, Closure Clsr, Closure... Clsrs>
51requires(std::same_as<typename Clsr::execution_space, typename Clsrs::execution_space> && ...)
53 using execution_space = typename Clsr::execution_space;
54
57 using closures_t = stdexec::__tuple<Clsr, Clsrs...>;
58
61
62 constexpr explicit OpStateBase(Rcvr rcvr, Clsr clsr_, Clsrs... clsrs_) noexcept(
63 std::is_nothrow_constructible_v<completion_signal_t, Rcvr&&> && std::is_nothrow_move_constructible_v<Clsr>
64 && (std::is_nothrow_move_constructible_v<Clsrs> && ...))
65 : completion_signal(std::move(rcvr))
66 , clsrs(std::move(clsr_), std::move(clsrs_)...) {
67 }
68
69 void complete(stdexec::set_value_t) noexcept {
70 try {
71 stdexec::__apply([](auto&... clsr) { (clsr.submit(), ...); }, clsrs);
72 } catch (...) {
73 this->complete(stdexec::set_error, std::current_exception());
74 return;
75 }
76 completion_signal.propagate(stdexec::set_value, this->query(Impl::get_exec).get());
77 }
78
79 template <typename Error>
80 void complete(stdexec::set_error_t, Error&& error) noexcept {
81 completion_signal.propagate(stdexec::set_error, std::forward<Error>(error));
82 }
83
84 void complete(stdexec::set_stopped_t) noexcept {
85 completion_signal.propagate(stdexec::set_stopped);
86 }
87
89 [[nodiscard]]
91 return Impl::ExecutionSpaceRef<execution_space>{stdexec::__get<0>(clsrs).get_policy().space()};
92 }
93
94 [[nodiscard]]
95 constexpr auto get_env() const noexcept -> stdexec::env_of_t<Rcvr> {
96 return stdexec::get_env(this->completion_signal.rcvr);
97 }
98};
99
100template <stdexec::sender Sndr, stdexec::receiver Rcvr, Closure... Clsrs>
103 : public Impl::Immovable
104 , public OpStateBase<Rcvr, Clsrs...> {
105 using operation_state_concept = stdexec::operation_state_tag;
106
107 using base_t = OpStateBase<Rcvr, Clsrs...>;
109
110 using inner_opstate_t = stdexec::connect_result_t<Sndr, rcvr_t>;
111
113 std::is_nothrow_constructible_v<base_t, Rcvr&&, Clsrs&&...>;
114
115 static constexpr bool inner_opstate_is_nothrow_constructible = stdexec::__nothrow_connectable<Sndr&&, rcvr_t>;
116
118
120#if defined(KOKKOS_EXECUTION_IMPL_OPSTATE_IMMOVABLE_FIX)
121 STDEXEC_IMMOVABLE(OpState);
122#endif
123
124 constexpr explicit OpState(
125 Sndr&& sndr, // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
126 Rcvr rcvr_,
128 : base_t(std::move(rcvr_), std::move(clsrs_)...)
129 , inner_opstate(stdexec::connect(std::forward<Sndr>(sndr), rcvr_t{this})) {
130 }
131
132 void start() & noexcept {
133 stdexec::start(inner_opstate);
134 }
135};
136
137template <typename Sndr, typename Rcvr, typename... Clsrs>
139
140template <typename Sndr, typename Rcvr, typename... Clsrs>
141using opstate_t = typename make_opstate_t<Sndr, Rcvr, Clsrs...>::type;
142
143} // namespace Kokkos::Execution::ExecutionSpaceImpl
144
145#endif // KOKKOS_EXECUTION_EXECUTION_SPACE_OPERATION_STATE_HPP
Concept that constrains the type of a sender that dispatches a functor for execution.
decltype(select_sync_policy< Exec, Rcvr >()) select_sync_policy_t
typename make_opstate_t< Sndr, Rcvr, Clsrs... >::type opstate_t
Impl::MakeOpState< Domain, OpState >::Huddle< Sndr, Rcvr, Clsrs... > make_opstate_t
constexpr get_exec_t get_exec
Definition get_exec.hpp:17
constexpr auto get_env() const noexcept -> stdexec::env_of_t< Rcvr >
constexpr auto query(Impl::get_exec_t) const noexcept -> Impl::ExecutionSpaceRef< execution_space >
void complete(stdexec::set_stopped_t) noexcept
select_sync_policy_t< execution_space, Rcvr > sync_policy_t
stdexec::__tuple< Clsr, Clsrs... > closures_t
constexpr OpStateBase(Rcvr rcvr, Clsr clsr_, Clsrs... clsrs_) noexcept(std::is_nothrow_constructible_v< completion_signal_t, Rcvr && > &&std::is_nothrow_move_constructible_v< Clsr > &&(std::is_nothrow_move_constructible_v< Clsrs > &&...))
void complete(stdexec::set_error_t, Error &&error) noexcept
Impl::CompletionSignal< sync_policy_t, execution_space, Rcvr > completion_signal_t
void complete(stdexec::set_value_t) noexcept
constexpr OpState(Sndr &&sndr, Rcvr rcvr_, Clsrs... clsrs_) noexcept(opstate_base_is_nothrow_constructible &&inner_opstate_is_nothrow_constructible)
stdexec::connect_result_t< Sndr, rcvr_t > inner_opstate_t
stdexec::operation_state_tag operation_state_concept
Wrap a Kokkos execution space to make it cheap to copy/move in new environments.
Definition get_exec.hpp:27
Receiver for an object parent_op that implements complete.
Definition receiver.hpp:12