Main Content

Random number stream on a GPU

Use `parallel.gpu.RandStream`

to control the global GPU random
number stream and create multiple independent streams on the GPU. When you generate random
numbers on a GPU, the numbers are drawn from the GPU random number stream. This stream is
different from the random stream of the client MATLAB^{®} session on the CPU.

To create random numbers on the GPU, use the random number generator functions `rand`

, `randi`

, and `randn`

with `gpuArrays`

. By default, these functions draw
numbers from the global GPU random number stream. To use a different stream, follow the
syntaxes described in Object Functions. If you use a GPU random
number stream, the results are returned as a `gpuArray`

.

Use the following syntaxes to create a single `parallel.gpu.RandStream`

object. If you want to create multiple independent streams simultaneously, use the `parallel.gpu.RandStream.create`

function.

`s = parallel.gpu.RandStream(`

creates
a random number stream that uses the uniform pseudorandom number generator algorithm
specified by `gentype`

)`'gentype'`

.

`s = parallel.gpu.RandStream(`

also specifies one or more optional `gentype`

,`Name,Value`

)`Name,Value`

pairs to control
properties of the stream.

`parallel.gpu.RandStream.create` | Create independent random number streams on a GPU |

`parallel.gpu.RandStream.list` | Random number generator algorithms on the GPU |

`parallel.gpu.RandStream.getGlobalStream` | Current global GPU random number stream |

`parallel.gpu.RandStream.setGlobalStream` | Set GPU global random number stream |

`reset (RandStream)` | Reset random number stream |

By default, when you create random numbers on the GPU using random number generation
functions, such as `rand`

, the random numbers are drawn from the
global random number stream on the GPU. To specify a different stream, create a
`parallel.gpu.RandStream`

object and pass it as the first input argument. For
instance, create a 4-by-1 vector of random numbers using the Philox generator
algorithm.

s = parallel.gpu.RandStream('Philox'); r = rand(s,4,1);

These functions accept a `parallel.gpu.RandStream`

object and generate
random numbers on the GPU:

`rand` | Uniformly distributed random numbers | Supported syntaxes, where X = rand(s) X = rand(s,n) X = rand(s,sz1,...,szN) X = rand(s,sz) X = rand(s,typename) `rand` , `randi` , and `randn` . |

`randi` | Uniformly distributed pseudorandom integers | |

`randn` | Normally distributed random numbers | |

`randperm` | Random permutation of integers | Supported syntaxes, where p = randperm(s,n) p = randperm(s,n,k) `randperm` . |