HenrikBengtsson / doFuture

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

doRNG warning on: Foreach loop had changed the current RNG type: RNG was restored to same type, next state #42

Closed HenrikBengtsson closed 4 years ago

HenrikBengtsson commented 4 years ago

Issue

With

library(future)
library(doFuture)
registerDoFuture()
library(doRNG)

we get a warning if we do:

> y <- foreach(x = 1:2) %dorng% { rnorm(1); x }
Warning message:
In foreach(x = 1:2) %dorng% { :
  Foreach loop had changed the current RNG type: RNG was restored to same type, next state

or

registerDoRNG()
y <- foreach(x = 1:2) %dopar% { rnorm(1); x }
Warning message:
In list(args = (1:2)(), argnames = "x", evalenv = <environment>,  :
  Foreach loop had changed the current RNG type: RNG was restored to same type, next state

Workaround

Setting the RNG kind to "L'Ecuyer-CMRG" upfront avoids this warning;

> RNGkind("L'Ecuyer-CMRG")
> y <- foreach(x = 1:2) %dorng% { rnorm(1); x }
>

Session info

> sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] doRNG_1.7.1      rngtools_1.4     pkgmaker_0.27    registry_0.5-1  
[5] doFuture_0.8.2   iterators_1.0.12 foreach_1.4.7    globals_0.12.5  
[9] future_1.15.1   

loaded via a namespace (and not attached):
 [1] codetools_0.2-16 listenv_0.8.0    digest_0.6.23    withr_2.1.2     
 [5] xtable_1.8-4     magrittr_1.5     bibtex_0.4.2.2   stringi_1.4.3   
 [9] tools_3.6.2      stringr_1.4.0    compiler_3.6.2 

Troubleshooting / Action

This most likely originates from the future package.

HenrikBengtsson commented 4 years ago

This warning appears only with plan(sequential).

HenrikBengtsson commented 4 years ago

Aha. It is not any of the future packages that updated the RNG kind, it is actually doRNG itself that sets it in the same main global environment that is checked when we run in sequential mode. The same would have happened with:

foreach::registerDoSEQ()

if it would be for doRNG is hardcoded to ignore that case;

            rng_type_changed <- !identical(RNGtype(), RNGtype(RNG.old))
            known_changing_cases <- is.null(dp) || dp=='doSEQ' || dp=='doMPI'
            if( known_changing_cases || rng_type_changed){
                if( rng_type_changed && !known_changing_cases ){
                    warning("Foreach loop had changed the current RNG type: RNG was restored to same type, next state")
                }else{
                    message("* Detected known RNG side effect: ", dp)
                }
                message("* Restoring RNG as after RNG sequence generation")
                if( verbose ) showRNG(RNG.old, indent = "  -")
                RNGseed(RNG.old)
                message("OK")
            }
HenrikBengtsson commented 4 years ago

Requested doFuture to be added to the doRNG ignore list (https://github.com/renozao/doRNG/issues/14).

HenrikBengtsson commented 4 years ago

doRNG now had as an option for registering doFuture to be a recognized backend.

HenrikBengtsson commented 4 years ago

Using doFuture no longer produces that warning by having registerDoFuture() set option doRNG.rng_change_warning_skip introduced in doRNG (>= 1.8.2);

library(foreach)
doFuture::registerDoFuture()
library(doRNG)
y <- foreach(x = 1:2) %dorng% { rnorm(1); x }