[Cython] OpenMP support

mark florisson markflorisson88 at gmail.com
Sat Mar 12 13:12:44 CET 2011


On 11 March 2011 14:54, Sturla Molden <sturla at molden.no> wrote:
> 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.

That's the same thing I proposed with different syntax :) The problem
with the pragmas is that we can't declare arbitrary blocks in Cython,
to which the pragmas should be applied, in which case get the kind of
awkwardness seen below.

What I liked about your proposed closures is the neat pythonic syntax
for the scoping rules of variables in the loops. I think I might have
changed my mind entirely about OpenMP(-like) support, as users can
already do a lot, and if they really want OpenMP then it will be
likely most convenient to write it in C and simply wrap it.

> 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:
> <suite>
>
> 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
>   {
> <suite>
>   }
>
>
>
>
> Sturla
>
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> http://mail.python.org/mailman/listinfo/cython-devel
>


More information about the cython-devel mailing list