将全局和功能环境中的对象导出到群集

时间:2016-12-28 07:38:28

标签: r

我在下面的示例代码中使用了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"""**

我的问题是如何将全局和功能环境中的所有对象导出到集群。

1 个答案:

答案 0 :(得分:0)

library(doParallel)
clusters =  makeCluster(4)
registerDoParallel(clusters)


fun1 <- function(param1, param2, param3)
{
  param4 = param1+param2
  param5 = param2+param3
  param6 = param3+param1   

  mmm = foreach(i = 1:length(param1), .combine = rbind, .export = "fun2") %dopar% fun2(i, param4, param5, param6)
  mmm
}

fun2 <- function(i, param4, param5, param6)
{
  param4[i] * param5[i] * param6[i]
}

param1 = 1:10
param2 = 2:11
param3 = 3:12
fun1(param1, param2, param3)
#           [,1]
#result.1     60
#result.2    210
#result.3    504
#result.4    990
#result.5   1716
#result.6   2730
#result.7   4080
#result.8   5814
#result.9   7980
#result.10 10626