• 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/inner_product.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     std::cout << "size: " << PERF_N << std::endl;
31 
32     boost::compute::device device = boost::compute::system::default_device();
33     boost::compute::context context(device);
34     boost::compute::command_queue queue(context, device);
35     std::cout << "device: " << device.name() << std::endl;
36 
37     std::vector<int> h1(PERF_N);
38     std::vector<int> h2(PERF_N);
39     std::generate(h1.begin(), h1.end(), rand_int);
40     std::generate(h2.begin(), h2.end(), rand_int);
41 
42     // create vector on the device and copy the data
43     boost::compute::vector<int> d1(PERF_N, context);
44     boost::compute::vector<int> d2(PERF_N, context);
45     boost::compute::copy(h1.begin(), h1.end(), d1.begin(), queue);
46     boost::compute::copy(h2.begin(), h2.end(), d2.begin(), queue);
47 
48     int product = 0;
49     perf_timer t;
50     for(size_t trial = 0; trial < PERF_TRIALS; trial++){
51         t.start();
52         product = boost::compute::inner_product(
53             d1.begin(), d1.end(), d2.begin(), int(0), queue
54         );
55         queue.finish();
56         t.stop();
57     }
58     std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
59 
60     // verify product is correct
61     int host_product = std::inner_product(
62         h1.begin(), h1.end(), h2.begin(), int(0)
63     );
64     if(product != host_product){
65         std::cout << "ERROR: "
66                   << "device_product (" << product << ") "
67                   << "!= "
68                   << "host_product (" << host_product << ")"
69                   << std::endl;
70         return -1;
71     }
72 
73     return 0;
74 }
75