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 <vector>
12 #include <cstdlib>
13 #include <iostream>
14 
15 #include <boost/compute.hpp>
16 
main(int argc,char * argv[])17 int main(int argc, char *argv[])
18 {
19     size_t size = 1000;
20     if(argc >= 2){
21         size = boost::lexical_cast<size_t>(argv[1]);
22     }
23 
24     boost::compute::device device = boost::compute::system::default_device();
25     boost::compute::context context(device);
26 
27     boost::compute::command_queue::properties
28         properties = boost::compute::command_queue::enable_profiling;
29     boost::compute::command_queue queue(context, device, properties);
30 
31     std::vector<int> host_vector(size);
32     std::generate(host_vector.begin(), host_vector.end(), rand);
33 
34     boost::compute::vector<int> device_vector(host_vector.size(), context);
35 
36     boost::compute::future<void> future =
37         boost::compute::copy_async(host_vector.begin(),
38                                    host_vector.end(),
39                                    device_vector.begin(),
40                                    queue);
41 
42     // wait for copy to finish
43     future.wait();
44 
45     // get elapsed time in nanoseconds
46     size_t elapsed =
47         future.get_event().duration<boost::chrono::nanoseconds>().count();
48 
49     std::cout << "time: " << elapsed / 1e6 << " ms" << std::endl;
50 
51     float rate = (float(size * sizeof(int)) / elapsed) * 1000.f;
52     std::cout << "rate: " << rate << " MB/s" << std::endl;
53 
54     return 0;
55 }
56