• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@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/partial_sum.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     using boost::compute::int_;
30 
31     perf_parse_args(argc, argv);
32 
33     std::cout << "size: " << PERF_N << std::endl;
34 
35     // setup context and queue for the default device
36     boost::compute::device device = boost::compute::system::default_device();
37     boost::compute::context context(device);
38     boost::compute::command_queue queue(context, device);
39     std::cout << "device: " << device.name() << std::endl;
40 
41     // create vector of random numbers on the host
42     std::vector<int_> host_vector(PERF_N);
43     std::generate(host_vector.begin(), host_vector.end(), rand_int);
44 
45     // create vector on the device and copy the data
46     boost::compute::vector<int_> device_vector(PERF_N, context);
47     boost::compute::vector<int_> device_res(PERF_N,context);
48     boost::compute::copy(
49         host_vector.begin(),
50         host_vector.end(),
51         device_vector.begin(),
52         queue
53     );
54 
55     // sum vector
56     perf_timer t;
57     for(size_t trial = 0; trial < PERF_TRIALS; trial++){
58         boost::compute::copy(
59             host_vector.begin(),
60             host_vector.end(),
61             device_vector.begin(),
62             queue
63         );
64 
65         t.start();
66         boost::compute::partial_sum(
67             device_vector.begin(),
68             device_vector.end(),
69             device_res.begin(),
70             queue
71         );
72         queue.finish();
73         t.stop();
74     }
75     std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
76 
77     // verify sum is correct
78     std::partial_sum(
79         host_vector.begin(),
80         host_vector.end(),
81         host_vector.begin()
82     );
83 
84     int device_sum = device_res.back();
85     int host_sum = host_vector.back();
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