doRNG: Reproducible Parallel Foreach Backend

Description Usage Arguments Value See Also Examples

Description

%dorng% is a foreach operator that provides an alternative operator %dopar%, which enable reproducible foreach loops to be performed.

Usage

1
  obj %dorng% ex

Arguments

obj

a foreach object as returned by a call to foreach.

ex

the R expression to evaluate.

Value

%dorng% returns the result of the foreach loop. See %dopar%. The whole sequence of RNG seeds is stored in the result object as an attribute. Use attr(res, 'rng') to retrieve it.

See Also

foreach, doParallel , registerDoParallel, doMPI

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
library(doParallel)
cl <- makeCluster(2)
registerDoParallel(cl)

# standard %dopar% loops are _not_ reproducible
set.seed(1234)
s1 <- foreach(i=1:4) %dopar% { runif(1) }
set.seed(1234)
s2 <- foreach(i=1:4) %dopar% { runif(1) }
identical(s1, s2)

# single %dorng% loops are reproducible
r1 <- foreach(i=1:4, .options.RNG=1234) %dorng% { runif(1) }
r2 <- foreach(i=1:4, .options.RNG=1234) %dorng% { runif(1) }
identical(r1, r2)
# the sequence os RNG seed is stored as an attribute
attr(r1, 'rng')

# stop cluster
stopCluster(cl)

# More examples can be found in demo `doRNG`
## Not run: 
demo('doRNG')

## End(Not run)

doRNG documentation built on May 2, 2019, 4:43 p.m.