1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2015 Jakub Pola <jakub.pola@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #define BOOST_TEST_MODULE TestScatterIf
12 #include <boost/test/unit_test.hpp>
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/algorithm/scatter_if.hpp>
16 #include <boost/compute/container/vector.hpp>
17 #include <boost/compute/iterator/constant_buffer_iterator.hpp>
18 #include <boost/compute/iterator/counting_iterator.hpp>
19 #include <boost/compute/functional.hpp>
20 #include "check_macros.hpp"
21 #include "context_setup.hpp"
23 namespace bc
= boost::compute
;
25 BOOST_AUTO_TEST_CASE(scatter_if_int
)
27 int input_data
[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
28 bc::vector
<int> input(input_data
, input_data
+ 10, queue
);
30 int map_data
[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
32 bc::vector
<int> map(map_data
, map_data
+ 10, queue
);
34 int stencil_data
[] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
36 bc::vector
<bc::uint_
> stencil(stencil_data
, stencil_data
+ 10, queue
);
38 bc::vector
<int> output(input
.size(), -1, queue
);
40 bc::scatter_if(input
.begin(), input
.end(),
41 map
.begin(), stencil
.begin(),
45 CHECK_RANGE_EQUAL(int, 10, output
, (9, -1, 7, -1, 5, -1, 3, -1, 1, -1) );
48 BOOST_AUTO_TEST_CASE(scatter_if_constant_indices
)
50 int input_data
[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
51 bc::vector
<int> input(input_data
, input_data
+ 10, queue
);
53 int map_data
[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
54 bc::buffer
map_buffer(context
,
56 bc::buffer::read_only
| bc::buffer::use_host_ptr
,
59 int stencil_data
[] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
60 bc::buffer
stencil_buffer(context
,
61 10 * sizeof(bc::uint_
),
62 bc::buffer::read_only
| bc::buffer::use_host_ptr
,
65 bc::vector
<int> output(input
.size(), -1, queue
);
67 bc::scatter_if(input
.begin(),
69 bc::make_constant_buffer_iterator
<int>(map_buffer
, 0),
70 bc::make_constant_buffer_iterator
<int>(stencil_buffer
, 0),
74 CHECK_RANGE_EQUAL(int, 10, output
, (9, -1, 7, -1, 5, -1, 3, -1, 1, -1) );
78 BOOST_AUTO_TEST_CASE(scatter_if_function
)
80 int input_data
[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
81 bc::vector
<int> input(input_data
, input_data
+ 10, queue
);
83 int map_data
[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
84 bc::vector
<int> map(map_data
, map_data
+ 10, queue
);
86 int stencil_data
[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
87 bc::vector
<bc::uint_
> stencil(stencil_data
, stencil_data
+ 10, queue
);
89 bc::vector
<int> output(input
.size(), -1, queue
);
91 BOOST_COMPUTE_FUNCTION(int, gt_than_5
, (int x
),
99 bc::scatter_if(input
.begin(),
107 CHECK_RANGE_EQUAL(int, 10, output
, (9, 8, 7, 6, -1, -1, -1, -1, -1, -1) );
111 BOOST_AUTO_TEST_CASE(scatter_if_counting_iterator
)
113 int input_data
[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
114 bc::vector
<int> input(input_data
, input_data
+ 10, queue
);
116 int map_data
[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
117 bc::vector
<int> map(map_data
, map_data
+ 10, queue
);
119 bc::vector
<int> output(input
.size(), -1, queue
);
121 BOOST_COMPUTE_FUNCTION(int, gt_than_5
, (int x
),
129 bc::scatter_if(input
.begin(),
132 bc::make_counting_iterator
<int>(0),
137 CHECK_RANGE_EQUAL(int, 10, output
, (9, 8, 7, 6, -1, -1, -1, -1, -1, -1) );
141 BOOST_AUTO_TEST_SUITE_END()