Scholar
Scholar

Reputation: 512

R: doParallel (FORK), foreach and random number generation

When running a foreach loop using the doParallel package in R and FORK, each worker will start off with the same random seed thus leading to duplicate 'random' numbers.

set.seed(1)
cl <- makeCluster(2, type = "FORK")
registerDoParallel(cl)
foreach(1:4, .combine = 'c') %dopar% {rnorm(1, mean = 0, sd = 1)}
stopImplicitCluster()

[1] -0.6264538 -0.6264538  0.1836433  0.1836433

What is the best way to solve this?

Right now, I work around this problem by setting a new seed during every loop iteration, i.e.

cl <- makeCluster(2, type = "FORK")
registerDoParallel(cl)
foreach(i = 1:4, .combine = 'c') %dopar% {
  set.seed(i)
  rnorm(1, mean = 0, sd = 1)}
stopImplicitCluster()

[1] -0.8969145 -0.9619334  0.2167549 -0.8408555

Upvotes: 2

Views: 1868

Answers (1)

Ralf Stubner
Ralf Stubner

Reputation: 26833

You can make use of doRNG to register an additional foreach backend for independent and reproducible random numbers:

library(doParallel)
library(doRNG)
cl <- makeCluster(2, type = "FORK")
registerDoParallel(cl)
registerDoRNG(seed = 123)
foreach(i=1:4, .combine = 'c') %dopar% {rnorm(1, mean = 0, sd = 1)}
stopImplicitCluster()

Result:

[1]  0.4254817 -0.8817684 -0.4448349 -1.7773268
attr(,"rng")
attr(,"rng")[[1]]
[1]         407   642048078    81368183 -2093158836   506506973  1421492218 -1906381517

attr(,"rng")[[2]]
[1]         407  1340772676 -1389246211  -999053355  -953732024  1888105061  2010658538

attr(,"rng")[[3]]
[1]         407 -1318496690  -948316584   683309249  -990823268 -1895972179  1275914972

attr(,"rng")[[4]]
[1]         407   524763474  1715794407  1887051490 -1833874283   494155061 -1221391662

Note that it is important here to name the iterator variable, even if it is not used within the foreach body.

Upvotes: 4

Related Questions