1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2015 Jakub Szuppe <j.szuppe@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 TestReduceByKey
12 #include <boost/test/unit_test.hpp>
14 #include <boost/compute/lambda.hpp>
15 #include <boost/compute/system.hpp>
16 #include <boost/compute/functional.hpp>
17 #include <boost/compute/algorithm/inclusive_scan.hpp>
18 #include <boost/compute/algorithm/reduce_by_key.hpp>
19 #include <boost/compute/container/vector.hpp>
21 #include "check_macros.hpp"
22 #include "context_setup.hpp"
24 namespace bc
= boost::compute
;
26 BOOST_AUTO_TEST_CASE(reduce_by_key_int
)
28 //! [reduce_by_key_int]
29 // setup keys and values
30 int keys
[] = { 0, 2, -3, -3, -3, -3, -3, 4 };
31 int data
[] = { 1, 1, 1, 1, 1, 2, 5, 1 };
33 boost::compute::vector
<int> keys_input(keys
, keys
+ 8, queue
);
34 boost::compute::vector
<int> values_input(data
, data
+ 8, queue
);
36 boost::compute::vector
<int> keys_output(8, context
);
37 boost::compute::vector
<int> values_output(8, context
);
39 boost::compute::reduce_by_key(keys_input
.begin(), keys_input
.end(), values_input
.begin(),
40 keys_output
.begin(), values_output
.begin(), queue
);
41 // keys_output = { 0, 2, -3, 4 }
42 // values_output = { 1, 1, 10, 1 }
43 //! [reduce_by_key_int]
44 CHECK_RANGE_EQUAL(int, 4, keys_output
, (0, 2, -3, 4));
45 CHECK_RANGE_EQUAL(int, 4, values_output
, (1, 1, 10, 1));
48 BOOST_AUTO_TEST_CASE(reduce_by_key_int_long_vector
)
51 bc::vector
<int> keys_input(size
, int(0), queue
);
52 bc::vector
<int> values_input(size
, int(1), queue
);
54 bc::vector
<int> keys_output(size
, context
);
55 bc::vector
<int> values_output(size
, context
);
57 bc::reduce_by_key(keys_input
.begin(), keys_input
.end(), values_input
.begin(),
58 keys_output
.begin(), values_output
.begin(), queue
);
60 CHECK_RANGE_EQUAL(int, 1, keys_output
, (0));
61 CHECK_RANGE_EQUAL(int, 1, values_output
, (static_cast<int>(size
)));
66 bc::inclusive_scan(keys_input
.begin(), keys_input
.end(), keys_input
.begin(), queue
);
68 bc::reduce_by_key(keys_input
.begin(), keys_input
.end(), values_input
.begin(),
69 keys_output
.begin(), values_output
.begin(), queue
);
71 CHECK_RANGE_EQUAL(int, 4, keys_output
, (0, 1, 2, 3));
72 CHECK_RANGE_EQUAL(int, 4, values_output
, (137, 540, 324, 23));
75 BOOST_AUTO_TEST_CASE(reduce_by_key_empty_vector
)
77 bc::vector
<int> keys_input(context
);
78 bc::vector
<int> values_input(context
);
80 bc::vector
<int> keys_output(context
);
81 bc::vector
<int> values_output(context
);
83 bc::reduce_by_key(keys_input
.begin(), keys_input
.end(), values_input
.begin(),
84 keys_output
.begin(), values_output
.begin(), queue
);
86 BOOST_CHECK(keys_output
.empty());
87 BOOST_CHECK(values_output
.empty());
90 BOOST_AUTO_TEST_CASE(reduce_by_key_int_one_key_value
)
95 bc::vector
<int> keys_input(keys
, keys
+ 1, queue
);
96 bc::vector
<int> values_input(data
, data
+ 1, queue
);
98 bc::vector
<int> keys_output(1, context
);
99 bc::vector
<int> values_output(1, context
);
101 bc::reduce_by_key(keys_input
.begin(), keys_input
.end(), values_input
.begin(),
102 keys_output
.begin(), values_output
.begin(), queue
);
104 CHECK_RANGE_EQUAL(int, 1, keys_output
, (22));
105 CHECK_RANGE_EQUAL(int, 1, values_output
, (-9));
108 BOOST_AUTO_TEST_CASE(reduce_by_key_int_min_max
)
110 int keys
[] = { 0, 2, 2, 3, 3, 3, 3, 3, 4 };
111 int data
[] = { 1, 2, 1, -3, 1, 4, 2, 5, 77 };
113 bc::vector
<int> keys_input(keys
, keys
+ 9, queue
);
114 bc::vector
<int> values_input(data
, data
+ 9, queue
);
116 bc::vector
<int> keys_output(9, context
);
117 bc::vector
<int> values_output(9, context
);
119 bc::reduce_by_key(keys_input
.begin(), keys_input
.end(), values_input
.begin(),
120 keys_output
.begin(), values_output
.begin(), bc::min
<int>(),
121 bc::equal_to
<int>(), queue
);
123 CHECK_RANGE_EQUAL(int, 4, keys_output
, (0, 2, 3, 4));
124 CHECK_RANGE_EQUAL(int, 4, values_output
, (1, 1, -3, 77));
126 bc::reduce_by_key(keys_input
.begin(), keys_input
.end(), values_input
.begin(),
127 keys_output
.begin(), values_output
.begin(), bc::max
<int>(),
128 bc::equal_to
<int>(), queue
);
130 CHECK_RANGE_EQUAL(int, 4, keys_output
, (0, 2, 3, 4));
131 CHECK_RANGE_EQUAL(int, 4, values_output
, (1, 2, 5, 77));
134 BOOST_AUTO_TEST_CASE(reduce_by_key_float_max
)
136 int keys
[] = { 0, 2, 2, 3, 3, 3, 3, 3, 4 };
137 float data
[] = { 1.0, 2.0, -1.5, -3.0, 1.0, -0.24, 2, 5, 77.1 };
139 bc::vector
<int> keys_input(keys
, keys
+ 9, queue
);
140 bc::vector
<float> values_input(data
, data
+ 9, queue
);
142 bc::vector
<int> keys_output(9, context
);
143 bc::vector
<float> values_output(9, context
);
145 bc::reduce_by_key(keys_input
.begin(), keys_input
.end(), values_input
.begin(),
146 keys_output
.begin(), values_output
.begin(), bc::max
<float>(),
149 CHECK_RANGE_EQUAL(int, 4, keys_output
, (0, 2, 3, 4));
150 BOOST_CHECK_CLOSE(float(values_output
[0]), 1.0f
, 1e-4f
);
151 BOOST_CHECK_CLOSE(float(values_output
[1]), 2.0f
, 1e-4f
);
152 BOOST_CHECK_CLOSE(float(values_output
[2]), 5.0f
, 1e-4f
);
153 BOOST_CHECK_CLOSE(float(values_output
[3]), 77.1f
, 1e-4f
);
156 BOOST_AUTO_TEST_CASE(reduce_by_key_int2
)
160 int keys
[] = { 0, 2, 3, 3, 3, 3, 4, 4 };
162 int2_(0, 1), int2_(-3, 2), int2_(0, 1), int2_(0, 1),
163 int2_(-3, 0), int2_(0, 0), int2_(-3, 2), int2_(-7, -2)
166 bc::vector
<int> keys_input(keys
, keys
+ 8, queue
);
167 bc::vector
<int2_
> values_input(data
, data
+ 8, queue
);
169 bc::vector
<int> keys_output(8, context
);
170 bc::vector
<int2_
> values_output(8, context
);
172 bc::reduce_by_key(keys_input
.begin(), keys_input
.end(), values_input
.begin(),
173 keys_output
.begin(), values_output
.begin(), queue
);
175 CHECK_RANGE_EQUAL(int, 4, keys_output
, (0, 2, 3, 4));
176 CHECK_RANGE_EQUAL(int2_
, 4, values_output
,
177 (int2_(0, 1), int2_(-3, 2), int2_(-3, 2), int2_(-10, 0)));
180 BOOST_AUTO_TEST_CASE(reduce_by_key_int2_long_vector
)
185 bc::vector
<int> keys_input(size
, int(0), queue
);
186 bc::vector
<int2_
> values_input(size
, int2_(1, -1), queue
);
188 bc::vector
<int> keys_output(size
, context
);
189 bc::vector
<int2_
> values_output(size
, context
);
191 bc::reduce_by_key(keys_input
.begin(), keys_input
.end(), values_input
.begin(),
192 keys_output
.begin(), values_output
.begin(), queue
);
194 CHECK_RANGE_EQUAL(int, 1, keys_output
, (0));
195 CHECK_RANGE_EQUAL(int2_
, 1, values_output
, (int2_(int(size
), -int(size
))));
199 keys_input
[1001] = 1;
200 bc::inclusive_scan(keys_input
.begin(), keys_input
.end(), keys_input
.begin(), queue
);
202 bc::reduce_by_key(keys_input
.begin(), keys_input
.end(), values_input
.begin(),
203 keys_output
.begin(), values_output
.begin(), queue
);
205 CHECK_RANGE_EQUAL(int, 4, keys_output
, (0, 1, 2, 3));
206 CHECK_RANGE_EQUAL(int2_
, 4, values_output
,
207 (int2_(137, -137), int2_(540, -540), int2_(324, -324), int2_(23, -23)));
210 BOOST_AUTO_TEST_SUITE_END()