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