167c11716SAlexey Oralov.. _Task_Scheduler_Init: 267c11716SAlexey Oralov 367c11716SAlexey OralovMigrating from tbb::task_scheduler_init 467c11716SAlexey Oralov======================================= 567c11716SAlexey Oralov 667c11716SAlexey Oralov``tbb::task_scheduler_init`` was a multipurpose functionality in the previous versions of Threading 767c11716SAlexey OralovBuilding Blocks (TBB). This section considers different use cases and how they can be covered with 867c11716SAlexey OralovoneTBB. 967c11716SAlexey Oralov 1067c11716SAlexey OralovManaging the number of threads 1167c11716SAlexey Oralov--------------------------------------- 1267c11716SAlexey Oralov 1367c11716SAlexey OralovQuerying the default number of threads 1467c11716SAlexey Oralov^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1567c11716SAlexey Oralov 1667c11716SAlexey Oralov* `oneapi::tbb::info::default_concurrency() 1767c11716SAlexey Oralov <https://spec.oneapi.com/versions/latest/elements/oneTBB/source/info_namespace.html>`_ 1867c11716SAlexey Oralov returns the maximum concurrency that will be created by *default* in implicit or explicit ``task_arena``. 1967c11716SAlexey Oralov 2067c11716SAlexey Oralov* `oneapi::tbb::this_task_arena::max_concurrency() 2167c11716SAlexey Oralov <https://spec.oneapi.com/versions/latest/elements/oneTBB/source/task_scheduler/task_arena/this_task_arena_ns.html>`_ 2267c11716SAlexey Oralov returns the maximum number of threads available for the parallel algorithms within the current context 2367c11716SAlexey Oralov (or *default* if an implicit arena is not initialized) 2467c11716SAlexey Oralov 2567c11716SAlexey Oralov* `oneapi::tbb::global_control::active_value(tbb::global_control::max_allowed_parallelism) 2667c11716SAlexey Oralov <https://spec.oneapi.com/versions/latest/elements/oneTBB/source/task_scheduler/scheduling_controls/global_control_cls.html>`_ 2767c11716SAlexey Oralov returns the current limit of the thread pool (or *default* if oneTBB scheduler is not initialized) 2867c11716SAlexey Oralov 2967c11716SAlexey OralovSetting the maximum concurrency 3067c11716SAlexey Oralov^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 3167c11716SAlexey Oralov 3267c11716SAlexey Oralov* `task_arena(/* max_concurrency */) 3367c11716SAlexey Oralov <https://spec.oneapi.com/versions/latest/elements/oneTBB/source/task_scheduler/task_arena/this_task_arena_ns.html>`_ 3467c11716SAlexey Oralov limits the maximum concurrency of the parallel algorithm running inside ``task_arena`` 3567c11716SAlexey Oralov 3667c11716SAlexey Oralov* `tbb::global_control(tbb::global_control::max_allowed_parallelism, /* max_concurrency */) 3767c11716SAlexey Oralov <https://spec.oneapi.com/versions/latest/elements/oneTBB/source/task_scheduler/scheduling_controls/global_control_cls.html>`_ 3867c11716SAlexey Oralov limits the total number of oneTBB worker threads 3967c11716SAlexey Oralov 4067c11716SAlexey OralovExamples 4167c11716SAlexey Oralov^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 4267c11716SAlexey Oralov 4367c11716SAlexey OralovThe default parallelism: 4467c11716SAlexey Oralov 4567c11716SAlexey Oralov.. code:: cpp 4667c11716SAlexey Oralov 4767c11716SAlexey Oralov #include <oneapi/tbb/info.h> 4867c11716SAlexey Oralov #include <oneapi/tbb/parallel_for.h> 4967c11716SAlexey Oralov #include <oneapi/tbb/task_arena.h> 5067c11716SAlexey Oralov #include <cassert> 5167c11716SAlexey Oralov 5267c11716SAlexey Oralov int main() { 5367c11716SAlexey Oralov // Get the default number of threads 5467c11716SAlexey Oralov int num_threads = oneapi::tbb::info::default_concurrency(); 5567c11716SAlexey Oralov 5667c11716SAlexey Oralov // Run the default parallelism 5767c11716SAlexey Oralov oneapi::tbb::parallel_for( /* ... */ [] { 5867c11716SAlexey Oralov // Assert the maximum number of threads 5967c11716SAlexey Oralov assert(num_threads == oneapi::tbb::this_task_arena::max_concurrency()); 6067c11716SAlexey Oralov }); 6167c11716SAlexey Oralov 6267c11716SAlexey Oralov // Create the default task_arena 6367c11716SAlexey Oralov oneapi::tbb::task_arena arena; 6467c11716SAlexey Oralov arena.execute([]{ 6567c11716SAlexey Oralov oneapi::tbb::parallel_for( /* ... */ [] { 6667c11716SAlexey Oralov // Assert the maximum number of threads 6767c11716SAlexey Oralov assert(num_threads == oneapi::tbb::this_task_arena::max_concurrency()); 6867c11716SAlexey Oralov }); 6967c11716SAlexey Oralov }); 7067c11716SAlexey Oralov 7167c11716SAlexey Oralov return 0; 7267c11716SAlexey Oralov } 7367c11716SAlexey Oralov 7467c11716SAlexey OralovThe limited parallelism: 7567c11716SAlexey Oralov 7667c11716SAlexey Oralov.. code:: cpp 7767c11716SAlexey Oralov 7867c11716SAlexey Oralov #include <oneapi/tbb/info.h> 7967c11716SAlexey Oralov #include <oneapi/tbb/parallel_for.h> 8067c11716SAlexey Oralov #include <oneapi/tbb/task_arena.h> 8167c11716SAlexey Oralov #include <oneapi/tbb/global_control.h> 8267c11716SAlexey Oralov #include <cassert> 8367c11716SAlexey Oralov 8467c11716SAlexey Oralov int main() { 8567c11716SAlexey Oralov // Create the custom task_arena with four threads 8667c11716SAlexey Oralov oneapi::tbb::task_arena arena(4); 8767c11716SAlexey Oralov arena.execute([]{ 8867c11716SAlexey Oralov oneapi::tbb::parallel_for( /* ... */ [] { 8967c11716SAlexey Oralov // This arena is limited with for threads 9067c11716SAlexey Oralov assert(oneapi::tbb::this_task_arena::max_concurrency() == 4); 9167c11716SAlexey Oralov }); 9267c11716SAlexey Oralov }); 9367c11716SAlexey Oralov 9467c11716SAlexey Oralov // Limit the number of threads to two for all oneTBB parallel interfaces 9567c11716SAlexey Oralov oneapi::tbb::global_control global_limit(oneapi::tbb::global_control::max_allowed_parallelism, 2); 9667c11716SAlexey Oralov 9767c11716SAlexey Oralov // the default parallelism 9867c11716SAlexey Oralov oneapi::tbb::parallel_for( /* ... */ [] { 9967c11716SAlexey Oralov // No more than two threads is expected; however, tbb::this_task_arena::max_concurrency() can return a bigger value 10067c11716SAlexey Oralov int thread_limit = oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::max_allowed_parallelism); 10167c11716SAlexey Oralov assert(thread_limit == 2); 10267c11716SAlexey Oralov }); 10367c11716SAlexey Oralov 10467c11716SAlexey Oralov arena.execute([]{ 10567c11716SAlexey Oralov oneapi::tbb::parallel_for( /* ... */ [] { 10667c11716SAlexey Oralov // No more than two threads is expected; however, tbb::this_task_arena::max_concurrency() is four 10767c11716SAlexey Oralov int thread_limit = oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::max_allowed_parallelism); 10867c11716SAlexey Oralov assert(thread_limit == 2); 10967c11716SAlexey Oralov assert(tbb::this_task_arena::max_concurrency() == 4); 11067c11716SAlexey Oralov }); 11167c11716SAlexey Oralov }); 11267c11716SAlexey Oralov 11367c11716SAlexey Oralov return 0; 11467c11716SAlexey Oralov } 11567c11716SAlexey Oralov 11667c11716SAlexey OralovSetting thread stack size 11767c11716SAlexey Oralov--------------------------------------- 11867c11716SAlexey OralovUse `oneapi::tbb::global_control(oneapi::tbb::global_control::thread_stack_size, /* stack_size */) 11967c11716SAlexey Oralov<https://spec.oneapi.com/versions/latest/elements/oneTBB/source/task_scheduler/scheduling_controls/global_control_cls.html>`_ 12067c11716SAlexey Oralovto set the stack size for oneTBB worker threads: 12167c11716SAlexey Oralov 12267c11716SAlexey Oralov.. code:: cpp 12367c11716SAlexey Oralov 12467c11716SAlexey Oralov #include <oneapi/tbb/parallel_for.h> 12567c11716SAlexey Oralov #include <oneapi/tbb/global_control.h> 12667c11716SAlexey Oralov 12767c11716SAlexey Oralov int main() { 12867c11716SAlexey Oralov // Set 16 MB of the stack size for oneTBB worker threads. 12967c11716SAlexey Oralov // Note that the stack size of the main thread should be configured in accordace with the 13067c11716SAlexey Oralov // system documentation, e.g. at application startup moment 13167c11716SAlexey Oralov oneapi::tbb::global_control global_limit(tbb::global_control::thread_stack_size, 16 * 1024 * 1024); 13267c11716SAlexey Oralov 13367c11716SAlexey Oralov oneapi::tbb::parallel_for( /* ... */ [] { 13467c11716SAlexey Oralov // Create a big array in the stack 13567c11716SAlexey Oralov char big_array[10*1024*1024]; 13667c11716SAlexey Oralov }); 13767c11716SAlexey Oralov 13867c11716SAlexey Oralov return 0; 13967c11716SAlexey Oralov } 14067c11716SAlexey Oralov 14167c11716SAlexey OralovTerminating oneTBB scheduler 14267c11716SAlexey Oralov--------------------------------------- 143*95f95117SAlexandra 144*95f95117SAlexandra`task_scheduler_handle <https://oneapi-src.github.io/oneAPI-spec/spec/elements/oneTBB/source/task_scheduler/scheduling_controls/task_scheduler_handle_cls.html>`_ 14567c11716SAlexey Oralovallows waiting for oneTBB worker threads completion: 14667c11716SAlexey Oralov 14767c11716SAlexey Oralov.. code:: cpp 14867c11716SAlexey Oralov 14967c11716SAlexey Oralov #include <oneapi/tbb/global_control.h> 15067c11716SAlexey Oralov #include <oneapi/tbb/parallel_for.h> 15167c11716SAlexey Oralov 15267c11716SAlexey Oralov int main() { 1535fc0a5f6SAlex oneapi::tbb::task_scheduler_handle handle{tbb::attach{}}; 15467c11716SAlexey Oralov // Do some parallel work here 15567c11716SAlexey Oralov oneapi::tbb::parallel_for(/* ... */); 15667c11716SAlexey Oralov oneapi::tbb::finalize(handle); 15767c11716SAlexey Oralov return 0; 15867c11716SAlexey Oralov } 159