gpt4 book ai didi

c++ - Intel TBB 并行化开销

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:11:38 35 4
gpt4 key购买 nike

为什么英特尔线程构建模块 (TBB) parallel_for 的开销如此之大?根据 Tutorial.pdf 中的 3.2.2 Automatic Chunking 部分,它大约需要半毫秒。这是教程中的一个例子:

CAUTION: Typically a loop needs to take at least a million clock cycles for parallel_for to improve its performance. For example, a loop that takes at least 500 microseconds on a 2 GHz processor might benefit from parallel_for.

根据我目前所读到的内容,TBB 在内部使用线程池(工作线程池)模式,它通过最初只生成一次工作线程(花费数百微秒)来防止这种不良开销。

那么什么是花时间呢?使用互斥体的数据同步不是那么慢吗?此外,TBB 不使用无锁 数据结构进行同步吗?

最佳答案

From what I have read so far TBB uses the threadpool (pool of worker threads) pattern internally and it prevents such bad overheads by only spawning worker threads once initially (which costs hundreds of microseconds).

是的,TBB 预分配线程。它不会在看到 parallel_for 时实际创建和加入工作线程。 OpenMP 和其他并行库都进行预分配。

但是,仍然存在从池中唤醒线程并将逻辑任务分派(dispatch)给线程的开销。是的,TBB 利用无锁数据结构来最小化开销,但它仍然需要一定量的并行开销(即串行部分)。这就是为什么 TBB 手册建议避免非常短的循环。

一般来说,您必须有足够的作业才能获得并行加速。我认为即使是 1 毫秒(=1,000 微秒)也太小了。根据我的经验,为了看到有意义的加速,我需要将执行时间增加大约 100 毫秒。

如果您真的担心 TBB parallel_for 的并行开销,则可能值得尝试简单的静态调度。我不太了解 TBB 的静态调度实现。但是,您可以轻松尝试 OpenMP 的一个:omp parallel for schedule(static)。我相信这种开销将是并行的最小成本。但是,由于它使用的是静态调度,动态调度的好处(尤其是当工作负载不均匀时)将失去。

关于c++ - Intel TBB 并行化开销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6784523/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com