time_copy.cpp 1.9 KB

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