1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Rastko Anicic <anicic.rastko@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/max_element.hpp>
17 #include <boost/compute/container/vector.hpp>
18
19 #include "perf.hpp"
20
rand_int()21 int rand_int()
22 {
23 return static_cast<int>(rand() % 10000000);
24 }
25
main(int argc,char * argv[])26 int main(int argc, char *argv[])
27 {
28 perf_parse_args(argc, argv);
29 std::cout << "size: " << PERF_N << std::endl;
30
31 // setup context and queue for the default device
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 // create vector of random numbers on the host
38 std::vector<int> host_vector(PERF_N);
39 std::generate(host_vector.begin(), host_vector.end(), rand_int);
40
41 // create vector on the device and copy the data
42 boost::compute::vector<int> device_vector(PERF_N, context);
43 boost::compute::copy(
44 host_vector.begin(),
45 host_vector.end(),
46 device_vector.begin(),
47 queue
48 );
49
50 boost::compute::vector<int>::iterator device_max_iter
51 = device_vector.begin();
52
53 perf_timer t;
54 for(size_t trial = 0; trial < PERF_TRIALS; trial++){
55 t.start();
56 device_max_iter = boost::compute::max_element(
57 device_vector.begin(), device_vector.end(), queue
58 );
59 queue.finish();
60 t.stop();
61 }
62
63 int device_max = device_max_iter.read(queue);
64 std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
65 std::cout << "max: " << device_max << std::endl;
66
67 // verify max is correct
68 std::vector<int>::iterator host_max_iter
69 = std::max_element(host_vector.begin(), host_vector.end());
70
71 int host_max = *host_max_iter;
72 if(device_max != host_max){
73 std::cout << "ERROR: "
74 << "device_max (" << device_max << ") "
75 << "!= "
76 << "host_max (" << host_max << ")"
77 << std::endl;
78 return -1;
79 }
80
81 size_t host_max_idx = std::distance(host_vector.begin(), host_max_iter);
82 size_t device_max_idx = std::distance(device_vector.begin(), device_max_iter);
83 if(device_max_idx != host_max_idx){
84 std::cout << "ERROR: "
85 << "device_max index (" << device_max_idx << ") "
86 << "!= "
87 << "host_max index (" << host_max_idx << ")"
88 << std::endl;
89 return -1;
90 }
91
92 return 0;
93 }
94