1 /* 2 Copyright (c) 2005-2020 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 #include "common/parallel_reduce_common.h" 18 #include "common/concurrency_tracker.h" 19 20 #include "../tbb/test_partitioner.h" 21 22 //! \file conformance_parallel_reduce.cpp 23 //! \brief Test for [algorithms.parallel_reduce algorithms.parallel_deterministic_reduce] specification 24 25 class RotOp { 26 public: 27 using Type = int; 28 int operator() ( int x, int i ) const { 29 return ( x<<1 ) ^ i; 30 } 31 int join( int x, int y ) const { 32 return operator()( x, y ); 33 } 34 }; 35 36 template <class Op> 37 struct ReduceBody { 38 using result_type = typename Op::Type; 39 result_type my_value; 40 41 ReduceBody() : my_value() {} 42 ReduceBody( ReduceBody &, oneapi::tbb::split ) : my_value() {} 43 44 void operator() ( const oneapi::tbb::blocked_range<int>& r ) { 45 utils::ConcurrencyTracker ct; 46 for ( int i = r.begin(); i != r.end(); ++i ) { 47 Op op; 48 my_value = op(my_value, i); 49 } 50 } 51 52 void join( const ReduceBody& y ) { 53 Op op; 54 my_value = op.join(my_value, y.my_value); 55 } 56 }; 57 58 template <class Partitioner> 59 void TestDeterministicReductionFor() { 60 const int N = 1000; 61 const oneapi::tbb::blocked_range<int> range(0, N); 62 using BodyType = ReduceBody<RotOp>; 63 using Type = RotOp::Type; 64 65 BodyType benchmark_body; 66 deterministic_reduce_invoker(range, benchmark_body, Partitioner()); 67 for ( int i=0; i<100; ++i ) { 68 BodyType measurement_body; 69 deterministic_reduce_invoker(range, measurement_body, Partitioner()); 70 REQUIRE_MESSAGE( benchmark_body.my_value == measurement_body.my_value, 71 "parallel_deterministic_reduce behaves differently from run to run" ); 72 73 Type lambda_measurement_result = deterministic_reduce_invoker<Type>( range, 74 [](const oneapi::tbb::blocked_range<int>& br, Type value) -> Type { 75 utils::ConcurrencyTracker ct; 76 for ( int ii = br.begin(); ii != br.end(); ++ii ) { 77 RotOp op; 78 value = op(value, ii); 79 } 80 return value; 81 }, 82 [](const Type& v1, const Type& v2) -> Type { 83 RotOp op; 84 return op.join(v1,v2); 85 }, 86 Partitioner() 87 ); 88 REQUIRE_MESSAGE( benchmark_body.my_value == lambda_measurement_result, 89 "lambda-based parallel_deterministic_reduce behaves differently from run to run" ); 90 } 91 } 92 93 //! Test that deterministic reduction returns the same result during several measurements 94 //! \brief \ref requirement \ref interface 95 TEST_CASE("Test deterministic reduce correctness") { 96 for ( auto concurrency_level : utils::concurrency_range() ) { 97 oneapi::tbb::global_control control(oneapi::tbb::global_control::max_allowed_parallelism, concurrency_level); 98 TestDeterministicReductionFor<oneapi::tbb::simple_partitioner>(); 99 TestDeterministicReductionFor<oneapi::tbb::static_partitioner>(); 100 TestDeterministicReductionFor<utils_default_partitioner>(); 101 } 102 } 103 104 //! Test partitioners interaction with various ranges 105 //! \brief \ref requirement \ref interface 106 TEST_CASE("Test partitioners interaction with various ranges") { 107 using namespace test_partitioner_utils::interaction_with_range_and_partitioner; 108 for ( auto concurrency_level : utils::concurrency_range() ) { 109 oneapi::tbb::global_control control(oneapi::tbb::global_control::max_allowed_parallelism, concurrency_level); 110 111 test_partitioner_utils::SimpleReduceBody body; 112 oneapi::tbb::affinity_partitioner ap; 113 114 parallel_reduce(Range1(/*assert_in_split*/ true, /*assert_in_proportional_split*/ false), body, ap); 115 parallel_reduce(Range6(false, true), body, ap); 116 117 parallel_reduce(Range1(/*assert_in_split*/ true, /*assert_in_proportional_split*/ false), body, oneapi::tbb::static_partitioner()); 118 parallel_reduce(Range6(false, true), body, oneapi::tbb::static_partitioner()); 119 120 parallel_reduce(Range1(/*assert_in_split*/ false, /*assert_in_proportional_split*/ true), body, oneapi::tbb::simple_partitioner()); 121 parallel_reduce(Range6(false, true), body, oneapi::tbb::simple_partitioner()); 122 123 parallel_reduce(Range1(/*assert_in_split*/ false, /*assert_in_proportional_split*/ true), body, oneapi::tbb::auto_partitioner()); 124 parallel_reduce(Range6(false, true), body, oneapi::tbb::auto_partitioner()); 125 126 parallel_deterministic_reduce(Range1(/*assert_in_split*/true, /*assert_in_proportional_split*/ false), body, oneapi::tbb::static_partitioner()); 127 parallel_deterministic_reduce(Range6(false, true), body, oneapi::tbb::static_partitioner()); 128 129 parallel_deterministic_reduce(Range1(/*assert_in_split*/false, /*assert_in_proportional_split*/ true), body, oneapi::tbb::simple_partitioner()); 130 parallel_deterministic_reduce(Range6(false, true), body, oneapi::tbb::simple_partitioner()); 131 } 132 } 133