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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
|
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// 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.
//---------------------------------------------------------------------------//
// Undefining BOOST_COMPUTE_USE_OFFLINE_CACHE macro as we want to modify cached
// parameters for copy algorithm without any undesirable consequences (like
// saving modified values of those parameters).
#ifdef BOOST_COMPUTE_USE_OFFLINE_CACHE
#undef BOOST_COMPUTE_USE_OFFLINE_CACHE
#endif
#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)
{
using boost::compute::uint_;
using boost::compute::int_;
int_ data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
bc::vector<int_> vector(data, data + 12, queue);
BOOST_CHECK_EQUAL(vector.size(), size_t(12));
bc::vector<int_> result(12, context);
BOOST_CHECK_EQUAL(result.size(), size_t(12));
// inclusive scan
bc::inclusive_scan(vector.begin(), vector.end(), result.begin(), queue);
CHECK_RANGE_EQUAL(int_, 12, result, (0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66));
// in-place inclusive scan
CHECK_RANGE_EQUAL(int_, 12, vector, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
bc::inclusive_scan(vector.begin(), vector.end(), vector.begin(), queue);
CHECK_RANGE_EQUAL(int_, 12, vector, (0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66));
// scan_on_cpu
bc::copy(data, data + 12, vector.begin(), queue);
// make sure parallel scan_on_cpu is used, no serial_scan
std::string cache_key =
"__boost_scan_cpu_4";
boost::shared_ptr<bc::detail::parameter_cache> parameters =
bc::detail::parameter_cache::get_global_cache(device);
// save
uint_ map_copy_threshold =
parameters->get(cache_key, "serial_scan_threshold", 0);
// force parallel scan_on_cpu
parameters->set(cache_key, "serial_scan_threshold", 0);
// inclusive scan
bc::inclusive_scan(vector.begin(), vector.end(), result.begin(), queue);
CHECK_RANGE_EQUAL(int_, 12, result, (0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66));
// in-place inclusive scan
CHECK_RANGE_EQUAL(int_, 12, vector, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
bc::inclusive_scan(vector.begin(), vector.end(), vector.begin(), queue);
CHECK_RANGE_EQUAL(int_, 12, vector, (0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66));
// restore
parameters->set(cache_key, "serial_scan_threshold", map_copy_threshold);
}
BOOST_AUTO_TEST_CASE(exclusive_scan_int)
{
using boost::compute::uint_;
using boost::compute::int_;
int_ data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
bc::vector<int_> vector(data, data + 12, queue);
BOOST_CHECK_EQUAL(vector.size(), size_t(12));
bc::vector<int_> result(size_t(12), int_(0), queue);
BOOST_CHECK_EQUAL(result.size(), size_t(12));
// exclusive scan
bc::exclusive_scan(vector.begin(), vector.end(), result.begin(), queue);
CHECK_RANGE_EQUAL(int_, 12, result, (0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55));
// in-place exclusive scan
CHECK_RANGE_EQUAL(int_, 12, vector, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
bc::exclusive_scan(vector.begin(), vector.end(), vector.begin(), queue);
CHECK_RANGE_EQUAL(int_, 12, vector, (0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55));
// scan_on_cpu
bc::copy(data, data + 12, vector.begin(), queue);
// make sure parallel scan_on_cpu is used, no serial_scan
std::string cache_key =
"__boost_scan_cpu_4";
boost::shared_ptr<bc::detail::parameter_cache> parameters =
bc::detail::parameter_cache::get_global_cache(device);
// save
uint_ map_copy_threshold =
parameters->get(cache_key, "serial_scan_threshold", 0);
// force parallel scan_on_cpu
parameters->set(cache_key, "serial_scan_threshold", 0);
// exclusive scan
bc::exclusive_scan(vector.begin(), vector.end(), result.begin(), queue);
CHECK_RANGE_EQUAL(int_, 12, result, (0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55));
// in-place exclusive scan
CHECK_RANGE_EQUAL(int_, 12, vector, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
bc::exclusive_scan(vector.begin(), vector.end(), vector.begin(), queue);
CHECK_RANGE_EQUAL(int_, 12, vector, (0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55));
// restore
parameters->set(cache_key, "serial_scan_threshold", map_copy_threshold);
}
BOOST_AUTO_TEST_CASE(inclusive_scan_int2)
{
using boost::compute::int_;
using boost::compute::uint_;
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))
);
// scan_on_cpu
// make sure parallel scan_on_cpu is used, no serial_scan
std::string cache_key =
"__boost_scan_cpu_8";
boost::shared_ptr<bc::detail::parameter_cache> parameters =
bc::detail::parameter_cache::get_global_cache(device);
// save
uint_ map_copy_threshold =
parameters->get(cache_key, "serial_scan_threshold", 0);
// force parallel scan_on_cpu
parameters->set(cache_key, "serial_scan_threshold", 0);
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))
);
// restore
parameters->set(cache_key, "serial_scan_threshold", map_copy_threshold);
}
BOOST_AUTO_TEST_CASE(inclusive_scan_counting_iterator)
{
using boost::compute::int_;
using boost::compute::uint_;
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));
// scan_on_cpu
// make sure parallel scan_on_cpu is used, no serial_scan
std::string cache_key =
"__boost_scan_cpu_4";
boost::shared_ptr<bc::detail::parameter_cache> parameters =
bc::detail::parameter_cache::get_global_cache(device);
// save
uint_ map_copy_threshold =
parameters->get(cache_key, "serial_scan_threshold", 0);
// force parallel scan_on_cpu
parameters->set(cache_key, "serial_scan_threshold", 0);
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));
// restore
parameters->set(cache_key, "serial_scan_threshold", map_copy_threshold);
}
BOOST_AUTO_TEST_CASE(exclusive_scan_counting_iterator)
{
using boost::compute::int_;
using boost::compute::uint_;
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));
// scan_on_cpu
// make sure parallel scan_on_cpu is used, no serial_scan
std::string cache_key =
"__boost_scan_cpu_4";
boost::shared_ptr<bc::detail::parameter_cache> parameters =
bc::detail::parameter_cache::get_global_cache(device);
// save
uint_ map_copy_threshold =
parameters->get(cache_key, "serial_scan_threshold", 0);
// force parallel scan_on_cpu
parameters->set(cache_key, "serial_scan_threshold", 0);
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));
// restore
parameters->set(cache_key, "serial_scan_threshold", map_copy_threshold);
}
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);
queue.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);
queue.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()
|