HenrikBengtsson / doFuture

:rocket: R package: doFuture - Use Foreach to Parallelize via Future Framework
https://doFuture.futureverse.org
84 stars 6 forks source link

Correlated random numbers in nested loops #74

Open lbau7 opened 1 year ago

lbau7 commented 1 year ago

I saw this bug on future.apply's GitHub page [HB edit: https://github.com/HenrikBengtsson/future.apply/issues/108] and checked whether the same issue exists with dofuture-loops, since this is something that I used in an R-package that I'm currently developing. Is this the same bug or am I doing something wrong?

Describe the bug When random numbers are generated in a nested loop using the %dofuture% adapter many random numbers appear several times

Reproduce example library(doFuture) plan(multisession, workers = 4) foreach::foreach(i = 1:4,.combine = 'cbind') %dofuture% { b <- foreach::foreach(j = 1:4) %dofuture% { runif(1) } %seed% TRUE b } %seed% TRUE

Expected behavior Random numbers should be unique.

Session information R version 4.2.1 (2022-06-23 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale: [1] LC_COLLATE=German_Germany.utf8 LC_CTYPE=German_Germany.utf8 LC_MONETARY=German_Germany.utf8 [4] LC_NUMERIC=C LC_TIME=German_Germany.utf8

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] doFuture_1.0.0 future_1.32.0 foreach_1.5.2

loaded via a namespace (and not attached): [1] compiler_4.2.1 parallelly_1.35.0 parallel_4.2.1 tools_4.2.1 future.apply_1.10.0 [6] listenv_0.9.0 codetools_0.2-18 iterators_1.0.14 digest_0.6.31 globals_0.16.2

HenrikBengtsson commented 11 months ago

checked whether the same issue exists with dofuture-loops, ...

Yes, this is exactly the same problem. You'd find it with furrr too. You're not doing anything wrong. It's a fundamental, important problem that needs some seriously deep focus to be solved properly. When solved for future.apply, it'll be solved elsewhere too.