1#ifndef KOKKOS_EXECUTION_GRAPH_OPERATION_STATE_HPP
2#define KOKKOS_EXECUTION_GRAPH_OPERATION_STATE_HPP
6#if defined(KOKKOS_EXECUTION_ENABLE_DEBUG_LOGGING)
10#include "Kokkos_Core.hpp"
11#include "Kokkos_Graph.hpp"
29template <
typename Clsr>
31 typename Clsr::execution_space;
32 requires std::same_as<typename Clsr::device_handle_t, Kokkos::Impl::DeviceHandle<typename Clsr::execution_space>>;
33 typename Clsr::node_props_t;
37template <stdexec::operation_state OpState, Kokkos::ExecutionSpace Exec>
40 requires(
const OpState& opstate) {
45 std::same_as<typename OpState::execution_space, Exec>
50template <stdexec::operation_state OpState, Kokkos::ExecutionSpace Exec>
51requires(stdexec::__is_instance_of<OpState, Kokkos::Execution::GraphImpl::Scheduler<Exec>::template
OpState>)
55template <stdexec::operation_state OpState, Kokkos::ExecutionSpace Exec>
58 && stdexec::__is_instance_of<OpState, Kokkos::Execution::GraphImpl::Scheduler<Exec>::template
OpState>)
62template <stdexec::operation_state OpState, Kokkos::ExecutionSpace Exec>
66template <
typename GraphCompositionPolicy, Kokkos::ExecutionSpace Exec>
70template <Kokkos::ExecutionSpace Exec>
74 using graph_t = Kokkos::Experimental::Graph<Exec>;
76 explicit State(
const Kokkos::Impl::DeviceHandle<Exec>&) {
81template <Kokkos::ExecutionSpace Exec>
87 using graph_t = Kokkos::Experimental::Graph<Exec>;
88 using root_t =
typename graph_t::root_t;
99 return graph.get_device_handle();
103 if (Kokkos::Impl::GraphAccess::get_node_ptr(
m_root) ==
nullptr) {
115template <Kokkos::ExecutionSpace Exec, stdexec::receiver Rcvr>
130 constexpr explicit OpStateBase(Rcvr rcvr)
noexcept(std::is_nothrow_constructible_v<completion_signal_t, Rcvr&&>)
134 template <
typename Error>
135 void complete(stdexec::set_error_t, Error&& error)
noexcept {
136 stdexec::set_error(std::move(
completion_signal.rcvr), std::forward<Error>(error));
145template <
typename Predecessor, Closure FirstClosure, Closure... RestOfClosures>
146requires NodeRef<std::remove_cvref_t<Predecessor>>
147static auto add_nodes(Predecessor&& predecessor, FirstClosure&& clsr, RestOfClosures&&... clsrs) {
148 auto node = std::forward<FirstClosure>(clsr).add_node(std::forward<Predecessor>(predecessor));
149 if constexpr (
sizeof...(RestOfClosures) == 0) {
152 return add_nodes(std::move(node), std::forward<RestOfClosures>(clsrs)...);
157template <stdexec::sender Sndr, stdexec::receiver Rcvr, Closure FirstClosure, Closure... RestOfClosures>
160 ,
public OpStateBase<typename FirstClosure::execution_space, Rcvr> {
167 static_assert((std::same_as<typename RestOfClosures::execution_space, execution_space> && ...));
175 static constexpr bool is_graph_create = std::same_as<graph_composition_policy_t, GraphComposition::Create>;
178 std::declval<predecessor_t>(),
179 std::declval<FirstClosure>(),
180 std::declval<RestOfClosures>()...));
191 RestOfClosures... clsrs)
noexcept(
false)
196#if defined(KOKKOS_EXECUTION_ENABLE_DEBUG_LOGGING)
197 PLOG_INFO <<
"Operation state graph composition policy is "
198 << Kokkos::Impl::TypeInfo<graph_composition_policy_t>::name() <<
", the receiver is of type "
199 << Kokkos::Impl::TypeInfo<Rcvr>::name() <<
" and the inner operation state is of type "
200 << Kokkos::Impl::TypeInfo<inner_opstate_t>::name() <<
'.';
210#if defined(KOKKOS_EXECUTION_ENABLE_DEBUG_LOGGING)
214 return state.get_root_node();
216 if constexpr (stdexec::__queryable_with<inner_opstate_t, get_node_t>) {
228 template <
typename Error>
229 void complete(stdexec::set_error_t, Error&& error)
noexcept {
230 stdexec::set_error(std::move(this->
completion_signal.rcvr), std::forward<Error>(error));
239#if defined(KOKKOS_EXECUTION_ENABLE_DEBUG_LOGGING)
241 << Kokkos::Tools::Experimental::device_id(
state.get_device_handle().m_exec) <<
'.';
246 this->
complete(stdexec::set_error, std::current_exception());
287template <
typename Sndr,
typename Rcvr,
typename... Clsrs>
290template <
typename Sndr,
typename Rcvr,
typename... Clsrs>
293#define KOKKOS_EXECUTION_GRAPH_OPERATION_STATE_CONNECT \
294 template <stdexec::receiver Rcvr> \
295 constexpr auto connect(Rcvr rcvr) && noexcept(noexcept(make_opstate_t<Sndr, Rcvr, closure_t>{}( \
296 std::declval<Sndr>(), std::declval<Rcvr>(), std::declval<closure_t>()))) -> opstate_t<Sndr, Rcvr, closure_t> { \
297 return make_opstate_t<Sndr, Rcvr, closure_t>{}(std::forward<Sndr>(sndr), std::move(rcvr), std::move(clsr)); \
300#if defined(KOKKOS_EXECUTION_ENABLE_DEBUG_LOGGING)
301# define KOKKOS_EXECUTION_IMPL_GRAPH_ADD_NODE_DEBUG_LOGGING(_type_, _node_, _predecessor_) \
302 PLOG_INFO << "Adding '" _type_ "' node " << get_node_ptr(_node_) << " to graph " << get_graph_impl_ptr(_node_) \
303 << " after " << get_node_ptr(_predecessor_) << " on device " \
304 << Kokkos::Tools::Experimental::device_id(get_node_ptr(_node_)->get_device_handle().m_exec) << '.';
306# define KOKKOS_EXECUTION_IMPL_GRAPH_ADD_NODE_DEBUG_LOGGING(_type_, _node_, _predecessor_)
312template <stdexec::sender Sndr, stdexec::receiver Rcvr,
typename FirstClosure,
typename... RestOfClosures>
313extern __mtype<Kokkos::Execution::GraphImpl::OpState<__demangle_t<Sndr>, Rcvr, FirstClosure, RestOfClosures...>>
#define KOKKOS_EXECUTION_GET_ENV(_type_, _obj_)
Retrieve the environment of _obj_. // NOLINTNEXTLINE(cppcoreguidelines-macro-usage).
typename make_opstate_t< Sndr, Rcvr, Clsrs... >::type opstate_t
constexpr get_node_t get_node
auto create_graph(const Kokkos::Impl::DeviceHandle< Exec > &device_handle, Args &&... args)
Create a graph and record the associated event with graph_create_event.
Impl::MakeOpState< Domain, OpState >::Huddle< Sndr, Rcvr, Clsrs... > make_opstate_t
constexpr get_graph_t get_graph
auto * get_graph_impl_ptr(const NodeType &node) noexcept
Retrieve the raw graph pointer from a node.
static auto add_nodes(Predecessor &&predecessor, FirstClosure &&clsr, RestOfClosures &&... clsrs)
Add all nodes as a sequence. Hence, only the first node may be added after the root node.
void submit_graph(const Kokkos::Experimental::Graph< Exec > &graph, const Exec &exec)
Submit a graph and record the associated event with graph_submit_event.
constexpr get_exec_t get_exec
Attach to the existing graph of the predecessor.
Create a new graph and attach after the root node.
Inspired by https://github.com/NVIDIA/stdexec/blob/8c5eedd0fcf9a8ebcdb75d988f72f88efcf64a37/include/s...
typename node_helper_t< Exec, Queryables... >::type node_t
std::conditional_t<(stdexec::__queryable_with< Queryables, get_node_t >||...), Attach, Create > policy_t
Use the Attach policy if any Queryables is queryable with get_node_t.
decltype(select_completion_signal_policy()) completion_signal_policy_t
Impl::CompletionSignal< completion_signal_policy_t, Exec, Rcvr > completion_signal_t
void complete(stdexec::set_error_t, Error &&error) noexcept
static consteval auto select_completion_signal_policy() noexcept
void complete(stdexec::set_stopped_t) noexcept
completion_signal_t completion_signal
constexpr OpStateBase(Rcvr rcvr) noexcept(std::is_nothrow_constructible_v< completion_signal_t, Rcvr && >)
Operation state that adds all closures as a sequence of nodes.
GraphComposition::node_t< execution_space, inner_opstate_t, Rcvr > predecessor_t
stdexec::connect_result_t< Sndr, rcvr_t > inner_opstate_t
void complete(stdexec::set_error_t, Error &&error) noexcept
Impl::SubmittedOperationStateTag operation_state_concept
constexpr OpState(Sndr &&sndr, Rcvr rcvr, FirstClosure clsr, RestOfClosures... clsrs) noexcept(false)
State< graph_composition_policy_t, execution_space > state_t
inner_opstate_t inner_opstate
typename FirstClosure::device_handle_t device_handle_t
static constexpr bool is_graph_create
void complete(stdexec::set_stopped_t) noexcept
Impl::Receiver< OpState, stdexec::env_of_t< Rcvr > > rcvr_t
Ensure that all closures are on the same execution space type.
typename FirstClosure::execution_space execution_space
decltype(add_nodes( std::declval< predecessor_t >(), std::declval< FirstClosure >(), std::declval< RestOfClosures >()...)) node_t
void complete(stdexec::set_value_t) noexcept
const predecessor_t & get_predecessor() const noexcept
constexpr auto query(get_node_t) const &noexcept -> const node_t &
GraphComposition::policy_t< inner_opstate_t, Rcvr > graph_composition_policy_t
constexpr auto query(Impl::get_exec_t) const noexcept -> Impl::ExecutionSpaceRef< execution_space >
constexpr auto query(get_graph_t) const &noexcept -> const typename state_t::graph_t &
constexpr auto query(Impl::get_exec_t) const noexcept -> decltype(auto) requires(!is_graph_create)
State(const Kokkos::Impl::DeviceHandle< Exec > &)
Kokkos::Experimental::Graph< Exec > graph_t
GraphComposition::Attach graph_composition_policy_t
typename graph_t::root_t root_t
const device_handle_t & get_device_handle() const
State(const device_handle_t &device_handle)
Kokkos::Impl::DeviceHandle< Exec > device_handle_t
const root_t & get_root_node() const
Kokkos::Experimental::Graph< Exec > graph_t
GraphComposition::Create graph_composition_policy_t
Wrap a Kokkos execution space to make it cheap to copy/move in new environments.
Receiver for an object parent_op that implements complete.