问题
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()
?
回答1:
Yes, there are few natural ways to limit concurrency of a certain algorithm while keep the rest as is.
- 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. - 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. - 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(...); });
来源:https://stackoverflow.com/questions/25430790/tbb-with-fixed-number-of-threads-for-one-task-and-default-for-others