pbdMPI (version 0.3-9)

seed for RNG: Seed Functions for Random Number Generators

Description

These functions set/end/reset seeds to all ranks. By default, these functions are wrappers of rlecuyer which implements the random number generator with multiple independent streams developed by L'Ecuyer et al (2002).

Usage

comm.set.seed(seed, diff = FALSE, state = NULL,
              comm = .pbd_env$SPMD.CT$comm)
comm.seed.state(comm = .pbd_env$SPMD.CT$comm)
comm.end.seed(comm = .pbd_env$SPMD.CT$comm)
comm.reset.seed(comm = .pbd_env$SPMD.CT$comm)

Arguments

seed

one integer or six integers as in rlecuyer.

diff

if all ranks use the same stream. (default = FALSE)

state

a new state to overwrite seed.

comm

a communicator number.

Value

Several hidden objects are set in the .GlobalEnv, see rlecuyer package for details.

Details

comm.set.seed() sets the given seed to all ranks. If diff = FALSE, then all ranks generate one stream and use that stream. Otherwise, all ranks generate COMM.SIZE streams and use the stream named by COMM.RANK.

Also, comm.set.seed() can assign to arbitrarily state obtained from comm.seed.state().

comm.seed.state() obtains current state of seed which ends the stream first (update state), gets the state, and continues the stream (pretend as nothing happens).

comm.end.seed() ends and deletes seed from all ranks.

comm.reset.seed() resets seed to initial start steps which end the current seed and reset everything back to the start stream. Use this function with caution.

References

Pierre L'Ecuyer, Simard, R., Chen, E.J., and Kelton, W.D. (2002) An Object-Oriented Random-Number Package with Many Long Streams and Substreams. Operations Research, 50(6), 1073-1075.

http://www.iro.umontreal.ca/~lecuyer/myftp/papers/streams00.pdf

Sevcikova, H. and Rossini, T. (2012) rlecuyer: R interface to RNG with multiple streams. R Package, URL https://cran.r-project.org/package=rlecuyer

Programming with Big Data in R Website: http://r-pbd.org/

See Also

.lec.SetPackageSeed(), .lec.CreateStream(), .lec.CurrentStream(), .lec.CurrentStreamEnd(), .lec.DeleteStream(), .lec.SetSeed(), and .lec.GetState().

Examples

Run this code
# NOT RUN {
### Save code in a file "demo.r" and run with 2 processors by
### SHELL> mpiexec -np 2 Rscript demo.r

spmd.code <- "
### Initial.
suppressMessages(library(pbdMPI, quietly = TRUE))
init()

### Examples.
comm.set.seed(123456)
comm.print(runif(5), all.rank = TRUE)
comm.reset.seed()
comm.print(runif(5), all.rank = TRUE)
comm.end.seed()

### Obtain the seed state.
comm.set.seed(123456, diff = TRUE)
comm.print(runif(5), all.rank = TRUE)
saved.seed <- comm.seed.state()   ### save the state.
comm.print(runif(5), all.rank = TRUE)
comm.end.seed()

### Start from a saved state.
comm.set.seed(123456, state = saved.seed) ### rewind to the state.
comm.print(runif(5), all.rank = TRUE)
comm.end.seed()

### Finish.
finalize()
"
# execmpi(spmd.code, nranks = 2L)
# }

Run the code above in your browser using DataLab