test_mapped_view.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #define BOOST_TEST_MODULE TestMappedView
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/algorithm/copy.hpp>
  14. #include <boost/compute/algorithm/sort.hpp>
  15. #include <boost/compute/algorithm/reduce.hpp>
  16. #include <boost/compute/container/mapped_view.hpp>
  17. #include "check_macros.hpp"
  18. #include "context_setup.hpp"
  19. namespace compute = boost::compute;
  20. BOOST_AUTO_TEST_CASE(fill)
  21. {
  22. int data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  23. for(int i = 0; i < 8; i++){
  24. BOOST_CHECK_EQUAL(data[i], i+1);
  25. }
  26. compute::mapped_view<int> view(data, 8, context);
  27. compute::fill(view.begin(), view.end(), 4, queue);
  28. view.map(CL_MAP_READ, queue);
  29. for(int i = 0; i < 8; i++){
  30. BOOST_CHECK_EQUAL(data[i], 4);
  31. }
  32. view.unmap(queue);
  33. }
  34. BOOST_AUTO_TEST_CASE(sort)
  35. {
  36. int data[] = { 5, 2, 3, 1, 8, 7, 4, 9 };
  37. compute::mapped_view<int> view(data, 8, context);
  38. compute::sort(view.begin(), view.end(), queue);
  39. view.map(CL_MAP_READ, queue);
  40. BOOST_CHECK_EQUAL(data[0], 1);
  41. BOOST_CHECK_EQUAL(data[7], 9);
  42. view.unmap(queue);
  43. }
  44. BOOST_AUTO_TEST_CASE(mapped_view_reduce_doctest)
  45. {
  46. //! [reduce]
  47. // create data array on the host
  48. int data[] = { 5, 2, 3, 1, 8, 7, 4, 9 };
  49. boost::compute::mapped_view<int> view(data, 8, context);
  50. // use reduce() to calculate the sum on the device
  51. int sum = 0;
  52. boost::compute::reduce(view.begin(), view.end(), &sum, queue);
  53. //! [reduce]
  54. BOOST_CHECK_EQUAL(sum, 39);
  55. }
  56. BOOST_AUTO_TEST_SUITE_END()