Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/algorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#pragma once

#include "algorithm/extrema.h"
#include "algorithm/reduce.h"
#include "algorithm/sort.h"
193 changes: 193 additions & 0 deletions include/algorithm/extrema.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@

#pragma once

#include "internal/defines.h"

#if defined(XTD_CUDA_BACKEND) || defined(XTD_HIP_BACKEND)
#include <thrust/extrema.h>
#elif defined(XTD_SYCL_BACKEND)
#include <oneapi/dpl/algorithm>
#include <oneapi/dpl/execution>
#else
#include <algorithm>
#endif

namespace xtd {

template <typename ForwardIterator>
XTD_HOST_FUNCTION inline constexpr ForwardIterator min_element(ForwardIterator first,
ForwardIterator last) {
#if defined(XTD_CUDA_BACKEND)
return thrust::min_element(thrust::device, first, last);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::min_element(thrust::hip::par, first, last);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::min_element(oneapi::dpl::execution::dpcpp_default, first, last);
#else
return std::min_element(first, last);
#endif
}

template <typename ExecutionPolicy, typename ForwardIterator>
XTD_HOST_FUNCTION inline constexpr ForwardIterator min_element(ExecutionPolicy&& policy,
ForwardIterator first,
ForwardIterator last) {
#if defined(XTD_CUDA_BACKEND)
return thrust::min_element(std::forward<ExecutionPolicy>(policy), first, last);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::min_element(std::forward<ExecutionPolicy>(policy), first, last);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::min_element(std::forward<ExecutionPolicy>(policy), first, last);
#else
return std::min_element(std::forward<ExecutionPolicy>(policy), first, last);
#endif
}

template <typename ForwardIterator, typename BinaryPredicate>
XTD_HOST_FUNCTION inline constexpr ForwardIterator min_element(ForwardIterator first,
ForwardIterator last,
BinaryPredicate comp) {
#if defined(XTD_CUDA_BACKEND)
return thrust::min_element(thrust::device, first, last, comp);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::min_element(thrust::hip::par, first, last, comp);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::min_element(oneapi::dpl::execution::dpcpp_default, first, last, comp);
#else
return std::min_element(first, last, comp);
#endif
}

template <typename ExecutionPolicy, typename ForwardIterator, typename BinaryPredicate>
XTD_HOST_FUNCTION inline constexpr ForwardIterator min_element(ExecutionPolicy&& policy,
ForwardIterator first,
ForwardIterator last,
BinaryPredicate comp) {
#if defined(XTD_CUDA_BACKEND)
return thrust::min_element(std::forward<ExecutionPolicy>(policy), first, last, comp);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::min_element(std::forward<ExecutionPolicy>(policy), first, last, comp);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::min_element(std::forward<ExecutionPolicy>(policy), first, last, comp);
#else
return std::min_element(std::forward<ExecutionPolicy>(policy), first, last, comp);
#endif
}

template <typename ForwardIterator>
XTD_HOST_FUNCTION inline constexpr ForwardIterator max_element(ForwardIterator first,
ForwardIterator last) {
#if defined(XTD_CUDA_BACKEND)
return thrust::max_element(thrust::device, first, last);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::max_element(thrust::hip::par, first, last);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::max_element(oneapi::dpl::execution::dpcpp_default, first, last);
#else
return std::max_element(first, last);
#endif
}

template <typename ExecutionPolicy, typename ForwardIterator>
XTD_HOST_FUNCTION inline constexpr ForwardIterator max_element(ExecutionPolicy&& policy,
ForwardIterator first,
ForwardIterator last) {
#if defined(XTD_CUDA_BACKEND)
return thrust::max_element(std::forward<ExecutionPolicy>(policy), first, last);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::max_element(std::forward<ExecutionPolicy>(policy), first, last);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::max_element(std::forward<ExecutionPolicy>(policy), first, last);
#else
return std::max_element(std::forward<ExecutionPolicy>(policy), first, last);
#endif
}

template <typename ForwardIterator, typename BinaryPredicate>
XTD_HOST_FUNCTION inline constexpr ForwardIterator max_element(ForwardIterator first,
ForwardIterator last,
BinaryPredicate comp) {
#if defined(XTD_CUDA_BACKEND)
return thrust::max_element(thrust::device, first, last, comp);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::max_element(thrust::hip::par, first, last, comp);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::max_element(oneapi::dpl::execution::dpcpp_default, first, last, comp);
#else
return std::max_element(first, last, comp);
#endif
}

template <typename ExecutionPolicy, typename ForwardIterator, typename BinaryPredicate>
XTD_HOST_FUNCTION inline constexpr ForwardIterator max_element(ExecutionPolicy&& policy,
ForwardIterator first,
ForwardIterator last,
BinaryPredicate comp) {
#if defined(XTD_CUDA_BACKEND)
return thrust::max_element(std::forward<ExecutionPolicy>(policy), first, last, comp);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::max_element(std::forward<ExecutionPolicy>(policy), first, last, comp);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::max_element(std::forward<ExecutionPolicy>(policy), first, last, comp);
#else
return std::max_element(std::forward<ExecutionPolicy>(policy), first, last, comp);
#endif
}

template <typename ForwardIterator>
XTD_HOST_FUNCTION inline constexpr std::pair<ForwardIterator, ForwardIterator> minmax_element(
ForwardIterator first, ForwardIterator last) {
#if defined(XTD_CUDA_BACKEND)
return thrust::minmax_element(thrust::device, first, last);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::minmax_element(thrust::hip::par, first, last);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::minmax_element(oneapi::dpl::execution::dpcpp_default, first, last);
#else
return std::minmax_element(first, last);
#endif
}

template <typename ExecutionPolicy, typename ForwardIterator>
XTD_HOST_FUNCTION inline constexpr std::pair<ForwardIterator, ForwardIterator> minmax_element(
ExecutionPolicy&& policy, ForwardIterator first, ForwardIterator last) {
#if defined(XTD_CUDA_BACKEND)
return thrust::minmax_element(std::forward<ExecutionPolicy>(policy), first, last);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::minmax_element(std::forward<ExecutionPolicy>(policy), first, last);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::minmax_element(std::forward<ExecutionPolicy>(policy), first, last);
#else
return std::minmax_element(std::forward<ExecutionPolicy>(policy), first, last);
#endif
}

template <typename ForwardIterator, typename BinaryPredicate>
XTD_HOST_FUNCTION inline constexpr std::pair<ForwardIterator, ForwardIterator> minmax_element(
ForwardIterator first, ForwardIterator last, BinaryPredicate comp) {
#if defined(XTD_CUDA_BACKEND)
return thrust::minmax_element(thrust::device, first, last, comp);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::minmax_element(thrust::hip::par, first, last, comp);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::minmax_element(oneapi::dpl::execution::dpcpp_default, first, last, comp);
#else
return std::minmax_element(first, last, comp);
#endif
}

template <typename ExecutionPolicy, typename ForwardIterator, typename BinaryPredicate>
XTD_HOST_FUNCTION inline constexpr std::pair<ForwardIterator, ForwardIterator> minmax_element(
ExecutionPolicy&& policy, ForwardIterator first, ForwardIterator last, BinaryPredicate comp) {
#if defined(XTD_CUDA_BACKEND)
return thrust::minmax_element(std::forward<ExecutionPolicy>(policy), first, last, comp);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::minmax_element(std::forward<ExecutionPolicy>(policy), first, last, comp);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::minmax_element(std::forward<ExecutionPolicy>(policy), first, last, comp);
#else
return std::minmax_element(std::forward<ExecutionPolicy>(policy), first, last, comp);
#endif
}

} // namespace xtd
107 changes: 107 additions & 0 deletions include/algorithm/reduce.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

