| #include "cupy_thrust.h" |
| #include <cupy/type_dispatcher.cuh> |
| #include <thrust/device_ptr.h> |
| #include <thrust/device_vector.h> |
| #include <thrust/iterator/zip_iterator.h> |
| #include <thrust/iterator/constant_iterator.h> |
| #include <thrust/sequence.h> |
| #include <thrust/sort.h> |
| #include <thrust/tuple.h> |
| #include <thrust/execution_policy.h> |
| #include <type_traits> |
| #if (__CUDACC_VER_MAJOR__ >11 || (__CUDACC_VER_MAJOR__ == 11 && __CUDACC_VER_MINOR__ >= 2) || HIP_VERSION >= 402) |
| |
| |
| |
| |
| #include <thrust/optional.h> |
|
|
| #ifdef _MSC_VER |
| #define THRUST_OPTIONAL_CPP11_CONSTEXPR_LESS constexpr |
| #else |
| #define THRUST_OPTIONAL_CPP11_CONSTEXPR_LESS THRUST_OPTIONAL_CPP11_CONSTEXPR |
| #endif |
|
|
| #endif |
|
|
|
|
| #if CUPY_USE_HIP |
| typedef hipStream_t cudaStream_t; |
| namespace cuda { |
| using thrust::hip::par; |
| } |
| #else |
| namespace cuda { |
| using thrust::cuda::par; |
| } |
| #endif |
|
|
|
|
| extern "C" char *cupy_malloc(void *, size_t); |
| extern "C" int cupy_free(void *, char *); |
|
|
|
|
| class cupy_allocator { |
| private: |
| void* memory; |
|
|
| public: |
| typedef char value_type; |
|
|
| cupy_allocator(void* memory) : memory(memory) {} |
|
|
| char *allocate(size_t num_bytes) { |
| return cupy_malloc(memory, num_bytes); |
| } |
|
|
| void deallocate(char *ptr, size_t n) { |
| cupy_free(memory, ptr); |
| } |
| }; |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #if ((__CUDACC_VER_MAJOR__ > 9 || (__CUDACC_VER_MAJOR__ == 9 && __CUDACC_VER_MINOR__ == 2)) \ |
| && (__CUDA_ARCH__ >= 530 || !defined(__CUDA_ARCH__))) || (defined(__HIPCC__) || defined(CUPY_USE_HIP)) |
| #define ENABLE_HALF |
| #endif |
|
|
| #if (__CUDACC_VER_MAJOR__ >11 || (__CUDACC_VER_MAJOR__ == 11 && __CUDACC_VER_MINOR__ >= 2)) |
| #define CONSTEXPR_FUNC THRUST_OPTIONAL_CPP11_CONSTEXPR |
| #else |
| #define CONSTEXPR_FUNC |
| #endif |
|
|
| #if (__CUDACC_VER_MAJOR__ >11 || (__CUDACC_VER_MAJOR__ == 11 && __CUDACC_VER_MINOR__ >= 2) || HIP_VERSION >= 402) |
| #define CONSTEXPR_COMPARATOR THRUST_OPTIONAL_CPP11_CONSTEXPR_LESS |
| #else |
| #define CONSTEXPR_COMPARATOR |
| #endif |
|
|
| #ifdef ENABLE_HALF |
| __host__ __device__ __forceinline__ bool isnan(const __half& x) { |
| #if (defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)) |
| return __hisnan(x); |
| #else |
| return false; |
| #endif |
| } |
| #endif |
|
|
| template <typename T> |
| __host__ __device__ __forceinline__ CONSTEXPR_FUNC |
| static bool real_less(const T& lhs, const T& rhs) { |
| #if (defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)) |
| if (isnan(lhs)) { |
| return false; |
| } else if (isnan(rhs)) { |
| return true; |
| } else { |
| return lhs < rhs; |
| } |
| #else |
| return false; |
| #endif |
| } |
|
|
| template <typename T> |
| __host__ __device__ __forceinline__ CONSTEXPR_FUNC |
| static bool tuple_less(const thrust::tuple<size_t, T>& lhs, |
| const thrust::tuple<size_t, T>& rhs) { |
| const size_t& lhs_k = thrust::get<0>(lhs); |
| const size_t& rhs_k = thrust::get<0>(rhs); |
| const T& lhs_v = thrust::get<1>(lhs); |
| const T& rhs_v = thrust::get<1>(rhs); |
|
|
| |
| |
| if (lhs_k < rhs_k) { |
| return true; |
| } else if (lhs_k == rhs_k) { |
| |
| |
| return real_less(lhs_v, rhs_v); |
| } else { |
| return false; |
| } |
| } |
|
|
| |
| |
| |
| |
|
|
| template <typename T> |
| __host__ __device__ __forceinline__ CONSTEXPR_FUNC |
| static bool complex_less(const T& lhs, const T& rhs) { |
| const bool lhsRe = isnan(lhs.real()); |
| const bool lhsIm = isnan(lhs.imag()); |
| const bool rhsRe = isnan(rhs.real()); |
| const bool rhsIm = isnan(rhs.imag()); |
|
|
| |
| if (!lhsRe && !lhsIm && !rhsRe && !rhsIm) { |
| return lhs < rhs; |
| } |
|
|
| |
| if (!lhsRe && !lhsIm && (rhsRe || rhsIm)) { |
| return true; |
| } |
| if ((lhsRe || lhsIm) && !rhsRe && !rhsIm) { |
| return false; |
| } |
|
|
| |
| if (lhsRe && !rhsRe) { |
| return false; |
| } |
| if (!lhsRe && rhsRe) { |
| return true; |
| } |
| if (lhsIm && !rhsIm) { |
| return false; |
| } |
| if (!lhsIm && rhsIm) { |
| return true; |
| } |
|
|
| |
| return (((lhsIm && rhsIm) && (lhs.real() < rhs.real())) || ((lhsRe && rhsRe) && (lhs.imag() < rhs.imag()))); |
| } |
|
|
| |
| |
| |
| template <typename T> |
| struct select_less { |
| using type = thrust::less<T>; |
| }; |
|
|
| |
|
|
| template <> |
| struct select_less<complex<float>> { |
| struct type { |
| __host__ __device__ __forceinline__ CONSTEXPR_COMPARATOR |
| bool operator() ( |
| const complex<float>& lhs, const complex<float>& rhs) const { |
| return complex_less(lhs, rhs); |
| } |
| }; |
| }; |
|
|
| template <> |
| struct select_less<complex<double>> { |
| struct type { |
| __host__ __device__ __forceinline__ CONSTEXPR_COMPARATOR |
| bool operator() ( |
| const complex<double>& lhs, const complex<double>& rhs) const { |
| return complex_less(lhs, rhs); |
| } |
| }; |
| }; |
|
|
| template <> |
| struct select_less<thrust::tuple<size_t, complex<float>>> { |
| struct type { |
| __host__ __device__ __forceinline__ CONSTEXPR_COMPARATOR |
| bool operator() ( |
| const thrust::tuple<size_t, complex<float>>& lhs, const thrust::tuple<size_t, complex<float>>& rhs) const { |
| return tuple_less(lhs, rhs); |
| } |
| }; |
| }; |
|
|
| template <> |
| struct select_less<thrust::tuple<size_t, complex<double>>> { |
| struct type { |
| __host__ __device__ __forceinline__ CONSTEXPR_COMPARATOR |
| bool operator() ( |
| const thrust::tuple<size_t, complex<double>>& lhs, const thrust::tuple<size_t, complex<double>>& rhs) const { |
| return tuple_less(lhs, rhs); |
| } |
| }; |
| }; |
|
|
| template <> |
| struct select_less<thrust::tuple<size_t, float>> { |
| struct type { |
| __host__ __device__ __forceinline__ CONSTEXPR_COMPARATOR |
| bool operator() ( |
| const thrust::tuple<size_t, float>& lhs, const thrust::tuple<size_t, float>& rhs) const { |
| return tuple_less(lhs, rhs); |
| } |
| }; |
| }; |
|
|
| template <> |
| struct select_less<thrust::tuple<size_t, double>> { |
| struct type { |
| __host__ __device__ __forceinline__ CONSTEXPR_COMPARATOR |
| bool operator() ( |
| const thrust::tuple<size_t, double>& lhs, const thrust::tuple<size_t, double>& rhs) const { |
| return tuple_less(lhs, rhs); |
| } |
| }; |
| }; |
|
|
| |
|
|
| template <> |
| struct select_less<float> { |
| struct type { |
| __host__ __device__ __forceinline__ CONSTEXPR_COMPARATOR |
| bool operator() (const float& lhs, const float& rhs) const { |
| return real_less(lhs, rhs); |
| } |
| }; |
| }; |
|
|
| template <> |
| struct select_less<double> { |
| struct type { |
| __host__ __device__ __forceinline__ CONSTEXPR_COMPARATOR |
| bool operator() (const double& lhs, const double& rhs) const { |
| return real_less(lhs, rhs); |
| } |
| }; |
| }; |
|
|
| #ifdef ENABLE_HALF |
| template <> |
| struct select_less<__half> { |
| struct type { |
| __host__ __device__ __forceinline__ CONSTEXPR_COMPARATOR |
| bool operator() (const __half& lhs, const __half& rhs) const { |
| return real_less(lhs, rhs); |
| } |
| }; |
| }; |
|
|
| template <> |
| struct select_less<thrust::tuple<size_t, __half>> { |
| struct type { |
| __host__ __device__ __forceinline__ CONSTEXPR_COMPARATOR |
| bool operator() ( |
| const thrust::tuple<size_t, __half>& lhs, const thrust::tuple<size_t, __half>& rhs) const { |
|
|
| return tuple_less(lhs, rhs); |
| } |
| }; |
| }; |
| #endif |
|
|
| |
| |
| |
|
|
|
|
| |
| |
| |
|
|
| struct _sort { |
| template <typename T> |
| __forceinline__ void operator()(void *data_start, size_t *keys_start, |
| const std::vector<ptrdiff_t>& shape, intptr_t stream, |
| void* memory) { |
| size_t ndim = shape.size(); |
| ptrdiff_t size; |
| thrust::device_ptr<T> dp_data_first, dp_data_last; |
| thrust::device_ptr<size_t> dp_keys_first, dp_keys_last; |
| cudaStream_t stream_ = (cudaStream_t)stream; |
| cupy_allocator alloc(memory); |
|
|
| |
| size = shape[0]; |
| for (size_t i = 1; i < ndim; ++i) { |
| size *= shape[i]; |
| } |
|
|
| dp_data_first = thrust::device_pointer_cast(static_cast<T*>(data_start)); |
| dp_data_last = thrust::device_pointer_cast(static_cast<T*>(data_start) + size); |
|
|
| if (ndim == 1) { |
| |
| using compare_op = std::conditional_t<std::is_floating_point<T>::value, thrust::less<T>, typename select_less<T>::type>; |
| stable_sort(cuda::par(alloc).on(stream_), dp_data_first, dp_data_last, compare_op{}); |
| } else { |
| |
| dp_keys_first = thrust::device_pointer_cast(keys_start); |
| dp_keys_last = thrust::device_pointer_cast(keys_start + size); |
| transform(cuda::par(alloc).on(stream_), |
| #ifdef __HIP_PLATFORM_HCC__ |
| rocprim::make_counting_iterator<size_t>(0), |
| rocprim::make_counting_iterator<size_t>(size), |
| rocprim::make_constant_iterator<ptrdiff_t>(shape[ndim-1]), |
| #else |
| thrust::make_counting_iterator<size_t>(0), |
| thrust::make_counting_iterator<size_t>(size), |
| thrust::make_constant_iterator<ptrdiff_t>(shape[ndim-1]), |
| #endif |
| dp_keys_first, |
| thrust::divides<size_t>()); |
|
|
| stable_sort( |
| cuda::par(alloc).on(stream_), |
| make_zip_iterator(dp_keys_first, dp_data_first), |
| make_zip_iterator(dp_keys_last, dp_data_last), |
| typename select_less<thrust::tuple<size_t, T>>::type{}); |
| } |
| } |
| }; |
|
|
|
|
| |
| |
| |
|
|
| template <typename T> |
| class elem_less { |
| public: |
| elem_less(const T *data):_data(data) {} |
| __device__ __forceinline__ bool operator()(size_t i, size_t j) const { |
| return typename select_less<T>::type{}(_data[i], _data[j]); |
| } |
| private: |
| const T *_data; |
| }; |
|
|
| struct _lexsort { |
| template <typename T> |
| __forceinline__ void operator()(size_t *idx_start, void *keys_start, size_t k, |
| size_t n, intptr_t stream, void *memory) { |
| |
| |
| |
| thrust::device_ptr<size_t> dp_first = thrust::device_pointer_cast(idx_start); |
| thrust::device_ptr<size_t> dp_last = thrust::device_pointer_cast(idx_start + n); |
| cudaStream_t stream_ = (cudaStream_t)stream; |
| cupy_allocator alloc(memory); |
| sequence(cuda::par(alloc).on(stream_), dp_first, dp_last); |
| for (size_t i = 0; i < k; ++i) { |
| T *key_start = static_cast<T*>(keys_start) + i * n; |
| stable_sort( |
| cuda::par(alloc).on(stream_), |
| dp_first, |
| dp_last, |
| elem_less<T>(key_start) |
| ); |
| } |
| } |
| }; |
|
|
|
|
| |
| |
| |
|
|
| struct _argsort { |
| template <typename T> |
| __forceinline__ void operator()(size_t *idx_start, void *data_start, |
| void *keys_start, |
| const std::vector<ptrdiff_t>& shape, |
| intptr_t stream, void *memory) { |
| |
| |
| |
|
|
| size_t ndim = shape.size(); |
| ptrdiff_t size; |
| cudaStream_t stream_ = (cudaStream_t)stream; |
| cupy_allocator alloc(memory); |
|
|
| thrust::device_ptr<size_t> dp_idx_first, dp_idx_last; |
| thrust::device_ptr<T> dp_data_first, dp_data_last; |
| thrust::device_ptr<size_t> dp_keys_first, dp_keys_last; |
|
|
| |
| size = shape[0]; |
| for (size_t i = 1; i < ndim; ++i) { |
| size *= shape[i]; |
| } |
|
|
| |
| dp_data_first = thrust::device_pointer_cast(static_cast<T*>(data_start)); |
| dp_data_last = thrust::device_pointer_cast(static_cast<T*>(data_start) + size); |
|
|
| |
| dp_idx_first = thrust::device_pointer_cast(static_cast<size_t*>(idx_start)); |
| dp_idx_last = thrust::device_pointer_cast(static_cast<size_t*>(idx_start) + size); |
| transform(cuda::par(alloc).on(stream_), |
| #ifdef __HIP_PLATFORM_HCC__ |
| rocprim::make_counting_iterator<size_t>(0), |
| rocprim::make_counting_iterator<size_t>(size), |
| rocprim::make_constant_iterator<ptrdiff_t>(shape[ndim-1]), |
| #else |
| thrust::make_counting_iterator<size_t>(0), |
| thrust::make_counting_iterator<size_t>(size), |
| thrust::make_constant_iterator<ptrdiff_t>(shape[ndim-1]), |
| #endif |
| dp_idx_first, |
| thrust::modulus<size_t>()); |
|
|
| if (ndim == 1) { |
| |
| using compare_op = std::conditional_t<std::is_floating_point<T>::value, thrust::less<T>, typename select_less<T>::type>; |
| |
| stable_sort_by_key(cuda::par(alloc).on(stream_), |
| dp_data_first, |
| dp_data_last, |
| dp_idx_first, |
| compare_op{}); |
| } else { |
| |
| dp_keys_first = thrust::device_pointer_cast(static_cast<size_t*>(keys_start)); |
| dp_keys_last = thrust::device_pointer_cast(static_cast<size_t*>(keys_start) + size); |
| transform(cuda::par(alloc).on(stream_), |
| #ifdef __HIP_PLATFORM_HCC__ |
| rocprim::make_counting_iterator<size_t>(0), |
| rocprim::make_counting_iterator<size_t>(size), |
| rocprim::make_constant_iterator<ptrdiff_t>(shape[ndim-1]), |
| #else |
| thrust::make_counting_iterator<size_t>(0), |
| thrust::make_counting_iterator<size_t>(size), |
| thrust::make_constant_iterator<ptrdiff_t>(shape[ndim-1]), |
| #endif |
| dp_keys_first, |
| thrust::divides<size_t>()); |
|
|
| stable_sort_by_key( |
| cuda::par(alloc).on(stream_), |
| make_zip_iterator(dp_keys_first, dp_data_first), |
| make_zip_iterator(dp_keys_last, dp_data_last), |
| dp_idx_first, |
| typename select_less<thrust::tuple<size_t, T>>::type{}); |
| } |
| } |
| }; |
|
|
|
|
| |
| |
| |
|
|
| |
|
|
| void thrust_sort(int dtype_id, void *data_start, size_t *keys_start, |
| const std::vector<ptrdiff_t>& shape, intptr_t stream, void* memory) { |
|
|
| _sort op; |
| return dtype_dispatcher(dtype_id, op, data_start, keys_start, shape, stream, memory); |
| } |
|
|
|
|
| |
| void thrust_lexsort(int dtype_id, size_t *idx_start, void *keys_start, size_t k, |
| size_t n, intptr_t stream, void *memory) { |
|
|
| _lexsort op; |
| return dtype_dispatcher(dtype_id, op, idx_start, keys_start, k, n, stream, memory); |
| } |
|
|
|
|
| |
| void thrust_argsort(int dtype_id, size_t *idx_start, void *data_start, |
| void *keys_start, const std::vector<ptrdiff_t>& shape, intptr_t stream, void *memory) { |
|
|
| _argsort op; |
| return dtype_dispatcher(dtype_id, op, idx_start, data_start, keys_start, shape, |
| stream, memory); |
| } |
|
|
|
|