• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 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 //[time_copy_example
12 
13 #include <vector>
14 #include <cstdlib>
15 #include <iostream>
16 
17 #include <boost/compute/event.hpp>
18 #include <boost/compute/system.hpp>
19 #include <boost/compute/algorithm/copy.hpp>
20 #include <boost/compute/async/future.hpp>
21 #include <boost/compute/container/vector.hpp>
22 
23 namespace compute = boost::compute;
24 
main()25 int main()
26 {
27     // get the default device
28     compute::device gpu = compute::system::default_device();
29 
30     // create context for default device
31     compute::context context(gpu);
32 
33     // create command queue with profiling enabled
34     compute::command_queue queue(
35         context, gpu, compute::command_queue::enable_profiling
36     );
37 
38     // generate random data on the host
39     std::vector<int> host_vector(16000000);
40     std::generate(host_vector.begin(), host_vector.end(), rand);
41 
42     // create a vector on the device
43     compute::vector<int> device_vector(host_vector.size(), context);
44 
45     // copy data from the host to the device
46     compute::future<void> future = compute::copy_async(
47         host_vector.begin(), host_vector.end(), device_vector.begin(), queue
48     );
49 
50     // wait for copy to finish
51     future.wait();
52 
53     // get elapsed time from event profiling information
54     boost::chrono::milliseconds duration =
55         future.get_event().duration<boost::chrono::milliseconds>();
56 
57     // print elapsed time in milliseconds
58     std::cout << "time: " << duration.count() << " ms" << std::endl;
59 
60     return 0;
61 }
62 
63 //]
64