| | #include <thrust/detail/config.h> |
| | #include <thrust/device_vector.h> |
| | #include <thrust/reduce.h> |
| | #include <thrust/system/cuda/execution_policy.h> |
| | #include <cassert> |
| |
|
| | #if THRUST_CPP_DIALECT >= 2011 |
| | #include <future> |
| | #endif |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #ifdef THRUST_EXAMPLE_DEVICE_SIDE |
| | template<typename Iterator, typename T, typename BinaryOperation, typename Pointer> |
| | __global__ void reduce_kernel(Iterator first, Iterator last, T init, BinaryOperation binary_op, Pointer result) |
| | { |
| | *result = thrust::reduce(thrust::cuda::par, first, last, init, binary_op); |
| | } |
| | #endif |
| |
|
| | int main() |
| | { |
| | size_t n = 1 << 20; |
| | thrust::device_vector<unsigned int> data(n, 1); |
| | thrust::device_vector<unsigned int> result(1, 0); |
| |
|
| | |
| |
|
| | |
| | cudaStream_t s; |
| | cudaStreamCreate(&s); |
| |
|
| | |
| | #ifdef THRUST_EXAMPLE_DEVICE_SIDE |
| | reduce_kernel<<<1,1,0,s>>>(data.begin(), data.end(), 0, thrust::plus<int>(), result.data()); |
| | #else |
| | result[0] = thrust::reduce(thrust::cuda::par, data.begin(), data.end(), 0, thrust::plus<int>()); |
| | #endif |
| |
|
| | |
| | cudaStreamSynchronize(s); |
| |
|
| | |
| | assert(result[0] == n); |
| |
|
| | cudaStreamDestroy(s); |
| |
|
| | |
| | result[0] = 0; |
| |
|
| | #if THRUST_CPP_DIALECT >= 2011 |
| | |
| |
|
| | |
| | auto begin = data.begin(); |
| | auto end = data.end(); |
| | unsigned int init = 0; |
| | auto binary_op = thrust::plus<unsigned int>(); |
| |
|
| | |
| | |
| | std::future<unsigned int> future_result = std::async(std::launch::async, [=] |
| | { |
| | return thrust::reduce(begin, end, init, binary_op); |
| | }); |
| |
|
| | |
| | assert(future_result.get() == n); |
| | #endif |
| |
|
| | return 0; |
| | } |
| |
|
| |
|