Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproduce Serial Loop Output with Default RNGkind #24

Open
keatonwilson opened this issue Feb 28, 2024 · 0 comments
Open

Reproduce Serial Loop Output with Default RNGkind #24

keatonwilson opened this issue Feb 28, 2024 · 0 comments

Comments

@keatonwilson
Copy link

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!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant