我试图了解optimParallel如何处理嵌入式功能:

fn1 <- function(x){x^2-x+1}
fn2 <- function(x){fn1(x)}     # effectively fn1

cl <- parallel::makeCluster(parallel::detectCores()-1)
parallel::setDefaultCluster(cl = cl)

optimParallel::optimParallel(par = 0, fn = fn1)         # Worked
optimParallel::optimParallel(par = 0, fn = fn2)         # Not working

parallel::setDefaultCluster(cl=NULL)
parallel::stopCluster(cl)


为什么第二个不工作?错误消息是

Error in checkForRemoteErrors(val) :
  3 nodes produced errors; first error: could not find function "fn1"


如何解决?

最佳答案

此问题特定于使用的群集类型:

如果在函数定义之后创建了FORK集群,则它可以工作。
FORK群集仅在类似Linux的系统上可用:

library(optimParallel)
fn1 <- function(x) x^2-x+1
fn2 <- function(x) fn1(x)
cl <- makeCluster(detectCores()-1, type="FORK")
setDefaultCluster(cl=cl)
optimParallel(par=0, fn=fn2)[[1]]
## [1] 0.5


对于其他群集类型,可以将fn1作为参数添加到fn2并将其作为...参数添加到optimParallel()

fn1 <- function(x) x^2-x+1
fn2 <- function(x, fn1) fn1(x)
cl <- makeCluster(detectCores()-1)
setDefaultCluster(cl=cl)
optimParallel(par=0, fn=fn2, fn1=fn1)[[1]]
## [1] 0.5


或者,可以将fn1导出到集群中的所有R进程:

fn1 <- function(x) x^2-x+1
fn2 <- function(x) fn1(x)
cl <- makeCluster(detectCores()-1)
setDefaultCluster(cl=cl)
clusterExport(cl, "fn1")
optimParallel(par=0, fn=fn2)[[1]]
## [1] 0.5

关于r - R optimParallel-找不到功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58601196/

10-12 20:34