Skip to content

Reproduce Serial Loop Output with Default RNGkind  #24

@keatonwilson

Description

@keatonwilson

Hi there - thanks for all the work on this package! I'm working on adapting some legacy code using base R loops (and the default RNG type) to have parallel implementation (solid speed increases). Unfortunately, I need to replicate the outputs from previous runs of the code, and can't just swap over to L'Ecuyer-CMRG. From the documentation, it looks like it may be possible to replicate the old code by providing seeds explicitly:

.options.RNG with complete sequence of seeds: the complete description of the sequence of seeds to be used may be passed via options.RNG, as a list or a matrix
with the seeds in columns. This is useful to seed a loop exactly as desired, e.g. using
an RNG other than "L’Ecuyer-CMRG", or using different RNG kinds in each iteration, which probably have different seed length, in order to compare their stochastic
properties. It also allows to reproduce %dorng% loops without knowing their seeding
details:

But I can't seem to figure out the appropriate way to do this. Some example code:

library(doParallel)
library(foreach)
library(doSNOW)
library(doRNG)

# Traditional Seed and For Loop Approach ----------------------------------

set.seed(42)
out_1 <- list()

for(i in 1:3) {
  out_1[[i]] <- runif(3)
}

# foreach in serial -------------------------------------------------------

set.seed(42)

out_2 <- foreach(i = 1:3) %dopar% {
  runif(3)
}

identical(out_1, out_2)

# foreach in parallel -----------------------------------------------------

cluster <- snow::makeCluster(2)
doSNOW::registerDoSNOW(cluster)

# this is where I tried manually generating a seed matrix/list, but couldn't come 
# up with something that generated matching output

out_3 <- foreach(i = 1:3, options.RNG = manual_list) %dorng% {
  runif(3)
}

snow::stopCluster(cluster)
identical(out_1, out_3[1:3])

Any help you could provide would be amazing - and again, thanks so much for this package (and for the great vignette!).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions