Reputation: 205
I have written a script that runs fine, but it doesn't not appear to be doing the parallel processing. I tried changing the cores from 3 to 16 but the speed at which the data was being generated did not change. Can anyone let me know what I am doing wrong and how I can get this to work?
setwd("E:/Infections")
if (!require("pacman")) install.packages("pacman")
pacman::p_load(lakemorpho,rgdal,maptools,sp,doParallel,foreach,
doParallel)
cl <- makeCluster(5, outfile="E:/Infections/debug.txt")
registerDoParallel(cl)
x<-readOGR("E:/Infections/ByHUC6","Kodiak")
x_lake_length<-vector("numeric",length = nrow(x))
for(i in 1:nrow(x)){
tmp<-lakeMorphoClass(x[i,],NULL,NULL,NULL)
x_lake_length[i]<-lakeMaxLength(tmp,200)
print(i)
Sys.sleep(0.1)
}
df_Kodiak <- data.frame(x_lake_length)
write.table(df_Kodiak,file="E:/Infections/ByHUC6/Kodiak.csv",row.names=TRUE,col.names=TRUE, sep=",")
Upvotes: 4
Views: 460
Reputation: 23231
Alright, I think I got it by invoking foreach
and %dopar%
:
# Libraries ---------------------------------------------------------------
if (!require("pacman")) install.packages("pacman")
pacman::p_load(lakemorpho,rgdal,maptools,sp,doParallel,foreach,
doParallel)
# Data --------------------------------------------------------------------
ogrDrivers()
dsn <- system.file("vectors", package = "rgdal")[1]
ogrListLayers(dsn)
ogrInfo(dsn=dsn, layer="trin_inca_pl03")
owd <- getwd()
setwd(dsn)
ogrInfo(dsn="trin_inca_pl03.shp", layer="trin_inca_pl03")
setwd(owd)
x <- readOGR(dsn=dsn, layer="trin_inca_pl03")
summary(x)
# HPC ---------------------------------------------------------------------
cores_2_use <- detectCores() - 4
cl <- makeCluster(cores_2_use, useXDR = F)
clusterSetRNGStream(cl, 9956)
registerDoParallel(cl, cores_2_use)
# Analysis ----------------------------------------------------------------
myfun <- function(x,i){tmp<-lakeMorphoClass(x[i,],NULL,NULL,NULL)
x_lake_length<-vector("numeric",length = nrow(x))
x_lake_length[i]<-lakeMaxLength(tmp,200)
print(i)
Sys.sleep(0.1)}
foreach(i = 1:nrow(x),.combine=cbind,.packages=c("lakemorpho","rgdal")) %dopar% (
myfun(x,i)
)
df_Kodiak <- data.frame(x_lake_length)
As you can see in the screenshot below this creates an army of Rscript.exe processes using 20 of 24 CPU cores. Of course, the example data I used is small so it didn't really need all those cores, but it should serve as a proof of concept.
I never go above that ratio because if you use 100% of all CPU cores sometimes bad things happen and other server users may not be happy with you.
Upvotes: 2