Simple Parallel

Victor Feagins
3/2/2021

Starting and stopping clusters

library(parallel)
detectCores() #How many cores do you have
[1] 8
cl <- makeCluster(2) #starting a cluster
#cl <- makeForkCluster(8) #fork clusters don't work on windows
#stopCluster(cl) #Stopping cluster

Sending libraries to Clusters

clusterEvalQ(cl, {
  library(tidyverse)
  }) #clusterEvalQ make all the clusters run this code as is
[[1]]
 [1] "forcats"   "stringr"   "dplyr"     "purrr"     "readr"     "tidyr"    
 [7] "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics"  "grDevices"
[13] "utils"     "datasets"  "methods"   "base"     

[[2]]
 [1] "forcats"   "stringr"   "dplyr"     "purrr"     "readr"     "tidyr"    
 [7] "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics"  "grDevices"
[13] "utils"     "datasets"  "methods"   "base"     

Sending functions and variables to Clusters

a <-  2
square <-  function (num){num**2}
clusterExport(cl,c("a","square")) #sends variables and functions to clusters

clusterEvalQ(cl, {
  print(c(a,square(a)))
})
[[1]]
[1] 2 4

[[2]]
[1] 2 4

In series Time

ptm <- proc.time()
for (i in 1:5){
  Sys.sleep(3)
}
print(proc.time() - ptm)
   user  system elapsed 
   0.02    0.00   15.01 

In parallel Time

ptm <- proc.time()

invisible(parSapply(cl, rep(3,5),Sys.sleep)) #invisible here just hides the null list from Sapply

print(proc.time() - ptm)
   user  system elapsed 
   0.00    0.00    9.01 
stopCluster(cl)