#pragma once

#include "internal/defines.h"

#if defined(XTD_CUDA_BACKEND) || defined(XTD_HIP_BACKEND)
#include <thrust/reduce.h>
#elif defined(XTD_SYCL_BACKEND)
#include <oneapi/dpl/algorithm>
#include <oneapi/dpl/execution>
#else
#include <algorithm>
#endif

namespace xtd {

template <typename InputIterator>
XTD_HOST_FUNCTION inline constexpr typename std::iterator_traits<InputIterator>::value_type
reduce(InputIterator first, InputIterator last) {
#if defined(XTD_CUDA_BACKEND)
return thrust::reduce(thrust::device, first, last);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::reduce(thrust::hip::par, first, last);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::reduce(oneapi::dpl::execution::dpcpp_default, first, last);
#else
return std::reduce(first, last);
#endif
}

template <typename ExecutionPolicy, typename ForwardIterator>
XTD_HOST_FUNCTION inline constexpr typename std::iterator_traits<ForwardIterator>::value_type
reduce(ExecutionPolicy&& policy, ForwardIterator first, ForwardIterator last) {
#if defined(XTD_CUDA_BACKEND)
return thrust::reduce(std::forward<ExecutionPolicy>(policy), first, last);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::reduce(std::forward<ExecutionPolicy>(policy), first, last);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::reduce(std::forward<ExecutionPolicy>(policy), first, last);
#else
return std::reduce(std::forward<ExecutionPolicy>(policy), first, last);
#endif
}

template <typename InputIterator, typename T>
XTD_HOST_FUNCTION inline constexpr T reduce(InputIterator first, InputIterator last, T init) {
#if defined(XTD_CUDA_BACKEND)
return thrust::reduce(thrust::device, first, last, init);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::reduce(thrust::hip::par, first, last, init);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::reduce(oneapi::dpl::execution::dpcpp_default, first, last, init);
#else
return std::reduce(first, last, init);
#endif
}

template <typename ExecutionPolicy, typename ForwardIterator, typename T>
XTD_HOST_FUNCTION inline constexpr T reduce(ExecutionPolicy&& policy,
ForwardIterator first,
ForwardIterator last,
T init) {
#if defined(XTD_CUDA_BACKEND)
return thrust::reduce(std::forward<ExecutionPolicy>(policy), first, last, init);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::reduce(std::forward<ExecutionPolicy>(policy), first, last, init);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::reduce(std::forward<ExecutionPolicy>(policy), first, last, init);
#else
return std::reduce(std::forward<ExecutionPolicy>(policy), first, last, init);
#endif
}

template <typename InputIterator, typename T, typename BinaryOperation>
XTD_HOST_FUNCTION inline constexpr T reduce(InputIterator first,
InputIterator last,
T init,
BinaryOperation op) {
#if defined(XTD_CUDA_BACKEND)
return thrust::reduce(thrust::device, first, last, init, op);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::reduce(thrust::hip::par, first, last, init, op);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::reduce(oneapi::dpl::execution::dpcpp_default, first, last, init, op);
#else
return std::reduce(first, last, init, op);
#endif
}

template <typename ExecutionPolicy, typename ForwardIterator, typename T, typename BinaryOperation>
XTD_HOST_FUNCTION inline constexpr T reduce(ExecutionPolicy&& policy,
ForwardIterator first,
ForwardIterator last,
T init,
BinaryOperation op) {
#if defined(XTD_CUDA_BACKEND)
return thrust::reduce(std::forward<ExecutionPolicy>(policy), first, last, init, op);
#elif defined(XTD_HIP_BACKEND)
return rocthrust::reduce(std::forward<ExecutionPolicy>(policy), first, last, init, op);
#elif defined(XTD_SYCL_BACKEND)
return oneapi::dpl::reduce(std::forward<ExecutionPolicy>(policy), first, last, init, op);
#else
return std::reduce(std::forward<ExecutionPolicy>(policy), first, last, init, op);
#endif
}

} // namespace xtd
77 changes: 77 additions & 0 deletions include/algorithm/sort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

