New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parallel.prange loops producing random results #2316

Open
ngoldbaum opened this Issue Jun 8, 2018 · 5 comments

Comments

Projects
None yet
2 participants
@ngoldbaum

ngoldbaum commented Jun 8, 2018

This was originally found by @AshKelly in the context of his GSOC project with yt.

Take a look at this repo: https://github.com/ngoldbaum/cython-issue

You can build the .pyx file with pip install -e .

If you're on a machine with OpenMP support (e.g. on Linux, not MacOS), you'll see that fun_sum returns random results while fun_sum_scalar returns consistent results. For reference, here's what fun_sum looks like:

cimport numpy as np
import numpy as np
cimport cython
from cython.parallel import prange

def fun_sum():

    cdef np.float64_t[:] result = np.zeros(1)
    cdef int j
    cdef np.float64_t a = 3.0

    with nogil:
        for j in prange(0, 10000):
            result[0] += a + j

    return np.array(result)
@scoder

This comment has been minimized.

Contributor

scoder commented Jun 8, 2018

@ngoldbaum

This comment has been minimized.

ngoldbaum commented Jun 8, 2018

Yes, in the real example where this was originally found we're compositing data in parallel onto a 2D image array. We found we could trigger it with an array of shape (1,), so we figured that was simplest for the bug report.

@ngoldbaum

This comment has been minimized.

ngoldbaum commented Aug 31, 2018

@scoder so @AshKelly has come up with a way for cython to generate code that avoids the race condition. It seems cython is incorrectly not marking the array as a parallel reduction target. See the description in ngoldbaum/cython-issue#1 for details.

Unfortunately it looks like array reductions are only supported in relatively recent compilers (gcc 6 or newer, or any compiler that implements OpenMP 4.5 or newer). Out of curiosity what is the minimum supported OpenMP version for cython?

@ngoldbaum

This comment has been minimized.

ngoldbaum commented Aug 31, 2018

I guess this was already raised a couple years ago in #1504.

@scoder

This comment has been minimized.

Contributor

scoder commented Oct 14, 2018

I'm not sure there is a minimum OpenMP version. There's at least support for OpenMP 3 and probably also 2. We could just generate a readable compile time #error when this feature is used with an older OMP version. Maybe also issue a Cython warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment