From e2632e773ab3892a38e541dd4e6edb46e2bb260f Mon Sep 17 00:00:00 2001 From: Jacob Faibussowitsch Date: Tue, 14 Jul 2026 13:39:51 -0400 Subject: [PATCH] Add cudax::distributed and cudax::returned_to specifiers for cooperative algorithms --- .../cuda/experimental/__coop/any_of.cuh | 2 +- .../cuda/experimental/__coop/reduce.cuh | 2 +- .../experimental/__utility/broadcasted.cuh | 36 -------- .../experimental/__utility/result_policy.cuh | 85 +++++++++++++++++++ cudax/test/coop/reduce/this_block.cu | 3 + docs/cudax/Doxyfile | 3 +- 6 files changed, 92 insertions(+), 39 deletions(-) delete mode 100644 cudax/include/cuda/experimental/__utility/broadcasted.cuh create mode 100644 cudax/include/cuda/experimental/__utility/result_policy.cuh diff --git a/cudax/include/cuda/experimental/__coop/any_of.cuh b/cudax/include/cuda/experimental/__coop/any_of.cuh index aa47edd6adc..c00fa4c3df6 100644 --- a/cudax/include/cuda/experimental/__coop/any_of.cuh +++ b/cudax/include/cuda/experimental/__coop/any_of.cuh @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include diff --git a/cudax/include/cuda/experimental/__coop/reduce.cuh b/cudax/include/cuda/experimental/__coop/reduce.cuh index de5bc176cb5..c2939e685a7 100644 --- a/cudax/include/cuda/experimental/__coop/reduce.cuh +++ b/cudax/include/cuda/experimental/__coop/reduce.cuh @@ -37,7 +37,7 @@ #include #include -#include +#include #include #include diff --git a/cudax/include/cuda/experimental/__utility/broadcasted.cuh b/cudax/include/cuda/experimental/__utility/broadcasted.cuh deleted file mode 100644 index 8bf31762e92..00000000000 --- a/cudax/include/cuda/experimental/__utility/broadcasted.cuh +++ /dev/null @@ -1,36 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// Part of CUDA Experimental in CUDA C++ Core Libraries, -// under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. -// -//===----------------------------------------------------------------------===// - -#ifndef _CUDA_EXPERIMENTAL___UTILITY_BROADCASTED_CUH -#define _CUDA_EXPERIMENTAL___UTILITY_BROADCASTED_CUH - -#include - -#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) -# pragma GCC system_header -#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) -# pragma clang system_header -#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) -# pragma system_header -#endif // no system header - -#include - -namespace cuda::experimental -{ -enum class broadcasted_t : unsigned char -{ -}; -_CCCL_GLOBAL_CONSTANT broadcasted_t broadcasted{}; -} // namespace cuda::experimental - -#include - -#endif // _CUDA_EXPERIMENTAL___UTILITY_BROADCASTED_CUH diff --git a/cudax/include/cuda/experimental/__utility/result_policy.cuh b/cudax/include/cuda/experimental/__utility/result_policy.cuh new file mode 100644 index 00000000000..40a0047bdf7 --- /dev/null +++ b/cudax/include/cuda/experimental/__utility/result_policy.cuh @@ -0,0 +1,85 @@ +//===----------------------------------------------------------------------===// +// +// Part of CUDA Experimental in CUDA C++ Core Libraries, +// under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. +// +//===----------------------------------------------------------------------===// + +#ifndef _CUDA_EXPERIMENTAL___UTILITY_RESULT_POLICY_CUH +#define _CUDA_EXPERIMENTAL___UTILITY_RESULT_POLICY_CUH + +#include + +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) +# pragma GCC system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) +# pragma clang system_header +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) +# pragma system_header +#endif // no system header + +#include + +// NOLINTBEGIN(bugprone-reserved-identifier) + +namespace cuda::experimental +{ +template +struct __result_policy_base +{ + using __derived_type _CCCL_NODEBUG_ALIAS = _Derived; +}; + +//! @brief Result specifier requesting that only a single rank receives the result. +//! +//! Passed as the leading argument to a cooperative or distributed algorithm to indicate +//! that only the rank `dest` receives the result. Every other participating rank receives +//! either no result or an unspecified value; the precise value seen by the non-destination +//! ranks is defined by the algorithm. +template +struct returned_to : __result_policy_base> +{ + _CCCL_HIDE_FROM_ABI returned_to() = delete; + + _CCCL_API constexpr explicit returned_to(_Tp __rank) noexcept + : __rank_{__rank} + {} + + _Tp __rank_; +}; + +template +_CCCL_DEDUCTION_GUIDE_ATTRIBUTES returned_to(_Tp) -> returned_to<_Tp>; + +struct distributed_t : __result_policy_base +{}; + +//! @brief Result specifier requesting that each rank receives a slice of the result. +//! +//! Passed as the leading argument to a cooperative or distributed algorithm to indicate +//! that every participating rank receives a portion of the global result rather than the +//! whole. For example, a distributed sort delivers to each rank a slice of the globally +//! sorted sequence, and a distributed scan delivers to each rank its portion of the global +//! scan. +_CCCL_GLOBAL_CONSTANT distributed_t distributed{}; + +struct broadcasted_t : __result_policy_base +{}; + +//! @brief Result specifier requesting that every rank receives an identical result. +//! +//! Passed as the leading argument to a cooperative or distributed algorithm to indicate +//! that all participating ranks receive the same complete result. +//! +//! @snippet this_block.cu broadcasted reduce +_CCCL_GLOBAL_CONSTANT broadcasted_t broadcasted{}; +} // namespace cuda::experimental + +// NOLINTEND(bugprone-reserved-identifier) + +#include + +#endif // _CUDA_EXPERIMENTAL___UTILITY_RESULT_POLICY_CUH diff --git a/cudax/test/coop/reduce/this_block.cu b/cudax/test/coop/reduce/this_block.cu index 06f1f3a5ef4..30338f3aca6 100644 --- a/cudax/test/coop/reduce/this_block.cu +++ b/cudax/test/coop/reduce/this_block.cu @@ -51,7 +51,10 @@ struct ReduceKernel if constexpr (Broadcasted) { + //! [broadcasted reduce] + // Every thread in the block receives the same reduction result. const auto result = cudax::coop::reduce(cudax::broadcasted, block, thread_data, red_op); + //! [broadcasted reduce] d_out[cuda::gpu_thread.rank(block)] = result; } diff --git a/docs/cudax/Doxyfile b/docs/cudax/Doxyfile index 7e2ab4930a1..301ae2dcced 100644 --- a/docs/cudax/Doxyfile +++ b/docs/cudax/Doxyfile @@ -45,7 +45,8 @@ EXCLUDE_SYMBOLS = *detail* *RESERVED* *reserved* *__* _A* _B* _C* _D* _E* # Path for @snippet references to test/example files EXAMPLE_PATH = ../../cudax/include/cuda/experimental/__stf/utility \ - ../../cudax/test/multi_gpu/algorithms + ../../cudax/test/multi_gpu/algorithms \ + ../../cudax/test/coop/reduce FILE_PATTERNS = *.h *.hpp *.cuh EXTENSION_MAPPING = cuh=C++ cu=C++