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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
|
#pragma once
#include <thrust/detail/config.h>
#if THRUST_CPP_DIALECT >= 2014
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/sequence.h>
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/discard_iterator.h>
#include <thrust/type_traits/logical_metafunctions.h>
#include <unittest/unittest.h>
#include <unittest/util_async.h>
#include <tuple>
#include <type_traits>
// clang-format off
// This file contains a set of mix-in classes that define an algorithm
// definition for use with test_policy_overloads<algo_def>. The algorithm
// definition describes the details of a thrust::async algorithm invocation:
//
// - Input type and initialization
// - Output type and initialization (supports in-place, too)
// - Postfix arguments that define the algorithm's overload set
// - Abstracted invocation of the async algorithm
// - Abstracted invocation of a reference algorithm
// - Validation of async vs. reference output
// - A description string.
//
// This definition is used by test_policy_overloads to test each overload
// against a reference while injecting a variety of execution policies. This
// validates that each overload behaves correctly according to some reference.
//
// Since much of the algorithm definition is generic and may be reused in
// multiple tests with slight changes, a mix-in system is used to simplify
// the creation of algorithm definitions. The following namespace hierarchy is
// used to organize these generic components:
//
// * testing::async::mixin::
// ** ::input - Input types/values (device vectors, counting iterators, etc)
// ** ::output - Output types/values (device vectors, inplace device vectors,
// discard iterators, etc)
// ** ::postfix_args - Algorithm specific overload sets
// ** ::invoke_reference - Algorithm specific reference invocation
// ** ::invoke_async - Algorithm specific async algo invocation
// ** ::compare_outputs - Compare output values.
//
// Each algorithm should define its own `mixins.h` header to declare algorithm
// specific mixins (e.g. postfix_args, invoke_reference, and invoke_async)
// in a testing::async::<algorithm_name>::mixins namespace structure.
//
// For example, the test.async.exclusive_scan.basic test uses the following
// algorithm definition from mix-ins:
//
// ```
// #include <async/test_policy_overloads.h>
// #include <async/mixin.h>
// #include <async/exclusive_scan/mixin.h>
// template <typename input_value_type,
// typename output_value_type = input_value_type,
// typename initial_value_type = input_value_type,
// typename alternate_binary_op = thrust::maximum<>>
// struct basic_invoker
// : testing::async::mixin::input::device_vector<input_value_type>
// , testing::async::mixin::output::device_vector<output_value_type>
// , testing::async::exclusive_scan::mixin::postfix_args::
// all_overloads<initial_value_type, alternate_binary_op>
// , testing::async::exclusive_scan::mixin::invoke_reference::
// host_synchronous<input_value_type, output_value_type>
// , testing::async::exclusive_scan::mixin::invoke_async::basic
// , testing::async::mixin::compare_outputs::assert_equal_quiet
// {
// static std::string description()
// {
// return "basic invocation with device vectors";
// }
// };
//
// ...
//
// testing::async::test_policy_overloads<basic_invoker<T>>::run(num_values);
// ```
//
// The basic_invoker class expands to something similar to the following:
//
// ```
// template <typename input_value_type,
// typename output_value_type = input_value_type,
// typename initial_value_type = input_value_type,
// typename alternate_binary_op = thrust::maximum<>>
// struct basic_invoker
// {
// public:
//
// static std::string description()
// {
// return "basic invocation with device vectors";
// }
//
// //-------------------------------------------------------------------------
// // testing::async::mixin::input::device_vector
// //
// // input_type must provide idiomatic definitions of:
// // - `using iterator = ...;`
// // - `iterator begin() const { ... }`
// // - `iterator end() const { ... }`
// // - `size_t size() const { ... }`
// using input_type = thrust::device_vector<input_value_type>;
//
// // Generate an instance of the input:
// static input_type generate_input(std::size_t num_values)
// {
// input_type input(num_values);
// thrust::sequence(input.begin(), input.end(), 25, 3);
// return input;
// }
//
// //-------------------------------------------------------------------------
// // testing::async::mixin::output::device_vector
// //
// // output_type must provide idiomatic definitions of:
// // - `using iterator = ...;`
// // - `iterator begin() { ... }`
// using output_type = thrust::device_vector<output_value_type>;
//
// // Generate an instance of the output:
// // Might be more complicated, eg. fancy iterators, etc
// static output_type generate_output(std::size_t num_values)
// {
// return output_type(num_values);
// }
//
// //-------------------------------------------------------------------------
// // testing::async::exclusive_scan::mixin::postfix_args::all_overloads
// using postfix_args_type = std::tuple< // List any extra arg overloads:
// std::tuple<>, // - no extra args
// std::tuple<initial_value_type>, // - initial_value
// std::tuple<initial_value_type, alternate_binary_op> // - initial_value, binary_op
// >;
//
// // Create instances of the extra arguments to use when invoking the
// // algorithm:
// static postfix_args_type generate_postfix_args()
// {
// return postfix_args_type{
// std::tuple<>{}, // no extra args
// std::make_tuple(initial_value_type{42}), // initial_value
// // initial_value, binary_op:
// std::make_tuple(initial_value_Type{57}, alternate_binary_op{})
// };
// }
//
// //-------------------------------------------------------------------------
// //
// testing::async::exclusive_scan::mixin::invoke_reference::host_synchronous
// //
// // Invoke a reference implementation for a single overload as described by
// // postfix_tuple. This tuple contains instances of any trailing arguments
// // to pass to the algorithm. The tuple/index_sequence pattern is used to
// // support a "no extra args" overload, since the parameter pack expansion
// // will do exactly what we want in all cases.
// template <typename PostfixArgTuple, std::size_t... PostfixArgIndices>
// static void invoke_reference(input_type const &input,
// output_type &output,
// PostfixArgTuple &&postfix_tuple,
// std::index_sequence<PostfixArgIndices...>)
// {
// // Create host versions of the input/output:
// thrust::host_vector<input_value_type> host_input(input.cbegin(),
// input.cend());
// thrust::host_vector<output_value_type> host_output(host_input.size());
//
// // Run host synchronous algorithm to generate reference.
// thrust::exclusive_scan(host_input.cbegin(),
// host_input.cend(),
// host_output.begin(),
// std::get<PostfixArgIndices>(
// THRUST_FWD(postfix_tuple))...);
//
// // Copy back to device.
// output = host_output;
// }
//
// //-------------------------------------------------------------------------
// // testing::async::mixin::exclusive_scan::mixin::invoke_async::basic
// //
// // Invoke the async algorithm for a single overload as described by
// // the prefix and postfix tuples. These tuples contains instances of any
// // additional arguments to pass to the algorithm. The tuple/index_sequence
// // pattern is used to support the "no extra args" overload, since the
// // parameter pack expansion will do exactly what we want in all cases.
// // Prefix args are included here (but not for invoke_reference) to allow
// // the test framework to change the execution policy.
// // This method must return an event or future.
// template <typename PrefixArgTuple,
// std::size_t... PrefixArgIndices,
// typename PostfixArgTuple,
// std::size_t... PostfixArgIndices>
// static auto invoke_async(PrefixArgTuple &&prefix_tuple,
// std::index_sequence<PrefixArgIndices...>,
// input_type const &input,
// output_type &output,
// PostfixArgTuple &&postfix_tuple,
// std::index_sequence<PostfixArgIndices...>)
// {
// output.resize(input.size());
// auto e = thrust::async::exclusive_scan(
// std::get<PrefixArgIndices>(THRUST_FWD(prefix_tuple))...,
// input.cbegin(),
// input.cend(),
// output.begin(),
// std::get<PostfixArgIndices>(THRUST_FWD(postfix_tuple))...);
// return e;
// }
//
// //-------------------------------------------------------------------------
// // testing::async::mixin::compare_outputs::assert_equal_quiet
// //
// // Wait on and validate the event/future (usually with TEST_EVENT_WAIT /
// // TEST_FUTURE_VALUE_RETRIEVAL), then check that the reference output
// // matches the testing output.
// template <typename EventType>
// static void compare_outputs(EventType &e,
// output_type const &ref,
// output_type const &test)
// {
// TEST_EVENT_WAIT(e);
// ASSERT_EQUAL_QUIET(ref, test);
// }
// };
// ```
//
// Similar invokers with slight tweaks are used in other
// async/exclusive_scan/*.cu tests.
// clang-format on
namespace testing
{
namespace async
{
namespace mixin
{
//------------------------------------------------------------------------------
namespace input
{
template <typename value_type>
struct device_vector
{
using input_type = thrust::device_vector<value_type>;
static input_type generate_input(std::size_t num_values)
{
input_type input(num_values);
thrust::sequence(input.begin(),
input.end(),
static_cast<value_type>(1),
static_cast<value_type>(1));
return input;
}
};
template <typename value_type>
struct counting_iterator_from_0
{
struct input_type
{
using iterator = thrust::counting_iterator<value_type>;
std::size_t num_values;
iterator begin() const { return iterator{static_cast<value_type>(0)}; }
iterator cbegin() const { return iterator{static_cast<value_type>(0)}; }
iterator end() const { return iterator{static_cast<value_type>(num_values)}; }
iterator cend() const { return iterator{static_cast<value_type>(num_values)}; }
std::size_t size() const { return num_values; }
};
static input_type generate_input(std::size_t num_values)
{
return {num_values};
}
};
template <typename value_type>
struct counting_iterator_from_1
{
struct input_type
{
using iterator = thrust::counting_iterator<value_type>;
std::size_t num_values;
iterator begin() const { return iterator{static_cast<value_type>(1)}; }
iterator cbegin() const { return iterator{static_cast<value_type>(1)}; }
iterator end() const { return iterator{static_cast<value_type>(1 + num_values)}; }
iterator cend() const { return iterator{static_cast<value_type>(1 + num_values)}; }
std::size_t size() const { return num_values; }
};
static input_type generate_input(std::size_t num_values)
{
return {num_values};
}
};
template <typename value_type>
struct constant_iterator_1
{
struct input_type
{
using iterator = thrust::constant_iterator<value_type>;
std::size_t num_values;
iterator begin() const { return iterator{static_cast<value_type>(1)}; }
iterator cbegin() const { return iterator{static_cast<value_type>(1)}; }
iterator end() const
{
return iterator{static_cast<value_type>(1)} + num_values;
}
iterator cend() const
{
return iterator{static_cast<value_type>(1)} + num_values;
}
std::size_t size() const { return num_values; }
};
static input_type generate_input(std::size_t num_values)
{
return {num_values};
}
};
} // namespace input
//------------------------------------------------------------------------------
namespace output
{
template <typename value_type>
struct device_vector
{
using output_type = thrust::device_vector<value_type>;
template <typename InputType>
static output_type generate_output(std::size_t num_values,
InputType& /* unused */)
{
return output_type(num_values);
}
};
template <typename value_type>
struct device_vector_reuse_input
{
using output_type = thrust::device_vector<value_type>&;
template <typename InputType>
static output_type generate_output(std::size_t /*num_values*/,
InputType& input)
{
return input;
}
};
struct discard_iterator
{
struct output_type
{
using iterator = thrust::discard_iterator<>;
iterator begin() const { return thrust::make_discard_iterator(); }
iterator cbegin() const { return thrust::make_discard_iterator(); }
};
template <typename InputType>
static output_type generate_output(std::size_t /* num_values */,
InputType& /* input */)
{
return output_type{};
}
};
} // namespace output
//------------------------------------------------------------------------------
namespace postfix_args
{
/* Defined per algorithm. Example:
*
* // Defines several overloads:
* // algorithm([policy,] input, output) // no postfix args
* // algorithm([policy,] input, output, initial_value)
* // algorithm([policy,] input, output, initial_value, binary_op)
* template <typename value_type,
* typename alternate_binary_op = thrust::maximum<>>
* struct all_overloads
* {
* using postfix_args_type = std::tuple< // List any extra arg overloads:
* std::tuple<>, // - no extra args
* std::tuple<value_type>, // - initial_value
* std::tuple<value_type, alternate_binary_op> // - initial_value, binary_op
* >;
*
* static postfix_args_type generate_postfix_args()
* {
* return postfix_args_type{
* std::tuple<>{}, // no extra args
* std::make_tuple(initial_value_type{42}), // initial_value
* // initial_value, binary_op:
* std::make_tuple(initial_value_Type{57}, alternate_binary_op{})
* }
* };
*
*/
}
//------------------------------------------------------------------------------
namespace invoke_reference
{
/* Defined per algorithm. Example:
*
* template <typename input_value_type,
* typename output_value_type = input_value_type>
* struct host_synchronous
* {
* template <typename InputType,
* typename OutputType,
* typename PostfixArgTuple,
* std::size_t... PostfixArgIndices>
* static void invoke_reference(InputType const& input,
* OutputType& output,
* PostfixArgTuple&& postfix_tuple,
* std::index_sequence<PostfixArgIndices...>)
* {
* // Create host versions of the input/output:
* thrust::host_vector<input_value_type> host_input(input.cbegin(),
* input.cend());
* thrust::host_vector<output_value_type> host_output(host_input.size());
*
* // Run host synchronous algorithm to generate reference.
* // Be sure to call a backend that doesn't use the same underlying
* // implementation.
* thrust::exclusive_scan(host_input.cbegin(),
* host_input.cend(),
* host_output.begin(),
* std::get<PostfixArgIndices>(
* THRUST_FWD(postfix_tuple))...);
*
* // Copy back to device.
* output = host_output;
* }
* };
*
*/
// Used to save time when testing unverifiable invocations (discard_iterators)
struct noop
{
template <typename... Ts>
static void invoke_reference(Ts&&...)
{}
};
} // namespace invoke_reference
//------------------------------------------------------------------------------
namespace invoke_async
{
/* Defined per algorithm. Example:
*
* struct basic
* {
* template <typename PrefixArgTuple,
* std::size_t... PrefixArgIndices,
* typename InputType,
* typename OutputType,
* typename PostfixArgTuple,
* std::size_t... PostfixArgIndices>
* static auto invoke_async(PrefixArgTuple&& prefix_tuple,
* std::index_sequence<PrefixArgIndices...>,
* InputType const& input,
* OutputType& output,
* PostfixArgTuple&& postfix_tuple,
* std::index_sequence<PostfixArgIndices...>)
* {
* auto e = thrust::async::exclusive_scan(
* std::get<PrefixArgIndices>(THRUST_FWD(prefix_tuple))...,
* input.cbegin(),
* input.cend(),
* output.begin(),
* std::get<PostfixArgIndices>(THRUST_FWD(postfix_tuple))...);
* return e;
* }
* };
*/
} // namespace invoke_async
//------------------------------------------------------------------------------
namespace compare_outputs
{
namespace detail
{
void basic_event_validation(thrust::device_event& e)
{
TEST_EVENT_WAIT(e);
}
template <typename T>
void basic_event_validation(thrust::device_future<T>& f)
{
TEST_FUTURE_VALUE_RETRIEVAL(f);
}
} // namespace detail
struct assert_equal
{
template <typename EventType, typename OutputType>
static void compare_outputs(EventType& e,
OutputType const& ref,
OutputType const& test)
{
detail::basic_event_validation(e);
ASSERT_EQUAL(ref, test);
}
};
struct assert_almost_equal
{
template <typename EventType, typename OutputType>
static void compare_outputs(EventType& e,
OutputType const& ref,
OutputType const& test)
{
detail::basic_event_validation(e);
ASSERT_ALMOST_EQUAL(ref, test);
}
};
// Does an 'almost_equal' comparison for floating point types. Since fp
// addition is non-associative, this is sometimes necessary.
struct assert_almost_equal_if_fp
{
private:
template <typename EventType, typename OutputType>
static void compare_outputs_impl(EventType& e,
OutputType const& ref,
OutputType const& test,
std::false_type /* is_floating_point */)
{
detail::basic_event_validation(e);
ASSERT_EQUAL(ref, test);
}
template <typename EventType, typename OutputType>
static void compare_outputs_impl(EventType& e,
OutputType const& ref,
OutputType const& test,
std::true_type /* is_floating_point */)
{
detail::basic_event_validation(e);
ASSERT_ALMOST_EQUAL(ref, test);
}
public:
template <typename EventType, typename OutputType>
static void compare_outputs(EventType& e,
OutputType const& ref,
OutputType const& test)
{
using value_type = typename OutputType::value_type;
compare_outputs_impl(e, ref, test, std::is_floating_point<value_type>{});
}
};
struct assert_equal_quiet
{
template <typename EventType, typename OutputType>
static void compare_outputs(EventType& e,
OutputType const& ref,
OutputType const& test)
{
detail::basic_event_validation(e);
ASSERT_EQUAL_QUIET(ref, test);
}
};
// Does an 'almost_equal' comparison for floating point types, since fp
// addition is non-associative
struct assert_almost_equal_if_fp_quiet
{
private:
template <typename EventType, typename OutputType>
static void compare_outputs_impl(EventType& e,
OutputType const& ref,
OutputType const& test,
std::false_type /* is_floating_point */)
{
detail::basic_event_validation(e);
ASSERT_EQUAL_QUIET(ref, test);
}
template <typename EventType, typename OutputType>
static void compare_outputs_impl(EventType& e,
OutputType const& ref,
OutputType const& test,
std::true_type /* is_floating_point */)
{
detail::basic_event_validation(e);
ASSERT_ALMOST_EQUAL(ref, test);
}
public:
template <typename EventType, typename OutputType>
static void compare_outputs(EventType& e,
OutputType const& ref,
OutputType const& test)
{
using value_type = typename OutputType::value_type;
compare_outputs_impl(e, ref, test, std::is_floating_point<value_type>{});
}
};
// Used to save time when testing unverifiable invocations (discard_iterators).
// Just does basic validation of the future/event.
struct noop
{
template <typename EventType, typename... Ts>
static void compare_outputs(EventType &e, Ts&&...)
{
detail::basic_event_validation(e);
}
};
} // namespace compare_outputs
} // namespace mixin
} // namespace async
} // namespace testing
#endif // C++14
|