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/sort.hpp>
18 #include <boost/compute/container/vector.hpp>
19 
20 #include "perf.hpp"
21 
rand_float()22 float rand_float()
23 {
24     return ((rand() / float(RAND_MAX)) - 0.5f) * 100000.0f;
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     // 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     // create vector of random numbers on the host
39     std::vector<float> host_vector(PERF_N);
40     std::generate(host_vector.begin(), host_vector.end(), rand_float);
41 
42     // create vector on the device and copy the data
43     boost::compute::vector<float> device_vector(PERF_N, context);
44     boost::compute::copy(
45         host_vector.begin(),
46         host_vector.end(),
47         device_vector.begin(),
48         queue
49     );
50 
51     // sort vector
52     perf_timer t;
53     t.start();
54     boost::compute::sort(
55         device_vector.begin(),
56         device_vector.end(),
57         queue
58     );
59     queue.finish();
60     t.stop();
61     std::cout << "time: " << t.last_time() / 1e6 << " ms" << std::endl;
62 
63     // verify vector is sorted
64     if(!boost::compute::is_sorted(device_vector.begin(),
65                                   device_vector.end(),
66                                   queue)){
67         std::cout << "ERROR: is_sorted() returned false" << std::endl;
68         return -1;
69     }
70 
71     return 0;
72 }
73