GPU Workshop Sample Code
 All Data Structures Namespaces Files Functions Variables Macros Pages
Functions | Variables
rng.c File Reference

Functions for generating multiple streams of random numbers on the GPU. More...

#include <math.h>

Go to the source code of this file.

Functions

__device__ unsigned int TausStep (unsigned int *seed, int S1, int S2, int S3, unsigned int M)
 Tausworth random number generator. More...
 
__device__ unsigned int LCGStep (unsigned int *seed, unsigned int A, unsigned int C)
 Simple linear congruential random number generator. More...
 
__device__ float hybrid_tausworth_rng (unsigned int *seed1, unsigned int *seed2, unsigned int *seed3, unsigned int *seed4)
 Hybrid combined Tausworth RNG. More...
 
__device__ __host__ unsigned int mult_with_carry_rng (unsigned int *seed1, unsigned int *seed2)
 Multiply-with-carry RNG (George Marsaglia). More...
 
void initialize_rng_seeds (unsigned int *seeds, unsigned int *meta_seed, int n)
 Generate seeds for n streams. More...
 
__device__ void get_seed (unsigned int *seed, unsigned int *rng_state, int i, int n)
 
__device__ void put_seed (unsigned int *seed, unsigned int *rng_state, int i, int n)
 
__device__ float random_uniform (unsigned int *seed)
 
__device__ float random_uniform_positive (unsigned int *seed)
 
template<typename T >
__device__ void random_normal (T *z1, T *z2, unsigned int *seed)
 Box-Muller normal RNG. More...
 

Variables

const float TWOPI = 6.283185307179586
 

Detailed Description

Functions for generating multiple streams of random numbers on the GPU.

Definition in file rng.c.

Function Documentation

__device__ void get_seed ( unsigned int *  seed,
unsigned int *  rng_state,
int  i,
int  n 
)

Definition at line 127 of file rng.c.

__device__ float hybrid_tausworth_rng ( unsigned int *  seed1,
unsigned int *  seed2,
unsigned int *  seed3,
unsigned int *  seed4 
)

Hybrid combined Tausworth RNG.

Notes

Definition at line 59 of file rng.c.

void initialize_rng_seeds ( unsigned int *  seeds,
unsigned int *  meta_seed,
int  n 
)

Generate seeds for n streams.

Given a meta_seed (input), generate an array of seeds (one per intended rng stream).

Notes

  • This creates seeds on host. They should typically then be copied to GPU in order to use.
Parameters
seeds(out)
meta_seed(in)
n(in) number of seeds to generate

Definition at line 109 of file rng.c.

__device__ unsigned int LCGStep ( unsigned int *  seed,
unsigned int  A,
unsigned int  C 
)

Simple linear congruential random number generator.

Notes

Definition at line 41 of file rng.c.

__device__ __host__ unsigned int mult_with_carry_rng ( unsigned int *  seed1,
unsigned int *  seed2 
)

Multiply-with-carry RNG (George Marsaglia).

Notes

  • Intended to be used for setting seeds for multiple streams, thus returns unsigned int.
  • Multiply by 2.3283064365387e-10 to get a unif(0,1).
  • Check that result is >128 to use as a seed for Tausworth generator.
  • See http://en.wikipedia.org/wiki/Random_number_generation

Definition at line 88 of file rng.c.

__device__ void put_seed ( unsigned int *  seed,
unsigned int *  rng_state,
int  i,
int  n 
)

Definition at line 139 of file rng.c.

template<typename T >
__device__ void random_normal ( T *  z1,
T *  z2,
unsigned int *  seed 
)

Box-Muller normal RNG.

Notes

Definition at line 182 of file rng.c.

__device__ float random_uniform ( unsigned int *  seed)

Definition at line 152 of file rng.c.

__device__ float random_uniform_positive ( unsigned int *  seed)

Definition at line 161 of file rng.c.

__device__ unsigned int TausStep ( unsigned int *  seed,
int  S1,
int  S2,
int  S3,
unsigned int  M 
)

Tausworth random number generator.

Notes

Definition at line 22 of file rng.c.

Variable Documentation

const float TWOPI = 6.283185307179586

Definition at line 7 of file rng.c.