-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Description
the following code return the correct result
p1=function(x)x+ONE
doParallel::registerDoParallel(parallel::makeForkCluster(2)) # register doParallelSNOW
foreach::`%dopar%`(foreach::foreach(i=1:5) , {ONE=1;p1(i)})
but if we change parallel::makeForkCluster(2) to 2, it generate an error, Error in { : task 1 failed - "object 'ONE' not found"
p1=function(x)x+ONE
doParallel::registerDoParallel(2) # register doParallelMC
foreach::`%dopar%`(foreach::foreach(i=1:5) , {ONE=1;p1(i)})
the root cause it that, doParallelMC calls mclapply directly, which create an extra environmnet and thus failed to execute. minimal example:
f=function(x)x+ONE
g=function(y){
ONE=1 # here, ONE is assigned in g's environment, not the global environment
f(y) # here, f would search ONE firstly in its closure, and then search it directly in the global environment, thus it could not found `ONE` which is defined in g's environment.
}
g(1) # could not found ONE
y=1
eval(body(g)) # we could solve the question by eval body here.
Should there be some document about this thing?
Metadata
Metadata
Assignees
Labels
No labels