R 函数内的并行化——避免将对象自动导出到集群
Parallelisation within a function in R -- avoiding automatic export of objects to cluster
在 R 中,我希望能够使用并行包创建 psock 集群,并避免(我认为设计得很糟糕的)函数中的所有对象都自动导出到集群的行为(当 运行 来自 globalenv,没有对象被导出)。我希望能够通过从 globalenv 进行评估来做到这一点,但是正如您将在 testfunc2() 中看到的那样,这是行不通的。
我想这样做是为了避免我的函数中不必要的序列化开销。在此测试函数中,花费的时间可以忽略不计,我有一些应用程序是不可忽略的。
我之前在这里问过一个相关问题:
我在评论中发布的怪异技巧足以解决该问题,但我仍然认为/希望我现在要问的 'should' 是可能的。
library(parallel)
#all works as expected in the global environment
bob <- 4
cl=makeCluster(2,type='PSOCK')
clusterCall(cl,function(x) bob) #error initially
clusterExport(cl,'bob')
clusterCall(cl,function(x) bob) #but fine after explicitly sending bob to cl
stopCluster(cl)
rm(bob)
#inside a function, parallelisation is madness
testfunc <- function(){
bob <- 4
cl=makeCluster(2,type='PSOCK')
x <- clusterCall(cl,function(x) bob) #I want this to generate: Error: object 'bob' not found
stopCluster(cl)
return(x)
}
testfunc()
#tried evaluating in globalenv, setting cluster to globalenv, passing text string...
testfunc2 <- function(){
bob <- 4
cl=eval(makeCluster(2,type='PSOCK'),envir = globalenv())
environment(cl) <- globalenv()
x <- eval(clusterCall(cl,function(x) eval(parse(text='bob'))),envir = globalenv())
stopCluster(cl)
return(x)
}
testfunc2()
好的,我在上面尝试的不同变体起作用了:
testfunc3 <- function(doexport=FALSE){
bob <- 4
jane <- 7
cl=makeCluster(2,type='PSOCK')
if(doexport) clusterExport(cl,'bob',envir = environment())
x <- clusterCall(cl,function(x) eval(parse(text='bob'),envir=globalenv()))
x <- clusterCall(cl,function(x) eval(parse(text='jane'),envir=globalenv()))
stopCluster(cl)
return(x)
}
testfunc3()
testfunc3(TRUE) #now only errors on jane, which is not exported
在 R 中,我希望能够使用并行包创建 psock 集群,并避免(我认为设计得很糟糕的)函数中的所有对象都自动导出到集群的行为(当 运行 来自 globalenv,没有对象被导出)。我希望能够通过从 globalenv 进行评估来做到这一点,但是正如您将在 testfunc2() 中看到的那样,这是行不通的。 我想这样做是为了避免我的函数中不必要的序列化开销。在此测试函数中,花费的时间可以忽略不计,我有一些应用程序是不可忽略的。
我之前在这里问过一个相关问题:
library(parallel)
#all works as expected in the global environment
bob <- 4
cl=makeCluster(2,type='PSOCK')
clusterCall(cl,function(x) bob) #error initially
clusterExport(cl,'bob')
clusterCall(cl,function(x) bob) #but fine after explicitly sending bob to cl
stopCluster(cl)
rm(bob)
#inside a function, parallelisation is madness
testfunc <- function(){
bob <- 4
cl=makeCluster(2,type='PSOCK')
x <- clusterCall(cl,function(x) bob) #I want this to generate: Error: object 'bob' not found
stopCluster(cl)
return(x)
}
testfunc()
#tried evaluating in globalenv, setting cluster to globalenv, passing text string...
testfunc2 <- function(){
bob <- 4
cl=eval(makeCluster(2,type='PSOCK'),envir = globalenv())
environment(cl) <- globalenv()
x <- eval(clusterCall(cl,function(x) eval(parse(text='bob'))),envir = globalenv())
stopCluster(cl)
return(x)
}
testfunc2()
好的,我在上面尝试的不同变体起作用了:
testfunc3 <- function(doexport=FALSE){
bob <- 4
jane <- 7
cl=makeCluster(2,type='PSOCK')
if(doexport) clusterExport(cl,'bob',envir = environment())
x <- clusterCall(cl,function(x) eval(parse(text='bob'),envir=globalenv()))
x <- clusterCall(cl,function(x) eval(parse(text='jane'),envir=globalenv()))
stopCluster(cl)
return(x)
}
testfunc3()
testfunc3(TRUE) #now only errors on jane, which is not exported