• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2014 Benoit
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/exclusive_scan.hpp>
18 #include <boost/compute/container/vector.hpp>
19 
20 #include "perf.hpp"
21 
rand_int()22 int rand_int()
23 {
24     return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
25 }
26 
main(int argc,char * argv[])27 int main(int argc, char *argv[])
28 {
29     perf_parse_args(argc, argv);
30 
31     std::cout << "size: " << PERF_N << std::endl;
32 
33     // setup context and queue for the default device
34     boost::compute::device device = boost::compute::system::default_device();
35     boost::compute::context context(device);
36     boost::compute::command_queue queue(context, device);
37     std::cout << "device: " << device.name() << std::endl;
38 
39     // create vector of random numbers on the host
40     std::vector<int> host_vector(PERF_N);
41     std::generate(host_vector.begin(), host_vector.end(), rand_int);
42 
43     // create vector on the device and copy the data
44     boost::compute::vector<int> device_vector(PERF_N, context);
45     boost::compute::vector<int> device_res(PERF_N,context);
46     boost::compute::copy(
47         host_vector.begin(),
48         host_vector.end(),
49         device_vector.begin(),
50         queue
51     );
52 
53     // sum vector
54     perf_timer t;
55     for(size_t trial = 0; trial < PERF_TRIALS; trial++){
56         boost::compute::copy(
57             host_vector.begin(),
58             host_vector.end(),
59             device_vector.begin(),
60             queue
61         );
62 
63         t.start();
64         boost::compute::exclusive_scan(
65             device_vector.begin(),
66             device_vector.end(),
67             device_res.begin(),
68             queue
69         );
70         queue.finish();
71         t.stop();
72     }
73     std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
74 
75     // verify sum is correct
76     std::partial_sum(
77         host_vector.begin(),
78         host_vector.end(),
79         host_vector.begin()
80     );
81 
82     int device_sum = device_res.back();
83     // when scan is exclusive values are shifted by one on the left
84     // compared to a inclusive scan
85     int host_sum = host_vector[host_vector.size()-2];
86 
87     if(device_sum != host_sum){
88         std::cout << "ERROR: "
89                   << "device_sum (" << device_sum << ") "
90                   << "!= "
91                   << "host_sum (" << host_sum << ")"
92                   << std::endl;
93         return -1;
94     }
95 
96     return 0;
97 }
98