Debian Codegraph @

libstdc++-v3/testsuite/26_numerics/pstl/numeric_ops/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 //===-- 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 // We provide the no execution policy versions of the exclusive_scan and inclusive_scan due checking correctness result of the versions with execution policies.
30 //TODO: to add a macro for availability of ver implementations
31 template <class InputIterator, class OutputIterator, class T>
32 OutputIterator
33 exclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result, T init)
34 {
35 for (; first != last; ++first, ++result)
36 {
37 *result = init;
38 init = init + *first;
39 }
40 return result;
41 }
42
43 template <class InputIterator, class OutputIterator, class T, class BinaryOperation>
44 OutputIterator
45 exclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result, T init, BinaryOperation binary_op)
46 {
47 for (; first != last; ++first, ++result)
48 {
49 *result = init;
50 init = binary_op(init, *first);
51 }
52 return result;
53 }
54
55 // Note: N4582 is missing the ", class T". Issue was reported 2016-Apr-11 to [email protected]
56 template <class InputIterator, class OutputIterator, class BinaryOperation, class T>
57 OutputIterator
58 inclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op, T init)
59 {
60 for (; first != last; ++first, ++result)
61 {
62 init = binary_op(init, *first);
63 *result = init;
64 }
65 return result;
66 }
67
68 template <class InputIterator, class OutputIterator, class BinaryOperation>
69 OutputIterator
70 inclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op)
71 {
72 if (first != last)
73 {
74 auto tmp = *first;
75 *result = tmp;
76 return inclusive_scan_serial(++first, last, ++result, binary_op, tmp);
77 }
78 else
79 {
80 return result;
81 }
82 }
83
84 template <class InputIterator, class OutputIterator>
85 OutputIterator
86 inclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result)
87 {
88 typedef typename std::iterator_traits<InputIterator>::value_type input_type;
89 return inclusive_scan_serial(first, last, result, std::plus<input_type>());
90 }
91
92 // Most of the framework required for testing inclusive and exclusive scan is identical,
93 // so the tests for both are in this file. Which is being tested is controlled by the global
94 // flag inclusive, which is set to each alternative by main().
95 static bool inclusive;
96
97 template <typename Iterator, typename Size, typename T>
98 void
99 check_and_reset(Iterator expected_first, Iterator out_first, Size n, T trash)
100 {
101 EXPECT_EQ_N(expected_first, out_first, n,
102 inclusive ? "wrong result from inclusive_scan" : "wrong result from exclusive_scan");
103 std::fill_n(out_first, n, trash);
104 }
105
106 struct test_scan_with_plus
107 {
108 template <typename Policy, typename Iterator1, typename Iterator2, typename Iterator3, typename Size, typename T>
109 void
110 operator()(Policy&& exec, Iterator1 in_first, Iterator1 in_last, Iterator2 out_first, Iterator2 out_last,
111 Iterator3 expected_first, Iterator3, Size n, T init, T trash)
112 {
113 using namespace std;
114
115 auto orr1 = inclusive ? inclusive_scan_serial(in_first, in_last, expected_first)
116 : exclusive_scan_serial(in_first, in_last, expected_first, init);
117 (void)orr1;
118 auto orr = inclusive ? inclusive_scan(exec, in_first, in_last, out_first)
119 : exclusive_scan(exec, in_first, in_last, out_first, init);
120 EXPECT_TRUE(out_last == orr,
121 inclusive ? "inclusive_scan returned wrong iterator" : "exclusive_scan returned wrong iterator");
122
123 check_and_reset(expected_first, out_first, n, trash);
124 fill(out_first, out_last, trash);
125 }
126 };
127
128 template <typename T, typename Convert>
129 void
130 test_with_plus(T init, T trash, Convert convert)
131 {
132 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
133 {
134 Sequence<T> in(n, convert);
135 Sequence<T> expected(in);
136 Sequence<T> out(n, [&](int32_t) { return trash; });
137
138 invoke_on_all_policies(test_scan_with_plus(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(),
139 expected.end(), in.size(), init, trash);
140 invoke_on_all_policies(test_scan_with_plus(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(),
141 expected.end(), in.size(), init, trash);
142 }
143 }
144 struct test_scan_with_binary_op
145 {
146 template <typename Policy, typename Iterator1, typename Iterator2, typename Iterator3, typename Size, typename T,
147 typename BinaryOp>
148 typename std::enable_if<!TestUtils::isReverse<Iterator1>::value, void>::type
149 operator()(Policy&& exec, Iterator1 in_first, Iterator1 in_last, Iterator2 out_first, Iterator2 out_last,
150 Iterator3 expected_first, Iterator3, Size n, T init, BinaryOp binary_op, T trash)
151 {
152 using namespace std;
153
154 auto orr1 = inclusive ? inclusive_scan_serial(in_first, in_last, expected_first, binary_op, init)
155 : exclusive_scan_serial(in_first, in_last, expected_first, init, binary_op);
156 (void)orr1;
157 auto orr = inclusive ? inclusive_scan(exec, in_first, in_last, out_first, binary_op, init)
158 : exclusive_scan(exec, in_first, in_last, out_first, init, binary_op);
159
160 EXPECT_TRUE(out_last == orr, "scan returned wrong iterator");
161 check_and_reset(expected_first, out_first, n, trash);
162 }
163
164 template <typename Policy, typename Iterator1, typename Iterator2, typename Iterator3, typename Size, typename T,
165 typename BinaryOp>
166 typename std::enable_if<TestUtils::isReverse<Iterator1>::value, void>::type
167 operator()(Policy&&, Iterator1, Iterator1, Iterator2, Iterator2, Iterator3, Iterator3, Size, T, BinaryOp, T)
168 {
169 }
170 };
171
172 template <typename In, typename Out, typename BinaryOp>
173 void
174 test_matrix(Out init, BinaryOp binary_op, Out trash)
175 {
176 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
177 {
178 Sequence<In> in(n, [](size_t k) { return In(k, k + 1); });
179
180 Sequence<Out> out(n, [&](size_t) { return trash; });
181 Sequence<Out> expected(n, [&](size_t) { return trash; });
182
183 invoke_on_all_policies(test_scan_with_binary_op(), in.begin(), in.end(), out.begin(), out.end(),
184 expected.begin(), expected.end(), in.size(), init, binary_op, trash);
185 invoke_on_all_policies(test_scan_with_binary_op(), in.cbegin(), in.cend(), out.begin(), out.end(),
186 expected.begin(), expected.end(), in.size(), init, binary_op, trash);
187 }
188 }
189
190 int
191 main()
192 {
193 for (int32_t mode = 0; mode < 2; ++mode)
194 {
195 inclusive = mode != 0;
196 #if !defined(_PSTL_ICC_19_TEST_SIMD_UDS_WINDOWS_RELEASE_BROKEN)
197 // Test with highly restricted type and associative but not commutative operation
198 test_matrix<Matrix2x2<int32_t>, Matrix2x2<int32_t>>(Matrix2x2<int32_t>(), multiply_matrix<int32_t>,
199 Matrix2x2<int32_t>(-666, 666));
200 #endif
201
202 // Since the implict "+" forms of the scan delegate to the generic forms,
203 // there's little point in using a highly restricted type, so just use double.
204 test_with_plus<float64_t>(inclusive ? 0.0 : -1.0, -666.0,
205 [](uint32_t k) { return float64_t((k % 991 + 1) ^ (k % 997 + 2)); });
206 }
207 std::cout << done() << std::endl;
208 return 0;
209 }