我有这个可重复的R片段
rm(list=ls())
library(doSNOW)
f <- function(a, b) a+b
g <- function(c) f(c*c, c+c)
v <- c(1, 2, 3, 4, 5, 6)
cl <- makeMPIcluster(1)
cat( clusterApply(cl, v, g) )
stopCluster(cl)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
Error in checkForRemoteErrors(val) :
6 nodes produced errors; first error: could not find function "f"
Run Code Online (Sandbox Code Playgroud)
我在Ubuntu下使用R 2.14.1.MPI已安装并正常运行.
我知道foreach构造存在类似的问题,但它允许通过.export参数手动引用函数.我找不到与clusterApply类似的东西.这有解决方法吗?
谢谢!
Cha*_*lie 17
您的功能未发送给工作人员.也许最好的方法是直接导出函数:
clusterExport(cl, list("f", "g"))
Run Code Online (Sandbox Code Playgroud)
小智 9
我认为你的问题与"变量范围"有关.在Mac/Linux上,您可以选择使用自动包含所有环境变量的makeCluster(no_core,type ="FORK").在Windows上,您必须使用并行套接字群集(PSOCK),该群集仅从加载的基础软件包开始.因此,您总是准确指定哪些变量以及您包含的并行函数库.clusterExport()和clusterEvalQ()是必需的,以便函数分别查看所需的变量和包.请注意,忽略clusterExport之后对变量的任何更改.回到你的问题.您必须使用以下内容:
rm(list=ls())
library(doSNOW)
f <- function(a, b) a+b
g <- function(c) f(c*c, c+c)
v <- c(1, 2, 3, 4, 5, 6)
cl <- makeMPIcluster(1)
# insert code here
clusterExport(cl, list("f", "g"))
# insert clusterEvalQ(cl, library(...)) if you need library for function to parallel
cat( clusterApply(cl, v, g) )
stopCluster(cl)
Run Code Online (Sandbox Code Playgroud)