• 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 <vector>
14 
15 #include <boost/compute/system.hpp>
16 #include <boost/compute/algorithm/is_sorted.hpp>
17 #include <boost/compute/algorithm/reverse.hpp>
18 #include <boost/compute/algorithm/sort.hpp>
19 #include <boost/compute/container/vector.hpp>
20 
21 #include "perf.hpp"
22 
main(int argc,char * argv[])23 int main(int argc, char *argv[])
24 {
25     perf_parse_args(argc, argv);
26     std::cout << "size: " << PERF_N << std::endl;
27 
28     // setup context and queue for the default device
29     boost::compute::device device = boost::compute::system::default_device();
30     boost::compute::context context(device);
31     boost::compute::command_queue queue(context, device);
32     std::cout << "device: " << device.name() << std::endl;
33 
34     // create vector of random numbers on the host
35     std::vector<int> host_vector(PERF_N);
36     std::generate(host_vector.begin(), host_vector.end(), rand);
37 
38     // create vector on the device and copy the data
39     boost::compute::vector<int> device_vector(PERF_N, context);
40     boost::compute::copy(
41         host_vector.begin(), host_vector.end(), device_vector.begin(), queue
42     );
43 
44     // sort and then reverse the random vector
45     boost::compute::sort(device_vector.begin(), device_vector.end(), queue);
46     boost::compute::reverse(device_vector.begin(), device_vector.end(), queue);
47 
48     perf_timer t;
49     for(size_t trial = 0; trial < PERF_TRIALS; trial++){
50         t.start();
51         bool sorted = boost::compute::is_sorted(
52             device_vector.begin(), device_vector.end(), queue
53         );
54         queue.finish();
55         t.stop();
56         if(sorted){
57             std::cerr << "ERROR: is_sorted() returned true" << std::endl;
58         }
59     }
60     std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
61 
62     return 0;
63 }
64