| 1 |
// -*- C++ -*- |
| 2 |
// { dg-options "-ltbb" } |
| 3 |
// { dg-do run { target c++17 } } |
| 4 |
// { dg-timeout-factor 3 } |
| 5 |
// { dg-require-effective-target tbb_backend } |
| 6 |
|
| 7 |
//===-- reduce.pass.cpp ---------------------------------------------------===// |
| 8 |
// |
| 9 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 10 |
// See https://llvm.org/LICENSE.txt for license information. |
| 11 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 12 |
// |
| 13 |
//===----------------------------------------------------------------------===// |
| 14 |
|
| 15 |
#include "pstl/pstl_test_config.h" |
| 16 |
|
| 17 |
#ifdef PSTL_STANDALONE_TESTS |
| 18 |
#include "pstl/execution" |
| 19 |
#include "pstl/numeric" |
| 20 |
#else |
| 21 |
#include <execution> |
| 22 |
#include <numeric> |
| 23 |
#endif // PSTL_STANDALONE_TESTS |
| 24 |
|
| 25 |
#include "pstl/test_utils.h" |
| 26 |
|
| 27 |
using namespace TestUtils; |
| 28 |
|
| 29 |
struct test_long_forms_for_one_policy |
| 30 |
{ |
| 31 |
template <typename Policy, typename Iterator, typename T, typename BinaryOp> |
| 32 |
void |
| 33 |
operator()(Policy&& exec, Iterator first, Iterator last, T init, BinaryOp binary, T expected) |
| 34 |
{ |
| 35 |
T result_r = std::reduce(exec, first, last, init, binary); |
| 36 |
EXPECT_EQ(expected, result_r, "bad result from reduce(exec, first, last, init, binary_op)"); |
| 37 |
} |
| 38 |
}; |
| 39 |
|
| 40 |
template <typename T, typename BinaryOp, typename F> |
| 41 |
void |
| 42 |
test_long_form(T init, BinaryOp binary_op, F f) |
| 43 |
{ |
| 44 |
// Try sequences of various lengths |
| 45 |
for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) |
| 46 |
{ |
| 47 |
T expected(init); |
| 48 |
Sequence<T> in(n, [n, f](size_t k) { return f((int32_t(k ^ n) % 1000 - 500)); }); |
| 49 |
for (size_t k = 0; k < n; ++k) |
| 50 |
expected = binary_op(expected, in[k]); |
| 51 |
|
| 52 |
using namespace std; |
| 53 |
|
| 54 |
T result = transform_reduce_serial(in.cfbegin(), in.cfend(), init, binary_op, [](const T& t) { return t; }); |
| 55 |
EXPECT_EQ(expected, result, "bad result from reduce(first, last, init, binary_op_op)"); |
| 56 |
|
| 57 |
invoke_on_all_policies(test_long_forms_for_one_policy(), in.begin(), in.end(), init, binary_op, expected); |
| 58 |
invoke_on_all_policies(test_long_forms_for_one_policy(), in.cbegin(), in.cend(), init, binary_op, expected); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
struct test_two_short_forms |
| 63 |
{ |
| 64 |
|
| 65 |
#if defined(_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN) //dummy specialization by policy type, in case of broken configuration |
| 66 |
template <typename Iterator> |
| 67 |
void |
| 68 |
operator()(__pstl::execution::parallel_policy, Iterator first, Iterator last, Sum init, Sum expected) |
| 69 |
{ |
| 70 |
} |
| 71 |
template <typename Iterator> |
| 72 |
void |
| 73 |
operator()(__pstl::execution::parallel_unsequenced_policy, Iterator first, Iterator last, Sum init, Sum expected) |
| 74 |
{ |
| 75 |
} |
| 76 |
#endif |
| 77 |
|
| 78 |
template <typename Policy, typename Iterator> |
| 79 |
void |
| 80 |
operator()(Policy&& exec, Iterator first, Iterator last, Sum init, Sum expected) |
| 81 |
{ |
| 82 |
using namespace std; |
| 83 |
|
| 84 |
Sum r0 = init + reduce(exec, first, last); |
| 85 |
EXPECT_EQ(expected, r0, "bad result from reduce(exec, first, last)"); |
| 86 |
|
| 87 |
Sum r1 = reduce(exec, first, last, init); |
| 88 |
EXPECT_EQ(expected, r1, "bad result from reduce(exec, first, last, init)"); |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
// Test forms of reduce(...) that omit the binary_op or init operands. |
| 93 |
void |
| 94 |
test_short_forms() |
| 95 |
{ |
| 96 |
for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) |
| 97 |
{ |
| 98 |
Sum init(42, OddTag()); |
| 99 |
Sum expected(init); |
| 100 |
Sequence<Sum> in(n, [n](size_t k) { return Sum((int32_t(k ^ n) % 1000 - 500), OddTag()); }); |
| 101 |
for (size_t k = 0; k < n; ++k) |
| 102 |
expected = expected + in[k]; |
| 103 |
invoke_on_all_policies(test_two_short_forms(), in.begin(), in.end(), init, expected); |
| 104 |
invoke_on_all_policies(test_two_short_forms(), in.cbegin(), in.cend(), init, expected); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
int |
| 109 |
main() |
| 110 |
{ |
| 111 |
// Test for popular types |
| 112 |
test_long_form(42, std::plus<int32_t>(), [](int32_t x) { return x; }); |
| 113 |
test_long_form(42.0, std::plus<float64_t>(), [](float64_t x) { return x; }); |
| 114 |
|
| 115 |
// Test for strict types |
| 116 |
test_long_form<Number>(Number(42, OddTag()), Add(OddTag()), [](int32_t x) { return Number(x, OddTag()); }); |
| 117 |
|
| 118 |
// Short forms are just facade for long forms, so just test with a single type. |
| 119 |
test_short_forms(); |
| 120 |
std::cout << done() << std::endl; |
| 121 |
return 0; |
| 122 |
} |