copy_data.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. //[copy_data_example
  11. #include <vector>
  12. #include <boost/compute/algorithm/copy.hpp>
  13. #include <boost/compute/container/vector.hpp>
  14. namespace compute = boost::compute;
  15. int main()
  16. {
  17. // get default device and setup context
  18. compute::device device = compute::system::default_device();
  19. compute::context context(device);
  20. compute::command_queue queue(context, device);
  21. // create data array on host
  22. int host_data[] = { 1, 3, 5, 7, 9 };
  23. // create vector on device
  24. compute::vector<int> device_vector(5, context);
  25. // copy from host to device
  26. compute::copy(
  27. host_data, host_data + 5, device_vector.begin(), queue
  28. );
  29. // create vector on host
  30. std::vector<int> host_vector(5);
  31. // copy data back to host
  32. compute::copy(
  33. device_vector.begin(), device_vector.end(), host_vector.begin(), queue
  34. );
  35. return 0;
  36. }
  37. //]