C++ 并行化库:OpenMP 与线程构建块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/615264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Parallelization Libraries: OpenMP vs. Thread Building Blocks
提问by Adrian Grigore
I'm going to retrofit my custom graphics engine so that it takes advantage of multicore CPUs. More exactly, I am looking for a library to parallelize loops.
我将改造我的自定义图形引擎,以便它利用多核 CPU。更确切地说,我正在寻找一个库来并行化循环。
It seems to me that both OpenMP and Intel's Thread Building Blocks are very well suited for the job. Also, both are supported by Visual Studio's C++ compiler and most other popular compilers. And both libraries seem quite straight-forward to use.
在我看来,OpenMP 和英特尔的线程构建块都非常适合这项工作。此外,Visual Studio 的 C++ 编译器和大多数其他流行的编译器都支持两者。而且这两个库似乎都非常易于使用。
So, which one should I choose? Has anyone tried both libraries and can give me some cons and pros of using either library? Also, what did you choose to work with in the end?
那么,我应该选择哪一个呢?有没有人尝试过这两个库,并且可以给我一些使用这两个库的优缺点?另外,你最后选择了什么工作?
Thanks,
谢谢,
Adrian
阿德里安
回答by Peter
I haven't used TBB extensively, but my impression is that they complement each other more than competing. TBB provides threadsafe containers and some parallel algorithms, whereas OpenMP is more of a way to parallelise existing code.
我没有广泛使用 TBB,但我的印象是它们相互补充而不是竞争。TBB 提供线程安全容器和一些并行算法,而 OpenMP 更像是一种并行化现有代码的方法。
Personally I've found OpenMP very easy to drop into existing code where you have a parallelisable loop or bunch of sections that can be run in parallel. However it doesn't help you particularly for a case where you need to modify some shared data - where TBB's concurrent containers might be exactly what you want.
就我个人而言,我发现 OpenMP 很容易放入现有代码中,其中您有一个可并行化的循环或一堆可以并行运行的部分。但是,对于需要修改一些共享数据的情况,它对您没有帮助——在这种情况下,TBB 的并发容器可能正是您想要的。
If all you want is to parallelise loops where the iterations are independent (or can be fairly easily made so), I'd go for OpenMP. If you're going to need more interaction between the threads, I think TBB may offer a little more in that regard.
如果您只想并行化迭代独立的循环(或者可以很容易地这样做),我会选择 OpenMP。如果您需要在线程之间进行更多交互,我认为 TBB 可能会在这方面提供更多信息。
回答by Anonymous
From Intel's software blog: Compare Windows* threads, OpenMP*, Intel? Threading Building Blocks for parallel programming
来自英特尔的软件博客:比较 Windows* 线程、OpenMP*、英特尔?用于并行编程的线程构建块
It is also the matter of style - for me TBB is very C++ like, while I don't like OpenMP pragmas that much (reeks of C a bit, would use it if I had to write in C).
这也是风格的问题——对我来说,TBB 非常像 C++,而我不太喜欢 OpenMP 编译指示(有点 C 的味道,如果我必须用 C 编写,我会使用它)。
I would also consider the existing knowledge and experience of the team. Learning a new library (especially when it comes to threading/concurrency) does take some time. I think that for now, OpenMP is more widely known and deployed than TBB (but this is just mine opinion).
我也会考虑团队现有的知识和经验。学习一个新的库(尤其是在线程/并发方面)确实需要一些时间。我认为就目前而言,OpenMP 比 TBB 更广为人知和部署(但这只是我的意见)。
Yet another factor - but considering most common platforms, probably not an issue - portability. But the license might be an issue.
另一个因素 - 但考虑到最常见的平台,可能不是问题 - 可移植性。但是许可证可能是一个问题。
- TBB incorporates some of nice research originating from academic research, for example recursive data parallel approach.
- There is some work on cache-friendliness, for example.
- Lecture of the Intel blog seems really interesting.
回答by Nikhil
In general I have found that using TBB requires much more time consuming changes to the code base with a high payoff while OpenMP gives a quick but moderate payoff. If you are staring a new module from scratch and thinking long term go with TBB. If you want small but immediate gains go with OpenMP.
总的来说,我发现使用 TBB 需要花费更多时间对代码库进行高回报的更改,而 OpenMP 可提供快速但适中的回报。如果您从头开始着手一个新模块并从长远考虑,请选择 TBB。如果您想要小而立竿见影的收益,请使用 OpenMP。
Also, TBB and OpenMP are not mutually exclusive.
此外,TBB 和 OpenMP 并不相互排斥。
回答by Ben
I've actually used both, and my general impression is that if your algorithm is fairly easy to make parallel (e.g. loops of even size, not too much data interdependence) OpenMP is easier, and quite nice to work with. In fact, if you find you can use OpenMP, it's probably the better way to go, if you know your platform will support it. I haven't used OpenMP's new Task structures, which are much more general than the original loop and section options.
我实际上使用了这两种方法,我的总体印象是,如果您的算法很容易并行(例如,大小均匀的循环,没有太多数据相互依赖),OpenMP 会更容易,并且非常好用。事实上,如果您发现可以使用 OpenMP,如果您知道您的平台会支持它,那么这可能是更好的选择。我没有使用 OpenMP 的新任务结构,它比原始循环和节选项更通用。
TBB gives you more data structures up front, but definitely requires more up front. As a plus, it mightbe better at making you aware of race condition bugs. What I mean by this is that it is fairly easy in OpenMP to enable race conditions by not making something shared (or whatever) that should be. You only see this when you get bad results. I think this is a bit less likely to occur with TBB.
TBB 预先为您提供了更多的数据结构,但肯定需要更多的预先。作为一个加号,它可能更好地让您意识到竞争条件错误。我的意思是,在 OpenMP 中通过不共享(或其他)应该共享的东西来启用竞争条件是相当容易的。只有在得到不好的结果时才会看到这一点。我认为 TBB 不太可能发生这种情况。
Overall my personal preference was for OpenMP, especially given its increased expressiveness with tasks.
总的来说,我个人更喜欢 OpenMP,尤其是考虑到它对任务的表达能力更强。
回答by Richard
Yes, TBB is much more C++ friendly while OpenMP is more appropriate for FORTRAN-style C code given its design. The new task feature in OpenMP looks very interesting, while at the same time the Lambda and function object in C++0x may make TBB easier to use.
是的,TBB 对 C++ 更友好,而考虑到 OpenMP 的设计,它更适合 FORTRAN 风格的 C 代码。OpenMP 中的新任务功能看起来很有趣,同时 C++0x 中的 Lambda 和函数对象可能会使 TBB 更易于使用。
回答by Contango
In Visual Studio 2008, you can add the following line to parallelize any "for" loop. It even works with multiple nested for loops. Here is an example:
在 Visual Studio 2008 中,您可以添加以下行来并行化任何“for”循环。它甚至适用于多个嵌套的 for 循环。下面是一个例子:
#pragma omp parallel for private(i,j)
for (i=0; i<num_particles; i++)
{
p[i].fitness = fitnessFunction(p[i].present);
if (p[i].fitness > p[i].pbestFitness)
{
p[i].pbestFitness = p[i].fitness;
for (j=0; j<p[i].numVars; j++) p[i].pbest[j] = p[i].present[j];
}
}
gbest = pso_get_best(num_particles, p);
After we added the #pragma omp parallel, both cores on my Core 2 Duo were used to their maximum capacity, so total CPU usage went from 50% to 100%.
添加#pragma omp parallel 后,我的Core 2 Duo 上的两个内核都被使用到了它们的最大容量,因此总CPU 使用率从50% 上升到100%。
回答by Peter Meyer
As far as i know, TBB (there is an OpenSource Version under GPLv2 avaiable) adresses more the C++ then C Area. These times it's hard to find C++ and general OOP parallelization specific Informations.The most adresses functional stuff like c (the same on CUDA or OpenCL). If you need C++ Support for parallelization go for TBB!
据我所知,TBB(在 GPLv2 下有一个可用的开源版本)比 C 区域更适用于 C++。这些时候很难找到 C++ 和一般的 OOP 并行化特定信息。大多数地址像 c 这样的功能性东西(在 CUDA 或 OpenCL 上相同)。如果您需要 C++ 支持并行化,请选择 TBB!
回答by Peter Meyer
Viva64 links: Parallel Programming.
Viva64 链接:并行编程。