|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of libcu++, the C++ Standard Library for your entire system, |
| 4 | +// under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. |
| 8 | +// |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#ifndef _CUDA_STD___PSTL_CUDA_FOR_EACH_N_H |
| 12 | +#define _CUDA_STD___PSTL_CUDA_FOR_EACH_N_H |
| 13 | + |
| 14 | +#include <cuda/std/detail/__config> |
| 15 | + |
| 16 | +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) |
| 17 | +# pragma GCC system_header |
| 18 | +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) |
| 19 | +# pragma clang system_header |
| 20 | +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) |
| 21 | +# pragma system_header |
| 22 | +#endif // no system header |
| 23 | + |
| 24 | +#if _CCCL_HAS_BACKEND_CUDA() |
| 25 | + |
| 26 | +# include <cub/device/device_for.cuh> |
| 27 | + |
| 28 | +# include <cuda/__execution/policy.h> |
| 29 | +# include <cuda/__runtime/api_wrapper.h> |
| 30 | +# include <cuda/__stream/stream_ref.h> |
| 31 | +# include <cuda/std/__algorithm/for_each_n.h> |
| 32 | +# include <cuda/std/__exception/cuda_error.h> |
| 33 | +# include <cuda/std/__exception/terminate.h> |
| 34 | +# include <cuda/std/__execution/policy.h> |
| 35 | +# include <cuda/std/__iterator/iterator_traits.h> |
| 36 | +# include <cuda/std/__pstl/dispatch.h> |
| 37 | +# include <cuda/std/__type_traits/always_false.h> |
| 38 | +# include <cuda/std/__utility/convert_to_integral.h> |
| 39 | +# include <cuda/std/__utility/move.h> |
| 40 | + |
| 41 | +# include <nv/target> |
| 42 | + |
| 43 | +# include <cuda/std/__cccl/prologue.h> |
| 44 | + |
| 45 | +_CCCL_BEGIN_NAMESPACE_CUDA_STD_EXECUTION |
| 46 | + |
| 47 | +_CCCL_BEGIN_NAMESPACE_ARCH_DEPENDENT |
| 48 | + |
| 49 | +template <> |
| 50 | +struct __pstl_dispatch<__pstl_algorithm::__for_each_n, __execution_backend::__cuda> |
| 51 | +{ |
| 52 | + template <class _Policy, class _Iter, class _Size, class _Fn> |
| 53 | + [[nodiscard]] _CCCL_HOST_API static _Iter |
| 54 | + __par_impl([[maybe_unused]] _Policy __policy, _Iter __first, _Size __orig_n, _Fn __func) noexcept |
| 55 | + { |
| 56 | + const auto __count = ::cuda::std::__convert_to_integral(__orig_n); |
| 57 | + ::cuda::stream_ref __stream{cudaStreamPerThread}; |
| 58 | + |
| 59 | + _CCCL_TRY_CUDA_API( |
| 60 | + ::cub::DeviceFor::ForEachN, |
| 61 | + "__pstl_dispatch: kernel launch failed", |
| 62 | + __first, |
| 63 | + __count, |
| 64 | + ::cuda::std::move(__func), |
| 65 | + __stream.get()); |
| 66 | + |
| 67 | + __stream.sync(); |
| 68 | + |
| 69 | + return __first + __count; |
| 70 | + } |
| 71 | + |
| 72 | + template <class _Policy, class _Iter, class _Size, class _Fn> |
| 73 | + [[nodiscard]] _CCCL_HOST_API _CCCL_FORCEINLINE _Iter |
| 74 | + operator()(_Policy __policy, _Iter __first, _Size __orig_n, _Fn __func) const noexcept |
| 75 | + { |
| 76 | + if constexpr (::cuda::std::__has_random_access_traversal<_Iter>) |
| 77 | + { |
| 78 | + return __par_impl(::cuda::std::move(__policy), ::cuda::std::move(__first), __orig_n, ::cuda::std::move(__func)); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + static_assert(__always_false_v<_Policy>, |
| 83 | + "__pstl_dispatch: CUDA backend of cuda::std::for_each_n requires at least random access iterators"); |
| 84 | + return ::cuda::std::for_each_n(::cuda::std::move(__first), __orig_n, ::cuda::std::move(__func)); |
| 85 | + } |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +_CCCL_END_NAMESPACE_ARCH_DEPENDENT |
| 90 | + |
| 91 | +_CCCL_END_NAMESPACE_CUDA_STD_EXECUTION |
| 92 | + |
| 93 | +# include <cuda/std/__cccl/epilogue.h> |
| 94 | + |
| 95 | +#endif /// _CCCL_HAS_BACKEND_CUDA() |
| 96 | + |
| 97 | +#endif // _CUDA_STD___PSTL_CUDA_FOR_EACH_N_H |
0 commit comments