Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to execute a for-loop in parallel (using TBB) over a set of blocks, where each block will be processed using a user-supplied function. Normally, I would do this using tbb::parallel_for(). For various reasons, I want to be able to limit the number of threads processing the blocks to a prescribed number, call it j. Normally, I would do this using tbb::task_scheduler_init(j).

However, I would like the user to have the option to use TBB and, specifically, let the user-supplied function use however many cores remain. So I think tbb::task_scheduler_init() is out. The only solution I can see is to let the user call tbb::task_scheduler_init() (or ignore it all together), and just spin j instances of tbb::tbb_thread on my own in a normal for-loop. Am I missing anything? Is there a more natural way to do this in TBB? Is there some kind of a hierarchical version of tbb::task_scheduler_init()?

like image 792
foxcub Avatar asked Aug 21 '14 15:08

foxcub


1 Answers

Yes, there are few natural ways to limit concurrency of a certain algorithm while keep the rest as is.

  1. Create separate thread and initialize it for the limited concurrency using tbb::task_scheduler_init as you described. Since the master threads are isolated, it will not affect main and other threads. So, you can start the parallel_for from inside of that special limited thread.
  2. Use tbb::parallel_pipeline instead of parallel_for and specify the number of tokens = j in order to limit the number of concurrently processing tasks.
  3. Use tbb::task_arena (was a preview feature till TBB 4.3) to do the same as described in (1) but without additional master thread since the work can be put into isolated concurrency context (arena) using just its API

Example of (3):

tbb::task_arena limited_arena(j);
limited_arena.execute([]{ tbb::parallel_for(...); });
like image 171
Anton Avatar answered Sep 24 '22 17:09

Anton