多核编程。 Boost 的 MPI、OpenMP、TBB 还是其他?
我在多核编程方面完全是个新手,但我确实知道如何编写C++。
现在,我正在寻找多核编程库。我只是想尝试一下,只是为了好玩,现在,我找到了 3 个 API,但我不确定应该坚持使用哪一个。现在,我看到Boost的MPI、OpenMP和TBB。
对于任何使用过这 3 个 API(或任何其他 API)的人,您能告诉我它们之间的区别吗?是否有任何需要考虑的因素,例如 AMD 或 Intel 架构?
I am totally a novice in Multi-Core Programming, but I do know how to program C++.
Now, I am looking around for Multi-Core Programming library. I just want to give it a try, just for fun, and right now, I found 3 APIs, but I am not sure which one should I stick with. Right now, I see Boost's MPI, OpenMP and TBB.
For anyone who have experienced with any of these 3 API (or any other API), could you please tell me the difference between these? Are there any factor to consider, like AMD or Intel architecture?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
作为起点,我建议使用 OpenMP。有了这个,您可以非常简单地执行三种基本类型的并行性:循环、部分和任务。
并行循环
允许您将循环迭代拆分到多个线程上。例如:
如果您使用两个线程,那么第一个线程将执行迭代的前半部分。第二个线程将执行后半部分。
这些部分
允许您静态地将工作划分到多个线程上。当有明显的工作可以并行执行时,这非常有用。然而,这不是一个非常灵活的方法。
任务
任务是最灵活的方法。它们是动态创建的,并且它们的执行是异步执行的,或者由创建它们的线程,或者由另一个线程执行。
优点
OpenMP 有几个优点。
基于指令:编译器执行创建和同步线程的工作。
增量并行性:您可以仅关注需要并行化的代码区域。
串行和并行代码的一个源代码库:仅当您使用标志 (
-fopenmp
for gcc )。因此,您可以使用相同的源库来生成串行和并行代码。这意味着您可以关闭该标志以查看是否从代码的串行版本中获得相同的结果。这样您就可以将并行性错误与算法中的错误隔离开来。您可以在 http://www.openmp.org/ 找到完整的 OpenMP 规范
As a starting point I'd suggest OpenMP. With this you can very simply do three basic types of parallelism: loops, sections, and tasks.
Parallel loops
These allow you to split loop iterations over multiple threads. For instance:
If you were using two threads, then the first thread would perform the first half of the iteration. The second thread would perform the second half.
Sections
These allow you to statically partition the work over multiple threads. This is useful when there is obvious work that can be performed in parallel. However, it's not a very flexible approach.
Tasks
Tasks are the most flexible approach. These are created dynamically and their execution is performed asynchronously, either by the thread that created them, or by another thread.
Advantages
OpenMP has several things going for it.
Directive-based: the compiler does the work of creating and synchronizing the threads.
Incremental parallelism: you can focus on just the region of code that you need to parallelise.
One source base for serial and parallel code: The OpenMP directives are only recognized by the compiler when you run it with a flag (
-fopenmp
for gcc). So you can use the same source base to generate both serial and parallel code. This means you can turn off the flag to see if you get the same result from the serial version of the code or not. That way you can isolate parallelism errors from errors in the algorithm.You can find the entire OpenMP spec at http://www.openmp.org/
在底层,OpenMP 是多线程编程,但抽象级别高于 TBB 及其同类。对于多核计算机上的并行编程,两者之间的选择与同一领域内任何高级和低级软件之间的选择大致相同:在表达性和可控性之间存在权衡。
我认为Intel vs AMD 是无关紧要的。
你的选择应该取决于你想要实现的目标;例如,如果你想学习TBB,那么TBB绝对是你的最佳选择。但如果您想通过简单的步骤并行化现有的 C++ 程序,那么 OpenMP 可能是更好的首选; TBB 稍后仍然存在,供您解决。我一开始可能会避开 MPI,除非我确信我会从共享内存编程(这主要是在多核上执行的操作)转移到分布式内存编程(在集群或网络上)。一如既往,您选择的技术应该取决于您的要求。
Under the hood OpenMP is multi-threaded programming but at a higher level of abstraction than TBB and its ilk. The choice between the two, for parallel programming on a multi-core computer, is approximately the same as the choice between any higher and lower level software within the same domain: there is a trade off between expressivity and controllability.
Intel vs AMD is irrelevant I think.
And your choice ought to depend on what you are trying to achieve; for example, if you want to learn TBB then TBB is definitely the way to go. But if you want to parallelise an existing C++ program in easy steps, then OpenMP is probably a better first choice; TBB will still be around later for you to tackle. I'd probably steer clear of MPI at first unless I was certain that I would be transferring from shared-memory programming (which is mostly what you do on a multi-core) to distributed-memory programming (on clusters or networks). As ever , the technology you choose ought to depend on your requirements.
我建议你尝试一下 MapReduce。您可以在同一台计算机上安装多个虚拟机实例,每个虚拟机实例运行一个 Hadoop 实例(Hadoop 是一个雅虎!MapReduce 的开源实现)。网上有很多关于设置 Hadoop 的教程。
顺便说一句,MPI 和 OpenMP 不是一回事。 OpenMP是用于共享内存编程,这通常意味着多核编程,而不是在几台机器上并行编程。
I'd suggest you to play with MapReduce for sometime. You can install several virtual machines instances on the same machine, each of which runs a Hadoop instance (Hadoop is a Yahoo! open source implementation of MapReduce). There are a lot of tutorials online for setting up Hadoop.
btw, MPI and OpenMP are not the same thing. OpenMP is for shared memory programming, which generally means, multi-core programming, not parallel programming on several machines.
取决于你的重点。如果您主要对多线程编程感兴趣,请选择 TBB。如果您对进程级并发性更感兴趣,那么 MPI 是您的最佳选择。
Depends on your focus. If you are mainly interested in multi threaded programming go with TBB. If you are more interested in process level concurrency then MPI is the way to go.
另一个有趣的库是 OpenCL。它基本上允许您以最佳方式使用所有硬件(CPU、GPU、DSP...)。
它有一些有趣的功能,例如可以创建数百个线程而不影响性能。
Another interesting library is OpenCL. It basically allows you to use all your hardware (CPU, GPU, DSP, ...) in the best way.
It has some interesting features, like the possibility to create hundreds of threads without performance penalties.