transform_sqrt.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. //[transform_sqrt_example
  11. #include <vector>
  12. #include <algorithm>
  13. #include <boost/compute/algorithm/transform.hpp>
  14. #include <boost/compute/container/vector.hpp>
  15. #include <boost/compute/functional/math.hpp>
  16. namespace compute = boost::compute;
  17. int main()
  18. {
  19. // get default device and setup context
  20. compute::device device = compute::system::default_device();
  21. compute::context context(device);
  22. compute::command_queue queue(context, device);
  23. // generate random data on the host
  24. std::vector<float> host_vector(10000);
  25. std::generate(host_vector.begin(), host_vector.end(), rand);
  26. // create a vector on the device
  27. compute::vector<float> device_vector(host_vector.size(), context);
  28. // transfer data from the host to the device
  29. compute::copy(
  30. host_vector.begin(), host_vector.end(), device_vector.begin(), queue
  31. );
  32. // calculate the square-root of each element in-place
  33. compute::transform(
  34. device_vector.begin(),
  35. device_vector.end(),
  36. device_vector.begin(),
  37. compute::sqrt<float>(),
  38. queue
  39. );
  40. // copy values back to the host
  41. compute::copy(
  42. device_vector.begin(), device_vector.end(), host_vector.begin(), queue
  43. );
  44. return 0;
  45. }
  46. //]