generate random numbers in a kernel

0 views
Skip to first unread message

Claudio Martella

unread,
Jan 25, 2016, 3:36:22 PM1/25/16
to Numba Public Discussion - Public
I've been trying for some time to generate random numbers in numba inside of a kernel (e.g., inside a vectorize). I've tried using numpy.random.uniform, i've tried using cuda.np.random.uniform, i've tried random.uniform, but all complain of undefined types in the nopython frontend.

Is there a way to generate random numbers ala curadom in cudalib?

something like:

@vectorize("float64(float64)", target="cuda") # could be target="parallel" too
def get_random(a):
    return a + numpy.random.uniform(0, 1)


# in principle in my app they won't be zeros of couse
data = numpy.zeros(10000000, dtype=float64)
get_random(data)

Thanks,
/CM

Stanley Seibert

unread,
Jan 25, 2016, 4:17:00 PM1/25/16
to Numba Public Discussion - Public
Hi,

We don't yet have a built-in random number generator for the CUDA target.  The curand device functions that NVIDIA provides for CUDA C are not available at the LLVM level where we generate CUDA code.  (We've asked them to put these functions into their LLVM bitcode library, so hopefully they will in the future.)  Fixing this is on our todo list.

In the mean time, Siu has an xorshift implementation that you can use with Numba:

https://gist.github.com/sklam/2b9574b51c6bd2e6cf9e

(We don't want to support this RNG officially because there may be some problems with xorshift that we aren't aware of.)

--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users...@continuum.io.
To post to this group, send email to numba...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/1ef5145c-e7c8-48a8-bc0a-8e03645899c7%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Claudio Martella

unread,
Jan 25, 2016, 5:53:37 PM1/25/16
to Numba Public Discussion - Public
Thanks for the quick answer. xorshift* does indeed look like a good approach. One question about the usage of that implementation (thanks for it):
how am I supposed to initialize states and id? it is not completely clear to me how to use them, for example, if i want to add a random number to each element of an array (simplest example).

rall...@cfa.harvard.edu

unread,
Oct 18, 2016, 4:34:33 PM10/18/16
to Numba Public Discussion - Public
Hi Stanley,

Thanks for linking to the example, but it is terribly unclear how to use it. What is states and what is id?

Ryan

Siu Kwan Lam

unread,
Oct 18, 2016, 5:25:47 PM10/18/16
to Numba Public Discussion - Public

--
Siu Kwan Lam
Software Engineer
Continuum Analytics

Kevin Sheppard

unread,
Oct 18, 2016, 5:44:58 PM10/18/16
to Numba Public Discussion - Public

The most common approach is to read from random or urandom (or WinCryptProvider).  If you need more bits, say if you have a very large state, you can use 64 bits from system RNGs plus splitmix64 to generate more than enough to generate an initial state for an xorshift RNG. 


Reply all
Reply to author
Forward
0 new messages