• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2016 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 <vector>
14 
15 #include <boost/program_options.hpp>
16 
17 #include <boost/compute/system.hpp>
18 #include <boost/compute/algorithm/sort.hpp>
19 #include <boost/compute/algorithm/is_sorted.hpp>
20 #include <boost/compute/container/vector.hpp>
21 
22 #include "perf.hpp"
23 
24 namespace po = boost::program_options;
25 namespace compute = boost::compute;
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     // setup context and queue for the default device
33     boost::compute::device device = boost::compute::system::default_device();
34     boost::compute::context context(device);
35     boost::compute::command_queue queue(context, device);
36     std::cout << "device: " << device.name() << std::endl;
37 
38     using boost::compute::int_;
39 
40     // create vector of random numbers on the host
41     std::vector<int_> host_vector(PERF_N);
42     std::generate(host_vector.begin(), host_vector.end(), rand);
43 
44     // create vector on the device and copy the data
45     boost::compute::vector<int_> device_vector(PERF_N, context);
46 
47     // less function for float
48     BOOST_COMPUTE_FUNCTION(bool, comp, (int_ a, int_ b),
49     {
50         return a < b;
51     });
52 
53     // sort 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         queue.finish();
63 
64         t.start();
65         boost::compute::sort(
66             device_vector.begin(),
67             device_vector.end(),
68             comp,
69             queue
70         );
71         queue.finish();
72         t.stop();
73     };
74     std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
75 
76     // verify vector is sorted
77     if(!boost::compute::is_sorted(device_vector.begin(),
78                                   device_vector.end(),
79                                   comp,
80                                   queue)){
81         std::cout << "ERROR: is_sorted() returned false" << std::endl;
82         return -1;
83     }
84 
85     return 0;
86 }
87