[Cython] OpenMP support

Sturla Molden sturla at molden.no
Fri Mar 11 14:54:45 CET 2011

Den 11.03.2011 11:42, skrev Matej Laitl:
> #pragma omp parallel for private(var1) reduction(+:var2) schedule(guided)
> for i in range(n):
>      do_work(i)

I do like this, as it is valid Python and can be turned on/off with a 
compiler flag to Cython.

Issues to warn about:

- We cannot jump out of a parallel block from C/C++/Fortran (goto, 
longjmp, C++ exception). That applies to Python exceptions as well, and 
the generated Cython code.

- GIL issue: CPython interpreter actually call GetCurrentThreadId(), 
e.g. in thread_nt.h. So the OpenMP thread using the Python CAPI must be 
the OS thread holding the GIL. It is not sufficient that the master 
thread does.

- Remember that NumPy arrays are unboxed. Those local variables should 
be silently passed as firstprivate.

- Refcounting with Python objects and private variables.

None of the above applies if we go with the library approach. But then 
it would look less like OpenMP in C.

Also, do we want this?

    #pragma omp parallel
    if 1:

It is a consequence of just re-using the C-syntax for OpenMP, as 
intendation matters in Cython. There are no anonymous blocks similar to 
C in Cython:

    #pragma omp parallel


More information about the cython-devel mailing list