I would like to understand the "doParallel" package more. Im playing around with environments. I would like to create a global environment .someEnv<-new.env(parent = emptyenv())
outside of foreach() %dopar%{…}
I know that foreach() needs to import all packages, functions, values and data to the foreach()
-function using .export= “”
and .packages=””
.
My question is, is there a ways to import a Global environment into foreach()
, read and write into this environment and use it as a way to cache calculations? (please no Comment on cacheing calculations using .Rdata, .RDS, .feather etc)
here is an example:
require(doParallel)
library(doSNOW)
getDoParWorkers()
getDoParName()
cl<-makeCluster(4, type = "SOCK")
registerDoSNOW(cl)
getDoParWorkers()
getDoParName()
#define environment
.someEnv<-new.env(parent = emptyenv())
.someEnv$var<-1:10
.someEnv$squared<-matrix(nrow=10)
#define Function for "foreach"
do.something<-function(x)
{
.someEnv$squared[x]<-.someEnv$var[x] *.someEnv$var[x]
return(.someEnv$squared[x])
}
foreach(i=1:10) %dopar% do.something(i)
stopCluster(cl)
Error Message:
Error in do.something(i) :
task 1 failed - "Objekt '.someEnv' nicht gefunden"