[R-sig-Geo] How to parallize my code?
Bastien Ferland-Raymond
Bastien.Ferland-Raymond at mrn.gouv.qc.ca
Mon Feb 3 14:21:08 CET 2014
Hello Ping,
Your code is very long and I saddly don't have time to read it all.
However, I'll show you one short example of my favourite way to parallelise.
It may not be the best, but it works fine.
The trick is to replace your "for" loop by a function and call this function
with a parLapply.
Here is my example:
## function to calculate mean and sd of rapidEye satellite band:
stat.par.bande <- function(band, stack.re){
library(raster); library(rgdal)
re <- stack.re[[band]]
re.agg.mean <- aggregate(re,4, fun=mean)
re.agg.sd <- aggregate(re,4, fun=sd)
stack(re.agg.mean, re.agg.sd)
}
##
## and then run it.
library(parallel)
cl <- makeCluster(6)
res.lap <- parLapply(cl,1:nlayers(the.re), stat.par.bande,the.re)
res.stat <- do.call("stack",res.lap)
stopCluster(cl)
###############
Important points to remember: You have to pass through every objects and
functions used by your main function (in my case "stat.par.bande()") into
your worker. You also have to call your packages.
Good luck,
Bastien
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/How-to-parallize-my-code-tp7585672p7585676.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
More information about the R-sig-Geo
mailing list