10000 BUG: Remove data race in mtrand: two threads could mutate the state. by ssbr · Pull Request #7305 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Remove data race in mtrand: two threads could mutate the state. #7305

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

Merged
merged 1 commit into from
Feb 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions numpy/random/mtrand/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ cdef object cont0_array(rk_state *state, rk_cont0 func, object size,
cdef npy_intp i

if size is None:
return func(state)
with lock, nogil:
rv = func(state)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to just do

with lock, nogil:
    return func(state)

? Ditto for the other nine or so cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I wrote at first, but Cython forbids returning a PyObject without the GIL, apparently. (Seems like a bug in Cython, but whatever).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a deliberate choice, not a bug. Either way, it's certainly a good reason to write the code the way you did. LGTM

return rv
else:
array = <ndarray>np.empty(size, np.float64)
length = PyArray_SIZE(array)
Expand All @@ -177,7 +179,9 @@ cdef object cont1_array_sc(rk_state *state, rk_cont1 func, object size, double a
cdef npy_intp i

if size is None:
return func(state, a)
with lock, nogil:
rv = func(state, a)
return rv
else:
array = <ndarray>np.empty(size, np.float64)
length = PyArray_SIZE(array)
Expand Down Expand Up @@ -229,7 +233,9 @@ cdef object cont2_array_sc(rk_state *state, rk_cont2 func, object size, double a
cdef npy_intp i

if size is None:
return func(state, a, b)
with lock, nogil:
rv = func(state, a, b)
return rv
else:
array = <ndarray>np.empty(size, np.float64)
length = PyArray_SIZE(array)
Expand Down Expand Up @@ -278,7 +284,9 @@ cdef object cont3_array_sc(rk_state *state, rk_cont3 func, object size, double a
cdef npy_intp i

if size is None:
return func(state, a, b, c)
with lock, nogil:
rv = func(state, a, b, c)
return rv
else:
array = <ndarray>np.empty(size, np.float64)
length = PyArray_SIZE(array)
Expand Down Expand Up @@ -327,7 +335,9 @@ cdef object disc0_array(rk_state *state, rk_disc0 func, object size, object lock
cdef npy_intp i

if size is None:
return func(state)
with lock, nogil:
rv = func(state)
return rv
else:
array = <ndarray>np.empty(size, int)
length = PyArray_SIZE(array)
Expand All @@ -345,7 +355,9 @@ cdef object discnp_array_sc(rk_state *state, rk_discnp func, object size,
cdef npy_intp i

if size is None:
return func(state, n, p)
with lock, nogil:
rv = func(state, n, p)
return rv
else:
array = <ndarray>np.empty(size, int)
length = PyArray_SIZE(array)
Expand Down Expand Up @@ -392,7 +404,9 @@ cdef object discdd_array_sc(rk_state *state, rk_discdd func, object size,
cdef npy_intp i

if size is None:
return func(state, n, p)
with lock, nogil:
rv = func(state, n, p)
return rv
else:
array = <ndarray>np.empty(size, int)
length = PyArray_SIZE(array)
Expand Down Expand Up @@ -439,7 +453,9 @@ cdef object discnmN_array_sc(rk_state *state, rk_discnmN func, object size,
cdef npy_intp i

if size is None:
return func(state, n, m, N)
with lock, nogil:
rv = func(state, n, m, N)
return rv
else:
array = <ndarray>np.empty(size, int)
length = PyArray_SIZE(array)
Expand Down Expand Up @@ -488,7 +504,9 @@ cdef object discd_array_sc(rk_state *state, rk_discd func, object size,
cdef npy_intp i

if size is None:
return func(state, a)
with lock, nogil:
rv = func(state, a)
return rv
else:
array = <ndarray>np.empty(size, int)
length = PyArray_SIZE(array)
Expand Down
0