英文:
Do openMP calls pthreads? How threads are created in openMP?
问题
我有以下疑问:
- openMP是否进行pthread调用?
- openMP中线程是如何创建的?
- openMP是pthread的替代品吗?还是openMP和pthread完全不同?
- 如果openMP和pthread不同,那么在C级别哪个提供更好的并行性,即在openBLAS数学库中,openBLAS + openMP更好还是openBLAS + pthreads更好?
英文:
I have the following doubts:
- do openMP make pthread calls?
- how threads are created in openMP?
- is openMP a replacement to pthreads? or openMP and pthreads are entirely different?
- if openMP and pthreads are different, then which gives better parallelism at C level i.e in openBLAS math library, openBLAS + openMP is better or openBLAS + pthreads is better?
答案1
得分: 3
-
OpenMP是一个跨平台的标准。标准可以以实现者希望的任何方式来实现。显然,在没有像Windows这样的POSIX线程库的平台上,OpenMP将不会通过pthread来实现。由于pthread本身是一个跨平台的标准,OpenMP库可以使用它,也可以直接使用特定于平台的低级接口。
-
关于线程的创建具体取决于实现方式。通常情况下,您不需要担心这个。
-
OpenMP接口适用于非常特定的并行化风格,比如经典的循环分支并行化。Pthreads更通用,但需要您手动完成OpenMP提供的许多工作,比如在线程之间分发工作。
-
当OpenMP的编程模型适用于您的用例时,它会为您节省工作,并带来适合这种并行化风格的低级性能调整。例如,OpenMP具有线程池,处理CPU绑定,并且其同步原语已经调整/可调整以适应其并行化风格(使用较长的自旋次数而不是直接睡眠)。
就OpenBLAS或FFTW而言,我认为主要好处在于OpenMP版本可以重用线程池,而不是每个库使用一个线程池。这减少了上下文切换的次数。
英文:
> 1. do openMP make pthread calls?
OpenMP is a cross-platform standard. The standard can be implemented in any way the implementor wants. Obviously on a platform without the POSIX threads library like Windows, OpenMP will not be implemented via pthreads. Since pthreads itself is a cross-platform standard, the OpenMP library may use it or go straight for the platform-specific low-level interface.
However, the OpenMP implementations provided by GCC and Clang do indeed call pthreads, as far as I know. At the very least they are compatible so that you can mix-and-match the libraries, e.g. use pthread's thread-local variables in conjunction with OpenMP's.
> 2. how threads are created in openMP?
Again, specific to the implementation. Normally you don't need to worry about it
> 3. is openMP a replacement to pthreads? or openMP and pthreads are entirely different?
The OpenMP interface caters to very specific styles of parallelization, like classic fork-join parallelization of loops. Pthreads is more general-purpose but requires you to do a lot of the things manually that OpenMP provides, such as distributing work across threads.
> 4. if openMP and pthreads are different, then which gives better parallelism at C level i.e in openBLAS math library, openBLAS + openMP is better or openBLAS + pthreads is better?
When the programming model of OpenMP fits your use-case, it will save you work and brings with it low-level performance tunings that fit this style of parallelization. For example OpenMP has a thread-pool, handles CPU binding, and its synchronization primitives are tuned / tuneable to its style of parallelization (using longer spin-counts instead of sleeping directly).
As far as OpenBLAS or FFTW are concerned, I see the main benefit in that the OpenMP version can reuse the thread pool instead of using one thread pool per library. This reduces the number of context switches.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论