Skip to content

enhancements openmp

DagSverreSeljebotn edited this page Apr 5, 2009 · 3 revisions

OpenMP support

** Under construction! **

OpenMP is supported as #pragmas in most common C compilers, and so it could make sense to treat it as a de facto standard extension to the C language and have specific support in Cython for it. At least it would be rather impossible to write Cython code using OpenMP without explicit Cython support.

This is a draft making a syntax for OpenMP support. It tries to be a close mapping 1:1 to the OpenMP standard because that will be least surprising. However the syntax is at a slightly higher level than in C (e.g. a seperate for iterator function rather than a directive before a normal for loop, and so on).

Example

from cython cimport openmp
cdef dot_product(int* x, int* y, size_t N):
    cdef int i, result = 0
    for i in openmp.range(N, reduce="+:result"):
        result += x[i]*y[i]
    return result

Issues

The GIL

The GIL can be held during OpenMP execution, but only accessed by one thread at the time. OpenMP code will often run in "nogil" sections, but doesn't have to.

It is proposed that Cython uses the GIL-checking code to allow GIL-requiring operations inside the default critical sections (OpenMP has critical sections which carry names, similar to locks, and there's a default one without name). Also a openmp.ignoregil is introduced which can be used to get around it if you manually lock and make sure only one thread is accessing the variable.

Clone this wiki locally