1 #include <thrust/host_vector.h>
2 #include <thrust/device_vector.h>
3 #include <thrust/generate.h>
4 #include <thrust/reduce.h>
5 #include <thrust/functional.h>
6 #include <thrust/random.h>
7 
my_rand(void)8 int my_rand(void)
9 {
10   static thrust::default_random_engine rng;
11   static thrust::uniform_int_distribution<int> dist(0, 9999);
12   return dist(rng);
13 }
14 
main(void)15 int main(void)
16 {
17   // generate random data on the host
18   thrust::host_vector<int> h_vec(100);
19   thrust::generate(h_vec.begin(), h_vec.end(), my_rand);
20 
21   // transfer to device and compute sum
22   thrust::device_vector<int> d_vec = h_vec;
23 
24   // initial value of the reduction
25   int init = 0;
26 
27   // binary operation used to reduce values
28   thrust::plus<int> binary_op;
29 
30   // compute sum on the device
31   int sum = thrust::reduce(d_vec.begin(), d_vec.end(), init, binary_op);
32 
33   // print the sum
34   std::cout << "sum is " << sum << std::endl;
35 
36   return 0;
37 }
38