问题描述
我想在一组块上并行执行for循环(使用TBB),其中每个块将使用用户提供的函数进行处理.通常,我将使用tbb::parallel_for()
进行此操作.由于各种原因,我希望能够将处理块的线程数限制为指定的数,将其称为j
.通常,我会使用tbb::task_scheduler_init(j)
.
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)
.
但是,我希望用户可以选择使用TBB,特别是让用户提供的功能可以使用,但仍然需要许多内核.所以我认为tbb::task_scheduler_init()
已经出局了.我能看到的唯一解决方案是让用户调用tbb::task_scheduler_init()
(或全部忽略),并在正常的for循环中自行旋转tbb::tbb_thread
的j
实例.我有什么想念的吗?在TBB中有更自然的方法吗? tbb::task_scheduler_init()
是否有某种层次版本?
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()
?
推荐答案
是的,很少有自然的方法来限制某种算法的并发性,而其余的保持原样.
Yes, there are few natural ways to limit concurrency of a certain algorithm while keep the rest as is.
- 创建单独的线程,并使用
tbb::task_scheduler_init
对其进行初始化以实现有限的并发性.由于主线程是隔离的,因此不会影响主线程和其他线程.因此,您可以从该特殊的受限线程内部启动parallel_for. - 使用
tbb::parallel_pipeline
代替parallel_for并指定令牌数= j,以限制并发处理任务的数量. - 使用
tbb::task_arena
(在TBB 4.3之前是预览功能)执行(1)中所述的操作,但没有其他主线程,因为可以仅使用其API将工作放到隔离的并发上下文(竞技场)中
- 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
示例(3):
tbb::task_arena limited_arena(j);
limited_arena.execute([]{ tbb::parallel_for(...); });
这篇关于具有固定数量的线程的TBB可以完成一项任务,其他任务则默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!