parallel::clusterExport how to pass nested functions from global environment?

谁都会走 提交于 2019-12-22 18:04:25

问题


I'm making a function (myFUN) that calls parallel::parApply at one point, with a function yourFUN that is supplied as an argument.

In many situations, yourFUN will contain custom functions from the global environment.

So, while I can pass "yourFUN" to parallel::clusterExport, I cannot know the names of functions inside it beforehand, and clusterExport returns me an error because it cannot find them.

I don't want to export the whole enclosing environment of yourFUN, since it might be very big.

Is there a way for me to export only the variables necessary for running yourFUN?

The actual function is very long, here is a minimized example of the error:

mydata <- matrix(data = 1:9, 3, 3)

perfFUN <- function(x) 2*x

opt_perfFUN <- function(y) max(perfFUN(y))

avg_perfFUN <- function(w) perfFUN(mean(w))

myFUN <- function(data, yourFUN, n_cores = 1){

  cl <- parallel::makeCluster(n_cores)
  parallel::clusterExport(cl, varlist = c("yourFUN"), envir = environment())

  parallel::parApply(cl, data, 1, yourFUN)

}

myFUN(data = mydata, yourFUN = opt_perfFUN)
myFUN(data = mydata, yourFUN = avg_perfFUN)

 Error in checkForRemoteErrors(val) : one node produced an error: could not find function "perfFUN" 

Thank you very much!


回答1:


A possible solution, use:

myFUN <- function(data, yourFUN, n_cores = 1) {

  cl <- parallel::makeCluster(n_cores)
  on.exit(parallel::stopCluster(cl), add = TRUE)

  envir <- environment(yourFUN)
  parallel::clusterExport(cl, varlist = ls(envir), envir = envir)

  parallel::parApply(cl, data, 1, yourFUN)  
}


来源:https://stackoverflow.com/questions/48292531/parallelclusterexport-how-to-pass-nested-functions-from-global-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!