mapped_view.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 <iostream>
  11. #include <vector>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/algorithm/reduce.hpp>
  14. #include <boost/compute/container/mapped_view.hpp>
  15. namespace compute = boost::compute;
  16. // this example demonstrates how to use the mapped_view class to map
  17. // an array of numbers to device memory and use the reduce() algorithm
  18. // to calculate the sum.
  19. int main()
  20. {
  21. // get default device and setup context
  22. compute::device gpu = compute::system::default_device();
  23. compute::context context(gpu);
  24. compute::command_queue queue(context, gpu);
  25. std::cout << "device: " << gpu.name() << std::endl;
  26. // create data on host
  27. int data[] = { 4, 2, 3, 7, 8, 9, 1, 6 };
  28. // create mapped view on device
  29. compute::mapped_view<int> view(data, 8, context);
  30. // use reduce() to calculate sum on the device
  31. int sum = 0;
  32. compute::reduce(view.begin(), view.end(), &sum, queue);
  33. // print the sum on the host
  34. std::cout << "sum: " << sum << std::endl;
  35. return 0;
  36. }