1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2015 Jakub Szuppe <j.szuppe@gmail.com>
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
15 #include <thrust/copy.h>
16 #include <thrust/device_vector.h>
17 #include <thrust/generate.h>
18 #include <thrust/host_vector.h>
19 #include <thrust/reduce.h>
25 return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
30 int avgValuesNoPerKey;
35 avgValuesNoPerKey = 512;
40 double p = double(1.0) / static_cast<double>(avgValuesNoPerKey);
41 if((rand() / double(RAND_MAX)) <= p)
47 int main(int argc, char *argv[])
49 perf_parse_args(argc, argv);
51 std::cout << "size: " << PERF_N << std::endl;
53 // create vector of keys and random values
54 thrust::host_vector<int> host_keys(PERF_N);
55 thrust::host_vector<int> host_values(PERF_N);
56 std::generate(host_keys.begin(), host_keys.end(), UniqueKey);
57 std::generate(host_values.begin(), host_values.end(), rand_int);
59 // transfer data to the device
60 thrust::device_vector<int> device_keys = host_keys;
61 thrust::device_vector<int> device_values = host_values;
63 // create device vectors for the results
64 thrust::device_vector<int> device_keys_results(PERF_N);
65 thrust::device_vector<int> device_values_results(PERF_N);
67 typedef typename thrust::device_vector<int>::iterator iterType;
68 thrust::pair<iterType, iterType> result;
71 for(size_t trial = 0; trial < PERF_TRIALS; trial++){
73 result = thrust::reduce_by_key(device_keys.begin(),
75 device_values.begin(),
76 device_keys_results.begin(),
77 device_values_results.begin());
78 cudaDeviceSynchronize();
81 std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
83 size_t result_size = thrust::distance(device_keys_results.begin(), result.first);
84 if(result_size != static_cast<size_t>(host_keys[PERF_N-1] + 1)){
85 std::cout << "ERROR: "
86 << "wrong number of keys"