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/test.h" 18 #include "common/utils.h" 19 20 #include "oneapi/tbb/task_arena.h" 21 #include "oneapi/tbb/task_scheduler_observer.h" 22 #include "oneapi/tbb/enumerable_thread_specific.h" 23 #include "oneapi/tbb/parallel_for.h" 24 25 //! \file conformance_task_arena.cpp 26 //! \brief Test for [scheduler.task_arena scheduler.task_scheduler_observer] specification 27 28 //! Test task arena interfaces 29 //! \brief \ref requirement \ref interface 30 TEST_CASE("Arena interfaces") { 31 //! Initialization interfaces 32 oneapi::tbb::task_arena a(1,1); a.initialize(); 33 //! Enqueue interface 34 a.enqueue(utils::DummyBody(10)); 35 //! Execute interface 36 a.execute([&] { 37 //! oneapi::tbb::this_task_arena interfaces 38 CHECK(oneapi::tbb::this_task_arena::max_concurrency() == 2); 39 CHECK(oneapi::tbb::this_task_arena::current_thread_index() >= 0); 40 //! Attach interface 41 oneapi::tbb::task_arena attached_arena = oneapi::tbb::task_arena(oneapi::tbb::task_arena::attach()); 42 CHECK(attached_arena.is_active()); 43 }); 44 //! Terminate interface 45 a.terminate(); 46 } 47 48 //! Test tasks isolation for inner oneapi::tbb::parallel_for loop 49 //! \brief \ref requirement \ref interface 50 TEST_CASE("Task isolation") { 51 const int N1 = 1000, N2 = 1000; 52 oneapi::tbb::enumerable_thread_specific<int> ets; 53 oneapi::tbb::parallel_for(0, N1, [&](int i) { 54 // Set a thread specific value 55 ets.local() = i; 56 // Run the second parallel loop in an isolated region to prevent the current thread 57 // from taking tasks related to the outer parallel loop. 58 oneapi::tbb::this_task_arena::isolate([&]{ 59 oneapi::tbb::parallel_for(0, N2, utils::DummyBody(10)); 60 }); 61 REQUIRE(ets.local() == i); 62 }); 63 } 64 65 class conformance_observer: public oneapi::tbb::task_scheduler_observer { 66 public: 67 std::atomic<bool> is_entry_called{false}; 68 std::atomic<bool> is_exit_called{false}; 69 70 conformance_observer( oneapi::tbb::task_arena &a ) : oneapi::tbb::task_scheduler_observer(a) { 71 observe(true); // activate the observer 72 } 73 74 void on_scheduler_entry(bool) override { 75 is_entry_called.store(true, std::memory_order_relaxed); 76 } 77 78 void on_scheduler_exit(bool) override { 79 is_exit_called.store(true, std::memory_order_relaxed); 80 } 81 82 bool is_callbacks_called() { 83 return is_entry_called.load(std::memory_order_relaxed) 84 && is_exit_called.load(std::memory_order_relaxed); 85 } 86 }; 87 88 //! Test task arena observer interfaces 89 //! \brief \ref requirement \ref interface 90 TEST_CASE("Task arena observer") { 91 oneapi::tbb::task_arena a; a.initialize(); 92 conformance_observer observer(a); 93 a.execute([&] { 94 oneapi::tbb::parallel_for(0, 100, utils::DummyBody(10), oneapi::tbb::simple_partitioner()); 95 }); 96 REQUIRE(observer.is_callbacks_called()); 97 } 98 99 //! Test task arena copy constructor 100 //! \brief \ref interface \ref requirement 101 TEST_CASE("Task arena copy constructor") { 102 oneapi::tbb::task_arena arena(1); 103 oneapi::tbb::task_arena copy = arena; 104 105 REQUIRE(arena.max_concurrency() == copy.max_concurrency()); 106 REQUIRE(arena.is_active() == copy.is_active()); 107 } 108