Victor Feagins
3/2/2021
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
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"
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
ptm <- proc.time()
for (i in 1:5){
Sys.sleep(3)
}
print(proc.time() - ptm)
user system elapsed
0.02 0.00 15.01
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)