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)