Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Random Numbers in CUDA ITCS 4/5145 Parallel Programming Spring 2012, April 12a, 2012.

Similar presentations


Presentation on theme: "Using Random Numbers in CUDA ITCS 4/5145 Parallel Programming Spring 2012, April 12a, 2012."— Presentation transcript:

1 Using Random Numbers in CUDA ITCS 4/5145 Parallel Programming Spring 2012, April 12a, 2012

2 2 Monte Carlo Computations Embarrassingly parallel computations attractive for GPUs. Use random numbers to make random selections that are then used in the computation. Many application areas: numerical integration, physical simulations, business models, finance, … Principle issue is how to generate (pseudo) random sequences. Cannot call rand() or any other C library function from within a CUDA kernel. * http://developer.download.nvidia.com/compute/cuda/3_2_prod/toolkit/docs/CURAND_Library.pdf

3 3 Generating random numbers Possible solutions: 1.Call rand() in the CPU code and copy the random numbers across to the GPU (not the best way) 2.Use NVIDIA CUDA CURAND library* 3.Hand-code the rand() function in kernel: Common random number generator formula is: x i+1 = (a * x i + c) mod m Good values for a, c, and m are a = 16807, c = 0, and m = 2 31 - 1 (a prime number). Will need to use long ints because of the size of numbers. * http://developer.download.nvidia.com/compute/cuda/3_2_prod/toolkit/docs/CURAND_Library.pdf

4 4 Hand-coded device random number generator __device__ float my_rand(unsigned int *seed) { // constants for random no gen. unsigned long a = 16807; unsigned long m = 2147483647; // 2^31 - 1 unsigned long x = (unsigned long) *seed; x = (a * x)%m; *seed = (unsigned int) x; return ((float)x)/m; } __global__ void myKernel(…) … unsigned int seed = tid + 1; … float randnumber = my_rand(&seed); //between 0&1 … }

5 5 Using CUDA SDK Random number generator __global__ void myKernel(…, curandState *states) { unsigned int tid = threadIdx.x + blockDim.x * blockIdx.x; curand_init(1234, tid, 0, &states[tid]); // Initialize CURAND float randnumber = curand_uniform (&states[tid]); // between 0 and 1 … } int main (int argc, char *argv[]) { … curandState *devStates; cudaMalloc( (void **)&devStates, THREADS*BLOCKS*sizeof(curandState) ); … myKernel >>(…, devStates); … }

6 Questions


Download ppt "Using Random Numbers in CUDA ITCS 4/5145 Parallel Programming Spring 2012, April 12a, 2012."

Similar presentations


Ads by Google