| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| |
| |
| |
|
|
| #pragma once |
|
|
| #include <thrust/detail/config.h> |
|
|
| #if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) |
| # pragma GCC system_header |
| #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) |
| # pragma clang system_header |
| #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) |
| # pragma system_header |
| #endif |
| #include <thrust/detail/get_iterator_value.h> |
| #include <thrust/extrema.h> |
| #include <thrust/functional.h> |
| #include <thrust/pair.h> |
| #include <thrust/reduce.h> |
| #include <thrust/transform_reduce.h> |
|
|
| #include <thrust/iterator/iterator_traits.h> |
| #include <thrust/iterator/counting_iterator.h> |
| #include <thrust/iterator/zip_iterator.h> |
|
|
| THRUST_NAMESPACE_BEGIN |
| namespace system |
| { |
| namespace detail |
| { |
| namespace generic |
| { |
| namespace detail |
| { |
|
|
|
|
| |
| |
| |
| |
|
|
| |
| |
| template <typename InputType, typename IndexType, typename BinaryPredicate> |
| struct min_element_reduction |
| { |
| BinaryPredicate comp; |
|
|
| __host__ __device__ |
| min_element_reduction(BinaryPredicate comp) : comp(comp){} |
|
|
| __host__ __device__ |
| thrust::tuple<InputType, IndexType> |
| operator()(const thrust::tuple<InputType, IndexType>& lhs, |
| const thrust::tuple<InputType, IndexType>& rhs ) |
| { |
| if(comp(thrust::get<0>(lhs), thrust::get<0>(rhs))) |
| return lhs; |
| if(comp(thrust::get<0>(rhs), thrust::get<0>(lhs))) |
| return rhs; |
|
|
| |
| if(thrust::get<1>(lhs) < thrust::get<1>(rhs)) |
| return lhs; |
| else |
| return rhs; |
| } |
| }; |
|
|
|
|
| template <typename InputType, typename IndexType, typename BinaryPredicate> |
| struct max_element_reduction |
| { |
| BinaryPredicate comp; |
|
|
| __host__ __device__ |
| max_element_reduction(BinaryPredicate comp) : comp(comp){} |
|
|
| __host__ __device__ |
| thrust::tuple<InputType, IndexType> |
| operator()(const thrust::tuple<InputType, IndexType>& lhs, |
| const thrust::tuple<InputType, IndexType>& rhs ) |
| { |
| if(comp(thrust::get<0>(lhs), thrust::get<0>(rhs))) |
| return rhs; |
| if(comp(thrust::get<0>(rhs), thrust::get<0>(lhs))) |
| return lhs; |
|
|
| |
| if(thrust::get<1>(lhs) < thrust::get<1>(rhs)) |
| return lhs; |
| else |
| return rhs; |
| } |
| }; |
|
|
|
|
| |
| |
| template <typename InputType, typename IndexType, typename BinaryPredicate> |
| struct minmax_element_reduction |
| { |
| BinaryPredicate comp; |
|
|
| __host__ __device__ |
| minmax_element_reduction(BinaryPredicate comp) : comp(comp){} |
|
|
| __host__ __device__ |
| thrust::tuple< thrust::tuple<InputType,IndexType>, thrust::tuple<InputType,IndexType> > |
| operator()(const thrust::tuple< thrust::tuple<InputType,IndexType>, thrust::tuple<InputType,IndexType> >& lhs, |
| const thrust::tuple< thrust::tuple<InputType,IndexType>, thrust::tuple<InputType,IndexType> >& rhs ) |
| { |
|
|
| return thrust::make_tuple(min_element_reduction<InputType, IndexType, BinaryPredicate>(comp)(thrust::get<0>(lhs), thrust::get<0>(rhs)), |
| max_element_reduction<InputType, IndexType, BinaryPredicate>(comp)(thrust::get<1>(lhs), thrust::get<1>(rhs))); |
| } |
| }; |
|
|
|
|
| template <typename InputType, typename IndexType> |
| struct duplicate_tuple |
| { |
| __host__ __device__ |
| thrust::tuple< thrust::tuple<InputType,IndexType>, thrust::tuple<InputType,IndexType> > |
| operator()(const thrust::tuple<InputType,IndexType>& t) |
| { |
| return thrust::make_tuple(t, t); |
| } |
| }; |
|
|
|
|
| } |
|
|
|
|
| template <typename DerivedPolicy, typename ForwardIterator> |
| __host__ __device__ |
| ForwardIterator min_element(thrust::execution_policy<DerivedPolicy> &exec, |
| ForwardIterator first, |
| ForwardIterator last) |
| { |
| typedef typename thrust::iterator_value<ForwardIterator>::type value_type; |
|
|
| return thrust::min_element(exec, first, last, thrust::less<value_type>()); |
| } |
|
|
|
|
| template <typename DerivedPolicy, typename ForwardIterator, typename BinaryPredicate> |
| __host__ __device__ |
| ForwardIterator min_element(thrust::execution_policy<DerivedPolicy> &exec, |
| ForwardIterator first, |
| ForwardIterator last, |
| BinaryPredicate comp) |
| { |
| if (first == last) |
| return last; |
|
|
| typedef typename thrust::iterator_traits<ForwardIterator>::value_type InputType; |
| typedef typename thrust::iterator_traits<ForwardIterator>::difference_type IndexType; |
|
|
| thrust::tuple<InputType, IndexType> result = |
| thrust::reduce |
| (exec, |
| thrust::make_zip_iterator(thrust::make_tuple(first, thrust::counting_iterator<IndexType>(0))), |
| thrust::make_zip_iterator(thrust::make_tuple(first, thrust::counting_iterator<IndexType>(0))) + (last - first), |
| thrust::tuple<InputType, IndexType>(thrust::detail::get_iterator_value(derived_cast(exec), first), 0), |
| detail::min_element_reduction<InputType, IndexType, BinaryPredicate>(comp)); |
|
|
| return first + thrust::get<1>(result); |
| } |
|
|
|
|
| template <typename DerivedPolicy, typename ForwardIterator> |
| __host__ __device__ |
| ForwardIterator max_element(thrust::execution_policy<DerivedPolicy> &exec, |
| ForwardIterator first, |
| ForwardIterator last) |
| { |
| typedef typename thrust::iterator_value<ForwardIterator>::type value_type; |
|
|
| return thrust::max_element(exec, first, last, thrust::less<value_type>()); |
| } |
|
|
|
|
| template <typename DerivedPolicy, typename ForwardIterator, typename BinaryPredicate> |
| __host__ __device__ |
| ForwardIterator max_element(thrust::execution_policy<DerivedPolicy> &exec, |
| ForwardIterator first, |
| ForwardIterator last, |
| BinaryPredicate comp) |
| { |
| if (first == last) |
| return last; |
|
|
| typedef typename thrust::iterator_traits<ForwardIterator>::value_type InputType; |
| typedef typename thrust::iterator_traits<ForwardIterator>::difference_type IndexType; |
|
|
| thrust::tuple<InputType, IndexType> result = |
| thrust::reduce |
| (exec, |
| thrust::make_zip_iterator(thrust::make_tuple(first, thrust::counting_iterator<IndexType>(0))), |
| thrust::make_zip_iterator(thrust::make_tuple(first, thrust::counting_iterator<IndexType>(0))) + (last - first), |
| thrust::tuple<InputType, IndexType>(thrust::detail::get_iterator_value(derived_cast(exec),first), 0), |
| detail::max_element_reduction<InputType, IndexType, BinaryPredicate>(comp)); |
|
|
| return first + thrust::get<1>(result); |
| } |
|
|
|
|
| template <typename DerivedPolicy, typename ForwardIterator> |
| __host__ __device__ |
| thrust::pair<ForwardIterator,ForwardIterator> minmax_element(thrust::execution_policy<DerivedPolicy> &exec, |
| ForwardIterator first, |
| ForwardIterator last) |
| { |
| typedef typename thrust::iterator_value<ForwardIterator>::type value_type; |
|
|
| return thrust::minmax_element(exec, first, last, thrust::less<value_type>()); |
| } |
|
|
|
|
| template <typename DerivedPolicy, typename ForwardIterator, typename BinaryPredicate> |
| __host__ __device__ |
| thrust::pair<ForwardIterator,ForwardIterator> minmax_element(thrust::execution_policy<DerivedPolicy> &exec, |
| ForwardIterator first, |
| ForwardIterator last, |
| BinaryPredicate comp) |
| { |
| if (first == last) |
| return thrust::make_pair(last, last); |
|
|
| typedef typename thrust::iterator_traits<ForwardIterator>::value_type InputType; |
| typedef typename thrust::iterator_traits<ForwardIterator>::difference_type IndexType; |
|
|
| thrust::tuple< thrust::tuple<InputType,IndexType>, thrust::tuple<InputType,IndexType> > result = |
| thrust::transform_reduce |
| (exec, |
| thrust::make_zip_iterator(thrust::make_tuple(first, thrust::counting_iterator<IndexType>(0))), |
| thrust::make_zip_iterator(thrust::make_tuple(first, thrust::counting_iterator<IndexType>(0))) + (last - first), |
| detail::duplicate_tuple<InputType, IndexType>(), |
| detail::duplicate_tuple<InputType, IndexType>()( |
| thrust::tuple<InputType, IndexType>(thrust::detail::get_iterator_value(derived_cast(exec),first), 0)), |
| detail::minmax_element_reduction<InputType, IndexType, BinaryPredicate>(comp)); |
|
|
| return thrust::make_pair(first + thrust::get<1>(thrust::get<0>(result)), first + thrust::get<1>(thrust::get<1>(result))); |
| } |
|
|
|
|
| } |
| } |
| } |
| THRUST_NAMESPACE_END |
|
|
|
|