Search code examples
cudapycuda

Random number generation with pyCUDA


I want to generate random numbers with pyCUDA.

To this end, I'm using the following code, which I'm running on the Kaggle virtual machine:

import numpy           as np
import time

import pycuda.gpuarray as     gpuarray
import pycuda.driver   as     cuda
import pycuda.autoinit
from pycuda.compiler   import SourceModule

N = 10

from pycuda.curandom import XORWOWRandomNumberGenerator
rng   = XORWOWRandomNumberGenerator()
d_x   = rng.gen_uniform((N,), dtype = np.float32)

My question is on how do I feed the random number generator with a seed.

At the pyCUDA documentation page, it says that

class pycuda.curandom.XORWOWRandomNumberGenerator(seed_getter=None, offset=0)

Parameters:

seed_getter – a function that, given an integer count, will yield an int32 GPUArray of seeds.

offset – Starting index into the XORWOW sequence, given seed.

What is an example of the seed_getter function?


Solution

  • The curandom module has two built-in functions for generating random seeds:

    1. seed_getter_uniform which will return an array length N initialized with a single random seed, and
    2. seed_getter_unique which will return an array initialized with N different random seeds.

    Use one or the other depending on whether you want all internal generator instances to used the same seed or a unique seed.