1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2015 Jakub Szuppe <j.szuppe@gmail.com>
3 //
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
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 #include <algorithm>
12 #include <iostream>
13 #include <numeric>
14 #include <vector>
15 
16 #include <boost/compute/system.hpp>
17 #include <boost/compute/algorithm/fill.hpp>
18 #include <boost/compute/algorithm/reduce_by_key.hpp>
19 #include <boost/compute/container/vector.hpp>
20 
21 #include "perf.hpp"
22 
rand_int()23 int rand_int()
24 {
25     return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
26 }
27 
28 struct unique_key {
29   int current;
30   int avgValuesNoPerKey;
31 
unique_keyunique_key32   unique_key()
33   {
34       current = 0;
35       avgValuesNoPerKey = 512;
36   }
37 
operator ()unique_key38   int operator()()
39   {
40       double p = double(1.0) / static_cast<double>(avgValuesNoPerKey);
41       if((rand() / double(RAND_MAX)) <= p)
42           return ++current;
43       return current;
44   }
45 } UniqueKey;
46 
main(int argc,char * argv[])47 int main(int argc, char *argv[])
48 {
49     perf_parse_args(argc, argv);
50 
51     std::cout << "size: " << PERF_N << std::endl;
52 
53     // setup context and queue for the default device
54     boost::compute::device device = boost::compute::system::default_device();
55     boost::compute::context context(device);
56     boost::compute::command_queue queue(context, device);
57     std::cout << "device: " << device.name() << std::endl;
58 
59     // create vector of keys and random values
60     std::vector<int> host_keys(PERF_N);
61     std::vector<int> host_values(PERF_N);
62     std::generate(host_keys.begin(), host_keys.end(), UniqueKey);
63     std::generate(host_values.begin(), host_values.end(), rand_int);
64 
65     // create vectors for keys and values on the device and copy the data
66     boost::compute::vector<int> device_keys(PERF_N, context);
67     boost::compute::vector<int> device_values(PERF_N,context);
68     boost::compute::copy(
69         host_keys.begin(),
70         host_keys.end(),
71         device_keys.begin(),
72         queue
73     );
74     boost::compute::copy(
75         host_values.begin(),
76         host_values.end(),
77         device_values.begin(),
78         queue
79     );
80 
81     // vectors for the results
82     boost::compute::vector<int> device_keys_results(PERF_N, context);
83     boost::compute::vector<int> device_values_results(PERF_N,context);
84 
85     typedef boost::compute::vector<int>::iterator iterType;
86     std::pair<iterType, iterType> result(
87         device_keys_results.begin(),
88         device_values_results.begin()
89     );
90 
91     // reduce by key
92     perf_timer t;
93     for(size_t trial = 0; trial < PERF_TRIALS; trial++){
94         t.start();
95         result = boost::compute::reduce_by_key(device_keys.begin(),
96                                                device_keys.end(),
97                                                device_values.begin(),
98                                                device_keys_results.begin(),
99                                                device_values_results.begin(),
100                                                queue);
101         t.stop();
102     }
103     std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
104 
105     size_t result_size = std::distance(device_keys_results.begin(), result.first);
106     if(result_size != static_cast<size_t>(host_keys[PERF_N-1] + 1)){
107         std::cout << "ERROR: "
108                   << "wrong number of keys" << result_size << "\n" << (host_keys[PERF_N-1] + 1)
109                   << std::endl;
110         return -1;
111     }
112 
113     return 0;
114 }
115