openMP是否调用pthread?openMP中如何创建线程?

huangapple go评论72阅读模式
英文:

Do openMP calls pthreads? How threads are created in openMP?

问题

我有以下疑问:

  1. openMP是否进行pthread调用?
  2. openMP中线程是如何创建的?
  3. openMP是pthread的替代品吗?还是openMP和pthread完全不同?
  4. 如果openMP和pthread不同,那么在C级别哪个提供更好的并行性,即在openBLAS数学库中,openBLAS + openMP更好还是openBLAS + pthreads更好?
英文:

I have the following doubts:

  1. do openMP make pthread calls?
  2. how threads are created in openMP?
  3. is openMP a replacement to pthreads? or openMP and pthreads are entirely different?
  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?

答案1

得分: 3

  1. OpenMP是一个跨平台的标准。标准可以以实现者希望的任何方式来实现。显然,在没有像Windows这样的POSIX线程库的平台上,OpenMP将不会通过pthread来实现。由于pthread本身是一个跨平台的标准,OpenMP库可以使用它,也可以直接使用特定于平台的低级接口。

  2. 关于线程的创建具体取决于实现方式。通常情况下,您不需要担心这个。

  3. OpenMP接口适用于非常特定的并行化风格,比如经典的循环分支并行化。Pthreads更通用,但需要您手动完成OpenMP提供的许多工作,比如在线程之间分发工作。

  4. 当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.

huangapple
  • 本文由 发表于 2023年5月28日 19:17:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定