如何在foreach循环中进行clusterExport(操作系统:Windows)

时间:2017-08-27 01:30:50

标签: r windows parallel-processing environment-variables

我有以下问题。我需要在全局环境中使用变量的时间索引,但是当我想在从全球环境进行并行处理时将它们导出到我的群集中时,我收到以下消息:

错误{:任务1失败 - "对象' Szenario'找不到"

我的原始代码的最小示例,它产生错误:

    Historical <- structure(c(18.5501872568473, 24.3295787432955, 14.9342384460288, 
    13.0653757599636, 8.67294618896797, 13.4587662721594, 20.792126254714, 
    17.5162747884424, 28.8253151239752, 23.0568765432192), index = structure(c(-7305, 
    -7304, -7303, -7302, -7301, -7300, -7299, -7298, -7297, -7296
    ), class = "Date"), class = "zoo")

    Szenario <- structure(c(10.2800684124652, 14.5495871489361, 9.8565852930294, 
    21.1654540678686, 21.1936990312861, 12.4209005842752, 9.77473132000267, 
    17.1997402736739, 17.884107611858, 13.622588360244), index = structure(c(13149, 
    13150, 13151, 13152, 13153, 13154, 13155, 13156, 13157, 13158
    ), class = "Date"), class = "zoo")

    library(doParallel)   
    library(foreach)     
    library(raster)
    library(zoo)
    library(parallel)

    # Parallelisation Settings
    # Definition of how many cores you want to use
    UseCores <- detectCores() -2 # -1 at max because one core has to be used for other tasks 
    # Register CoreCluster
    cl <- makeCluster(UseCores)
    registerDoParallel(cl)

foreach(fn=1:1) %dopar% {

  library(raster) # needed libraries have to be loaded inside the loop, while parallel processing occurs
  library(zoo)
  library(base)
  library(parallel)

  #In my original script, I'm looping through Filenames, which are called like my variables in my Global environment (without .tif at the end of the filename), variables names are saved as characters
  file.referenz.name <- c("Historical")
  file.szenario.name <- c("Szenario")

  #Create timeindex für rasterstacks to subset later on with them (getZ, setZ)

  clusterExport(cl, varlist = c(file.szenario.name, file.referenz.name), envir = .GlobalEnv)
  time.index.szenario <- index(get(file.szenario.name))
  time.index.referenz <- index(get(file.referenz.name))

}

#end cluster
stopCluster(cl)

1 个答案:

答案 0 :(得分:1)

试试这个

foreach(fn=1:1, .export=c("Szenario"), 
                .packages=c("raster", "zoo", "base", "parallel") %dopar%

它让clusterExport内的%dopar% {}感到困惑。您可以在clusterExport之前clforeach,或者只使用.export中的foreach

您可以删除library中的%dopar% {}语句。