Muthu manimaran
Muthu manimaran

Reputation: 141

running parallel programming with openmp in python

I want to run say 10 tasks, in different cpus of same function with different 10 parameters (10 tasks) each run on different cpu. how do i do it using openmp in python code. For some reasons mpi4py and multiprocessing packages are blocked in local cluster. So i am wondering, whether i can parallelize the code using openmp alone.

what i tried:


import numpy as np
import time

t1_start = time.process_time()

def func(a):
     print("helloworld")

for b in range(max):
     obs = func(b)

print(time.process_time()-t1_start) 

i want obs = func(b) to run on different processors assigned automatically for a range of values. In mpi4y package, i can use MPI.scatter() to do this automatically. But i don't know whether it is possible to do the same with openmp alone.

Upvotes: 1

Views: 1234

Answers (1)

Jérôme Richard
Jérôme Richard

Reputation: 50433

This is not really possible in pure Python using the standard interpreter (CPython). Actually, it is possible but it is generally completely useless for computationally intensive codes. Indeed, if you use multiple threads, the Global Interpreter Lock (GIL) of CPython will prevent any parallel speed up of computationally intensive codes (it only speed up IOs mainly) since the computation will be serialized in the end. In fast, the computation will even be slower because of the lock contention. Python does not (officially) support OpenMP. It has its own threading layer. Note that some library like Numpy can release the GIL for some function but the speed up is often disappointing (because the GIL is not completely released).

If you want to use OpenMP in Python, you can use JIT/AOT compilers like Numba or Cython. Numba only supports parallel loops (using prange and the flag parallel=True) and basic reductions, so the support is minimalist (no atomic variables for CPU codes, no locks, no tasks, no way to tweak the loop scheduling, no SIMD directives, etc.). AFAIK, this is the same thing for Cython. If you need advanced feature, then you need to use languages officially supporting OpenMP like C, C++ and FORTRAN. You can also write Numba/Cython that release the GIL and call them from a C/C++/FORTRAN function using OpenMP but the OpenMP features will not be available from the Python function.

Upvotes: 2

Related Questions