| 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 |
//===-- transform_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 |
|
| 19 |
#include "pstl/execution" |
| 20 |
#include "pstl/numeric" |
| 21 |
#else |
| 22 |
#include <execution> |
| 23 |
#include <numeric> |
| 24 |
#endif // PSTL_STANDALONE_TESTS |
| 25 |
|
| 26 |
#include "pstl/test_utils.h" |
| 27 |
|
| 28 |
using namespace TestUtils; |
| 29 |
|
| 30 |
// Equal for all types |
| 31 |
template <typename T> |
| 32 |
static bool |
| 33 |
Equal(T x, T y) |
| 34 |
{ |
| 35 |
return x == y; |
| 36 |
} |
| 37 |
|
| 38 |
// Functor for xor-operation for modeling binary operations in inner_product |
| 39 |
class XOR |
| 40 |
{ |
| 41 |
public: |
| 42 |
template <typename T> |
| 43 |
T |
| 44 |
operator()(const T& left, const T& right) const |
| 45 |
{ |
| 46 |
return left ^ right; |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
// Model of User-defined class |
| 51 |
class MyClass |
| 52 |
{ |
| 53 |
public: |
| 54 |
int32_t my_field; |
| 55 |
MyClass() { my_field = 0; } |
| 56 |
MyClass(int32_t in) { my_field = in; } |
| 57 |
MyClass(const MyClass& in) = default; |
| 58 |
|
| 59 |
friend MyClass |
| 60 |
operator+(const MyClass& x, const MyClass& y) |
| 61 |
{ |
| 62 |
return MyClass(x.my_field + y.my_field); |
| 63 |
} |
| 64 |
friend MyClass |
| 65 |
operator-(const MyClass& x) |
| 66 |
{ |
| 67 |
return MyClass(-x.my_field); |
| 68 |
} |
| 69 |
friend MyClass operator*(const MyClass& x, const MyClass& y) |
| 70 |
{ |
| 71 |
return MyClass(x.my_field * y.my_field); |
| 72 |
} |
| 73 |
friend bool operator==(const MyClass& x, const MyClass& y) |
| 74 |
{ |
| 75 |
return x.my_field == y.my_field; |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
template <typename T> |
| 80 |
void |
| 81 |
CheckResults(const T& expected, const T& in) |
| 82 |
{ |
| 83 |
EXPECT_TRUE(expected == in, "wrong result of transform_reduce"); |
| 84 |
} |
| 85 |
|
| 86 |
// We need to check correctness only for "int" (for example) except cases |
| 87 |
// if we have "floating-point type"-specialization |
| 88 |
void |
| 89 |
CheckResults(const float32_t&, const float32_t&) |
| 90 |
{ |
| 91 |
} |
| 92 |
|
| 93 |
// Test for different types and operations with different iterators |
| 94 |
struct test_transform_reduce |
| 95 |
{ |
| 96 |
template <typename Policy, typename InputIterator1, typename InputIterator2, typename T, typename BinaryOperation1, |
| 97 |
typename BinaryOperation2, typename UnaryOp> |
| 98 |
void |
| 99 |
operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2, |
| 100 |
T init, BinaryOperation1 opB1, BinaryOperation2 opB2, UnaryOp opU) |
| 101 |
{ |
| 102 |
|
| 103 |
auto expectedB = std::inner_product(first1, last1, first2, init, opB1, opB2); |
| 104 |
auto expectedU = transform_reduce_serial(first1, last1, init, opB1, opU); |
| 105 |
T resRA = std::transform_reduce(exec, first1, last1, first2, init, opB1, opB2); |
| 106 |
CheckResults(expectedB, resRA); |
| 107 |
resRA = std::transform_reduce(exec, first1, last1, init, opB1, opU); |
| 108 |
CheckResults(expectedU, resRA); |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
template <typename T, typename BinaryOperation1, typename BinaryOperation2, typename UnaryOp, typename Initializer> |
| 113 |
void |
| 114 |
test_by_type(T init, BinaryOperation1 opB1, BinaryOperation2 opB2, UnaryOp opU, Initializer initObj) |
| 115 |
{ |
| 116 |
|
| 117 |
std::size_t maxSize = 100000; |
| 118 |
Sequence<T> in1(maxSize, initObj); |
| 119 |
Sequence<T> in2(maxSize, initObj); |
| 120 |
|
| 121 |
for (std::size_t n = 0; n < maxSize; n = n < 16 ? n + 1 : size_t(3.1415 * n)) |
| 122 |
{ |
| 123 |
invoke_on_all_policies(test_transform_reduce(), in1.begin(), in1.begin() + n, in2.begin(), in2.begin() + n, |
| 124 |
init, opB1, opB2, opU); |
| 125 |
invoke_on_all_policies(test_transform_reduce(), in1.cbegin(), in1.cbegin() + n, in2.cbegin(), in2.cbegin() + n, |
| 126 |
init, opB1, opB2, opU); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
int |
| 131 |
main() |
| 132 |
{ |
| 133 |
test_by_type<int32_t>(42, std::plus<int32_t>(), std::multiplies<int32_t>(), std::negate<int32_t>(), |
| 134 |
[](std::size_t) -> int32_t { return int32_t(rand() % 1000); }); |
| 135 |
test_by_type<int64_t>(0, [](const int64_t& a, const int64_t& b) -> int64_t { return a | b; }, XOR(), |
| 136 |
[](const int64_t& x) -> int64_t { return x * 2; }, |
| 137 |
[](std::size_t) -> int64_t { return int64_t(rand() % 1000); }); |
| 138 |
test_by_type<float32_t>( |
| 139 |
1.0f, std::multiplies<float32_t>(), [](const float32_t& a, const float32_t& b) -> float32_t { return a + b; }, |
| 140 |
[](const float32_t& x) -> float32_t { return x + 2; }, [](std::size_t) -> float32_t { return rand() % 1000; }); |
| 141 |
test_by_type<MyClass>(MyClass(), std::plus<MyClass>(), std::multiplies<MyClass>(), std::negate<MyClass>(), |
| 142 |
[](std::size_t) -> MyClass { return MyClass(rand() % 1000); }); |
| 143 |
|
| 144 |
std::cout << done() << std::endl; |
| 145 |
return 0; |
| 146 |
} |