The code below is a parallel version of a some code I had written earlier. Both versions do what they are supposed to. Both versions take about 5.7 minutes to complete. Constructing a reproducible example would require me to make several tif files available so I haven't done that yet. I'm hoping some expert in using doParallel can point out some code changes that will make doParallel work faster.
library(doParallel) #Foreach Parallel Adaptor
library(foreach) #Provides foreach looping construct
rcpChoices <- c("rcp45", "rcp85")
modelChoices <- c("HadGEM2-ES", "GFDL-ESM2M", "MIROC5", "IPSL-CM5A-LR")
comboChoices <- c("midCentury", "endCentury")
bpListRaw <- data.table::as.data.table(read.csv("data-raw/animals/breakpointslistRaw.csv", stringsAsFactors = FALSE))
bpListRaw[, (c("max", "model", "period", "rcp")) := as.character(max, model, period, rcp)]
thiList <- c("thi.cattle", "thi.sheep", "thi.goat", "thi.yak", "thi.broiler", "thi.layer", "thi.chicken", "thi.swine")
bpList.modMean <- bpListRaw[0,]
bpTemp <- bpListRaw
#Use foreach loop and %dopar% command
#Register CoreCluster
#Define how many cores you want to use
# generate mean and the standard deviation values across the earth system models (four for the ISIMIP data)
comboChoicesP <- comboChoices[!comboChoices %in% "observed"]
modelChoicesP <- modelChoices[!modelChoices %in% "modMean"]
UseCores <- detectCores() - 1
cl <- makeCluster(UseCores)
registerDoParallel(cl)
clusterEvalQ(cl,{
library(raster)
library(data.table)})
clusterExport(cl, varlist = c("modelChoicesP", "month.abb", "thiList", "bpTemp"))
start_time <- Sys.time()
x <- foreach(l = comboChoicesP) %:%
foreach(j = rcpChoices) %:%
foreach(k = thiList) %dopar% {
modelChoices_short <- unlist(lapply(strsplit(modelChoicesP, "-"), `[[`, 1))
speciesName <- gsub("thi.", "", k)
for (i in 1:length(modelChoicesP)) {
fileName <- paste0("results/", k, "_", modelChoicesP[i], "_", l, "_", j, ".tif")
rasterName <- paste0("thinameMod", i)
rtemp <- raster::stack(fileName)
names(rtemp) <- month.abb
assign(rasterName, rtemp)
}
ras.test <- raster::stack(thinameMod1, thinameMod2, thinameMod3, thinameMod4)
raster::values(ras.test)[raster::values(ras.test) < 0] = 0
indices <- format(as.Date(names(ras.test), format = "%b.%d"), format = "%m")
indices <- as.numeric(indices)
ras.test.mean <- raster::stackApply(ras.test, indices, fun = mean, na.rm = TRUE)
ras.test.sd <- raster::stackApply(ras.test, indices, fun = sd, na.rm = TRUE)
names(ras.test.mean) <- month.abb
names(ras.test.sd) <- month.abb
fileNameMean <- paste0("results/modMean", "_", k, "_", l, "_",j, ".tif")
fileNameSD <- paste0("results/modSD", "_", k, "_", l, "_",j, ".tif")
writeRaster(ras.test.mean, filename = fileNameMean, format = "GTiff", overwrite = TRUE)
writeRaster(ras.test.sd, filename = fileNameSD, format = "GTiff", overwrite = TRUE)
temp <- bpTemp[species %in% speciesName, ]
maxVal <- ceiling(max(maxValue(ras.test.mean)))
temp[, (c("max", "model", "period", "rcp")) := list(maxVal, "modMean", l, j)]
# bpList.modMean <- rbind(bpList.modMean, temp)
}
end_time <- Sys.time()
end_time - start_time
#end cluster
stopCluster(cl)