Most (legacy) software is written for serial computation:
Figure from here
Figure from here
A classification of computer architectures (Flynn, 1972)
Figure from here
Rscript
There are many R packages for parallelization, check out the CRAN Task View on High-Performance and Parallel Computing for an overview. For example:
In this session we’ll focus on the foreach package, which has numerous advantages including:
for() loop-like syntaxmulticore, parallel, snow, Rmpi, etc.)fork on linux/mac and snow on windows).for loopsfor()## [1] 1 4 9
foreach()## [[1]]
## [1] 1
##
## [[2]]
## [1] 4
##
## [[3]]
## [1] 9
## [[1]]
## [1] 1
##
## [[2]]
## [1] 4
##
## [[3]]
## [1] 9
Note that x is a list with one element for each iterator variable (i). You can also specify a function to use to combine the outputs with .combine. Let’s concatenate the results into a vector with c.
foreach() loop with .combine## [1] 1 4 9
Tells foreach() to first calculate each iteration, then .combine them with a c(...)
foreach() loop with .combine## [,1]
## result.1 1
## result.2 4
## result.3 9
foreach() loopSo far we’ve only used %do% which only uses a single processor.
Must register a parallel backend with one of the do functions such as doParallel(). On most multicore systems, the easiest backend is typically doParallel(). On linux and mac, it uses fork system call and on Windows machines it uses snow backend. The nice thing is it chooses automatically for the system.
registerDoParallel(3) # register specified number of workers
#registerDoParallel() # or, reserve all all available cores
getDoParWorkers() # check registered cores## [1] 3
foreach() loopTo run in parallel, simply change the %do% to %dopar%. Wasn’t that easy?
## [1] 1 4 9
Most parallel computing:
i=1:3)%dopar%).combine)foreach parameters.inorder (true/false) results combined in the same order that they were submitted?.errorhandling (stop/remove/pass).packages packages to made available to sub-processes.export variables to export to sub-processesSome functions in the raster package also easy to parallelize.
Does not work with:
However, check out the spatial.tools package
rasterEngine executes a function on Raster* object(s) using foreach, to achieve parallel reads, executions and writes.
Each task should involve computationally-intensive work. If the tasks are very small, it can take longer to run in parallel.