TBB with a fixed number of threads for one task and default for others

I want the for-loop to run in parallel (using TBB) over a set of blocks, where each block will be processed using a function provided by the user. I usually do this with tbb::parallel_for() . For various reasons, I want to limit the number of threads processing blocks to a given number, name it j . I usually did this with tbb::task_scheduler_init(j) .

However, I would like the user to be able to use TBB, and in particular to allow the functions provided by the user to use many cores. Therefore, I think tbb::task_scheduler_init() missing. The only solution I see is to allow the user to call tbb::task_scheduler_init() (or ignore it all together) and just include j instances of tbb::tbb_thread their own in a regular for-loop. Did I miss something? Is there a more natural way to do this in TBB? Is there any hierarchical version of tbb::task_scheduler_init() ?

+5
source share
1 answer

Yes, there are several natural ways to limit the concurrency of a particular algorithm, keeping the rest as it is.

  • Create a separate thread and initialize it for limited concurrency using tbb::task_scheduler_init , as you described. Since the main threads are isolated, they will not affect the main and other threads. So you can run parallel_for from within this special restricted thread.
  • Use tbb::parallel_pipeline instead of parallel_for and specify the number of tokens = j to limit the number of simultaneously processed tasks.
  • Use tbb::task_arena (there was a preview function before TBB 4.3) to do the same as described in (1), but without an additional main thread, since the work can be placed in an isolated concurrency context (arena), using only it API

Example (3):

 tbb::task_arena limited_arena(j); limited_arena.execute([]{ tbb::parallel_for(...); }); 
+8
source

Source: https://habr.com/ru/post/1200851/


All Articles