我在下面的示例代码中使用了 foreach 和 doParallel。
library(foreach)
library(doParallel)
clusters = makeCluster(4)
registerDoParallel(clusters)
fun1 <- function(param1, param2, param3)
{
param4 = param1+param2
param5 = param2+param3
param6 = param3+param1
print(ls(envir = environment()))
print(ls(envir = .GlobalEnv))
# clusterExport(cl = clusters, varlist = ls(), envir = environment())
clusterExport(cl = clusters, varlist = ls(), envir = .GlobalEnv)
mmm = foreach(i = 1:length(param1), .combine = rbind) %dopar% fun2(i, param4, param5, param6)
print(mmm)
}
fun2 <- function(i, param4, param5, param6)
{
j = param4[i] * param5[i] * param6[i]
}
param1 = 1:10
param2 = 2:11
param3 = 3:12
fun1(param1, param2, param3)
在 clusterExport 的 envir 参数中,当我使用envir = .GlobalEnv时,出现错误
**"Error in get(name, envir = envir) : object 'param4' not found"**
.
当我使用envir = environment()时,错误是
**"Error in fun2(i, param4, param5, param6) :
task 1 failed - "could not find function "fun2"""**
我的问题是如何将全局和功能环境中的所有对象导出到集群。