Pymp – OpenMP-like Python Programming

Installing Pymp

Although I use Anaconda, I also use Pip when needed. When I examined both Anaconda and Pip for Pymp, the latest version they both had was 0.01, although the last version released was 0.4.2 on September 7, 2018; therefore, I installed Pymp according to the instructions on the website.

After downloading and exploding the .tar.gz or .zip source file and moving to the directory created, I ran the command,

$ python setup.py develop

which builds Pymp. Fortunately, the Pymp codebase is fairly small, so the build goes very fast.

Note that the website proposes using the latest master branch from the Git repo if you want the absolute latest version of Pymp.

Simple Introductory Code

To illustrate how to code with Pymp, the sample code in Listing 2 from the website begins with basic Python code. To keep things simple, this is a serial code with a single array. Listing 3 is the Pymp version of the same code.

Listing 2: Python Code

from __future__ import print_function
 
ex_array = np.zeros((100,), dtype='uint8')
for index in range(0, 100):
    ex_array[index] = 1
    print('Yay! {} done!'.format(index))

Listing 3: Pymp Code

from __future__ import print_function
 
import pymp
 
ex_array = pymp.shared.array((100,), dtype='uint8')
with pymp.Parallel(4) as p:
    for index in p.range(0, 100):
        ex_array[index] = 1
        # The parallel print function takes care of asynchronous output.
        p.print('Yay! {} done!'.format(index))

The first change to the serial code is creating a shared array with a pymp method. The next change is to add the statement creating the number of processes (with pymp.Parallel(4) as p ). Remember that these are forked processes and not threads.

The final action is to change the range function to p.range(0, 100) . According to the Pymp website, this is the same as using the static schedule.

The approach illustrated in these code samples bypasses the GIL in favor of the operating system’s fork method. From the GitHub site, “Due to the copy-on-write strategy, this causes only a minimal overhead and results in the expected semantics.” Note that using the system fork operation excludes Windows because it lacks a fork mechanism.

Related content

comments powered by Disqus