/* Copyright (c) 2005-2021 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #define DOCTEST_CONFIG_SUPER_FAST_ASSERTS #include "common/test.h" #include "common/utils.h" #include "common/utils_report.h" #include "oneapi/tbb/parallel_for.h" #include "oneapi/tbb/tick_count.h" #include "../tbb/test_partitioner.h" #include //! \file conformance_parallel_for.cpp //! \brief Test for [algorithms.parallel_for algorithms.auto_partitioner algorithms.simple_partitioner algorithms.static_partitioner algorithms.affinity_partitioner] specification static const int N = 500; static std::atomic Array[N]; struct parallel_tag {}; struct empty_partitioner_tag {}; // Testing parallel_for with step support const std::size_t PFOR_BUFFER_TEST_SIZE = 1024; // test_buffer has some extra items beyond its right bound const std::size_t PFOR_BUFFER_ACTUAL_SIZE = PFOR_BUFFER_TEST_SIZE + 1024; size_t pfor_buffer[PFOR_BUFFER_ACTUAL_SIZE]; template class TestFunctor{ public: void operator ()(T index) const { pfor_buffer[index]++; } }; static std::atomic FooBodyCount; // A range object whose only public members are those required by the Range concept. template class FooRange { // Start of range int start; // Size of range int size; FooRange( int start_, int size_ ) : start(start_), size(size_) { utils::zero_fill(pad, Pad); pad[Pad-1] = 'x'; } template friend void Flog( ); template friend class FooBody; void operator&(); char pad[Pad]; public: bool empty() const {return size==0;} bool is_divisible() const {return size>1;} FooRange( FooRange& original, oneapi::tbb::split ) : size(original.size/2) { original.size -= size; start = original.start+original.size; CHECK_FAST( original.pad[Pad-1]=='x'); pad[Pad-1] = 'x'; } }; // A range object whose only public members are those required by the parallel_for.h body concept. template class FooBody { public: ~FooBody() { --FooBodyCount; for( std::size_t i=0; i(this)[i] = -1; } // Copy constructor FooBody( const FooBody& other ) : array(other.array), state(other.state) { ++FooBodyCount; CHECK_FAST(state == LIVE); } void operator()( FooRange& r ) const { for (int k = r.start; k < r.start + r.size; ++k) { CHECK_FAST(array[k].load(std::memory_order_relaxed) == 0); array[k].store(1, std::memory_order_relaxed); } } private: const int LIVE = 0x1234; std::atomic* array; int state; friend class FooRange; template friend void Flog( ); FooBody( std::atomic* array_ ) : array(array_), state(LIVE) {} }; template struct Invoker; template struct Invoker { void operator()( const Range& r, const Body& body, empty_partitioner_tag& ) { oneapi::tbb::parallel_for( r, body ); } }; template struct Invoker { void operator()( const Range& r, const Body& body, Partitioner& p ) { oneapi::tbb::parallel_for( r, body, p ); } }; template struct InvokerStep; template struct InvokerStep { void operator()( const T& first, const T& last, const Body& f, empty_partitioner_tag& ) { oneapi::tbb::parallel_for( first, last, f ); } void operator()( const T& first, const T& last, const T& step, const Body& f, empty_partitioner_tag& ) { oneapi::tbb::parallel_for( first, last, step, f ); } }; template struct InvokerStep { void operator()( const T& first, const T& last, const Body& f, Partitioner& p ) { oneapi::tbb::parallel_for( first, last, f, p ); } void operator()( const T& first, const T& last, const T& step, const Body& f, Partitioner& p ) { oneapi::tbb::parallel_for( first, last, step, f, p ); } }; template void Flog() { for ( int i=0; i r( 0, i ); const FooRange rc = r; FooBody f( Array ); const FooBody fc = f; for (int a_i = 0; a_i < N; a_i++) { Array[a_i].store(0, std::memory_order_relaxed); } FooBodyCount = 1; switch (mode) { case 0: { empty_partitioner_tag p; Invoker< Flavor, empty_partitioner_tag, FooRange, FooBody > invoke_for; invoke_for( rc, fc, p ); } break; case 1: { Invoker< Flavor, const oneapi::tbb::simple_partitioner, FooRange, FooBody > invoke_for; invoke_for( rc, fc, oneapi::tbb::simple_partitioner() ); } break; case 2: { Invoker< Flavor, const oneapi::tbb::auto_partitioner, FooRange, FooBody > invoke_for; invoke_for( rc, fc, oneapi::tbb::auto_partitioner() ); } break; case 3: { static oneapi::tbb::affinity_partitioner affinity; Invoker< Flavor, oneapi::tbb::affinity_partitioner, FooRange, FooBody > invoke_for; invoke_for( rc, fc, affinity ); } break; } CHECK(std::find_if_not(Array, Array + i, [](const std::atomic& v) { return v.load(std::memory_order_relaxed) == 1; }) == Array + i); CHECK(std::find_if_not(Array + i, Array + N, [](const std::atomic& v) { return v.load(std::memory_order_relaxed) == 0; }) == Array + N); CHECK(FooBodyCount == 1); } } } #include // std::invalid_argument template void TestParallelForWithStepSupportHelper(Partitioner& p) { const T pfor_buffer_test_size = static_cast(PFOR_BUFFER_TEST_SIZE); const T pfor_buffer_actual_size = static_cast(PFOR_BUFFER_ACTUAL_SIZE); // Testing parallel_for with different step values InvokerStep< Flavor, Partitioner, T, TestFunctor > invoke_for; for (T begin = 0; begin < pfor_buffer_test_size - 1; begin += pfor_buffer_test_size / 10 + 1) { T step; for (step = 1; step < pfor_buffer_test_size; step++) { std::memset(pfor_buffer, 0, pfor_buffer_actual_size * sizeof(std::size_t)); if (step == 1){ invoke_for(begin, pfor_buffer_test_size, TestFunctor(), p); } else { invoke_for(begin, pfor_buffer_test_size, step, TestFunctor(), p); } // Verifying that parallel_for processed all items it should for (T i = begin; i < pfor_buffer_test_size; i = i + step) { if (pfor_buffer[i] != 1) { CHECK_MESSAGE(false, "parallel_for didn't process all required elements"); } pfor_buffer[i] = 0; } // Verifying that no extra items were processed and right bound of array wasn't crossed for (T i = 0; i < pfor_buffer_actual_size; i++) { if (pfor_buffer[i] != 0) { CHECK_MESSAGE(false, "parallel_for processed an extra element"); } } } } } template void TestParallelForWithStepSupport() { static oneapi::tbb::affinity_partitioner affinity_p; oneapi::tbb::auto_partitioner auto_p; oneapi::tbb::simple_partitioner simple_p; oneapi::tbb::static_partitioner static_p; empty_partitioner_tag p; // Try out all partitioner combinations TestParallelForWithStepSupportHelper< Flavor,T,empty_partitioner_tag >(p); TestParallelForWithStepSupportHelper< Flavor,T,const oneapi::tbb::auto_partitioner >(auto_p); TestParallelForWithStepSupportHelper< Flavor,T,const oneapi::tbb::simple_partitioner >(simple_p); TestParallelForWithStepSupportHelper< Flavor,T,oneapi::tbb::affinity_partitioner >(affinity_p); TestParallelForWithStepSupportHelper< Flavor,T,oneapi::tbb::static_partitioner >(static_p); // Testing some corner cases oneapi::tbb::parallel_for(static_cast(2), static_cast(1), static_cast(1), TestFunctor()); } //! Test simple parallel_for with different partitioners //! \brief \ref interface \ref requirement TEST_CASE("Basic parallel_for") { std::atomic counter{}; const std::size_t number_of_partitioners = 5; const std::size_t iterations = 100000; oneapi::tbb::parallel_for(std::size_t(0), iterations, [&](std::size_t) { counter++; }); oneapi::tbb::parallel_for(std::size_t(0), iterations, [&](std::size_t) { counter++; }, oneapi::tbb::simple_partitioner()); oneapi::tbb::parallel_for(std::size_t(0), iterations, [&](std::size_t) { counter++; }, oneapi::tbb::auto_partitioner()); oneapi::tbb::parallel_for(std::size_t(0), iterations, [&](std::size_t) { counter++; }, oneapi::tbb::static_partitioner()); oneapi::tbb::affinity_partitioner aff; oneapi::tbb::parallel_for(std::size_t(0), iterations, [&](std::size_t) { counter++; }, aff); CHECK_EQ(counter.load(std::memory_order_relaxed), iterations * number_of_partitioners); } //! Testing parallel for with different partitioners and ranges ranges //! \brief \ref interface \ref requirement \ref stress TEST_CASE("Flog test") { Flog(); Flog(); Flog(); Flog(); Flog(); } //! Testing parallel for with different types and step //! \brief \ref interface \ref requirement TEST_CASE_TEMPLATE("parallel_for with step support", T, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, std::size_t) { // Testing with different integer types TestParallelForWithStepSupport(); } //! Testing with different types of ranges and partitioners //! \brief \ref interface \ref requirement TEST_CASE("Testing parallel_for with partitioners") { using namespace test_partitioner_utils::interaction_with_range_and_partitioner; test_partitioner_utils::SimpleBody b; oneapi::tbb::affinity_partitioner ap; parallel_for(Range1(true, false), b, ap); parallel_for(Range6(false, true), b, ap); parallel_for(Range1(false, true), b, oneapi::tbb::simple_partitioner()); parallel_for(Range6(false, true), b, oneapi::tbb::simple_partitioner()); parallel_for(Range1(false, true), b, oneapi::tbb::auto_partitioner()); parallel_for(Range6(false, true), b, oneapi::tbb::auto_partitioner()); parallel_for(Range1(true, false), b, oneapi::tbb::static_partitioner()); parallel_for(Range6(false, true), b, oneapi::tbb::static_partitioner()); }