[Cython] OpenMP support

mark florisson markflorisson88 at gmail.com
Tue Mar 8 11:34:01 CET 2011

I'd like to implement OpenMP support for Cython. Looking at
http://wiki.cython.org/enhancements/openmp I agree a close
1:1 mapping would be nice. It would probably make sense to start with
support for 'nogil' sections because GIL-holding
sections would be hard to deal with considering all the 'goto's that
are generated (because you may not leave the
OpenMP block). See this thread
http://comments.gmane.org/gmane.comp.python.cython.devel/8695 for a
previous discussion.

Looking also at http://wiki.cython.org/enhancements/parallel , for the
'parallel for' support I think the best syntax would
be to introduce a fake 'cython.openmp' module as Dag suggested. I
propose to start with the following syntax:

    from python (c)import openmp

    with nogil:
        with openmp.parallel('sections'):
            with openmp.section():

I'd like to support the directives as part of the parallel directive,
and the stand-alone counterparts.
So the equivalent of the above would read:

    with nogil:
        with openmp.parallel():
            with openmp.sections():
                with openmp.section():

Perhaps to apply an OpenMP directive to a single statement instead of
a structured block, we could follow OpenMP's
approach, i.e. allow writing the following instead of its 'with' equivalent:

a = 1

For the 'for' and 'parallel for' constructs I propose an
openmp.range() and openmp.prange() respectively, i.e.

    with openmp.parallel():
        for i in openmp.range(..., firstprivate=('a', 'b'),

could be written

    for i in openmp.prange(..., firstprivate=('a', 'b'), reduction='+:result'):

for short.

In the previous thread Sturla mentioned that perhaps it would be
easier to implement OpenMP support manually instead of
generating OpenMP pragmas and relying on OpenMP compiler support.
However, considering that OpenMP has quite a few constructs,
not to mention the functionality provided by the OpenMP library itself
(all the omp_* functions), I think it will be
easier to go the "pragma route", especially if we choose to provide
all (or a substantial part) of the OpenMP functionality.
Then the OpenMP runtime library (e.g. libgomp when using gcc) can
simply be wrapped with an appropriate .pxd in Cython/Includes.

What do you guys think?

More information about the cython-devel mailing list