test_gather.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #define BOOST_TEST_MODULE TestGather
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/algorithm/copy_if.hpp>
  14. #include <boost/compute/algorithm/gather.hpp>
  15. #include <boost/compute/container/vector.hpp>
  16. #include "check_macros.hpp"
  17. #include "context_setup.hpp"
  18. namespace compute = boost::compute;
  19. BOOST_AUTO_TEST_CASE(gather_int)
  20. {
  21. int input_data[] = { 1, 2, 3, 4, 5 };
  22. compute::vector<int> input(input_data, input_data + 5, queue);
  23. int indices_data[] = { 0, 4, 1, 3, 2 };
  24. compute::vector<int> indices(indices_data, indices_data + 5, queue);
  25. compute::vector<int> output(5, context);
  26. compute::gather(
  27. indices.begin(), indices.end(), input.begin(), output.begin(), queue
  28. );
  29. CHECK_RANGE_EQUAL(int, 5, output, (1, 5, 2, 4, 3));
  30. compute::gather(
  31. indices.begin() + 1, indices.end(), input.begin(), output.begin(), queue
  32. );
  33. CHECK_RANGE_EQUAL(int, 5, output, (5, 2, 4, 3, 3));
  34. }
  35. BOOST_AUTO_TEST_CASE(copy_index_then_gather)
  36. {
  37. // input data
  38. int data[] = { 1, 4, 3, 2, 5, 9, 8, 7 };
  39. compute::vector<int> input(data, data + 8, queue);
  40. // function returning true if the input is odd
  41. BOOST_COMPUTE_FUNCTION(bool, is_odd, (int x),
  42. {
  43. return x % 2 != 0;
  44. });
  45. // copy indices of all odd values
  46. compute::vector<int> odds(5, context);
  47. compute::detail::copy_index_if(
  48. input.begin(), input.end(), odds.begin(), is_odd, queue
  49. );
  50. CHECK_RANGE_EQUAL(int, 5, odds, (0, 2, 4, 5, 7));
  51. // gather all odd values
  52. compute::vector<int> odd_values(5, context);
  53. compute::gather(
  54. odds.begin(), odds.end(), input.begin(), odd_values.begin(), queue
  55. );
  56. CHECK_RANGE_EQUAL(int, 5, odd_values, (1, 3, 5, 9, 7));
  57. }
  58. BOOST_AUTO_TEST_SUITE_END()