|
| 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 _LIBCUDACXX___PSTL_DISPATCH_H |
| 12 | +#define _LIBCUDACXX___PSTL_DISPATCH_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 | +#include <cuda/std/__execution/policy.h> |
| 25 | +#include <cuda/std/__type_traits/is_base_of.h> |
| 26 | + |
| 27 | +#include <cuda/std/__cccl/prologue.h> |
| 28 | + |
| 29 | +_CCCL_BEGIN_NAMESPACE_EXECUTION |
| 30 | + |
| 31 | +enum class __pstl_algorithm |
| 32 | +{ |
| 33 | + // The find_if family |
| 34 | + __find, |
| 35 | + __find_if, |
| 36 | + __any_of, |
| 37 | + __all_of, |
| 38 | + __none_of, |
| 39 | + __is_partitioned, |
| 40 | + |
| 41 | + // merge family |
| 42 | + // non implemented |
| 43 | + |
| 44 | + // sort family |
| 45 | + __sort, |
| 46 | + |
| 47 | + // for_each family |
| 48 | + __for_each_n, |
| 49 | + __fill, |
| 50 | + __fill_n, |
| 51 | + __replace, |
| 52 | + __replace_if, |
| 53 | + __generate, |
| 54 | + __generate_n, |
| 55 | + |
| 56 | + // transform_reduce and transform_reduce_binary family |
| 57 | + __count_if, |
| 58 | + __count, |
| 59 | + __equal, |
| 60 | + __reduce, |
| 61 | + |
| 62 | + // transform and transform_binary family |
| 63 | + __replace_copy_if, |
| 64 | + __replace_copy, |
| 65 | + __move, |
| 66 | + __copy, |
| 67 | + __copy_n, |
| 68 | + __rotate_copy, |
| 69 | +}; |
| 70 | + |
| 71 | +//! @brief tag type to indicate that we cannot dispatch to a parallel algorithm and should run the algorithm serially |
| 72 | +struct __pstl_no_dispatch |
| 73 | +{}; |
| 74 | + |
| 75 | +//! @brief Dispatcher for a given @tparam _Algorith and @tparam _Policy |
| 76 | +//! If @class __pstl_dispatch is not specialized by the chosen backend we will fall back to serial execution |
| 77 | +template <__pstl_algorithm _Algorithm, __execution_policy _Policy> |
| 78 | +struct __pstl_dispatch : public __pstl_no_dispatch |
| 79 | +{}; |
| 80 | + |
| 81 | +//! @brief Helper variable that detects whether @class __pstl_dispatch has been specialized so that we can |
| 82 | +//! dispatch |
| 83 | +template <class> |
| 84 | +inline constexpr bool __pstl_can_dispatch = false; |
| 85 | + |
| 86 | +template <__pstl_algorithm _Algorithm, __execution_policy _Policy> |
| 87 | +inline constexpr bool __pstl_can_dispatch<__pstl_dispatch<_Algorithm, _Policy>> = |
| 88 | + !::cuda::std::is_base_of_v<__pstl_no_dispatch, __pstl_dispatch<_Algorithm, _Policy>>; |
| 89 | + |
| 90 | +//! @brief Top layer dispatcher that returns a concrete dispatch if possible |
| 91 | +template <__pstl_algorithm _Algorithm, __execution_policy _Policy> |
| 92 | +[[nodiscard]] _CCCL_API static constexpr auto __pstl_select_dispatch() noexcept |
| 93 | +{ |
| 94 | + // If the user requests a specific backend, we need to use that if available |
| 95 | +#if _CCCL_HAS_BACKEND_CUDA() |
| 96 | + if constexpr (::cuda::std::execution::__requires_unique_backend(_Policy, __execution_policy::__backend_cuda)) |
| 97 | + { |
| 98 | + return __pstl_dispatch<_Algorithm, __execution_policy::__backend_cuda>{}; |
| 99 | + } |
| 100 | +#endif // _CCCL_HAS_BACKEND_CUDA() |
| 101 | +#if _CCCL_HAS_BACKEND_OMP() |
| 102 | + if constexpr (::cuda::std::execution::__requires_unique_backend(_Policy, __execution_policy::__backend_omp)) |
| 103 | + { |
| 104 | + return __pstl_dispatch<_Algorithm, __execution_policy::__backend_omp>{}; |
| 105 | + } |
| 106 | +#endif // _CCCL_HAS_BACKEND_OMP() |
| 107 | +#if _CCCL_HAS_BACKEND_TBB() |
| 108 | + if constexpr (::cuda::std::execution::__requires_unique_backend(_Policy, __execution_policy::__backend_tbb)) |
| 109 | + { |
| 110 | + return __pstl_dispatch<_Algorithm, __execution_policy::__backend_tbb>{}; |
| 111 | + } |
| 112 | +#endif // _CCCL_HAS_BACKEND_TBB() |
| 113 | + |
| 114 | + // If the user requests multiple backends, we can take the first available one of the selected ones |
| 115 | +#if _CCCL_HAS_BACKEND_CUDA() |
| 116 | + if constexpr (::cuda::std::execution::__requires_matching_backend(_Policy, __execution_policy::__backend_cuda)) |
| 117 | + { |
| 118 | + using __dispatch = __pstl_dispatch<_Algorithm, __execution_policy::__backend_cuda>; |
| 119 | + if constexpr (__pstl_can_dispatch<__dispatch>) |
| 120 | + { |
| 121 | + return __dispatch{}; |
| 122 | + } |
| 123 | + } |
| 124 | +#endif // _CCCL_HAS_BACKEND_CUDA() |
| 125 | +#if _CCCL_HAS_BACKEND_OMP() |
| 126 | + if constexpr (::cuda::std::execution::__requires_matching_backend(_Policy, __execution_policy::__backend_omp)) |
| 127 | + { |
| 128 | + using __dispatch = __pstl_dispatch<_Algorithm, __execution_policy::__backend_omp>; |
| 129 | + if constexpr (__pstl_can_dispatch<__dispatch>) |
| 130 | + { |
| 131 | + return __dispatch{}; |
| 132 | + } |
| 133 | + } |
| 134 | +#endif // _CCCL_HAS_BACKEND_OMP() |
| 135 | +#if _CCCL_HAS_BACKEND_TBB() |
| 136 | + if constexpr (::cuda::std::execution::__requires_matching_backend(_Policy, __execution_policy::__backend_tbb)) |
| 137 | + { |
| 138 | + using __dispatch = __pstl_dispatch<_Algorithm, __execution_policy::__backend_tbb>; |
| 139 | + if constexpr (__pstl_can_dispatch<__dispatch>) |
| 140 | + { |
| 141 | + return __dispatch{}; |
| 142 | + } |
| 143 | + } |
| 144 | +#endif // _CCCL_HAS_BACKEND_TBB() |
| 145 | + |
| 146 | + // If the user requests no backend, we can take the first available one that suites us |
| 147 | +#if _CCCL_HAS_BACKEND_CUDA() |
| 148 | + if constexpr (__pstl_can_dispatch<__pstl_dispatch<_Algorithm, __execution_policy::__backend_cuda>>) |
| 149 | + { |
| 150 | + return __pstl_dispatch<_Algorithm, __execution_policy::__backend_cuda>{}; |
| 151 | + } |
| 152 | +#endif // _CCCL_HAS_BACKEND_CUDA() |
| 153 | +#if _CCCL_HAS_BACKEND_OMP() |
| 154 | + if constexpr (__pstl_can_dispatch<__pstl_dispatch<_Algorithm, __execution_policy::__backend_omp>>) |
| 155 | + { |
| 156 | + return __pstl_dispatch<_Algorithm, __execution_policy::__backend_omp>{}; |
| 157 | + } |
| 158 | +#endif // _CCCL_HAS_BACKEND_OMP() |
| 159 | +#if _CCCL_HAS_BACKEND_TBB() |
| 160 | + if constexpr (__pstl_can_dispatch<__pstl_dispatch<_Algorithm, __execution_policy::__backend_tbb>>) |
| 161 | + { |
| 162 | + return __pstl_dispatch<_Algorithm, __execution_policy::__backend_omp>{}; |
| 163 | + } |
| 164 | +#endif // _CCCL_HAS_BACKEND_TBB() |
| 165 | + |
| 166 | + // No dispatch found, return invalid to signal serial execution |
| 167 | + return __pstl_dispatch<_Algorithm, __execution_policy::__backend_invalid>{}; |
| 168 | +} |
| 169 | + |
| 170 | +_CCCL_END_NAMESPACE_EXECUTION |
| 171 | + |
| 172 | +#include <cuda/std/__cccl/epilogue.h> |
| 173 | + |
| 174 | +#endif // _LIBCUDACXX___PSTL_DISPATCH_H |
0 commit comments