doParallel

Packages

#install.packages("doParallel")
library("doParallel")
## Loading required package: foreach
## Loading required package: iterators
## Loading required package: parallel

Check how long a loop takes

start <- proc.time()
for (i in 1:10){
    sqrt(i)
}
base_loop=(proc.time()-start)

Check number of cores

library("doParallel")
detectCores()
## [1] 8
getDoParWorkers() #current number of worker
## [1] 1
registerDoSEQ() #switch back to original
getDoParWorkers()
## [1] 1

Let R know how many cores you want to use

registerDoParallel(6)

Note: You shouldn’t use all the cores since your computer might want some background programs to run.

We can also create cluster of workers

cluster = makeCluster(2)
registerDoParallel(cluster)
system.time(foreach(i=1:100) %dopar% sum((1:i)))
##    user  system elapsed 
##    0.03    0.00    0.04
stopCluster(cluster)

Compare different loops

%do%

start <- proc.time()
foreach(i=1:10) %do% {
    sqrt(i)
}
## [[1]]
## [1] 1
## 
## [[2]]
## [1] 1.414214
## 
## [[3]]
## [1] 1.732051
## 
## [[4]]
## [1] 2
## 
## [[5]]
## [1] 2.236068
## 
## [[6]]
## [1] 2.44949
## 
## [[7]]
## [1] 2.645751
## 
## [[8]]
## [1] 2.828427
## 
## [[9]]
## [1] 3
## 
## [[10]]
## [1] 3.162278
do_loop=(proc.time()-start)

%dopar% loop

cl <- makeCluster(2)
registerDoParallel(cl)
start <- proc.time()
foreach(i = 1:10, .combine = 'c') %dopar% {
  sqrt(i)
}
##  [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427
##  [9] 3.000000 3.162278
dopar_loop=(proc.time()-start)
stopCluster(cl)
print(rbind(base_loop,do_loop,dopar_loop)[,1:3])
##            user.self sys.self elapsed
## base_loop       0.01        0    0.02
## do_loop         0.02        0    0.01
## dopar_loop      0.02        0    0.04

%do% does not use more than base core, but %dopar% uses the number of cores that we specify in registerDoParallel

doParallel::mclapply is the equivalent of lapply.

Mike Nguyen, PhD
Mike Nguyen, PhD
Visiting Professor of Data Sciences and Operations

My research interests include marketing, and social science.

Next
Previous

Related