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

1

6

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()?

Hodgson answered 21/8, 2014 at 15:57 Comment(2)
I'd change the title like: TBB with fixed number of threads for one task, and default for othersCobweb
Sounds reasonable. Done. Although, I guess in my case the "others" are subtasks of the first task (parallel for-loop).Hodgson
C
9

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(...); });
Cobweb answered 21/8, 2014 at 20:26 Comment(1)
Thanks, @Anton. This is very helpful. I need to read up on the last two options.Hodgson

© 2022 - 2024 — McMap. All rights reserved.