最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Snow Package Stop Cluster and Progress Bar Not Working - Stack Overflow

programmeradmin1浏览0评论

I am using the Snow package to run some DEM modifications in Parallel as I have lots of DEMS that I need to reproject. I am having an issue where my cluster does not stop when I run the "stopCluster" command after all of the files that I need are reprojected. I assume that I am doing something incorrectly with the indexing so the cluster thinks it needs to keep going when it has done everything that I need. I am also having an issue with my progress bar where it is showing no progress even though the task is getting completed.

I am at best intermediate with R and a novice with running in parallel, I have just adapted some code that a colleague showed me to make this process go faster.

I have confirmed that the current example does need to do 2760 tasks as that is how many .tif files I am reprojecting.

The data I am using can be downloaded from this website

I downloaded the DEMS using a script and converted them all into .tif files for my work.

I am using R version 4.4.2 and Rstudio version 2024.12.0 Build 467

file_paths <- list.files(path="DEM_Path", full.names=TRUE, pattern="\\.tif$")

Index <- c(1:2760)

custom_parallel_function <- function(x) {
  for (N in 1:length(file_paths)) {
    Read_DEM <- raster::raster(file_paths[[N]])
    Read_DEM <-  raster::projectRaster(Read_DEM, crs=32618)
    if (file.exists(paste0("Existing_DEMS", N, ".tif")) == TRUE) {
      next
    } else {
      raster::writeRaster(Read_DEM, 
                          filename=(paste0("Existing_DEMS", N, ".tif")),
                          format="GTiff", overwrite=TRUE)
    } 
  }
}   

ncores <- 6
#=NUMBER OF OPERATIONS (e.g. nrow(data))
noperations <- 2760

cl <- snow::makeCluster(ncores, type="SOCK")

doSNOW::registerDoSNOW(cl)

pb <- utils::txtProgressBar(max=noperations, style=3)

progress <- function(n) {
  utils::setTxtProgressBar(pb, n)
}

opts <- list(progress=progress)
foreach::foreach(
  INDEX=Index, 
  .options.snow=opts
) %dopar% {custom_parallel_function(INDEX)}

snow::stopCluster(cl)
发布评论

评论列表(0)

  1. 暂无评论