library("parallel") # setup the cluster: cl <- makeCluster(rep("localhost", 3), type = "SOCK") cl <- makeCluster(3) clusterCall(cl, runif, 3) # run on all nodes ## initialize parallel RNG: clusterSetRNGStream(cl, 123) clusterCall(cl, runif, 3) # again stopCluster(cl) # stop for now ## 100 Mal soll foo() durchgeführt werden: foo <- function(x) rnorm(100000) ### ohne Parallelisierung: system.time(L <- sapply(1:100, foo)) ### Mit Parallelisierung, 2 Kerne: library("parallel") cl <- makeCluster(rep("localhost", 2), type = "SOCK") clusterSetRNGStream(cl) system.time(L <- parSapply(cl, 1:100, foo)) stopCluster(cl) ## 100 Mal soll foo() durchgeführt werden: foo <- function(x) sum(rnorm(100000)) ### ohne Parallelisierung: system.time(L <- sapply(1:100, foo)) ### Mit Parallelisierung, 2 Kerne: library("parallel") cl <- makeCluster(rep("localhost", 2), type = "SOCK") clusterSetupRNG(cl) system.time(L <- parSapply(cl, 1:100, foo)) stopCluster(cl)