prange and numpy.random

0 views
Skip to first unread message

Peter Helstrup Jensen

unread,
Nov 27, 2017, 9:06:01 AM11/27/17
to Numba Public Discussion - Public
As mentioned in https://groups.google.com/a/continuum.io/d/topic/numba-users/hGsFferxCbI/discussion from 2015 "built-in random number generator in Numba is not currently thread-safe" and in 2017, 0.35v documentation there's a note about numpy.random: "The generator is not thread-safe when releasing the GIL."

What about using numpy random inside a prange loop? Is it safe? If not how do I make it safe?

@njit(parallel=True)
def mutate(a):
for i in prange(a.size):
if np.random.rand() < mutate_prop:
a[i] += np.random.normal(mean, mu, 1)[0]
return a


Seems like releasing the GIL and prange is two different things according to the documentation note under the FAQ after mentioning prange: "You can also manually run computations on multiple threads yourself and use the nogil=True option (see releasing the GIL). Numba can also target parallel execution on GPU architectures using its CUDA and HSA backends."

Reply all
Reply to author
Forward
0 new messages