Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

parallel processing - OpenMP and Python

I have experience in coding OpenMP for Shared Memory machines (in both C and FORTRAN) to carry out simple tasks like matrix addition, multiplication etc. (Just to see how it competes with LAPACK). I know OpenMP enough to carry out simple tasks without the need to look at documentation.

Recently, I shifted to Python for my projects and I don't have any experience with Python beyond the absolute basics.

My question is :

What is the easiest way to use OpenMP in Python? By easiest, I mean the one that takes least effort on the programmer side (even if it comes at the expense of added system time)?

The reason I use OpenMP is because a serial code can be converted to a working parallel code with a few !$OMPs scattered around. The time required to achieve a rough parallelization is fascinatingly small. Is there any way to replicate this feature in Python?

From browsing around on SO, I can find:

  • C extensions
  • StackLess Python

Are there more? Which aligns best with my question?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Cython

Cython has OpenMP support: With Cython, OpenMP can be added by using the prange (parallel range) operator and adding the -fopenmp compiler directive to setup.py.

When working in a prange stanza, execution is performed in parallel because we disable the global interpreter lock (GIL) by using the with nogil: to specify the block where the GIL is disabled.

To compile _cython_np.pyx_ we have to modify the setup.py script as shown below. We tell it to inform the C compiler to use -fopenmp as an argument during compilation - to enable OpenMP and to link with the OpenMP libraries. setup.py

With Cython’s prange, we can choose different scheduling approaches. With static, the workload is distributed evenly across the available CPUs. However, as some of your calculation regions are expensive in time, while others are cheap - if we ask Cython to schedule the work chunks equally using static across the CPUs, then the results for some regions will complete faster than others and those threads will then sit idle. Both the dynamic and guided schedule options attempt to mitigate this problem by allocating work in smaller chunks dynamically at runtime so that the CPUs are more evenly distributed when the workload’s calculation time is variable. Thus, for your code, the correct choice will vary depending on the nature of your workload.

Numba

Numba’s premium version, NumbaPro, has experimental support of a prange parallelization operator for working with OpenMP.

Pythran

Pythran (a Python-to-C++ compiler for a subset of Python) can take advantage of vectorization possibilities and of OpenMP-based parallelization possibilities, though it runs using Python 2.7 only. You specify parallel sections using pragma omp directives (very similarly to Cython’s OpenMP support described above), e.g.:

pragma omp

PyPy

The JIT Python compiler PyPy supports the multiprocessing module (see following) and has a project called PyPy-STM "a special in-development version of PyPy which can run multiple independent CPU-hungry threads in the same process in parallel".

Side note: multiprocessing

OpenMP is a low-level interface to multiple cores. You may want to look at multiprocessing. The multiprocessing module works at a higher level, sharing Python data structures, while OpenMP works with C primitive objects (e.g., integers and floats) once you’ve compiled to C. It only makes sense to use OpenMP if you’re compiling your code; if you’re not compiling (e.g., if you’re using efficient numpy code and you want to run on many cores), then sticking with multiprocessing is probably the right approach.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...