From 7450cc998b92c82bfbddaa25b889398ae308bcfb Mon Sep 17 00:00:00 2001 From: Eric Niebler Date: Tue, 18 Aug 2026 16:27:53 -0700 Subject: [PATCH 1/2] fix the attributes of the `let_` senders --- include/nvexec/multi_gpu_context.cuh | 61 +-- include/nvexec/stream/common.cuh | 9 +- include/nvexec/stream/continues_on.cuh | 5 +- include/nvexec/stream/launch.cuh | 2 +- include/nvexec/stream_context.cuh | 44 +-- .../__detail/__completion_behavior.hpp | 10 +- include/stdexec/__detail/__let.hpp | 359 +++++++++--------- test/nvexec/bulk.cpp | 23 +- test/nvexec/continues_on.cpp | 5 +- test/nvexec/let_error.cpp | 40 +- test/nvexec/let_value.cpp | 2 +- .../stdexec/algos/adaptors/test_let_error.cpp | 12 +- .../algos/adaptors/test_let_stopped.cpp | 6 +- .../queries/test_get_completion_behavior.cpp | 4 +- test/test_common/schedulers.hpp | 2 +- 15 files changed, 323 insertions(+), 261 deletions(-) diff --git a/include/nvexec/multi_gpu_context.cuh b/include/nvexec/multi_gpu_context.cuh index 3f5940693..b4059603e 100644 --- a/include/nvexec/multi_gpu_context.cuh +++ b/include/nvexec/multi_gpu_context.cuh @@ -29,7 +29,8 @@ namespace nv::execution { namespace _strm { - struct multi_gpu_stream_scheduler : private stream_scheduler_env + struct multi_gpu_stream_scheduler + : private stream_scheduler_attrs { using scheduler_concept = STDEXEC::scheduler_tag; @@ -49,7 +50,7 @@ namespace nv::execution return sender{num_devices_, ctx_}; } - using stream_scheduler_env::query; + using stream_scheduler_attrs::query; private: template @@ -110,12 +111,37 @@ namespace nv::execution struct sender : stream_sender_base { + private: + struct attrs + { + template CompletionSignal> + [[nodiscard]] + auto query(get_completion_scheduler_t) const noexcept + -> multi_gpu_stream_scheduler + { + return multi_gpu_stream_scheduler{num_devices_, ctx_}; + } + + template CompletionSignal> + [[nodiscard]] + auto query(get_completion_domain_t) const noexcept -> stream_domain + { + return {}; + } + + int num_devices_; + context ctx_; + }; + + attrs attrs_; + + public: using completion_signatures = STDEXEC::completion_signatures; STDEXEC_ATTRIBUTE(host, device) explicit sender(int num_devices, context ctx) noexcept - : env_{.num_devices_ = num_devices, .ctx_ = ctx} + : attrs_{.num_devices_ = num_devices, .ctx_ = ctx} {} template @@ -126,27 +152,10 @@ namespace nv::execution } [[nodiscard]] - auto get_env() const noexcept -> decltype(auto) + auto get_env() const noexcept -> attrs const & { - return (env_); + return attrs_; } - - private: - struct attrs - { - template - [[nodiscard]] - auto query(get_completion_scheduler_t, __ignore = {}) const noexcept - -> multi_gpu_stream_scheduler - { - return multi_gpu_stream_scheduler{num_devices_, ctx_}; - } - - int num_devices_; - context ctx_; - }; - - attrs env_; }; public: @@ -154,14 +163,6 @@ namespace nv::execution int num_devices_{}; context ctx_; }; - - template <> - STDEXEC_ATTRIBUTE(nodiscard) - inline auto stream_scheduler_env::query( - get_completion_scheduler_t) const noexcept -> multi_gpu_stream_scheduler - { - return STDEXEC::__c_downcast(*this); - } } // namespace _strm using _strm::multi_gpu_stream_scheduler; diff --git a/include/nvexec/stream/common.cuh b/include/nvexec/stream/common.cuh index 6d4332814..14ccab1d4 100644 --- a/include/nvexec/stream/common.cuh +++ b/include/nvexec/stream/common.cuh @@ -429,10 +429,11 @@ namespace nv::execution using decayed_tuple_t = ::cuda::std::tuple<__decay_t...>; template - using variant_storage_t = - __for_each_completion_signature_t<__completion_signatures_of_t, - decayed_tuple_t, - __munique<__q<_nullable_variant_t>>::__f>; + using variant_storage_t = __for_each_completion_signature_t< + __completion_signatures_of_t, + decayed_tuple_t, + __munique< + __mbind_back_q<_nullable_variant_t, ::cuda::std::tuple>>::__f>; inline constexpr get_stream_provider_t get_stream_provider{}; diff --git a/include/nvexec/stream/continues_on.cuh b/include/nvexec/stream/continues_on.cuh index 2836312db..394749c26 100644 --- a/include/nvexec/stream/continues_on.cuh +++ b/include/nvexec/stream/continues_on.cuh @@ -239,9 +239,10 @@ namespace nv::execution::_strm template <__decay_copyable Self, class... Env> static consteval auto get_completion_signatures() - -> __completion_signatures_of_t<__copy_cvref_t, Env...> { - return {}; + return exec::concat_completion_signatures( + STDEXEC::get_completion_signatures<__copy_cvref_t, Env...>(), + STDEXEC::completion_signatures()); } [[nodiscard]] diff --git a/include/nvexec/stream/launch.cuh b/include/nvexec/stream/launch.cuh index 31f3c64e2..e294e723f 100644 --- a/include/nvexec/stream/launch.cuh +++ b/include/nvexec/stream/launch.cuh @@ -116,7 +116,7 @@ namespace nv::execution { if constexpr (__callable) { - return completion_signatures(); + return completion_signatures(); } else { diff --git a/include/nvexec/stream_context.cuh b/include/nvexec/stream_context.cuh index deb5fc4ef..0996e1cc9 100644 --- a/include/nvexec/stream_context.cuh +++ b/include/nvexec/stream_context.cuh @@ -48,26 +48,31 @@ namespace nv::execution { struct stream_scheduler; - template - struct stream_scheduler_env - { // NOLINT(bugprone-crtp-constructor-accessibility) + template + struct stream_scheduler_attrs + { STDEXEC_ATTRIBUTE(nodiscard) static auto query(get_forward_progress_guarantee_t) noexcept -> forward_progress_guarantee { return forward_progress_guarantee::weakly_parallel; } + template CompletionSignal> STDEXEC_ATTRIBUTE(nodiscard) - auto query(get_completion_scheduler_t) const noexcept -> StreamScheduler; + auto query(get_completion_scheduler_t) const noexcept -> StreamScheduler + { + return STDEXEC::__c_downcast(*this); + } + template CompletionSignal> STDEXEC_ATTRIBUTE(nodiscard) - constexpr auto query(get_completion_domain_t) const noexcept -> stream_domain + auto query(get_completion_domain_t) const noexcept -> stream_domain { return {}; } }; - struct stream_scheduler : private stream_scheduler_env + struct stream_scheduler : private stream_scheduler_attrs { using scheduler_concept = STDEXEC::scheduler_tag; @@ -85,7 +90,7 @@ namespace nv::execution return sender{ctx_}; } - using stream_scheduler_env::query; + using stream_scheduler_attrs::query; private: template @@ -103,7 +108,6 @@ namespace nv::execution private: friend stream_context; cudaStream_t stream_{}; - cudaError_t status_{cudaSuccess}; }; struct sender : stream_sender_base @@ -114,40 +118,38 @@ namespace nv::execution context ctx_; STDEXEC_ATTRIBUTE(nodiscard) - auto query(get_completion_scheduler_t, __ignore = {}) const noexcept - -> stream_scheduler + auto query(get_completion_scheduler_t) const noexcept -> stream_scheduler { return stream_scheduler{ctx_}; } STDEXEC_ATTRIBUTE(nodiscard) - constexpr auto query(get_completion_domain_t) const noexcept -> stream_domain + auto query(get_completion_domain_t) const noexcept -> stream_domain { return {}; } }; - attrs env_; + attrs attrs_; public: - using completion_signatures = - STDEXEC::completion_signatures; + using completion_signatures = STDEXEC::completion_signatures; STDEXEC_ATTRIBUTE(host, device) explicit sender(context ctx) noexcept - : env_{ctx} + : attrs_{ctx} {} template auto connect(Receiver rcvr) const & noexcept -> opstate { - return opstate(static_cast(rcvr), env_.ctx_); + return opstate(static_cast(rcvr), attrs_.ctx_); } STDEXEC_ATTRIBUTE(nodiscard) auto get_env() const noexcept -> attrs const & { - return (env_); + return attrs_; } }; @@ -155,14 +157,6 @@ namespace nv::execution // private: TODO context ctx_; }; - - template <> - STDEXEC_ATTRIBUTE(nodiscard) - inline auto stream_scheduler_env::query( - get_completion_scheduler_t) const noexcept -> stream_scheduler - { - return STDEXEC::__c_downcast(*this); - } } // namespace _strm using _strm::stream_scheduler; diff --git a/include/stdexec/__detail/__completion_behavior.hpp b/include/stdexec/__detail/__completion_behavior.hpp index c29f2ac07..479dff4d1 100644 --- a/include/stdexec/__detail/__completion_behavior.hpp +++ b/include/stdexec/__detail/__completion_behavior.hpp @@ -134,11 +134,17 @@ namespace STDEXEC struct __common_t { template <__behavior... _CSs> - requires(sizeof...(_CSs) > 0) STDEXEC_ATTRIBUTE(nodiscard, host, device) constexpr auto operator()(__constant_t<_CSs>... __cbs) const noexcept { - return (__cbs | ...); + if constexpr (sizeof...(_CSs) == 0) + { + return __completion_behavior::__unknown; + } + else + { + return (__cbs | ...); + } } }; diff --git a/include/stdexec/__detail/__let.hpp b/include/stdexec/__detail/__let.hpp index 509fe3a8b..41587142c 100644 --- a/include/stdexec/__detail/__let.hpp +++ b/include/stdexec/__detail/__let.hpp @@ -165,9 +165,10 @@ namespace STDEXEC using __sender_t = __invoke_result_t<_Fun, __decay_t<_Args>&...>; using __submit_t = __submit_result<__sender_t, __rcvr_env<_Receiver, _Env2>>; - constexpr bool __nothrow_store = (__nothrow_decay_copyable<_Args> && ...); - constexpr bool __nothrow_invoke = __nothrow_invocable<_Fun, __decay_t<_Args>&...>; - constexpr bool __nothrow_submit = + STDEXEC_CONSTEXPR_LOCAL bool __nothrow_store = (__nothrow_decay_copyable<_Args> && ...); + STDEXEC_CONSTEXPR_LOCAL bool __nothrow_invoke = + __nothrow_invocable<_Fun, __decay_t<_Args>&...>; + STDEXEC_CONSTEXPR_LOCAL bool __nothrow_submit = __nothrow_constructible_from<__submit_t, __sender_t, __second_rcvr_t>; STDEXEC_TRY @@ -315,172 +316,185 @@ namespace STDEXEC __op_state_variant_t __storage_{__no_init}; }; - // // The set_value completions of: - // // - // // * a let_value sender are: - // // * the value completions of the secondary senders - // // - // // * a let_error sender are: - // // * the value completions of the predecessor sender - // // * the value completions of the secondary senders - // // - // // * a let_stopped sender are: - // // * the value completions of the predecessor sender - // // * the value completions of the secondary sender - // // - // // The set_error completions of: - // // - // // * a let_value sender are: - // // * the error completions of the predecessor sender - // // * the error completions of the secondary senders - // // * the value completions of the predecessor sender if decay copying the arguments can throw - // // - // // * a let_error sender are: - // // * the error completions of the secondary senders - // // * the error completions of the predecessor sender if decay copying the errors can throw - // // - // // * a let_stopped sender are: - // // * the error completions of the predecessor sender - // // * the error completions of the secondary senders - // // - // // The set_stopped completions of: - // // - // // * a let_value sender are: - // // * the stopped completions of the predecessor sender - // // * the stopped completions of the secondary senders - // // - // // * a let_error sender are: - // // * the stopped completions of the predecessor sender - // // * the stopped completions of the secondary senders - // // - // // * a let_stopped sender are: - // // * the stopped completions of the secondary sender - // // - // template - // struct __result_completion_behavior_fn - // { - // template - // [[nodiscard]] - // static constexpr auto __impl() noexcept - // { - // using __sndr_t = - // __minvoke_or_q<__invoke_result_t, __not_a_sender<>, _Fun, __decay_t<_Ts>&...>; - // return STDEXEC::__get_completion_behavior<_SetTag, __sndr_t, _JoinEnv2...>(); - // } - - // template - // using __f = decltype(__impl<_Ts...>()); - // }; - - // template - // struct __domain_transform_fn - // { - // template - // using __f = __completion_domain_of_t<_SetTag, - // __invoke_result_t<_Fun, __decay_t<_As>&...>, - // __result_env_t<_SetTag, _Sender, _Env>...>; - // }; - - // //! @tparam _LetTag The tag type for the let_ operation. - // //! @tparam _SetTag The completion signal of the let_ sender itself that is being - // //! queried. For example, you may be querying a let_value sender for its set_error - // //! completion domain. - // template - // [[nodiscard]] - // consteval auto __get_completion_domain() noexcept - // { - // if constexpr (sender_in<_Sndr, _Env...>) - // { - // using __domain_transform_fn = __let::__domain_transform_fn<_SetTag, _Fun, _Sndr, _Env...>; - // return __minvoke_or_q<__gather_completion_signatures_t, - // indeterminate_domain<>, - // __t<_LetTag>, - // __completion_signatures_of_t<_Sndr, _Env...>, - // __domain_transform_fn, - // __qq<__common_domain_t>>(); - // } - // else - // { - // return indeterminate_domain<>{}; - // } - // } - - // template - // using __let_completion_domain_t = __unless_one_of_t< - // __result_of<__let::__get_completion_domain<_LetTag, _SetTag, _Sndr, _Fun, _Env...>>, - // indeterminate_domain<>>; - - // template - // struct __attrs - // { - // using __set_tag_t = STDEXEC::__t<_LetTag>; - - // template - // constexpr auto query(get_completion_scheduler_t<_Tag>) const = delete; - - // template - // [[nodiscard]] - // constexpr auto query(get_completion_domain_t<__set_tag_t>, _Env const &...) const noexcept - // -> __ensure_valid_domain_t< - // __let_completion_domain_t<_LetTag, __set_tag_t, _Sndr, _Fun, _Env...>> - // { - // return {}; - // } - - // template <__one_of _Tag, class... _Env> - // requires(__has_nothrow_completions_t<__set_tag_t, _Sndr, _Fun, _Env>::value && ...) - // [[nodiscard]] - // constexpr auto query(get_completion_domain_t<_Tag>, _Env const &...) const noexcept - // -> __ensure_valid_domain_t< - // __common_domain_t<__completion_domain_of_t<_Tag, _Sndr, __fwd_env_t<_Env>...>, - // __let_completion_domain_t<_LetTag, _Tag, _Sndr, _Fun, _Env...>>> - // { - // return {}; - // } - - // template - // requires(!__has_nothrow_completions_t<__set_tag_t, _Sndr, _Fun, _Env>::value) - // [[nodiscard]] - // constexpr auto query(get_completion_domain_t, _Env const &) const noexcept - // -> __ensure_valid_domain_t< - // __common_domain_t<__completion_domain_of_t<__set_tag_t, _Sndr, __fwd_env_t<_Env>>, - // __completion_domain_of_t>, - // __let_completion_domain_t<_LetTag, set_error_t, _Sndr, _Fun, _Env>>> - // { - // return {}; - // } - - // template - // [[nodiscard]] - // constexpr auto query(__get_completion_behavior_t<__set_tag_t>, _Env const &...) const noexcept - // { - // if constexpr (sender_in<_Sndr, __fwd_env_t<_Env>...>) - // { - // // The completion behavior of let_value(sndr, fn) is the union of the completion - // // behavior of sndr and all the senders that fn can potentially produce. (MSVC - // // needs the constexpr computation broken up, hence the local variables.) - // using __transform_fn = - // __result_completion_behavior_fn<__set_tag_t, - // _Fun, - // __result_env_t<__set_tag_t, _Sndr, _Env>...>; - // using __completions_t = __completion_signatures_of_t<_Sndr, __fwd_env_t<_Env>...>; - - // constexpr auto __pred_behavior = - // STDEXEC::__get_completion_behavior<__set_tag_t, _Sndr, __fwd_env_t<_Env>...>(); - // constexpr auto __result_behaviors = __gather_completion_signatures_t< - // __set_tag_t, - // __completions_t, - // __transform_fn, - // __mbind_front_q<__call_result_t, __completion_behavior::__common_t>>(); - - // return __pred_behavior | __result_behaviors; - // } - // else - // { - // return __completion_behavior::__unknown; - // } - // } - // }; + // The set_value completions of: + // + // * a let_value sender are: + // * the value completions of the secondary senders + // + // * a let_error sender are: + // * the value completions of the predecessor sender + // * the value completions of the secondary senders + // + // * a let_stopped sender are: + // * the value completions of the predecessor sender + // * the value completions of the secondary sender + // + // The set_error completions of: + // + // * a let_value sender are: + // * the error completions of the predecessor sender + // * the error completions of the secondary senders + // * the value completions of the predecessor sender if decay copying the arguments can throw + // + // * a let_error sender are: + // * the error completions of the secondary senders + // * the error completions of the predecessor sender if decay copying the errors can throw + // + // * a let_stopped sender are: + // * the error completions of the predecessor sender + // * the error completions of the secondary senders + // + // The set_stopped completions of: + // + // * a let_value sender are: + // * the stopped completions of the predecessor sender + // * the stopped completions of the secondary senders + // + // * a let_error sender are: + // * the stopped completions of the predecessor sender + // * the stopped completions of the secondary senders + // + // * a let_stopped sender are: + // * the stopped completions of the secondary sender + // + template + struct __result_completion_behavior_fn + { + template + [[nodiscard]] + static constexpr auto __impl() noexcept + { + using __sndr_t = + __minvoke_or_q<__invoke_result_t, __not_a_sender<>, _Fun, __decay_t<_Ts>&...>; + return STDEXEC::__get_completion_behavior<_SetTag, __sndr_t, _JoinEnv2...>(); + } + + template + using __f = decltype(__impl<_Ts...>()); + }; + + template + struct __domain_transform_fn + { + template + using __f = __completion_domain_of_t<_SetTag, + __invoke_result_t<_Fun, __decay_t<_As>&...>, + __result_env_t<_SetTag, _Sender, _Env>...>; + }; + + //! @tparam _LetTag The tag type for the let_ operation. + //! @tparam _SetTag The completion signal of the let_ sender itself that is being + //! queried. For example, you may be querying a let_value sender for its set_error + //! completion domain. + template + [[nodiscard]] + consteval auto __get_completion_domain() noexcept + { + if constexpr (sender_in<_Sndr, _Env...>) + { + using __domain_transform_fn = __let::__domain_transform_fn<_SetTag, _Fun, _Sndr, _Env...>; + return __minvoke_or_q<__gather_completion_signatures_t, + indeterminate_domain<>, + __t<_LetTag>, + __completion_signatures_of_t<_Sndr, _Env...>, + __domain_transform_fn, + __qq<__common_domain_t>>(); + } + else + { + return indeterminate_domain<>{}; + } + } + + template + using __let_completion_domain_t = __unless_one_of_t< + decltype(__let::__get_completion_domain<_LetTag, _SetTag, _Sndr, _Fun, _Env...>()), + indeterminate_domain<>>; + + template + struct __attrs + { + using __set_tag_t = STDEXEC::__t<_LetTag>; + + template + constexpr auto query(get_completion_scheduler_t<_Tag>) const = delete; + + template + [[nodiscard]] + constexpr auto query(get_completion_domain_t<__set_tag_t>, _Env const &...) const noexcept + -> __ensure_valid_domain_t< + __let_completion_domain_t<_LetTag, __set_tag_t, _Sndr, _Fun, _Env...>> + { + return {}; + } + + template <__one_of _Tag, class... _Env> + requires(__has_nothrow_completions_t<__set_tag_t, _Sndr, _Fun, _Env>::value && ...) + [[nodiscard]] + constexpr auto query(get_completion_domain_t<_Tag>, _Env const &...) const noexcept + -> __ensure_valid_domain_t< + __common_domain_t<__completion_domain_of_t<_Tag, _Sndr, __fwd_env_t<_Env>...>, + __let_completion_domain_t<_LetTag, _Tag, _Sndr, _Fun, _Env...>>> + { + return {}; + } + + template + requires(!__has_nothrow_completions_t<__set_tag_t, _Sndr, _Fun, _Env>::value) + [[nodiscard]] + constexpr auto query(get_completion_domain_t, _Env const &) const noexcept + -> __ensure_valid_domain_t< + __common_domain_t<__completion_domain_of_t<__set_tag_t, _Sndr, __fwd_env_t<_Env>>, + __completion_domain_of_t>, + __let_completion_domain_t<_LetTag, set_error_t, _Sndr, _Fun, _Env>>> + { + return {}; + } + + // This overload is needed so that let_stopped will look for customizations based on + // the set_stopped completion domain of the predecessor sender, not the set_value + // completion domain of the predecessor sender. Likewise for let_error and the + // set_error completion domain of the predecessor sender. + template + requires __callable, env_of_t<_Sndr>, _Env const &...> + [[nodiscard]] + constexpr auto query(get_completion_domain_t<>, _Env const &...) const noexcept + -> __call_result_t, env_of_t<_Sndr>, _Env const &...> + { + return {}; + } + + template + [[nodiscard]] + constexpr auto query(__get_completion_behavior_t<__set_tag_t>, _Env const &...) const noexcept + { + if constexpr (sender_in<_Sndr, __fwd_env_t<_Env>...>) + { + // The completion behavior of let_value(sndr, fn) is the union of the completion + // behavior of sndr and all the senders that fn can potentially produce. (MSVC + // needs the constexpr computation broken up, hence the local variables.) + using __transform_fn = + __result_completion_behavior_fn<__set_tag_t, + _Fun, + __result_env_t<__set_tag_t, _Sndr, _Env>...>; + using __completions_t = __completion_signatures_of_t<_Sndr, __fwd_env_t<_Env>...>; + + STDEXEC_CONSTEXPR_LOCAL auto __pred_behavior = + STDEXEC::__get_completion_behavior<__set_tag_t, _Sndr, __fwd_env_t<_Env>...>(); + STDEXEC_CONSTEXPR_LOCAL auto __result_behaviors = __gather_completion_signatures_t< + __set_tag_t, + __completions_t, + __transform_fn, + __mbind_front_q<__call_result_t, __completion_behavior::__common_t>>(); + + return __pred_behavior | __result_behaviors; + } + else + { + return __completion_behavior::__unknown; + } + } + }; //! Implementation of the `let_*_t` types, where `_SetTag` is, e.g., //! `set_value_t` for `let_value`. @@ -590,11 +604,10 @@ namespace STDEXEC } public: - static constexpr auto __get_attrs = - [](__ignore, __ignore, _Child const & __child) noexcept -> decltype(auto) + static constexpr auto __get_attrs = // + [](__ignore, _Fun const &, _Child const &) noexcept // { - // TODO(ericniebler): this needs a proper implementation - return __fwd_env(STDEXEC::get_env(__child)); + return __let::__attrs<_LetTag, _Child, _Fun>(); }; template diff --git a/test/nvexec/bulk.cpp b/test/nvexec/bulk.cpp index 35f5992f5..e5db94607 100644 --- a/test/nvexec/bulk.cpp +++ b/test/nvexec/bulk.cpp @@ -1,8 +1,12 @@ #include + +#include + +#include + #include #include "common.cuh" -#include "nvexec/stream_context.cuh" #include @@ -12,6 +16,21 @@ using nvexec::is_on_gpu; namespace { + using _attrs_base_t = + ex::env, nvexec::stream_scheduler>, + ex::prop, nvexec::stream_domain>>; + + struct _attrs : _attrs_base_t + {}; + + template + auto _proclaim_completes_on(nvexec::stream_scheduler sch) + { + return exec::write_attrs(_attrs{ + ex::env{ex::prop{ex::get_completion_scheduler, sch}, + ex::prop{ex::get_completion_domain, nvexec::stream_domain{}}} + }); + } TEST_CASE("nvexec bulk returns a sender", "[cuda][stream][adaptors][bulk]") { @@ -227,6 +246,8 @@ namespace [](std::size_t i, cuda::std::span out) { out[i] = (double) i; }) | STDEXEC::let_value([](cuda::std::span out) { return STDEXEC::just(out); }) + // proclaim that value completions will happen on the specified scheduler: + | ::_proclaim_completes_on(ctx.get_scheduler()) | STDEXEC::bulk(ex::par, nelems, [](std::size_t i, cuda::std::span out) diff --git a/test/nvexec/continues_on.cpp b/test/nvexec/continues_on.cpp index 69fdca48c..5f3fa718d 100644 --- a/test/nvexec/continues_on.cpp +++ b/test/nvexec/continues_on.cpp @@ -312,10 +312,13 @@ namespace template void record_streams_across(FirstSched first, SecondSched second, cudaStream_t* streams) { - auto sndr = STDEXEC::schedule(first) | STDEXEC::let_value([] { return nvexec::get_stream(); }) + auto sndr = STDEXEC::starts_on(first, nvexec::get_stream()) | STDEXEC::then([streams](cudaStream_t stream) { streams[0] = stream; }) | STDEXEC::continues_on(second) | STDEXEC::let_value([] { return nvexec::get_stream(); }) + // proclaim that value completions will happen on the specified scheduler: + | exec::write_attrs( + STDEXEC::prop{STDEXEC::get_completion_scheduler, second}) | STDEXEC::then([streams](cudaStream_t stream) { streams[1] = stream; }); STDEXEC::sync_wait(std::move(sndr)); diff --git a/test/nvexec/let_error.cpp b/test/nvexec/let_error.cpp index 22a937685..708650630 100644 --- a/test/nvexec/let_error.cpp +++ b/test/nvexec/let_error.cpp @@ -1,9 +1,12 @@ -#include #include + +#include + +#include + #include #include "common.cuh" -#include "nvexec/stream_context.cuh" namespace ex = STDEXEC; @@ -11,6 +14,21 @@ using nvexec::is_on_gpu; namespace { + using _attrs_base_t = + ex::env, nvexec::stream_scheduler>, + ex::prop, nvexec::stream_domain>>; + + struct _attrs : _attrs_base_t + {}; + + template + auto _proclaim_completes_on(nvexec::stream_scheduler sch) + { + return exec::write_attrs(_attrs{ + ex::env{ex::prop{ex::get_completion_scheduler, sch}, + ex::prop{ex::get_completion_domain, nvexec::stream_domain{}}} + }); + } TEST_CASE("nvexec let_error returns a sender", "[cuda][stream][adaptors][let_error]") { @@ -38,10 +56,10 @@ namespace flags.set(); } - return ex::just() - | exec::write_attrs(ex::prop{ex::get_domain, nvexec::stream_domain()}); - }); - STDEXEC::sync_wait(std::move(snd)); + return ex::just(); + }) + | ::_proclaim_completes_on(stream_ctx.get_scheduler()); + ex::sync_wait(std::move(snd)); REQUIRE(flags_storage.all_set_once()); } @@ -56,16 +74,16 @@ namespace auto snd = ex::just_error(42) | ex::continues_on(stream_ctx.get_scheduler()) | ex::let_error( - [flags](int err) + [flags, sch = stream_ctx.get_scheduler()](int err) { if (is_on_gpu() && err == 42) { flags.set(0); } - return ex::just() - | exec::write_attrs(ex::prop{ex::get_domain, nvexec::stream_domain()}); + return ex::just(); }) + | ::_proclaim_completes_on(stream_ctx.get_scheduler()) | a_sender( [flags] { @@ -74,7 +92,7 @@ namespace flags.set(1); } }); - STDEXEC::sync_wait(std::move(snd)); + ex::sync_wait(std::move(snd)); REQUIRE(flags_storage.all_set_once()); } @@ -98,7 +116,7 @@ namespace return ex::schedule(sch); }); - STDEXEC::sync_wait(std::move(snd)); + ex::sync_wait(std::move(snd)); REQUIRE(flags_storage.all_set_once()); } diff --git a/test/nvexec/let_value.cpp b/test/nvexec/let_value.cpp index a9345f636..f377dd4d6 100644 --- a/test/nvexec/let_value.cpp +++ b/test/nvexec/let_value.cpp @@ -165,7 +165,7 @@ namespace flags_storage_t flags_storage{}; auto flags = flags_storage.get(); - auto snd = ex::schedule(sch) | ex::let_value([] { return nvexec::get_stream(); }) + auto snd = ex::starts_on(sch, nvexec::get_stream()) | ex::then( [flags](cudaStream_t stream) { diff --git a/test/stdexec/algos/adaptors/test_let_error.cpp b/test/stdexec/algos/adaptors/test_let_error.cpp index 494c28cb3..c7d6edf23 100644 --- a/test/stdexec/algos/adaptors/test_let_error.cpp +++ b/test/stdexec/algos/adaptors/test_let_error.cpp @@ -15,6 +15,7 @@ */ #include +#define STDEXEC_PARALLEL_SCHEDULER_HEADER_ONLY 1 #include #include @@ -403,11 +404,12 @@ namespace TEST_CASE("let_error can be customized", "[adaptors][let_error]") { - basic_inline_scheduler sched{}; + auto attrs = ex::prop{ex::get_completion_domain, let_error_test_domain{}}; - // The customization will return a different value - auto snd = ex::just(std::string{"hello"}) - | ex::let_error([](std::exception_ptr) { return ex::just(std::string{"err"}); }); - wait_for_value(ex::starts_on(sched, std::move(snd)), std::string{"what error?"}); + // The customization will return a different stopped + auto snd = ex::schedule(ex::get_parallel_scheduler()) // + | exec::write_attrs(attrs) // + | ex::let_error([](std::exception_ptr) { return ex::just(std::string{"stopped"}); }); + wait_for_value(std::move(snd), std::string{"what error?"}); } } // namespace diff --git a/test/stdexec/algos/adaptors/test_let_stopped.cpp b/test/stdexec/algos/adaptors/test_let_stopped.cpp index 33b3988ad..f1dbcc27e 100644 --- a/test/stdexec/algos/adaptors/test_let_stopped.cpp +++ b/test/stdexec/algos/adaptors/test_let_stopped.cpp @@ -15,6 +15,7 @@ */ #include +#define STDEXEC_PARALLEL_SCHEDULER_HEADER_ONLY 1 #include #include @@ -232,10 +233,11 @@ namespace TEST_CASE("let_stopped can be customized", "[adaptors][let_stopped]") { - basic_inline_scheduler sched; + auto attrs = ex::prop{ex::get_completion_domain, let_stopped_test_domain{}}; // The customization will return a different stopped - auto snd = ex::just(std::string{"hello"}) | ex::continues_on(sched) + auto snd = ex::schedule(ex::get_parallel_scheduler()) // + | exec::write_attrs(attrs) // | ex::let_stopped([] { return ex::just(std::string{"stopped"}); }); wait_for_value(std::move(snd), std::string{"Don't stop me now"}); } diff --git a/test/stdexec/queries/test_get_completion_behavior.cpp b/test/stdexec/queries/test_get_completion_behavior.cpp index bd0a2c360..2c34ce41f 100644 --- a/test/stdexec/queries/test_get_completion_behavior.cpp +++ b/test/stdexec/queries/test_get_completion_behavior.cpp @@ -95,7 +95,7 @@ inline constexpr auto __reschedule_on_current = ex::get_scheduler() | ex::let_va // naming it fails the modular and nvc++ configurations. static_assert(ex::__completes_inline, - ex::env<>>); + ex::prop>); static_assert(!ex::__completes_inline, - ex::env<>>); + ex::prop>); diff --git a/test/test_common/schedulers.hpp b/test/test_common/schedulers.hpp index 3cac6f974..c7bfdf0fd 100644 --- a/test/test_common/schedulers.hpp +++ b/test/test_common/schedulers.hpp @@ -45,7 +45,7 @@ namespace template struct sched_attrs { - sched_attrs(Scheduler sched, Tags...) + explicit sched_attrs(Scheduler sched, Tags...) : scheduler_(std::move(sched)) {} From 6c4b10169fcb04e3091709092d071b87dcf18536 Mon Sep 17 00:00:00 2001 From: Eric Niebler Date: Sat, 14 Nov 2026 12:32:56 -0800 Subject: [PATCH 2/2] `let_*` attributes propagate predecessor scheduler when secondary senders are affine --- include/nvexec/stream/common.cuh | 26 +- include/stdexec/__detail/__let.hpp | 233 ++++++++++++++++-- test/nvexec/bulk.cpp | 18 -- test/nvexec/common.cuh | 25 ++ test/nvexec/let_error.cpp | 23 +- test/nvexec/let_stopped.cpp | 3 +- test/nvexec/let_value.cpp | 3 +- .../stdexec/algos/adaptors/test_let_error.cpp | 52 ++++ .../algos/adaptors/test_let_stopped.cpp | 52 ++++ .../stdexec/algos/adaptors/test_let_value.cpp | 62 +++++ test/test_common/schedulers.hpp | 76 ++++++ 11 files changed, 514 insertions(+), 59 deletions(-) diff --git a/include/nvexec/stream/common.cuh b/include/nvexec/stream/common.cuh index 14ccab1d4..8aa4a0ed1 100644 --- a/include/nvexec/stream/common.cuh +++ b/include/nvexec/stream/common.cuh @@ -430,10 +430,13 @@ namespace nv::execution template using variant_storage_t = __for_each_completion_signature_t< - __completion_signatures_of_t, + // The variant must always be able to hold a `cudaError_t` error, since that is how + // stream receivers report `std::exception_ptr` errors. Adding it to the completion + // signatures *before* uniquing avoids duplicate alternatives. + __concat_completion_signatures_t<__completion_signatures_of_t, + completion_signatures>, decayed_tuple_t, - __munique< - __mbind_back_q<_nullable_variant_t, ::cuda::std::tuple>>::__f>; + __munique<__q<_nullable_variant_t>>::__f>; inline constexpr get_stream_provider_t get_stream_provider{}; @@ -662,6 +665,23 @@ namespace nv::execution template struct opstate_base : stream_opstate_base { + // Stream operation states complete their receiver from a kernel. Connecting one directly + // to the host sync_wait receiver is a mistake: it means the default sync_wait was selected + // instead of the stream scheduler's. That happens when the sender passed to sync_wait does + // not report a stream completion scheduler. + static_assert(stream_receiver + || !__is_instance_of, + "A stream sender was passed to sync_wait, but the sender does not report that " + "it completes " + "on a stream scheduler, so the default sync_wait was selected. This happens, " + "for example, " + "after let_value, let_error and let_stopped, whose completion scheduler " + "depends on the " + "sender their function returns. Pipe the sender through a write_attrs adaptor " + "that " + "proclaims its completion scheduler and domain before passing it to " + "sync_wait."); + using operation_state_concept = STDEXEC::operation_state_tag; using outer_env_t = env_of_t; using env_t = make_stream_env_t; diff --git a/include/stdexec/__detail/__let.hpp b/include/stdexec/__detail/__let.hpp index 41587142c..54aa70eff 100644 --- a/include/stdexec/__detail/__let.hpp +++ b/include/stdexec/__detail/__let.hpp @@ -128,8 +128,8 @@ namespace STDEXEC template using __has_nothrow_completions_t = __gather_completion_signatures_t< - completion_signatures_of_t<_Child, _Env>, _SetTag, + completion_signatures_of_t<_Child, _Env>, __has_nothrow_completions_fn<_Fn, __result_env_t<_SetTag, _Child, _Env>>, __qq<__mand_t>>; @@ -373,13 +373,17 @@ namespace STDEXEC using __f = decltype(__impl<_Ts...>()); }; - template + //! @tparam _LetSetTag The completion tag of the let_ sender's own channel (e.g. + //! `set_value_t` for let_value). The secondary senders are started from the predecessor's + //! `_LetSetTag` completions, so that determines their environment. + //! @tparam _SetTag The completion channel whose domain is being computed. + template struct __domain_transform_fn { template using __f = __completion_domain_of_t<_SetTag, __invoke_result_t<_Fun, __decay_t<_As>&...>, - __result_env_t<_SetTag, _Sender, _Env>...>; + __result_env_t<_LetSetTag, _Sender, _Env>...>; }; //! @tparam _LetTag The tag type for the let_ operation. @@ -392,7 +396,8 @@ namespace STDEXEC { if constexpr (sender_in<_Sndr, _Env...>) { - using __domain_transform_fn = __let::__domain_transform_fn<_SetTag, _Fun, _Sndr, _Env...>; + using __domain_transform_fn = + __let::__domain_transform_fn<__t<_LetTag>, _SetTag, _Fun, _Sndr, _Env...>; return __minvoke_or_q<__gather_completion_signatures_t, indeterminate_domain<>, __t<_LetTag>, @@ -411,13 +416,161 @@ namespace STDEXEC decltype(__let::__get_completion_domain<_LetTag, _SetTag, _Sndr, _Fun, _Env...>()), indeterminate_domain<>>; + //! For a given let_ sender and completion tag, summarizes the senders that the let_ + //! function can return: whether any of them can complete with `_Tag`, and whether all + //! of those complete on the execution context on which they were started. + template + struct __secondary_summary + { + static constexpr bool __sends = _Sends; + static constexpr bool __affine = _Affine; + }; + + template + using __combine_summaries = + __secondary_summary<(_Summaries::__sends || ...), (_Summaries::__affine && ...)>; + + template + struct __secondary_summary_fn + { + template + [[nodiscard]] + static consteval auto __impl() noexcept + { + using __sndr_t = + __minvoke_or_q<__invoke_result_t, __not_a_sender<>, _Fun, __decay_t<_As>&...>; + if constexpr (__sends<_Tag, __sndr_t, __result_env_t<_SetTag, _Sender, _Env>...>) + { + constexpr auto __cb = + STDEXEC::__get_completion_behavior<_Tag, + __sndr_t, + __result_env_t<_SetTag, _Sender, _Env>...>(); + return __secondary_summary(); + } + else + { + return __secondary_summary(); + } + } + + template + using __f = decltype(__impl<_As...>()); + }; + + template + using __secondary_summary_t = + __minvoke_or_q<__gather_completion_signatures_t, + __secondary_summary, // unknown => conservative + _SetTag, + __completion_signatures_of_t<_Sender, _Env...>, + __secondary_summary_fn<_Tag, _SetTag, _Fun, _Sender, _Env...>, + __qq<__combine_summaries>>; + template struct __attrs { using __set_tag_t = STDEXEC::__t<_LetTag>; - template - constexpr auto query(get_completion_scheduler_t<_Tag>) const = delete; + private: + //! True if invoking the function and connecting the resulting senders cannot throw, + //! for each of the environments (or for the empty environment if there are none). If + //! the predecessor's completions cannot be determined in an environment, we + //! conservatively assume that they can throw. + template + [[nodiscard]] + static consteval auto __nothrow_completions_in() noexcept -> bool + { + if constexpr (sender_in<_Sndr, _Env>) + { + return __has_nothrow_completions_t<__set_tag_t, _Sndr, _Fun, _Env>::value; + } + else + { + return false; + } + } + + template + [[nodiscard]] + static consteval auto __nothrow_completions() noexcept -> bool + { + if constexpr (sizeof...(_Env) == 0) + { + return __nothrow_completions_in>(); + } + else + { + return (__nothrow_completions_in<_Env>() && ...); + } + } + + //! Where do `_Tag` completions of the let_ sender happen? Completions can come from: + //! - the predecessor, passed through unchanged (only for tags other than the let_ tag). + //! These happen on the predecessor's `_Tag` completion scheduler. + //! - the secondary senders (and, for `set_error_t`, exceptions thrown while invoking + //! the function or connecting the secondary sender). These are started on the + //! predecessor's `__set_tag_t` completion scheduler, so if the secondary senders + //! complete on the context they were started on, they too complete there. + //! + //! Completion schedulers are runtime values, so we can only report one when there is + //! exactly one source of `_Tag` completions. The answer depends on the environment (it + //! determines the completion behavior and domain of the secondary senders), so no + //! completion scheduler is reported when queried without one. + //! + //! @return 0 if there is no `_Tag` completion, 1 if `_Tag` completions come from the + //! predecessor's `_Tag` completions, 2 if they come from the predecessor's + //! `__set_tag_t` completion scheduler, and 3 if the source is ambiguous or unknown. + template + [[nodiscard]] + static consteval auto __scheduler_source() noexcept -> int + { + constexpr bool __pass = !__same_as<_Tag, __set_tag_t> + && __sends<_Tag, _Sndr, __fwd_env_t<_Env>...>; + + bool __invoked = false; + if constexpr (__sends<__set_tag_t, _Sndr, __fwd_env_t<_Env>...>) + { + using __summary_t = __secondary_summary_t<_Tag, __set_tag_t, _Fun, _Sndr, _Env...>; + constexpr bool __throws = __same_as<_Tag, set_error_t> + && !__nothrow_completions<_Env...>(); + if constexpr (__summary_t::__sends && !__summary_t::__affine) + { + return 3; + } + __invoked = __summary_t::__sends || __throws; + } + + return (__pass && __invoked) ? 3 : __pass ? 1 : __invoked ? 2 : 0; + } + + public: + constexpr explicit __attrs(_Sndr const & __sndr) noexcept + : __sndr_(__sndr) + {} + + //! The predecessor's completions are passed through unchanged. + template + requires(sizeof...(_Env) != 0) && (__scheduler_source<_Tag, _Env...>() == 1) + [[nodiscard]] + constexpr auto query(get_completion_scheduler_t<_Tag>, _Env const &... __env) const noexcept + -> __call_result_t, env_of_t<_Sndr>, __fwd_env_t<_Env>...> + { + return get_completion_scheduler<_Tag>(STDEXEC::get_env(__sndr_), __fwd_env(__env)...); + } + + //! The completions come from senders started from the predecessor's `__set_tag_t` + //! completion, which all complete on the context they were started on. + template + requires(sizeof...(_Env) != 0) && (__scheduler_source<_Tag, _Env...>() == 2) + [[nodiscard]] + constexpr auto query(get_completion_scheduler_t<_Tag>, _Env const &... __env) const noexcept + -> __call_result_t, + env_of_t<_Sndr>, + __fwd_env_t<_Env>...> + { + return get_completion_scheduler<__set_tag_t>(STDEXEC::get_env(__sndr_), + __fwd_env(__env)...); + } template [[nodiscard]] @@ -429,7 +582,7 @@ namespace STDEXEC } template <__one_of _Tag, class... _Env> - requires(__has_nothrow_completions_t<__set_tag_t, _Sndr, _Fun, _Env>::value && ...) + requires(__nothrow_completions<_Env...>()) [[nodiscard]] constexpr auto query(get_completion_domain_t<_Tag>, _Env const &...) const noexcept -> __ensure_valid_domain_t< @@ -439,14 +592,59 @@ namespace STDEXEC return {}; } - template - requires(!__has_nothrow_completions_t<__set_tag_t, _Sndr, _Fun, _Env>::value) + template + requires(!__nothrow_completions<_Env...>()) + [[nodiscard]] + constexpr auto query(get_completion_domain_t, _Env const &...) const noexcept + -> __ensure_valid_domain_t< + __common_domain_t<__completion_domain_of_t<__set_tag_t, _Sndr, __fwd_env_t<_Env>...>, + __completion_domain_of_t...>, + __let_completion_domain_t<_LetTag, set_error_t, _Sndr, _Fun, _Env...>>> + { + return {}; + } + + // The value completions of a let_error or let_stopped sender are the value + // completions of the predecessor, passed through unchanged, plus the value + // completions of the secondary senders (which only exist if the predecessor can + // complete with the let_ sender's own completion tag). For let_value, the value + // channel is handled by the first overload above. + template <__same_as _Tag, class... _Env> + requires(!__same_as<__set_tag_t, set_value_t>) + && __sends...> + && __never_sends<__set_tag_t, _Sndr, __fwd_env_t<_Env>...> + [[nodiscard]] + constexpr auto query(get_completion_domain_t<_Tag>, _Env const &...) const noexcept + -> __ensure_valid_domain_t< + __completion_domain_of_t...>> + { + return {}; + } + + //! @overload + template <__same_as _Tag, class... _Env> + requires(!__same_as<__set_tag_t, set_value_t>) + && __sends...> + && (!__never_sends<__set_tag_t, _Sndr, __fwd_env_t<_Env>...>) [[nodiscard]] - constexpr auto query(get_completion_domain_t, _Env const &) const noexcept + constexpr auto query(get_completion_domain_t<_Tag>, _Env const &...) const noexcept -> __ensure_valid_domain_t< - __common_domain_t<__completion_domain_of_t<__set_tag_t, _Sndr, __fwd_env_t<_Env>>, - __completion_domain_of_t>, - __let_completion_domain_t<_LetTag, set_error_t, _Sndr, _Fun, _Env>>> + __common_domain_t<__completion_domain_of_t...>, + __let_completion_domain_t<_LetTag, set_value_t, _Sndr, _Fun, _Env...>>> + { + return {}; + } + + //! @overload + //! The predecessor never sends values, so only the secondary senders contribute. + template <__same_as _Tag, class... _Env> + requires(!__same_as<__set_tag_t, set_value_t>) + && (!__sends...>) + && (!__never_sends<__set_tag_t, _Sndr, __fwd_env_t<_Env>...>) + [[nodiscard]] + constexpr auto query(get_completion_domain_t<_Tag>, _Env const &...) const noexcept + -> __ensure_valid_domain_t< + __let_completion_domain_t<_LetTag, set_value_t, _Sndr, _Fun, _Env...>> { return {}; } @@ -494,6 +692,9 @@ namespace STDEXEC return __completion_behavior::__unknown; } } + + private: + _Sndr const & __sndr_; }; //! Implementation of the `let_*_t` types, where `_SetTag` is, e.g., @@ -604,10 +805,10 @@ namespace STDEXEC } public: - static constexpr auto __get_attrs = // - [](__ignore, _Fun const &, _Child const &) noexcept // + static constexpr auto __get_attrs = // + [](__ignore, _Fun const &, _Child const & __child) noexcept // { - return __let::__attrs<_LetTag, _Child, _Fun>(); + return __let::__attrs<_LetTag, _Child, _Fun>(__child); }; template diff --git a/test/nvexec/bulk.cpp b/test/nvexec/bulk.cpp index e5db94607..33826e4f9 100644 --- a/test/nvexec/bulk.cpp +++ b/test/nvexec/bulk.cpp @@ -16,22 +16,6 @@ using nvexec::is_on_gpu; namespace { - using _attrs_base_t = - ex::env, nvexec::stream_scheduler>, - ex::prop, nvexec::stream_domain>>; - - struct _attrs : _attrs_base_t - {}; - - template - auto _proclaim_completes_on(nvexec::stream_scheduler sch) - { - return exec::write_attrs(_attrs{ - ex::env{ex::prop{ex::get_completion_scheduler, sch}, - ex::prop{ex::get_completion_domain, nvexec::stream_domain{}}} - }); - } - TEST_CASE("nvexec bulk returns a sender", "[cuda][stream][adaptors][bulk]") { nvexec::stream_context stream_ctx{}; @@ -246,8 +230,6 @@ namespace [](std::size_t i, cuda::std::span out) { out[i] = (double) i; }) | STDEXEC::let_value([](cuda::std::span out) { return STDEXEC::just(out); }) - // proclaim that value completions will happen on the specified scheduler: - | ::_proclaim_completes_on(ctx.get_scheduler()) | STDEXEC::bulk(ex::par, nelems, [](std::size_t i, cuda::std::span out) diff --git a/test/nvexec/common.cuh b/test/nvexec/common.cuh index ad81ec050..289c15e02 100644 --- a/test/nvexec/common.cuh +++ b/test/nvexec/common.cuh @@ -18,6 +18,7 @@ #pragma once +#include "../../include/exec/env.hpp" #include "../../include/exec/receiver_adaptor.hpp" #include "../../include/nvexec/detail/throw_on_cuda_error.cuh" #include "../../include/stdexec/execution.hpp" @@ -363,6 +364,30 @@ namespace }; static_assert(!std::is_trivially_copyable_v); + + // The completion scheduler of a let_value/let_error/let_stopped sender is not known, since + // it depends on the sender returned by the user's function. This proclaims that the value + // (or other `SetTag`) completions of the sender it is piped from will happen on `sch`, which + // lets the stream scheduler's customizations (e.g. sync_wait) be selected. + template + struct _proclaimed_attrs + : STDEXEC::env< + STDEXEC::prop, Sch>, + STDEXEC::prop< + STDEXEC::get_completion_domain_t, + STDEXEC::__call_result_t, Sch>>> + {}; + + template + auto _proclaim_completes_on(Sch sch) + { + using domain_t = + STDEXEC::__call_result_t, Sch>; + return exec::write_attrs(_proclaimed_attrs{ + STDEXEC::env{STDEXEC::prop{STDEXEC::get_completion_scheduler, sch}, + STDEXEC::prop{STDEXEC::get_completion_domain, domain_t{}}} + }); + } } // namespace STDEXEC_PRAGMA_POP() diff --git a/test/nvexec/let_error.cpp b/test/nvexec/let_error.cpp index 708650630..7fd2e9efc 100644 --- a/test/nvexec/let_error.cpp +++ b/test/nvexec/let_error.cpp @@ -14,22 +14,6 @@ using nvexec::is_on_gpu; namespace { - using _attrs_base_t = - ex::env, nvexec::stream_scheduler>, - ex::prop, nvexec::stream_domain>>; - - struct _attrs : _attrs_base_t - {}; - - template - auto _proclaim_completes_on(nvexec::stream_scheduler sch) - { - return exec::write_attrs(_attrs{ - ex::env{ex::prop{ex::get_completion_scheduler, sch}, - ex::prop{ex::get_completion_domain, nvexec::stream_domain{}}} - }); - } - TEST_CASE("nvexec let_error returns a sender", "[cuda][stream][adaptors][let_error]") { nvexec::stream_context stream_ctx{}; @@ -57,8 +41,7 @@ namespace } return ex::just(); - }) - | ::_proclaim_completes_on(stream_ctx.get_scheduler()); + }); ex::sync_wait(std::move(snd)); REQUIRE(flags_storage.all_set_once()); @@ -83,7 +66,6 @@ namespace return ex::just(); }) - | ::_proclaim_completes_on(stream_ctx.get_scheduler()) | a_sender( [flags] { @@ -115,7 +97,8 @@ namespace } return ex::schedule(sch); - }); + }) + | ::_proclaim_completes_on(sch); ex::sync_wait(std::move(snd)); REQUIRE(flags_storage.all_set_once()); diff --git a/test/nvexec/let_stopped.cpp b/test/nvexec/let_stopped.cpp index a1348eaab..54a5b508f 100644 --- a/test/nvexec/let_stopped.cpp +++ b/test/nvexec/let_stopped.cpp @@ -93,7 +93,8 @@ namespace } return ex::schedule(sch); - }); + }) + | ::_proclaim_completes_on(sch); STDEXEC::sync_wait(std::move(snd)); diff --git a/test/nvexec/let_value.cpp b/test/nvexec/let_value.cpp index f377dd4d6..85c538199 100644 --- a/test/nvexec/let_value.cpp +++ b/test/nvexec/let_value.cpp @@ -152,7 +152,8 @@ namespace } return ex::schedule(sch); - }); + }) + | ::_proclaim_completes_on(sch); STDEXEC::sync_wait(std::move(snd)); REQUIRE(flags_storage.all_set_once()); diff --git a/test/stdexec/algos/adaptors/test_let_error.cpp b/test/stdexec/algos/adaptors/test_let_error.cpp index c7d6edf23..6aa83b332 100644 --- a/test/stdexec/algos/adaptors/test_let_error.cpp +++ b/test/stdexec/algos/adaptors/test_let_error.cpp @@ -412,4 +412,56 @@ namespace | ex::let_error([](std::exception_ptr) { return ex::just(std::string{"stopped"}); }); wait_for_value(std::move(snd), std::string{"what error?"}); } + + struct let_error_value_domain + {}; + + TEST_CASE("let_error has a value completion domain", "[adaptors][let_error]") + { + // The value completions of a let_error sender are the predecessor's value completions, + // passed through, plus the value completions of the secondary senders. They all + // complete on sch, so that is the domain of the let_error sender's value channel. + basic_inline_scheduler sch{}; + auto pred = ex::continues_on(ex::just(1), sch); + auto snd = ex::let_error(pred, [sch](auto) { return ex::continues_on(ex::just(1), sch); }); + auto domain = ex::get_env(snd).query(ex::get_completion_domain, ex::env<>{}); + static_assert(std::same_as); + } + + TEST_CASE("let_error value completion domain includes the secondary senders'", + "[adaptors][let_error]") + { + // Here the predecessor can complete with the let_error tag, so the secondary senders + // can be invoked and also contribute value completions. + basic_inline_scheduler sch{}; + auto pred = ex::continues_on(ex::just_error(1), sch); + auto snd = ex::let_error(pred, [sch](auto) { return ex::continues_on(ex::just(1), sch); }); + auto domain = ex::get_env(snd).query(ex::get_completion_domain, ex::env<>{}); + static_assert(std::same_as); + } + + TEST_CASE("let_error reports the predecessor's value scheduler when the secondary senders " + "complete inline and the predecessor never completes with the let_error tag", + "[adaptors][let_error]") + { + runtime_id_scheduler sch{42}; + auto snd = ex::let_error(ex::continues_on(ex::just(1), sch), + [](auto) noexcept { return ex::just(2); }); + auto csch = ex::get_completion_scheduler(ex::get_env(snd), ex::env<>{}); + CHECK(csch.id_ == 42); + } + + TEST_CASE("let_error secondary senders are started in the environment of the predecessor's " + "let_error completion", + "[adaptors][let_error]") + { + // The predecessor has no value completions. just(2) is started from the predecessor's + // let_error completion, which happens on sch, so its (and the let_error sender's) value + // domain is sch's domain. + basic_inline_scheduler sch{}; + auto snd = ex::let_error(ex::continues_on(ex::just_error(1), sch), + [](auto) noexcept { return ex::just(2); }); + auto domain = ex::get_env(snd).query(ex::get_completion_domain, ex::env<>{}); + static_assert(std::same_as); + } } // namespace diff --git a/test/stdexec/algos/adaptors/test_let_stopped.cpp b/test/stdexec/algos/adaptors/test_let_stopped.cpp index f1dbcc27e..7c317458c 100644 --- a/test/stdexec/algos/adaptors/test_let_stopped.cpp +++ b/test/stdexec/algos/adaptors/test_let_stopped.cpp @@ -241,4 +241,56 @@ namespace | ex::let_stopped([] { return ex::just(std::string{"stopped"}); }); wait_for_value(std::move(snd), std::string{"Don't stop me now"}); } + + struct let_stopped_value_domain + {}; + + TEST_CASE("let_stopped has a value completion domain", "[adaptors][let_stopped]") + { + // The value completions of a let_stopped sender are the predecessor's value completions, + // passed through, plus the value completions of the secondary senders. They all + // complete on sch, so that is the domain of the let_stopped sender's value channel. + basic_inline_scheduler sch{}; + auto pred = ex::continues_on(ex::just(1), sch); + auto snd = ex::let_stopped(pred, [sch] { return ex::continues_on(ex::just(1), sch); }); + auto domain = ex::get_env(snd).query(ex::get_completion_domain, ex::env<>{}); + static_assert(std::same_as); + } + + TEST_CASE("let_stopped value completion domain includes the secondary senders'", + "[adaptors][let_stopped]") + { + // Here the predecessor can complete with the let_stopped tag, so the secondary senders + // can be invoked and also contribute value completions. + basic_inline_scheduler sch{}; + auto pred = ex::continues_on(ex::just_stopped(), sch); + auto snd = ex::let_stopped(pred, [sch] { return ex::continues_on(ex::just(1), sch); }); + auto domain = ex::get_env(snd).query(ex::get_completion_domain, ex::env<>{}); + static_assert(std::same_as); + } + + TEST_CASE("let_stopped reports the predecessor's value scheduler when the secondary senders " + "complete inline and the predecessor never completes with the let_stopped tag", + "[adaptors][let_stopped]") + { + runtime_id_scheduler sch{42}; + auto snd = ex::let_stopped(ex::continues_on(ex::just(1), sch), + []() noexcept { return ex::just(2); }); + auto csch = ex::get_completion_scheduler(ex::get_env(snd), ex::env<>{}); + CHECK(csch.id_ == 42); + } + + TEST_CASE("let_stopped secondary senders are started in the environment of the predecessor's " + "let_stopped completion", + "[adaptors][let_stopped]") + { + // The predecessor has no value completions. just(2) is started from the predecessor's + // let_stopped completion, which happens on sch, so its (and the let_stopped sender's) value + // domain is sch's domain. + basic_inline_scheduler sch{}; + auto snd = ex::let_stopped(ex::continues_on(ex::just_stopped(), sch), + []() noexcept { return ex::just(2); }); + auto domain = ex::get_env(snd).query(ex::get_completion_domain, ex::env<>{}); + static_assert(std::same_as); + } } // namespace diff --git a/test/stdexec/algos/adaptors/test_let_value.cpp b/test/stdexec/algos/adaptors/test_let_value.cpp index c135e670f..bf33f64b6 100644 --- a/test/stdexec/algos/adaptors/test_let_value.cpp +++ b/test/stdexec/algos/adaptors/test_let_value.cpp @@ -601,4 +601,66 @@ namespace STATIC_CHECK(test_receiver_may_refer_to_opstate_of_incomplete_type()); } + template + concept has_completion_scheduler_for = requires(Sender const & snd) { + ex::get_completion_scheduler(ex::get_env(snd), ex::env<>{}); + }; + + TEST_CASE("let_value reports the predecessor's value scheduler when the secondary senders " + "complete inline", + "[adaptors][let_value]") + { + runtime_id_scheduler sch{42}; + auto snd = ex::let_value(ex::continues_on(ex::just(1), sch), + [](int) noexcept { return ex::just(2); }); + auto csch = ex::get_completion_scheduler(ex::get_env(snd), ex::env<>{}); + CHECK(csch.id_ == 42); + } + + TEST_CASE("let_value has no completion scheduler when the secondary senders may complete " + "elsewhere", + "[adaptors][let_value]") + { + runtime_id_scheduler sch{42}; + impulse_scheduler other{}; + auto snd = ex::let_value(ex::continues_on(ex::just(1), sch), + [other](int) noexcept { return ex::schedule(other); }); + static_assert(!has_completion_scheduler_for); + (void) snd; + } + + TEST_CASE("let_value forwards the predecessor's error scheduler when only the predecessor " + "can fail", + "[adaptors][let_value]") + { + runtime_id_scheduler sch{42}; + // the predecessor has no value completions, so the function is never invoked + auto snd = ex::let_value(ex::continues_on(ex::just_error(1), sch), + []() noexcept { return ex::just(2); }); + auto csch = ex::get_completion_scheduler(ex::get_env(snd), ex::env<>{}); + CHECK(csch.id_ == 42); + static_assert(!has_completion_scheduler_for); + } + + TEST_CASE("let_value has no error completion scheduler when the predecessor's errors and the " + "function's exceptions come from different places", + "[adaptors][let_value]") + { + runtime_id_scheduler sch{42}; + // This predecessor can complete with a value or an error. + auto pred = ex::continues_on(ex::just(1) | ex::then([](int i) { return i; }), sch); + + // The function is nothrow and the secondary sender cannot fail, so errors can only come + // from the predecessor. + auto nothrow_snd = ex::let_value(pred, [](int) noexcept { return ex::just(2); }); + auto csch = ex::get_completion_scheduler(ex::get_env(nothrow_snd), + ex::env<>{}); + CHECK(csch.id_ == 42); + + // The function is potentially-throwing, so error completions can come from both the + // predecessor and from the invocation of the function. + auto throwing_snd = ex::let_value(pred, [](int) { return ex::just(2); }); + static_assert(!has_completion_scheduler_for); + static_assert(has_completion_scheduler_for); + } } // namespace diff --git a/test/test_common/schedulers.hpp b/test/test_common/schedulers.hpp index c7bfdf0fd..534c5c2c7 100644 --- a/test/test_common/schedulers.hpp +++ b/test/test_common/schedulers.hpp @@ -298,6 +298,82 @@ namespace }; }; + //! A scheduler with a runtime identity, whose schedule sender completes inline with a + //! value. Its completion scheduler for the value (and, if `Errors` is true, the error) + //! channel is itself. Useful for checking *which* scheduler a completion scheduler query + //! returns. + struct runtime_id_scheduler; + + struct runtime_id_scheduler_attrs + { + [[nodiscard]] + auto + query(ex::get_completion_scheduler_t) const noexcept -> runtime_id_scheduler; + + int id_; + }; + + template + struct runtime_id_scheduler_opstate : immovable + { + void start() & noexcept + { + ex::set_value(static_cast(rcvr_)); + } + + Receiver rcvr_; + }; + + struct runtime_id_scheduler_sender + { + using sender_concept = ex::sender_tag; + using completion_signatures = ex::completion_signatures; + + template + [[nodiscard]] + auto connect(Receiver rcvr) const -> runtime_id_scheduler_opstate + { + return {{}, static_cast(rcvr)}; + } + + [[nodiscard]] + auto get_env() const noexcept -> runtime_id_scheduler_attrs + { + return {id_}; + } + + int id_; + }; + + struct runtime_id_scheduler + { + using scheduler_concept = ex::scheduler_tag; + + int id_; + + auto operator==(runtime_id_scheduler const &) const noexcept -> bool = default; + + [[nodiscard]] + auto + query(ex::get_completion_scheduler_t) const noexcept -> runtime_id_scheduler + { + return *this; + } + + [[nodiscard]] + auto schedule() const noexcept -> runtime_id_scheduler_sender + { + return {id_}; + } + }; + + inline auto + runtime_id_scheduler_attrs::query(ex::get_completion_scheduler_t) const noexcept + -> runtime_id_scheduler + { + return runtime_id_scheduler{id_}; + } + using STDEXEC::inline_scheduler; template