# parallel.prange loops producing random results #2316

Open
opened this Issue Jun 8, 2018 · 5 comments

Projects
None yet
2 participants

### ngoldbaum commented Jun 8, 2018 • edited

 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)```
Contributor

### scoder commented Jun 8, 2018

 Quick question: you really meant to run a parallel sum reduction in both cases, right? Your code should sum up all the values into a global sum, and the only difference is where that sum is stored?

### 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.

Merged

### 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 commented Aug 31, 2018 • edited

 I guess this was already raised a couple years ago in #1504.
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.