forked from boostorg/compute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_scan.cpp
346 lines (280 loc) · 11.1 KB
/
test_scan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <[email protected]>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//
#define BOOST_TEST_MODULE TestScan
#include <boost/test/unit_test.hpp>
#include <numeric>
#include <functional>
#include <vector>
#include <boost/compute/functional.hpp>
#include <boost/compute/lambda.hpp>
#include <boost/compute/system.hpp>
#include <boost/compute/command_queue.hpp>
#include <boost/compute/algorithm/copy.hpp>
#include <boost/compute/algorithm/exclusive_scan.hpp>
#include <boost/compute/algorithm/inclusive_scan.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/iterator/counting_iterator.hpp>
#include <boost/compute/iterator/transform_iterator.hpp>
#include "check_macros.hpp"
#include "context_setup.hpp"
namespace bc = boost::compute;
BOOST_AUTO_TEST_CASE(inclusive_scan_int)
{
int data[] = { 1, 2, 1, 2, 3 };
bc::vector<int> vector(data, data + 5, queue);
BOOST_CHECK_EQUAL(vector.size(), size_t(5));
bc::vector<int> result(5, context);
BOOST_CHECK_EQUAL(result.size(), size_t(5));
// inclusive scan
bc::inclusive_scan(vector.begin(), vector.end(), result.begin(), queue);
CHECK_RANGE_EQUAL(int, 5, result, (1, 3, 4, 6, 9));
// in-place inclusive scan
CHECK_RANGE_EQUAL(int, 5, vector, (1, 2, 1, 2, 3));
bc::inclusive_scan(vector.begin(), vector.end(), vector.begin(), queue);
CHECK_RANGE_EQUAL(int, 5, vector, (1, 3, 4, 6, 9));
}
BOOST_AUTO_TEST_CASE(exclusive_scan_int)
{
int data[] = { 1, 2, 1, 2, 3 };
bc::vector<int> vector(data, data + 5, queue);
BOOST_CHECK_EQUAL(vector.size(), size_t(5));
bc::vector<int> result(5, context);
BOOST_CHECK_EQUAL(vector.size(), size_t(5));
// exclusive scan
bc::exclusive_scan(vector.begin(), vector.end(), result.begin(), queue);
CHECK_RANGE_EQUAL(int, 5, result, (0, 1, 3, 4, 6));
// in-place exclusive scan
CHECK_RANGE_EQUAL(int, 5, vector, (1, 2, 1, 2, 3));
bc::exclusive_scan(vector.begin(), vector.end(), vector.begin(), queue);
CHECK_RANGE_EQUAL(int, 5, vector, (0, 1, 3, 4, 6));
}
BOOST_AUTO_TEST_CASE(inclusive_scan_int2)
{
using boost::compute::int2_;
int data[] = { 1, 2,
3, 4,
5, 6,
7, 8,
9, 0 };
boost::compute::vector<int2_> input(reinterpret_cast<int2_*>(data),
reinterpret_cast<int2_*>(data) + 5,
queue);
BOOST_CHECK_EQUAL(input.size(), size_t(5));
boost::compute::vector<int2_> output(5, context);
boost::compute::inclusive_scan(input.begin(), input.end(), output.begin(),
queue);
CHECK_RANGE_EQUAL(
int2_, 5, output,
(int2_(1, 2), int2_(4, 6), int2_(9, 12), int2_(16, 20), int2_(25, 20))
);
}
BOOST_AUTO_TEST_CASE(inclusive_scan_counting_iterator)
{
bc::vector<int> result(10, context);
bc::inclusive_scan(bc::make_counting_iterator(1),
bc::make_counting_iterator(11),
result.begin(), queue);
CHECK_RANGE_EQUAL(int, 10, result, (1, 3, 6, 10, 15, 21, 28, 36, 45, 55));
}
BOOST_AUTO_TEST_CASE(exclusive_scan_counting_iterator)
{
bc::vector<int> result(10, context);
bc::exclusive_scan(bc::make_counting_iterator(1),
bc::make_counting_iterator(11),
result.begin(), queue);
CHECK_RANGE_EQUAL(int, 10, result, (0, 1, 3, 6, 10, 15, 21, 28, 36, 45));
}
BOOST_AUTO_TEST_CASE(inclusive_scan_transform_iterator)
{
float data[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
bc::vector<float> input(data, data + 5, queue);
bc::vector<float> output(5, context);
// normal inclusive scan of the input
bc::inclusive_scan(input.begin(), input.end(), output.begin(), queue);
bc::system::finish();
BOOST_CHECK_CLOSE(float(output[0]), 1.0f, 1e-4f);
BOOST_CHECK_CLOSE(float(output[1]), 3.0f, 1e-4f);
BOOST_CHECK_CLOSE(float(output[2]), 6.0f, 1e-4f);
BOOST_CHECK_CLOSE(float(output[3]), 10.0f, 1e-4f);
BOOST_CHECK_CLOSE(float(output[4]), 15.0f, 1e-4f);
// inclusive scan of squares of the input
using ::boost::compute::_1;
bc::inclusive_scan(bc::make_transform_iterator(input.begin(), pown(_1, 2)),
bc::make_transform_iterator(input.end(), pown(_1, 2)),
output.begin(), queue);
bc::system::finish();
BOOST_CHECK_CLOSE(float(output[0]), 1.0f, 1e-4f);
BOOST_CHECK_CLOSE(float(output[1]), 5.0f, 1e-4f);
BOOST_CHECK_CLOSE(float(output[2]), 14.0f, 1e-4f);
BOOST_CHECK_CLOSE(float(output[3]), 30.0f, 1e-4f);
BOOST_CHECK_CLOSE(float(output[4]), 55.0f, 1e-4f);
}
BOOST_AUTO_TEST_CASE(inclusive_scan_doctest)
{
//! [inclusive_scan_int]
// setup input
int data[] = { 1, 2, 3, 4 };
boost::compute::vector<int> input(data, data + 4, queue);
// setup output
boost::compute::vector<int> output(4, context);
// scan values
boost::compute::inclusive_scan(
input.begin(), input.end(), output.begin(), queue
);
// output = [ 1, 3, 6, 10 ]
//! [inclusive_scan_int]
CHECK_RANGE_EQUAL(int, 4, output, (1, 3, 6, 10));
}
BOOST_AUTO_TEST_CASE(exclusive_scan_doctest)
{
//! [exclusive_scan_int]
// setup input
int data[] = { 1, 2, 3, 4 };
boost::compute::vector<int> input(data, data + 4, queue);
// setup output
boost::compute::vector<int> output(4, context);
// scan values
boost::compute::exclusive_scan(
input.begin(), input.end(), output.begin(), queue
);
// output = [ 0, 1, 3, 6 ]
//! [exclusive_scan_int]
CHECK_RANGE_EQUAL(int, 4, output, (0, 1, 3, 6));
}
BOOST_AUTO_TEST_CASE(inclusive_scan_int_multiplies)
{
//! [inclusive_scan_int_multiplies]
// setup input
int data[] = { 1, 2, 1, 2, 3 };
boost::compute::vector<int> input(data, data + 5, queue);
// setup output
boost::compute::vector<int> output(5, context);
// inclusive scan with multiplication
boost::compute::inclusive_scan(
input.begin(), input.end(), output.begin(),
boost::compute::multiplies<int>(), queue
);
// output = [1, 2, 2, 4, 12]
//! [inclusive_scan_int_multiplies]
BOOST_CHECK_EQUAL(input.size(), size_t(5));
BOOST_CHECK_EQUAL(output.size(), size_t(5));
CHECK_RANGE_EQUAL(int, 5, output, (1, 2, 2, 4, 12));
// in-place inclusive scan
CHECK_RANGE_EQUAL(int, 5, input, (1, 2, 1, 2, 3));
boost::compute::inclusive_scan(input.begin(), input.end(), input.begin(),
boost::compute::multiplies<int>(), queue);
CHECK_RANGE_EQUAL(int, 5, input, (1, 2, 2, 4, 12));
}
BOOST_AUTO_TEST_CASE(exclusive_scan_int_multiplies)
{
//! [exclusive_scan_int_multiplies]
// setup input
int data[] = { 1, 2, 1, 2, 3 };
boost::compute::vector<int> input(data, data + 5, queue);
// setup output
boost::compute::vector<int> output(5, context);
// exclusive_scan with multiplication
// initial value equals 10
boost::compute::exclusive_scan(
input.begin(), input.end(), output.begin(),
int(10), boost::compute::multiplies<int>(), queue
);
// output = [10, 10, 20, 20, 40]
//! [exclusive_scan_int_multiplies]
BOOST_CHECK_EQUAL(input.size(), size_t(5));
BOOST_CHECK_EQUAL(output.size(), size_t(5));
CHECK_RANGE_EQUAL(int, 5, output, (10, 10, 20, 20, 40));
// in-place exclusive scan
CHECK_RANGE_EQUAL(int, 5, input, (1, 2, 1, 2, 3));
bc::exclusive_scan(input.begin(), input.end(), input.begin(),
int(10), bc::multiplies<int>(), queue);
CHECK_RANGE_EQUAL(int, 5, input, (10, 10, 20, 20, 40));
}
BOOST_AUTO_TEST_CASE(inclusive_scan_int_multiplies_long_vector)
{
size_t size = 1000;
bc::vector<int> device_vector(size, int(2), queue);
BOOST_CHECK_EQUAL(device_vector.size(), size);
bc::inclusive_scan(device_vector.begin(), device_vector.end(),
device_vector.begin(), bc::multiplies<int>(), queue);
std::vector<int> host_vector(size, 2);
BOOST_CHECK_EQUAL(host_vector.size(), size);
bc::copy(device_vector.begin(), device_vector.end(),
host_vector.begin(), queue);
std::vector<int> test(size, 2);
BOOST_CHECK_EQUAL(test.size(), size);
std::partial_sum(test.begin(), test.end(),
test.begin(), std::multiplies<int>());
BOOST_CHECK_EQUAL_COLLECTIONS(host_vector.begin(), host_vector.end(),
test.begin(), test.end());
}
BOOST_AUTO_TEST_CASE(exclusive_scan_int_multiplies_long_vector)
{
size_t size = 1000;
bc::vector<int> device_vector(size, int(2), queue);
BOOST_CHECK_EQUAL(device_vector.size(), size);
bc::exclusive_scan(device_vector.begin(), device_vector.end(),
device_vector.begin(), int(10), bc::multiplies<int>(),
queue);
std::vector<int> host_vector(size, 2);
BOOST_CHECK_EQUAL(host_vector.size(), size);
bc::copy(device_vector.begin(), device_vector.end(),
host_vector.begin(), queue);
std::vector<int> test(size, 2);
BOOST_CHECK_EQUAL(test.size(), size);
test[0] = 10;
std::partial_sum(test.begin(), test.end(),
test.begin(), std::multiplies<int>());
BOOST_CHECK_EQUAL_COLLECTIONS(host_vector.begin(), host_vector.end(),
test.begin(), test.end());
}
BOOST_AUTO_TEST_CASE(inclusive_scan_int_custom_function)
{
BOOST_COMPUTE_FUNCTION(int, multi, (int x, int y),
{
return x * y * 2;
});
int data[] = { 1, 2, 1, 2, 3 };
bc::vector<int> vector(data, data + 5, queue);
BOOST_CHECK_EQUAL(vector.size(), size_t(5));
bc::vector<int> result(5, context);
BOOST_CHECK_EQUAL(result.size(), size_t(5));
// inclusive scan
bc::inclusive_scan(vector.begin(), vector.end(), result.begin(),
multi, queue);
CHECK_RANGE_EQUAL(int, 5, result, (1, 4, 8, 32, 192));
// in-place inclusive scan
CHECK_RANGE_EQUAL(int, 5, vector, (1, 2, 1, 2, 3));
bc::inclusive_scan(vector.begin(), vector.end(), vector.begin(),
multi, queue);
CHECK_RANGE_EQUAL(int, 5, vector, (1, 4, 8, 32, 192));
}
BOOST_AUTO_TEST_CASE(exclusive_scan_int_custom_function)
{
BOOST_COMPUTE_FUNCTION(int, multi, (int x, int y),
{
return x * y * 2;
});
int data[] = { 1, 2, 1, 2, 3 };
bc::vector<int> vector(data, data + 5, queue);
BOOST_CHECK_EQUAL(vector.size(), size_t(5));
bc::vector<int> result(5, context);
BOOST_CHECK_EQUAL(result.size(), size_t(5));
// exclusive_scan
bc::exclusive_scan(vector.begin(), vector.end(), result.begin(),
int(1), multi, queue);
CHECK_RANGE_EQUAL(int, 5, result, (1, 2, 8, 16, 64));
// in-place exclusive scan
CHECK_RANGE_EQUAL(int, 5, vector, (1, 2, 1, 2, 3));
bc::exclusive_scan(vector.begin(), vector.end(), vector.begin(),
int(1), multi, queue);
CHECK_RANGE_EQUAL(int, 5, vector, (1, 2, 8, 16, 64));
}
BOOST_AUTO_TEST_SUITE_END()