| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #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 |
|
|
| #if THRUST_DEVICE_COMPILER == THRUST_DEVICE_COMPILER_NVCC |
|
|
| #include <thrust/detail/alignment.h> |
| #include <thrust/detail/cstdint.h> |
| #include <thrust/detail/minmax.h> |
| #include <thrust/detail/raw_reference_cast.h> |
| #include <thrust/detail/temporary_array.h> |
| #include <thrust/detail/type_traits/iterator/is_output_iterator.h> |
| #include <thrust/distance.h> |
| #include <thrust/functional.h> |
| #include <thrust/system/cuda/config.h> |
| #include <thrust/system/cuda/detail/cdp_dispatch.h> |
| #include <thrust/system/cuda/detail/core/agent_launcher.h> |
| #include <thrust/system/cuda/detail/dispatch.h> |
| #include <thrust/system/cuda/detail/get_value.h> |
| #include <thrust/system/cuda/detail/make_unsigned_special.h> |
| #include <thrust/system/cuda/detail/par_to_seq.h> |
| #include <thrust/system/cuda/detail/util.h> |
|
|
| #include <cub/device/device_reduce.cuh> |
| #include <cub/util_math.cuh> |
|
|
| THRUST_NAMESPACE_BEGIN |
|
|
| |
| |
| template <typename DerivedPolicy, |
| typename InputIterator, |
| typename T, |
| typename BinaryFunction> |
| T __host__ __device__ |
| reduce(const thrust::detail::execution_policy_base<DerivedPolicy> &exec, |
| InputIterator first, |
| InputIterator last, |
| T init, |
| BinaryFunction binary_op); |
|
|
| namespace cuda_cub { |
|
|
| namespace __reduce { |
|
|
| template<bool> |
| struct is_true : thrust::detail::false_type {}; |
| template<> |
| struct is_true<true> : thrust::detail::true_type {}; |
|
|
| template <int _BLOCK_THREADS, |
| int _ITEMS_PER_THREAD = 1, |
| int _VECTOR_LOAD_LENGTH = 1, |
| cub::BlockReduceAlgorithm _BLOCK_ALGORITHM = cub::BLOCK_REDUCE_RAKING, |
| cub::CacheLoadModifier _LOAD_MODIFIER = cub::LOAD_DEFAULT, |
| cub::GridMappingStrategy _GRID_MAPPING = cub::GRID_MAPPING_DYNAMIC> |
| struct PtxPolicy |
| { |
| enum |
| { |
| BLOCK_THREADS = _BLOCK_THREADS, |
| ITEMS_PER_THREAD = _ITEMS_PER_THREAD, |
| VECTOR_LOAD_LENGTH = _VECTOR_LOAD_LENGTH, |
| ITEMS_PER_TILE = _BLOCK_THREADS * _ITEMS_PER_THREAD |
| }; |
|
|
| static const cub::BlockReduceAlgorithm BLOCK_ALGORITHM = _BLOCK_ALGORITHM; |
| static const cub::CacheLoadModifier LOAD_MODIFIER = _LOAD_MODIFIER; |
| static const cub::GridMappingStrategy GRID_MAPPING = _GRID_MAPPING; |
| }; |
|
|
| template<class,class> |
| struct Tuning; |
|
|
| template <class T> |
| struct Tuning<sm30, T> |
| { |
| enum |
| { |
| |
| SCALE_FACTOR_4B = (sizeof(T) + 3) / 4, |
| |
| SCALE_FACTOR_1B = sizeof(T), |
| }; |
|
|
| typedef PtxPolicy<256, |
| CUB_MAX(1, 20 / SCALE_FACTOR_4B), |
| 2, |
| cub::BLOCK_REDUCE_WARP_REDUCTIONS, |
| cub::LOAD_DEFAULT, |
| cub::GRID_MAPPING_RAKE> |
| type; |
| }; |
|
|
| template <class T> |
| struct Tuning<sm35, T> : Tuning<sm30,T> |
| { |
| |
| typedef PtxPolicy<128, |
| CUB_MAX(1, 24 / Tuning::SCALE_FACTOR_1B), |
| 4, |
| cub::BLOCK_REDUCE_WARP_REDUCTIONS, |
| cub::LOAD_LDG, |
| cub::GRID_MAPPING_DYNAMIC> |
| ReducePolicy1B; |
|
|
| |
| typedef PtxPolicy<256, |
| CUB_MAX(1, 20 / Tuning::SCALE_FACTOR_4B), |
| 4, |
| cub::BLOCK_REDUCE_WARP_REDUCTIONS, |
| cub::LOAD_LDG, |
| cub::GRID_MAPPING_DYNAMIC> |
| ReducePolicy4B; |
|
|
| typedef typename thrust::detail::conditional<(sizeof(T) < 4), |
| ReducePolicy1B, |
| ReducePolicy4B>::type type; |
| }; |
|
|
| template <class InputIt, |
| class OutputIt, |
| class T, |
| class Size, |
| class ReductionOp> |
| struct ReduceAgent |
| { |
| typedef typename detail::make_unsigned_special<Size>::type UnsignedSize; |
|
|
| template<class Arch> |
| struct PtxPlan : Tuning<Arch,T>::type |
| { |
| |
| |
| |
| |
| typedef Tuning<Arch,T> tuning; |
|
|
| typedef typename cub::CubVector<T, PtxPlan::VECTOR_LOAD_LENGTH> Vector; |
| typedef typename core::LoadIterator<PtxPlan, InputIt>::type LoadIt; |
| typedef cub::BlockReduce<T, |
| PtxPlan::BLOCK_THREADS, |
| PtxPlan::BLOCK_ALGORITHM, |
| 1, |
| 1, |
| Arch::ver> |
| BlockReduce; |
|
|
| typedef cub::CacheModifiedInputIterator<PtxPlan::LOAD_MODIFIER, |
| Vector, |
| Size> |
| VectorLoadIt; |
|
|
| struct TempStorage |
| { |
| typename BlockReduce::TempStorage reduce; |
| |
| Size dequeue_offset; |
| }; |
|
|
|
|
| }; |
|
|
| |
| |
| |
| |
| |
| |
| struct Plan : core::AgentPlan |
| { |
| cub::GridMappingStrategy grid_mapping; |
|
|
| THRUST_RUNTIME_FUNCTION |
| Plan() {} |
|
|
| template <class P> |
| THRUST_RUNTIME_FUNCTION |
| Plan(P) : core::AgentPlan(P()), |
| grid_mapping(P::GRID_MAPPING) |
| { |
| } |
| }; |
|
|
| |
| |
| |
| |
| typedef typename core::specialize_plan_msvc10_war<PtxPlan>::type::type ptx_plan; |
|
|
| typedef typename ptx_plan::TempStorage TempStorage; |
| typedef typename ptx_plan::Vector Vector; |
| typedef typename ptx_plan::LoadIt LoadIt; |
| typedef typename ptx_plan::BlockReduce BlockReduce; |
| typedef typename ptx_plan::VectorLoadIt VectorLoadIt; |
|
|
| enum |
| { |
| ITEMS_PER_THREAD = ptx_plan::ITEMS_PER_THREAD, |
| BLOCK_THREADS = ptx_plan::BLOCK_THREADS, |
| ITEMS_PER_TILE = ptx_plan::ITEMS_PER_TILE, |
| VECTOR_LOAD_LENGTH = ptx_plan::VECTOR_LOAD_LENGTH, |
|
|
| ATTEMPT_VECTORIZATION = (VECTOR_LOAD_LENGTH > 1) && |
| (ITEMS_PER_THREAD % VECTOR_LOAD_LENGTH == 0) && |
| thrust::detail::is_pointer<InputIt>::value && |
| thrust::detail::is_arithmetic< |
| typename thrust::detail::remove_cv<T> >::value |
| }; |
|
|
| struct impl |
| { |
| |
| |
| |
|
|
| TempStorage &storage; |
| InputIt input_it; |
| LoadIt load_it; |
| ReductionOp reduction_op; |
|
|
| |
| |
| |
|
|
| THRUST_DEVICE_FUNCTION impl(TempStorage &storage_, |
| InputIt input_it_, |
| ReductionOp reduction_op_) |
| : storage(storage_), |
| input_it(input_it_), |
| load_it(core::make_load_iterator(ptx_plan(), input_it)), |
| reduction_op(reduction_op_) {} |
|
|
| |
| |
| |
|
|
|
|
| |
| |
| |
| template <class Iterator> |
| static THRUST_DEVICE_FUNCTION bool |
| is_aligned(Iterator d_in, |
| thrust::detail::true_type ) |
| { |
| return (size_t(d_in) & (sizeof(Vector) - 1)) == 0; |
| } |
|
|
| |
| |
| |
| template <class Iterator> |
| static THRUST_DEVICE_FUNCTION bool |
| is_aligned(Iterator, |
| thrust::detail::false_type ) |
| { |
| return false; |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| template <int IS_FIRST_TILE> |
| THRUST_DEVICE_FUNCTION void |
| consume_tile(T & thread_aggregate, |
| Size block_offset, |
| int , |
| thrust::detail::true_type , |
| thrust::detail::false_type ) |
| { |
| T items[ITEMS_PER_THREAD]; |
|
|
| |
| cub::LoadDirectStriped<BLOCK_THREADS>(threadIdx.x, |
| load_it + block_offset, |
| items); |
|
|
| |
| thread_aggregate = |
| (IS_FIRST_TILE) ? cub::internal::ThreadReduce(items, reduction_op) |
| : cub::internal::ThreadReduce(items, reduction_op, |
| thread_aggregate); |
| } |
|
|
| |
| |
| template <int IS_FIRST_TILE> |
| THRUST_DEVICE_FUNCTION void |
| consume_tile(T & thread_aggregate, |
| Size block_offset, |
| int , |
| thrust::detail::true_type , |
| thrust::detail::true_type ) |
| { |
| |
| enum |
| { |
| WORDS = ITEMS_PER_THREAD / VECTOR_LOAD_LENGTH |
| }; |
|
|
| T items[ITEMS_PER_THREAD]; |
|
|
| Vector *vec_items = reinterpret_cast<Vector *>(items); |
|
|
| |
| T *d_in_unqualified = const_cast<T *>(input_it) + |
| block_offset + |
| (threadIdx.x * VECTOR_LOAD_LENGTH); |
| VectorLoadIt vec_load_it(reinterpret_cast<Vector *>(d_in_unqualified)); |
|
|
| #pragma unroll |
| for (int i = 0; i < WORDS; ++i) |
| { |
| vec_items[i] = vec_load_it[BLOCK_THREADS * i]; |
| } |
|
|
|
|
| |
| thread_aggregate = |
| (IS_FIRST_TILE) ? cub::internal::ThreadReduce(items, reduction_op) |
| : cub::internal::ThreadReduce(items, reduction_op, |
| thread_aggregate); |
| } |
|
|
|
|
| |
| |
| template <int IS_FIRST_TILE, class CAN_VECTORIZE> |
| THRUST_DEVICE_FUNCTION void |
| consume_tile(T & thread_aggregate, |
| Size block_offset, |
| int valid_items, |
| thrust::detail::false_type , |
| CAN_VECTORIZE) |
| { |
| |
| int thread_offset = threadIdx.x; |
|
|
| |
| if ((IS_FIRST_TILE) && (thread_offset < valid_items)) |
| { |
| thread_aggregate = load_it[block_offset + thread_offset]; |
| thread_offset += BLOCK_THREADS; |
| } |
|
|
| |
| while (thread_offset < valid_items) |
| { |
| thread_aggregate = reduction_op( |
| thread_aggregate, |
| thrust::raw_reference_cast(load_it[block_offset + thread_offset])); |
| thread_offset += BLOCK_THREADS; |
| } |
| } |
|
|
| |
| |
| |
|
|
|
|
| |
| |
| template <class CAN_VECTORIZE> |
| THRUST_DEVICE_FUNCTION T |
| consume_range_impl(Size block_offset, |
| Size block_end, |
| CAN_VECTORIZE can_vectorize) |
| { |
| T thread_aggregate; |
|
|
| if (block_offset + ITEMS_PER_TILE > block_end) |
| { |
| |
| int valid_items = block_end - block_offset; |
| consume_tile<true>(thread_aggregate, |
| block_offset, |
| valid_items, |
| thrust::detail::false_type(), |
| can_vectorize); |
| return BlockReduce(storage.reduce) |
| .Reduce(thread_aggregate, reduction_op, valid_items); |
| } |
|
|
| |
| consume_tile<true>(thread_aggregate, |
| block_offset, |
| ITEMS_PER_TILE, |
| thrust::detail::true_type(), |
| can_vectorize); |
| block_offset += ITEMS_PER_TILE; |
|
|
| |
| while (block_offset + ITEMS_PER_TILE <= block_end) |
| { |
| consume_tile<false>(thread_aggregate, |
| block_offset, |
| ITEMS_PER_TILE, |
| thrust::detail::true_type(), |
| can_vectorize); |
| block_offset += ITEMS_PER_TILE; |
| } |
|
|
| |
| if (block_offset < block_end) |
| { |
| int valid_items = block_end - block_offset; |
| consume_tile<false>(thread_aggregate, |
| block_offset, |
| valid_items, |
| thrust::detail::false_type(), |
| can_vectorize); |
| } |
|
|
| |
| return BlockReduce(storage.reduce) |
| .Reduce(thread_aggregate, reduction_op); |
| } |
|
|
| |
| |
| THRUST_DEVICE_FUNCTION T consume_range(Size block_offset, |
| Size block_end) |
| { |
| typedef is_true<ATTEMPT_VECTORIZATION> attempt_vec; |
| typedef is_true<true && ATTEMPT_VECTORIZATION> path_a; |
| typedef is_true<false && ATTEMPT_VECTORIZATION> path_b; |
|
|
| return is_aligned(input_it + block_offset, attempt_vec()) |
| ? consume_range_impl(block_offset, block_end, path_a()) |
| : consume_range_impl(block_offset, block_end, path_b()); |
| } |
|
|
| |
| |
| THRUST_DEVICE_FUNCTION T |
| consume_tiles(Size , |
| cub::GridEvenShare<Size> &even_share, |
| cub::GridQueue<UnsignedSize> & , |
| thrust::detail::integral_constant<cub::GridMappingStrategy, cub::GRID_MAPPING_RAKE> ) |
| { |
| typedef is_true<ATTEMPT_VECTORIZATION> attempt_vec; |
| typedef is_true<true && ATTEMPT_VECTORIZATION> path_a; |
| typedef is_true<false && ATTEMPT_VECTORIZATION> path_b; |
|
|
| |
| even_share |
| .template BlockInit<ITEMS_PER_TILE, cub::GRID_MAPPING_RAKE>(); |
|
|
| return is_aligned(input_it, attempt_vec()) |
| ? consume_range_impl(even_share.block_offset, |
| even_share.block_end, |
| path_a()) |
| : consume_range_impl(even_share.block_offset, |
| even_share.block_end, |
| path_b()); |
| } |
|
|
|
|
| |
| |
| |
|
|
| |
| |
| template <class CAN_VECTORIZE> |
| THRUST_DEVICE_FUNCTION T |
| consume_tiles_impl(Size num_items, |
| cub::GridQueue<UnsignedSize> queue, |
| CAN_VECTORIZE can_vectorize) |
| { |
| using core::sync_threadblock; |
|
|
| |
| T thread_aggregate; |
| Size block_offset = blockIdx.x * ITEMS_PER_TILE; |
| Size even_share_base = gridDim.x * ITEMS_PER_TILE; |
|
|
| if (block_offset + ITEMS_PER_TILE > num_items) |
| { |
| |
| int valid_items = num_items - block_offset; |
| consume_tile<true>(thread_aggregate, |
| block_offset, |
| valid_items, |
| thrust::detail::false_type(), |
| can_vectorize); |
| return BlockReduce(storage.reduce) |
| .Reduce(thread_aggregate, reduction_op, valid_items); |
| } |
|
|
| |
| consume_tile<true>(thread_aggregate, |
| block_offset, |
| ITEMS_PER_TILE, |
| thrust::detail::true_type(), |
| can_vectorize); |
|
|
| if (num_items > even_share_base) |
| { |
| |
| if (threadIdx.x == 0) |
| storage.dequeue_offset = queue.Drain(ITEMS_PER_TILE) + |
| even_share_base; |
|
|
| sync_threadblock(); |
|
|
| |
| block_offset = storage.dequeue_offset; |
|
|
| |
| while (block_offset + ITEMS_PER_TILE <= num_items) |
| { |
| consume_tile<false>(thread_aggregate, |
| block_offset, |
| ITEMS_PER_TILE, |
| thrust::detail::true_type(), |
| can_vectorize); |
|
|
| sync_threadblock(); |
|
|
| |
| if (threadIdx.x == 0) |
| storage.dequeue_offset = queue.Drain(ITEMS_PER_TILE) + |
| even_share_base; |
|
|
| sync_threadblock(); |
|
|
| |
| block_offset = storage.dequeue_offset; |
| } |
|
|
| |
| if (block_offset < num_items) |
| { |
| int valid_items = num_items - block_offset; |
| consume_tile<false>(thread_aggregate, |
| block_offset, |
| valid_items, |
| thrust::detail::false_type(), |
| can_vectorize); |
| } |
| } |
|
|
| |
| return BlockReduce(storage.reduce) |
| .Reduce(thread_aggregate, reduction_op); |
| } |
|
|
|
|
| |
| |
| THRUST_DEVICE_FUNCTION T |
| consume_tiles( |
| Size num_items, |
| cub::GridEvenShare<Size> &, |
| cub::GridQueue<UnsignedSize> & queue, |
| thrust::detail::integral_constant<cub::GridMappingStrategy, cub::GRID_MAPPING_DYNAMIC>) |
| { |
| typedef is_true<ATTEMPT_VECTORIZATION> attempt_vec; |
| typedef is_true<true && ATTEMPT_VECTORIZATION> path_a; |
| typedef is_true<false && ATTEMPT_VECTORIZATION> path_b; |
|
|
| return is_aligned(input_it, attempt_vec()) |
| ? consume_tiles_impl(num_items, queue, path_a()) |
| : consume_tiles_impl(num_items, queue, path_b()); |
| } |
| }; |
|
|
| |
| |
| |
|
|
| |
| |
| THRUST_AGENT_ENTRY(InputIt input_it, |
| OutputIt output_it, |
| Size num_items, |
| ReductionOp reduction_op, |
| char * shmem) |
| { |
| TempStorage& storage = *reinterpret_cast<TempStorage*>(shmem); |
|
|
| if (num_items == 0) |
| { |
| return; |
| } |
|
|
| T block_aggregate = |
| impl(storage, input_it, reduction_op).consume_range((Size)0, num_items); |
|
|
| if (threadIdx.x == 0) |
| *output_it = block_aggregate; |
| } |
|
|
| |
| |
| THRUST_AGENT_ENTRY(InputIt input_it, |
| OutputIt output_it, |
| Size num_items, |
| ReductionOp reduction_op, |
| T init, |
| char * shmem) |
| { |
| TempStorage& storage = *reinterpret_cast<TempStorage*>(shmem); |
|
|
| if (num_items == 0) |
| { |
| if (threadIdx.x == 0) |
| *output_it = init; |
| return; |
| } |
|
|
| T block_aggregate = |
| impl(storage, input_it, reduction_op).consume_range((Size)0, num_items); |
|
|
| if (threadIdx.x == 0) |
| *output_it = reduction_op(init, block_aggregate); |
| } |
|
|
| THRUST_AGENT_ENTRY(InputIt input_it, |
| OutputIt output_it, |
| Size num_items, |
| cub::GridEvenShare<Size> even_share, |
| cub::GridQueue<UnsignedSize> queue, |
| ReductionOp reduction_op, |
| char * shmem) |
| { |
| TempStorage& storage = *reinterpret_cast<TempStorage*>(shmem); |
|
|
| typedef thrust::detail::integral_constant<cub::GridMappingStrategy, ptx_plan::GRID_MAPPING> grid_mapping; |
|
|
| T block_aggregate = |
| impl(storage, input_it, reduction_op) |
| .consume_tiles(num_items, even_share, queue, grid_mapping()); |
|
|
| if (threadIdx.x == 0) |
| output_it[blockIdx.x] = block_aggregate; |
| } |
| }; |
|
|
| template<class Size> |
| struct DrainAgent |
| { |
| typedef typename detail::make_unsigned_special<Size>::type UnsignedSize; |
|
|
| template <class Arch> |
| struct PtxPlan : PtxPolicy<1> {}; |
| typedef core::specialize_plan<PtxPlan> ptx_plan; |
|
|
| |
| |
| |
|
|
| THRUST_AGENT_ENTRY(cub::GridQueue<UnsignedSize> grid_queue, |
| Size num_items, |
| char * ) |
| { |
| grid_queue.FillAndResetDrain(num_items); |
| } |
| }; |
|
|
|
|
| template <class InputIt, |
| class OutputIt, |
| class Size, |
| class ReductionOp, |
| class T> |
| cudaError_t THRUST_RUNTIME_FUNCTION |
| doit_step(void * d_temp_storage, |
| size_t & temp_storage_bytes, |
| InputIt input_it, |
| Size num_items, |
| T init, |
| ReductionOp reduction_op, |
| OutputIt output_it, |
| cudaStream_t stream) |
| { |
| using core::AgentPlan; |
| using core::AgentLauncher; |
| using core::get_agent_plan; |
| using core::cuda_optional; |
|
|
| typedef typename detail::make_unsigned_special<Size>::type UnsignedSize; |
|
|
| if (num_items == 0) |
| return cudaErrorNotSupported; |
|
|
| typedef AgentLauncher< |
| ReduceAgent<InputIt, OutputIt, T, Size, ReductionOp> > |
| reduce_agent; |
|
|
| typename reduce_agent::Plan reduce_plan = reduce_agent::get_plan(stream); |
|
|
| cudaError_t status = cudaSuccess; |
|
|
|
|
| if (num_items <= reduce_plan.items_per_tile) |
| { |
| size_t vshmem_size = core::vshmem_size(reduce_plan.shared_memory_size, 1); |
|
|
| |
| if (d_temp_storage == NULL) |
| { |
| temp_storage_bytes = max<size_t>(1, vshmem_size); |
| return status; |
| } |
| char *vshmem_ptr = vshmem_size > 0 ? (char*)d_temp_storage : NULL; |
|
|
| reduce_agent ra(reduce_plan, num_items, stream, vshmem_ptr, "reduce_agent: single_tile only"); |
| ra.launch(input_it, output_it, num_items, reduction_op, init); |
| CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); |
| } |
| else |
| { |
| |
| cuda_optional<int> sm_count = core::get_sm_count(); |
| CUDA_CUB_RET_IF_FAIL(sm_count.status()); |
|
|
| |
| cuda_optional<int> max_blocks_per_sm = |
| reduce_agent:: |
| template get_max_blocks_per_sm<InputIt, |
| OutputIt, |
| Size, |
| cub::GridEvenShare<Size>, |
| cub::GridQueue<UnsignedSize>, |
| ReductionOp>(reduce_plan); |
| CUDA_CUB_RET_IF_FAIL(max_blocks_per_sm.status()); |
|
|
|
|
|
|
| int reduce_device_occupancy = (int)max_blocks_per_sm * sm_count; |
|
|
| int sm_oversubscription = 5; |
| int max_blocks = reduce_device_occupancy * sm_oversubscription; |
|
|
| cub::GridEvenShare<Size> even_share; |
| even_share.DispatchInit(static_cast<int>(num_items), max_blocks, |
| reduce_plan.items_per_tile); |
|
|
| |
| |
| |
| size_t vshmem_size = core::vshmem_size(reduce_plan.shared_memory_size, |
| max_blocks); |
|
|
| |
| void * allocations[3] = {NULL, NULL, NULL}; |
| size_t allocation_sizes[3] = |
| { |
| max_blocks * sizeof(T), |
| cub::GridQueue<UnsignedSize>::AllocationSize(), |
| vshmem_size |
| }; |
| status = cub::AliasTemporaries(d_temp_storage, |
| temp_storage_bytes, |
| allocations, |
| allocation_sizes); |
| CUDA_CUB_RET_IF_FAIL(status); |
| if (d_temp_storage == NULL) |
| { |
| return status; |
| } |
|
|
| T *d_block_reductions = (T*) allocations[0]; |
| cub::GridQueue<UnsignedSize> queue(allocations[1]); |
| char *vshmem_ptr = vshmem_size > 0 ? (char *)allocations[2] : NULL; |
|
|
|
|
| |
| int reduce_grid_size = 0; |
| if (reduce_plan.grid_mapping == cub::GRID_MAPPING_RAKE) |
| { |
| |
| reduce_grid_size = even_share.grid_size; |
| } |
| else if (reduce_plan.grid_mapping == cub::GRID_MAPPING_DYNAMIC) |
| { |
| |
| size_t num_tiles = cub::DivideAndRoundUp(num_items, reduce_plan.items_per_tile); |
|
|
| |
| |
| reduce_grid_size = static_cast<int>((min)(num_tiles, static_cast<size_t>(reduce_device_occupancy))); |
|
|
| typedef AgentLauncher<DrainAgent<Size> > drain_agent; |
| AgentPlan drain_plan = drain_agent::get_plan(); |
| drain_plan.grid_size = 1; |
| drain_agent da(drain_plan, stream, "__reduce::drain_agent"); |
| da.launch(queue, num_items); |
| CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); |
| } |
| else |
| { |
| CUDA_CUB_RET_IF_FAIL(cudaErrorNotSupported); |
| } |
|
|
| reduce_plan.grid_size = reduce_grid_size; |
| reduce_agent ra(reduce_plan, stream, vshmem_ptr, "reduce_agent: regular size reduce"); |
| ra.launch(input_it, |
| d_block_reductions, |
| num_items, |
| even_share, |
| queue, |
| reduction_op); |
| CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); |
|
|
|
|
| typedef AgentLauncher< |
| ReduceAgent<T*, OutputIt, T, Size, ReductionOp> > |
| reduce_agent_single; |
|
|
| reduce_plan.grid_size = 1; |
| reduce_agent_single ra1(reduce_plan, stream, vshmem_ptr, "reduce_agent: single tile reduce"); |
|
|
| ra1.launch(d_block_reductions, output_it, reduce_grid_size, reduction_op, init); |
| CUDA_CUB_RET_IF_FAIL(cudaPeekAtLastError()); |
| } |
|
|
| return status; |
| } |
|
|
|
|
| template <typename Derived, |
| typename InputIt, |
| typename Size, |
| typename T, |
| typename BinaryOp> |
| THRUST_RUNTIME_FUNCTION |
| T reduce(execution_policy<Derived>& policy, |
| InputIt first, |
| Size num_items, |
| T init, |
| BinaryOp binary_op) |
| { |
| if (num_items == 0) |
| return init; |
|
|
| size_t temp_storage_bytes = 0; |
| cudaStream_t stream = cuda_cub::stream(policy); |
|
|
| cudaError_t status; |
| status = doit_step(NULL, |
| temp_storage_bytes, |
| first, |
| num_items, |
| init, |
| binary_op, |
| reinterpret_cast<T*>(NULL), |
| stream); |
| cuda_cub::throw_on_error(status, "reduce failed on 1st step"); |
|
|
| size_t allocation_sizes[2] = {sizeof(T*), temp_storage_bytes}; |
| void * allocations[2] = {NULL, NULL}; |
|
|
| size_t storage_size = 0; |
| status = core::alias_storage(NULL, |
| storage_size, |
| allocations, |
| allocation_sizes); |
| cuda_cub::throw_on_error(status, "reduce failed on 1st alias_storage"); |
|
|
| |
| thrust::detail::temporary_array<thrust::detail::uint8_t, Derived> |
| tmp(policy, storage_size); |
| void *ptr = static_cast<void*>(tmp.data().get()); |
|
|
| status = core::alias_storage(ptr, |
| storage_size, |
| allocations, |
| allocation_sizes); |
| cuda_cub::throw_on_error(status, "reduce failed on 2nd alias_storage"); |
|
|
| T* d_result = thrust::detail::aligned_reinterpret_cast<T*>(allocations[0]); |
|
|
| status = doit_step(allocations[1], |
| temp_storage_bytes, |
| first, |
| num_items, |
| init, |
| binary_op, |
| d_result, |
| stream); |
| cuda_cub::throw_on_error(status, "reduce failed on 2nd step"); |
|
|
| status = cuda_cub::synchronize(policy); |
| cuda_cub::throw_on_error(status, "reduce failed to synchronize"); |
|
|
| T result = cuda_cub::get_value(policy, d_result); |
|
|
| return result; |
| } |
| } |
|
|
| namespace detail { |
|
|
| template <typename Derived, |
| typename InputIt, |
| typename Size, |
| typename T, |
| typename BinaryOp> |
| THRUST_RUNTIME_FUNCTION |
| T reduce_n_impl(execution_policy<Derived>& policy, |
| InputIt first, |
| Size num_items, |
| T init, |
| BinaryOp binary_op) |
| { |
| cudaStream_t stream = cuda_cub::stream(policy); |
| cudaError_t status; |
|
|
| |
|
|
| size_t tmp_size = 0; |
|
|
| THRUST_INDEX_TYPE_DISPATCH(status, |
| cub::DeviceReduce::Reduce, |
| num_items, |
| (NULL, tmp_size, first, reinterpret_cast<T*>(NULL), |
| num_items_fixed, binary_op, init, stream)); |
| cuda_cub::throw_on_error(status, "after reduction step 1"); |
|
|
| |
|
|
| thrust::detail::temporary_array<thrust::detail::uint8_t, Derived> |
| tmp(policy, sizeof(T) + tmp_size); |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| T* ret_ptr = thrust::detail::aligned_reinterpret_cast<T*>(tmp.data().get()); |
| void* tmp_ptr = static_cast<void*>((tmp.data() + sizeof(T)).get()); |
| THRUST_INDEX_TYPE_DISPATCH(status, |
| cub::DeviceReduce::Reduce, |
| num_items, |
| (tmp_ptr, tmp_size, first, ret_ptr, |
| num_items_fixed, binary_op, init, stream)); |
| cuda_cub::throw_on_error(status, "after reduction step 2"); |
|
|
| |
|
|
| status = cuda_cub::synchronize(policy); |
| cuda_cub::throw_on_error(status, "reduce failed to synchronize"); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| return thrust::cuda_cub::get_value(policy, |
| thrust::detail::aligned_reinterpret_cast<T*>(tmp.data().get())); |
| } |
|
|
| } |
|
|
| |
| |
| |
|
|
| __thrust_exec_check_disable__ |
| template <typename Derived, |
| typename InputIt, |
| typename Size, |
| typename T, |
| typename BinaryOp> |
| __host__ __device__ |
| T reduce_n(execution_policy<Derived>& policy, |
| InputIt first, |
| Size num_items, |
| T init, |
| BinaryOp binary_op) |
| { |
| THRUST_CDP_DISPATCH((init = |
| thrust::cuda_cub::detail::reduce_n_impl(policy, |
| first, |
| num_items, |
| init, |
| binary_op);), |
| (init = thrust::reduce(cvt_to_seq(derived_cast(policy)), |
| first, |
| first + num_items, |
| init, |
| binary_op);)); |
| return init; |
| } |
|
|
| template <class Derived, class InputIt, class T, class BinaryOp> |
| __host__ __device__ |
| T reduce(execution_policy<Derived> &policy, |
| InputIt first, |
| InputIt last, |
| T init, |
| BinaryOp binary_op) |
| { |
| typedef typename iterator_traits<InputIt>::difference_type size_type; |
| |
| size_type num_items = static_cast<size_type>(thrust::distance(first, last)); |
| return cuda_cub::reduce_n(policy, first, num_items, init, binary_op); |
| } |
|
|
| template <class Derived, |
| class InputIt, |
| class T> |
| __host__ __device__ |
| T reduce(execution_policy<Derived> &policy, |
| InputIt first, |
| InputIt last, |
| T init) |
| { |
| return cuda_cub::reduce(policy, first, last, init, plus<T>()); |
| } |
|
|
| template <class Derived, |
| class InputIt> |
| __host__ __device__ |
| typename iterator_traits<InputIt>::value_type |
| reduce(execution_policy<Derived> &policy, |
| InputIt first, |
| InputIt last) |
| { |
| typedef typename iterator_traits<InputIt>::value_type value_type; |
| return cuda_cub::reduce(policy, first, last, value_type(0)); |
| } |
|
|
|
|
| } |
|
|
| THRUST_NAMESPACE_END |
|
|
| #include <thrust/memory.h> |
| #include <thrust/reduce.h> |
|
|
| #endif |
|
|