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