• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2014 Roshan <thisisroshansmail@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/lambda.hpp>
18 #include <boost/compute/algorithm/detail/binary_find.hpp>
19 #include <boost/compute/algorithm/partition.hpp>
20 #include <boost/compute/container/vector.hpp>
21 
22 #include "perf.hpp"
23 
rand_int()24 int rand_int()
25 {
26     return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
27 }
28 
main(int argc,char * argv[])29 int main(int argc, char *argv[])
30 {
31     perf_parse_args(argc, argv);
32     std::cout << "size: " << PERF_N << std::endl;
33 
34     // setup context and queue for the default device
35     boost::compute::device device = boost::compute::system::default_device();
36     boost::compute::context context(device);
37     boost::compute::command_queue queue(context, device);
38     std::cout << "device: " << device.name() << std::endl;
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_int);
43 
44     // create vector on the device and copy the data
45     boost::compute::vector<int> device_vector(PERF_N, context);
46     boost::compute::copy(
47         host_vector.begin(), host_vector.end(), device_vector.begin(), queue
48     );
49 
50     using boost::compute::_1;
51     boost::compute::partition(
52         device_vector.begin(), device_vector.end(), _1 < 20, queue
53     );
54 
55     // just to be sure everything is finished before measuring execution time
56     // of binary_find algorithm
57     queue.finish();
58 
59     perf_timer t;
60     for(size_t trial = 0; trial < PERF_TRIALS; trial++){
61         t.start();
62         boost::compute::detail::binary_find(
63             device_vector.begin(), device_vector.end(), _1 >= 20, queue
64         );
65         queue.finish();
66         t.stop();
67     }
68     std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
69 
70     return 0;
71 }
72