8000 BUG: Make RandomState.set_state and RandomState.get_state threadsafe by sturlamolden · Pull Request #5388 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Make RandomState.set_state and RandomState.get_state threadsafe #5388

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

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 11 additions & 7 deletions numpy/random/mtrand/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,13 @@ cdef class RandomState:
"""
cdef ndarray state "arrayObject_state"
state = <ndarray>np.empty(624, np.uint)
memcpy(<void*>PyArray_DATA(state), <void*>(self.internal_state.key), 624*sizeof(long))
with self.lock:
memcpy(<void*>PyArray_DATA(state), <void*>(self.internal_state.key), 624*sizeof(long))
has_gauss = self.internal_state.has_gauss
gauss = self.internal_state.gauss
pos = self.internal_state.pos
state = <ndarray>np.asarray(state, np.uint32)
return ('MT19937', state, self.internal_state.pos,
self.internal_state.has_gauss, self.internal_state.gauss)
return ('MT19937', state, pos, has_gauss, gauss)

def set_state(self, state):
"""
Expand Down Expand Up @@ -761,10 +764,11 @@ cdef class RandomState:
obj = <ndarray>PyArray_ContiguousFromObject(key, NPY_LONG, 1, 1)
if PyArray_DIM(obj, 0) != 624:
raise ValueError("state must be 624 longs")
memcpy(<void*>(self.internal_state.key), <void*>PyArray_DATA(obj), 624*sizeof(long))
self.internal_state.pos = pos
self.internal_state.has_gauss = has_gauss
self.internal_state.gauss = cached_gaussian
with self.lock:
memcpy(<void*>(self.internal_state.key), <void*>PyArray_DATA(obj), 624*sizeof(long))
self.internal_state.pos = pos
self.internal_state.has_gauss = has_gauss
self.internal_state.gauss = cached_gaussian

# Pickling support:
def __getstate__(self):
Expand Down
0