#pragma once

#include "internal/defines.h"

#if defined(XTD_CUDA_BACKEND) || defined(XTD_HIP_BACKEND)
#include <thrust/sort.h>
#elif defined(XTD_SYCL_BACKEND)
#include <oneapi/dpl/algorithm>
#include <oneapi/dpl/execution>
#else
#include <algorithm>
#endif

namespace xtd {

template <typename RandomAccessIterator>
XTD_HOST_FUNCTION inline constexpr void sort(RandomAccessIterator first,
RandomAccessIterator last) {
#if defined(XTD_CUDA_BACKEND)
thrust::sort(thrust::device, first, last);
#elif defined(XTD_HIP_BACKEND)
rocthrust::sort(thrustd::hip::par, first, last);
#elif defined(XTD_SYCL_BACKEND)
oneapi::dpl::sort(oneapi::dpl::execution::dpcpp_default, first, last);
#else
std::sort(first, last);
#endif
}

template <typename ExecutionPolicy, typename RandomAccessIterator>
XTD_HOST_FUNCTION inline constexpr void sort(ExecutionPolicy&& policy,
RandomAccessIterator first,
RandomAccessIterator last) {
#if defined(XTD_CUDA_BACKEND)
thrust::sort(std::forward<ExecutionPolicy>(policy), first, last);
#elif defined(XTD_HIP_BACKEND)
rocthrust::sort(std::forward<ExecutionPolicy>(policy), first, last);
#elif defined(XTD_SYCL_BACKEND)
oneapi::dpl::sort(std::forward<ExecutionPolicy>(policy), first, last);
#else
std::sort(std::forward<ExecutionPolicy>(policy), first, last);
#endif
}

template <typename RandomAccessIterator, typename Compare>
XTD_HOST_FUNCTION inline constexpr void sort(RandomAccessIterator first,
RandomAccessIterator last,
Compare comp) {
#if defined(XTD_CUDA_BACKEND)
thrust::sort(thrust::device, first, last, comp);
#elif defined(XTD_HIP_BACKEND)
rocthrust::sort(thrust::hip::par, first, last, comp);
#elif defined(XTD_SYCL_BACKEND)
oneapi::dpl::sort(oneapi::dpl::execution::dpcpp_default, first, last, comp);
#else
std::sort(first, last, comp);
#endif
}

template <typename ExecutionPolicy, typename RandomAccessIterator, typename Compare>
XTD_HOST_FUNCTION inline constexpr void sort(ExecutionPolicy&& policy,
RandomAccessIterator first,
RandomAccessIterator last,
Compare comp) {
#if defined(XTD_CUDA_BACKEND)
thrust::sort(std::forward<ExecutionPolicy>(policy), first, last, comp);
#elif defined(XTD_HIP_BACKEND)
rocthrust::sort(std::forward<ExecutionPolicy>(policy), first, last, comp);
#elif defined(XTD_SYCL_BACKEND)
oneapi::dpl::sort(std::forward<ExecutionPolicy>(policy), first, last, comp);
#else
std::sort(std::forward<ExecutionPolicy>(policy), first, last, comp);
#endif
}

} // namespace xtd
Loading