Parallel Cython with OpenMP
Cython provides a convenient interface to perform shared-memory parallel processing through OpenMP. This lets you write extremely efficient parallel code directly in Cython without having to create a C wrapper.
OpenMP is a specification and an API designed to write multithreaded, parallel programs. The OpenMP specification includes a series of C preprocessor directives to manage threads and provides communication patterns, load balancing, and other synchronization features. Several C/C++ and Fortran compilers (including GCC) implement the OpenMP API.
We can introduce the Cython parallel features with a small example. Cython provides a simple API based on OpenMP in the cython.parallel
module. The simplest way to achieve parallelism is through prange
, which is a construct that automatically distributes loop operations in multiple threads.
First of all, we can write the serial version of a program that computes the square of each element of a NumPy array in the hello_parallel...