perf_copy_to_device.cpp 1.8 KB

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