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