• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
12 #include <algorithm>
13 #include <vector>
14 
15 #include <bolt/cl/copy.h>
16 #include <bolt/cl/device_vector.h>
17 #include <bolt/cl/reduce_by_key.h>
18 
19 #include "perf.hpp"
20 
rand_int()21 int rand_int()
22 {
23     return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
24 }
25 
26 struct unique_key {
27   int current;
28   int avgValuesNoPerKey;
29 
unique_keyunique_key30   unique_key()
31   {
32       current = 0;
33       avgValuesNoPerKey = 512;
34   }
35 
operator ()unique_key36   int operator()()
37   {
38       double p = double(1.0) / static_cast<double>(avgValuesNoPerKey);
39       if((rand() / double(RAND_MAX)) <= p)
40           return ++current;
41       return current;
42   }
43 } UniqueKey;
44 
main(int argc,char * argv[])45 int main(int argc, char *argv[])
46 {
47     perf_parse_args(argc, argv);
48 
49     std::cout << "size: " << PERF_N << std::endl;
50 
51     bolt::cl::control ctrl = bolt::cl::control::getDefault();
52     ::cl::Device device = ctrl.getDevice();
53     std::cout << "device: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;
54 
55     // create vector of keys and random values
56     std::vector<int> host_keys(PERF_N);
57     std::vector<int> host_values(PERF_N);
58     std::generate(host_keys.begin(), host_keys.end(), UniqueKey);
59     std::generate(host_values.begin(), host_values.end(), rand_int);
60 
61     // create device vectors for data
62     bolt::cl::device_vector<int> device_keys(PERF_N);
63     bolt::cl::device_vector<int> device_values(PERF_N);
64 
65     // transfer data to the device
66     bolt::cl::copy(host_keys.begin(), host_keys.end(), device_keys.begin());
67     bolt::cl::copy(host_values.begin(), host_values.end(), device_values.begin());
68 
69     // create device vectors for the results
70     bolt::cl::device_vector<int> device_keys_results(PERF_N);
71     bolt::cl::device_vector<int> device_values_results(PERF_N);
72 
73     typedef bolt::cl::device_vector<int>::iterator iterType;
74     bolt::cl::pair<iterType, iterType> result = {
75         device_keys_results.begin(),
76         device_values_results.begin()
77     };
78 
79     perf_timer t;
80     for(size_t trial = 0; trial < PERF_TRIALS; trial++){
81         t.start();
82         result = bolt::cl::reduce_by_key(device_keys.begin(),
83                                          device_keys.end(),
84                                          device_values.begin(),
85                                          device_keys_results.begin(),
86                                          device_values_results.begin());
87         t.stop();
88     }
89     std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
90 
91     size_t result_size = bolt::cl::distance(device_keys_results.begin(), result.first);
92     if(result_size != static_cast<size_t>(host_keys[PERF_N-1] + 1)){
93         std::cout << "ERROR: "
94                   << "wrong number of keys"
95                   << std::endl;
96         return -1;
97     }
98 
99     return 0;
100 }
101