2

I am using Maxent in R for performing species distribution modeling. My problem is that raster::predict() is very slow because of the resolution and extent of my environmental data. Is there a way to parallel process the prediction step to speed this up? At the current time it is running for days and this creates problems.

2 Answers 2

3

You could split your raster brick/stack along the y-axis and pass the subsets to different CPUs using foreach:

#load raster brick
filename = "predictors"
predictors = raster::brick(filename)

#register parallel computing backend
ncores=4
cl = parallel::makeCluster(ncores)
doParallel::registerDoParallel(cl,cores)

#compute indices for data splitting
rows = 1:nrow(predictors)
split = sort(rows%%ncores)+1

outname = "prediction"

#perform the prediction on subsets of the predictor dataset
prediction = foreach(i=unique(split), .combine=c)%dopar%{
  rows_sub = rows[split==i]
  sub = raster::crop(predictors,raster::extent(predictors, min(rows_sub), max(rows_sub), 1, ncol(predictors)))
  raster::predict(sub, model, filename=paste(outname, i, sep="_"))
}

This will write different files with a suffix "_1" to "_{ncores}" which can then easily mosaicked back together along the y-axis.

1
  • Thanks. This worked perfectly. However, it does eat up a lot of hard drive space and that is causing me some problems. I am going to ask a new question related to the temporary directory for raster().
    – user44796
    Aug 17, 2016 at 13:23
2

using clusterR from the raster package:

beginCluster()
preds_rf<- clusterR(rast, raster::predict, args = list(model = model))
endCluster()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.