Debian Codegraph @

libstdc++-v3/testsuite/26_numerics/pstl/numeric_ops/transform_scan.cc

raw
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_scan.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 // Most of the framework required for testing inclusive and exclusive transform-scans is identical,
30 // so the tests for both are in this file. Which is being tested is controlled by the global
31 // flag inclusive, which is set to each alternative by main().
32 static bool inclusive;
33
34 template <typename Iterator, typename Size, typename T>
35 void
36 check_and_reset(Iterator expected_first, Iterator out_first, Size n, T trash)
37 {
38 EXPECT_EQ_N(expected_first, out_first, n,
39 inclusive ? "wrong result from transform_inclusive_scan"
40 : "wrong result from transform_exclusive_scan");
41 std::fill_n(out_first, n, trash);
42 }
43
44 struct test_transform_scan
45 {
46 template <typename Policy, typename InputIterator, typename OutputIterator, typename Size, typename UnaryOp,
47 typename T, typename BinaryOp>
48 typename std::enable_if<!TestUtils::isReverse<InputIterator>::value, void>::type
49 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first,
50 OutputIterator out_last, OutputIterator expected_first, OutputIterator, Size n, UnaryOp unary_op, T init,
51 BinaryOp binary_op, T trash)
52 {
53 using namespace std;
54
55 auto orr1 =
56 inclusive
57 ? transform_inclusive_scan(std::execution::seq, first, last, expected_first, binary_op, unary_op, init)
58 : transform_exclusive_scan(std::execution::seq, first, last, expected_first, init, binary_op, unary_op);
59 auto orr2 = inclusive ? transform_inclusive_scan(exec, first, last, out_first, binary_op, unary_op, init)
60 : transform_exclusive_scan(exec, first, last, out_first, init, binary_op, unary_op);
61 EXPECT_TRUE(out_last == orr2, "transform...scan returned wrong iterator");
62 check_and_reset(expected_first, out_first, n, trash);
63
64 // Checks inclusive scan if init is not provided
65 if (inclusive && n > 0)
66 {
67 orr1 = transform_inclusive_scan(std::execution::seq, first, last, expected_first, binary_op, unary_op);
68 orr2 = transform_inclusive_scan(exec, first, last, out_first, binary_op, unary_op);
69 EXPECT_TRUE(out_last == orr2, "transform...scan returned wrong iterator");
70 check_and_reset(expected_first, out_first, n, trash);
71 }
72 }
73
74 template <typename Policy, typename InputIterator, typename OutputIterator, typename Size, typename UnaryOp,
75 typename T, typename BinaryOp>
76 typename std::enable_if<TestUtils::isReverse<InputIterator>::value, void>::type
77 operator()(Policy&&, InputIterator, InputIterator, OutputIterator, OutputIterator, OutputIterator, OutputIterator,
78 Size, UnaryOp, T, BinaryOp, T)
79 {
80 }
81 };
82
83 const uint32_t encryption_mask = 0x314;
84
85 template <typename InputIterator, typename OutputIterator, typename UnaryOperation, typename T,
86 typename BinaryOperation>
87 std::pair<OutputIterator, T>
88 transform_inclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation unary_op,
89 T init, BinaryOperation binary_op) noexcept
90 {
91 for (; first != last; ++first, ++result)
92 {
93 init = binary_op(init, unary_op(*first));
94 *result = init;
95 }
96 return std::make_pair(result, init);
97 }
98
99 template <typename InputIterator, typename OutputIterator, typename UnaryOperation, typename T,
100 typename BinaryOperation>
101 std::pair<OutputIterator, T>
102 transform_exclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation unary_op,
103 T init, BinaryOperation binary_op) noexcept
104 {
105 for (; first != last; ++first, ++result)
106 {
107 *result = init;
108 init = binary_op(init, unary_op(*first));
109 }
110 return std::make_pair(result, init);
111 }
112
113 template <typename In, typename Out, typename UnaryOp, typename BinaryOp>
114 void
115 test(UnaryOp unary_op, Out init, BinaryOp binary_op, Out trash)
116 {
117 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
118 {
119 Sequence<In> in(n, [](size_t k) { return In(k ^ encryption_mask); });
120
121 Out tmp = init;
122 Sequence<Out> expected(n, [&](size_t k) -> Out {
123 if (inclusive)
124 {
125 tmp = binary_op(tmp, unary_op(in[k]));
126 return tmp;
127 }
128 else
129 {
130 Out val = tmp;
131 tmp = binary_op(tmp, unary_op(in[k]));
132 return val;
133 }
134 });
135
136 Sequence<Out> out(n, [&](size_t) { return trash; });
137
138 auto result =
139 inclusive
140 ? transform_inclusive_scan_serial(in.cbegin(), in.cend(), out.fbegin(), unary_op, init, binary_op)
141 : transform_exclusive_scan_serial(in.cbegin(), in.cend(), out.fbegin(), unary_op, init, binary_op);
142 (void)result;
143 check_and_reset(expected.begin(), out.begin(), out.size(), trash);
144
145 invoke_on_all_policies(test_transform_scan(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(),
146 expected.end(), in.size(), unary_op, init, binary_op, trash);
147 invoke_on_all_policies(test_transform_scan(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(),
148 expected.end(), in.size(), unary_op, init, binary_op, trash);
149 }
150 }
151
152 template <typename In, typename Out, typename UnaryOp, typename BinaryOp>
153 void
154 test_matrix(UnaryOp unary_op, Out init, BinaryOp binary_op, Out trash)
155 {
156 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
157 {
158 Sequence<In> in(n, [](size_t k) { return In(k, k + 1); });
159
160 Sequence<Out> out(n, [&](size_t) { return trash; });
161 Sequence<Out> expected(n, [&](size_t) { return trash; });
162
163 invoke_on_all_policies(test_transform_scan(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(),
164 expected.end(), in.size(), unary_op, init, binary_op, trash);
165 invoke_on_all_policies(test_transform_scan(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(),
166 expected.end(), in.size(), unary_op, init, binary_op, trash);
167 }
168 }
169
170 int
171 main()
172 {
173 for (int32_t mode = 0; mode < 2; ++mode)
174 {
175 inclusive = mode != 0;
176 #if !defined(_PSTL_ICC_19_TEST_SIMD_UDS_WINDOWS_RELEASE_BROKEN)
177 test_matrix<Matrix2x2<int32_t>, Matrix2x2<int32_t>>([](const Matrix2x2<int32_t> x) { return x; },
178 Matrix2x2<int32_t>(), multiply_matrix<int32_t>,
179 Matrix2x2<int32_t>(-666, 666));
180 #endif
181 test<int32_t, uint32_t>([](int32_t x) { return x++; }, -123, [](int32_t x, int32_t y) { return x + y; }, 666);
182 }
183 std::cout << done() << std::endl;
184 return 0;
185 }