|
| 1 | + |
| 2 | +#pragma once |
| 3 | + |
| 4 | +#include "internal/defines.h" |
| 5 | + |
| 6 | +#if defined(XTD_CUDA_BACKEND) |
| 7 | +#include <thrust/reduce.h> |
| 8 | +#elif defined(XTD_HIP_BACKEND) |
| 9 | +#include <rocthrust/reduce.h> |
| 10 | +#elif defined(XTD_SYCL_BACKEND) |
| 11 | +#include <oneapi/dpl/algorithm> |
| 12 | +#include <oneapi/dpl/execution> |
| 13 | +#else |
| 14 | +#include <algorithm> |
| 15 | +#endif |
| 16 | + |
| 17 | +namespace xtd { |
| 18 | + |
| 19 | + template <typename InputIterator> |
| 20 | + XTD_HOST_FUNCTION inline constexpr typename std::iterator_traits<InputIterator>::value_type |
| 21 | + reduce(InputIterator first, InputIterator last) { |
| 22 | +#if defined(XTD_CUDA_BACKEND) |
| 23 | + return thrust::reduce(thrust::device, first, last); |
| 24 | +#elif defined(XTD_HIP_BACKEND) |
| 25 | + return rocthrust::reduce(thrust::hip::par, first, last); |
| 26 | +#elif defined(XTD_SYCL_BACKEND) |
| 27 | + return oneapi::dpl::reduce(oneapi::dpl::execution::dpcpp_default, first, last); |
| 28 | +#else |
| 29 | + return std::reduce(first, last); |
| 30 | +#endif |
| 31 | + } |
| 32 | + |
| 33 | + template <typename ExecutionPolicy, typename ForwardIterator> |
| 34 | + XTD_HOST_FUNCTION inline constexpr typename std::iterator_traits<ForwardIterator>::value_type |
| 35 | + reduce(ExecutionPolicy&& policy, ForwardIterator first, ForwardIterator last) { |
| 36 | +#if defined(XTD_CUDA_BACKEND) |
| 37 | + return thrust::reduce(std::forward<ExecutionPolicy>(policy), first, last); |
| 38 | +#elif defined(XTD_HIP_BACKEND) |
| 39 | + return rocthrust::reduce(std::forward<ExecutionPolicy>(policy), first, last); |
| 40 | +#elif defined(XTD_SYCL_BACKEND) |
| 41 | + return oneapi::dpl::reduce(std::forward<ExecutionPolicy>(policy), first, last); |
| 42 | +#else |
| 43 | + return std::reduce(std::forward<ExecutionPolicy>(policy), first, last); |
| 44 | +#endif |
| 45 | + } |
| 46 | + |
| 47 | + template <typename InputIterator, typename T> |
| 48 | + XTD_HOST_FUNCTION inline constexpr T reduce(InputIterator first, InputIterator last, T init) { |
| 49 | +#if defined(XTD_CUDA_BACKEND) |
| 50 | + return thrust::reduce(thrust::device, first, last, init); |
| 51 | +#elif defined(XTD_HIP_BACKEND) |
| 52 | + return rocthrust::reduce(thrust::hip::par, first, last, init); |
| 53 | +#elif defined(XTD_SYCL_BACKEND) |
| 54 | + return oneapi::dpl::reduce(oneapi::dpl::execution::dpcpp_default, first, last, init); |
| 55 | +#else |
| 56 | + return std::reduce(first, last, init); |
| 57 | +#endif |
| 58 | + } |
| 59 | + |
| 60 | + template <typename ExecutionPolicy, typename ForwardIterator, typename T> |
| 61 | + XTD_HOST_FUNCTION inline constexpr T reduce(ExecutionPolicy&& policy, |
| 62 | + ForwardIterator first, |
| 63 | + ForwardIterator last, |
| 64 | + T init) { |
| 65 | +#if defined(XTD_CUDA_BACKEND) |
| 66 | + return thrust::reduce(std::forward<ExecutionPolicy>(policy), first, last, init); |
| 67 | +#elif defined(XTD_HIP_BACKEND) |
| 68 | + return rocthrust::reduce(std::forward<ExecutionPolicy>(policy), first, last, init); |
| 69 | +#elif defined(XTD_SYCL_BACKEND) |
| 70 | + return oneapi::dpl::reduce(std::forward<ExecutionPolicy>(policy), first, last, init); |
| 71 | +#else |
| 72 | + return std::reduce(std::forward<ExecutionPolicy>(policy), first, last, init); |
| 73 | +#endif |
| 74 | + } |
| 75 | + |
| 76 | + template <typename InputIterator, typename T, typename BinaryOperation> |
| 77 | + XTD_HOST_FUNCTION inline constexpr T reduce(InputIterator first, |
| 78 | + InputIterator last, |
| 79 | + T init, |
| 80 | + BinaryOperation op) { |
| 81 | +#if defined(XTD_CUDA_BACKEND) |
| 82 | + return thrust::reduce(thrust::device, first, last, init, op); |
| 83 | +#elif defined(XTD_HIP_BACKEND) |
| 84 | + return rocthrust::reduce(thrust::hip::par, first, last, init, op); |
| 85 | +#elif defined(XTD_SYCL_BACKEND) |
| 86 | + return oneapi::dpl::reduce(oneapi::dpl::execution::dpcpp_default, first, last, init, op); |
| 87 | +#else |
| 88 | + return std::reduce(first, last, init, op); |
| 89 | +#endif |
| 90 | + } |
| 91 | + |
| 92 | + template <typename ExecutionPolicy, typename ForwardIterator, typename T, typename BinaryOperation> |
| 93 | + XTD_HOST_FUNCTION inline constexpr T reduce(ExecutionPolicy&& policy, |
| 94 | + ForwardIterator first, |
| 95 | + ForwardIterator last, |
| 96 | + T init, |
| 97 | + BinaryOperation op) { |
| 98 | +#if defined(XTD_CUDA_BACKEND) |
| 99 | + return thrust::reduce(std::forward<ExecutionPolicy>(policy), first, last, init, op); |
| 100 | +#elif defined(XTD_HIP_BACKEND) |
| 101 | + return rocthrust::reduce(std::forward<ExecutionPolicy>(policy), first, last, init, op); |
| 102 | +#elif defined(XTD_SYCL_BACKEND) |
| 103 | + return oneapi::dpl::reduce(std::forward<ExecutionPolicy>(policy), first, last, init, op); |
| 104 | +#else |
| 105 | + return std::reduce(std::forward<ExecutionPolicy>(policy), first, last, init, op); |
| 106 | +#endif |
| 107 | + } |
| 108 | + |
| 109 | +} // namespace xtd |
0 commit comments