Last active
August 29, 2017 12:38
-
-
Save valentinitnelav/5759083f60eb7a7141f32466fe94d17a to your computer and use it in GitHub Desktop.
closest geodesic distance with doParallel and foreach - example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of using foreach parallel loop | |
# This should work both on Linux and Windows without any modifications. | |
# for an interesting intro, check: | |
# http://bgc.yale.edu/sites/default/files/ParallelR.html | |
library(foreach) | |
library(doParallel) | |
# Prepare cluster | |
FractionCores=0.7 # fraction of cores to use | |
numCores <- round(FractCores * parallel::detectCores()) | |
cl <- parallel::makeCluster(numCores) | |
doParallel::registerDoParallel(cl) | |
system.time({ | |
res <- | |
foreach( | |
i = 1:length(PLdt_SPdf_NA), | |
.combine = rbind, # how to combine results (the default is a list) | |
.multicombine = TRUE, # if .combine = rbind (above) then .multicombine defaults to TRUE anyway (so redundant here) | |
.packages = c("geosphere"), # packages to load during parallel execution. This should be done even if you call geosphere::dist2Line below | |
.options.multicore = list(preschedule=FALSE) # load balancing (only if needed, default is TRUE) | |
) %dopar% | |
{ | |
geosphere::dist2Line(p = PLdt_SPdf_NA[i,], | |
line = poly.sp.rich, | |
distfun = distHaversine) | |
# if you have multiple operations here, it will return only the last assigned variable | |
} | |
}) | |
stopCluster(cl) | |
# Note the fact that the foreach() loop returns object(s), so assign the foreach to a variable name like res <- foreach(...), | |
# otherwise you'll lost the results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment