现在的位置: 首页 > 综合 > 正文

转自Stackoverflow:Parallelization: pthreads or OpenMP?

2013年12月07日 ⁄ 综合 ⁄ 共 2557字 ⁄ 字号 评论关闭

Q:

Most people in scientific computing use OpenMP as a quasi-standard when it comes to shared memory parallelization.

Is there any reason (other than readability) to use OpenMP over pthreads? The latter seems more basic and I suspect it could be faster and easier to optimize.

 

A1:

It basically boils down to what level of control you want over your parallelization. OpenMP is great if all you want to do is add a few #pragma statements and have a parallel version of your code quite quickly. If you want to do really interesting things with MIMD coding or complex queueing, you can still do all this with OpenMP, but it is probably a lot more straightforward to use threading in that case. OpenMP also has similar advantages in portability in that a lot of compilers for different platforms support it now, as with pthreads.

So you're absolutely correct - if you need fine-tuned control over your parallelization, use pthreads. If you want to parallelize with as little work as possible, use OpenMP.

Whichever way you decide to go, good luck!

 

A2:

One other reason: the OpenMP is task-based, Pthreads is thread based. It means that OpenMP will allocate the same number of threads as number of cores. So you will get scalable solution. It is not so easy task to do it using raw threads.

The second opinion: OpenMP provides reduction features: when you need to compute partial results in threads and combine them. You can implement it just using single line of code. But using raw threads you should do more job.

Just think about your requirements and try to understand: is OpenMP enough for you? You will save lots of time.

 

A3:

OpenMP requires a compiler that supports it, and works with pragmas. The advantage to this is that when compiling without OpenMP-support (e.g. PCC or Clang/LLVM as of now), the code will still compile. Also, have a look at what Charles Leiserson wrote about DIY multithreading (1).

Pthreads is a POSIX standard (IEEE POSIX 1003.1c (2)) for libraries, while OpenMP specifications (3) are to be implemented on compilers; that being said, there are a variety of pthread implementations (e.g. OpenBSD rthreads, NPTL), and a number of compilers that support OpenMP (e.g. GCC with the -fopenmp flag, MSVC++ 2008).

Pthreads are only effective for parallelization when multiple processors are available, and only when the code is optimized for the number of processors available. Code for OpenMP is more-easily scalable as a result. You can mix code that compiles with OpenMP with code using pthreads, too.

1: cilk.com/multicore-blog/bid/5847/The-Folly-Of-Do-It-Yourself-Multithreading

2: opengroup.org/onlinepubs/009695399/basedefs/pthread.h.html

3: openmp.org/wp/openmp-specifications/

 

A4:

you get the complete answer here:

http://openmp.blogspot.com/2011/01/what-is-open-mp-why-open-mp.html

it clearly explains why openmp?

A brief of it as far as i could understand is,

  • First, OpenMP is easy to learn and implement on a fresh projects
  • Secondly, Minimum code change required to alter existing applications

抱歉!评论已关闭.