Windows中将来的多进程并行

时间:2019-05-21 22:24:39

标签: r parallel-processing future raster terrain

我正在尝试实施并行过程,以将栅格数据转换为地形产品(如长宽比,坡度等)。 我通过以下代码使用Future进行操作:

dem = raster("./dem/dem.asc")
output = "./output/"
crs(dem) <- epsg

plan(multiprocess, workers = availableCores()-1, gc = TRUE)
f1 %<-% terrain(dem, filename = paste0(output,"01_slope.asc"), opt = "slope", unit = 'degrees', neighbors = 8)
f2 %<-% terrain(dem, filename = paste0(output,"02_aspect.asc"), opt = "aspect", unit = 'degrees', neighbors = 8)
f1; f2

这些过程是并行开始的,但是却有些奇怪:它同时生成两个文件,每个文件都有自己的名字,但是两个文件是完全相同的(在这种情况下,两个栅格都是倾斜栅格)。 我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我终于使用了此功能,并且有效:

dempath = "./dem/dem.asc"
output = "./output/"
crs(dem) <- epsg    

#CREATE DEM PRODUCTS IN PARALLEL
demproductspar <- function(dempath, output, epsg = crs('+init=epsg:25829'), cor = 3){

    print ('Leyendo dem...')
    dem = raster(dempath)
    crs(dem) <- epsg
    plan(multiprocess, workers = cor, gc = TRUE)
    plan(multiprocess, workers = availableCores(), gc = TRUE)

    f1 <- future({ terrain(dem, filename = paste0(output,"01_slope.asc"), opt = "slope", unit = 'degrees', neighbors = 8) })
    f2 <- future({
        terrain(dem, filename = paste0(output,"02_aspect.asc"), opt = "aspect", unit = 'degrees', neighbors = 8)
        aspectreclass(aspectdir = paste0(output,"02_aspect.asc"), output = paste0(output,"02r_aspect.asc")) })
    f3 <- future({ terrain(dem, filename = paste0(output,"03_roughness.asc"), opt = "roughness", unit = 'degrees', neighbors = 8) })
    f4 <- future({ terrain(dem, filename = paste0(output,"04_tri.asc"), opt = "tri", unit = 'degrees', neighbors = 8) })
    f5 <- future({ terrain(dem, filename = paste0(output,"05_tpi.asc"), opt = "tpi", unit = 'degrees', neighbors = 8) })

    print ('Procesando dem...')
    f1; f2; f3; f4; f5
    gc()
}

demproductspar(dempath, output)