KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm trying to compare parallelization options. Specifically, I'm comparing the standard SNOW and mulitcore implementations to those using doSNOW or doMC and foreach . As a sample problem, I'm illustrating the central limit theorem by computing the means of samples drawn from a standard normal distribution many times. Here's the standard code: CltSim <- function(nSims=1000, size=100, mu=0, sigma=1){ sapply(1:nSims, function(x){ mean(rnorm(n=size, mean=mu, sd=sigma)) }) } Here's the SNOW implementation: library(snow) cl <- makeCluster(2) ParCltSim <- function(cluster, nSims=1000, size=100, mu=0, sigma=1){ parSapply(cluster, 1:nSims, function(x){ mean(rnorm(n=size, mean=mu, sd=sigma)) }) } Next, the doSNOW method: library(foreach) library(doSNOW) registerDoSNOW(cl) FECltSim <- function(nSims=1000, size=100, mu=0, sigma=1) { x <- numeric(nSims) foreach(i=1:nSims, .combine=cbind) %dopar% { x[i] <- mean(rnorm(n=size, mean=mu, sd=sigma)) } } I get the following results: > system.time(CltSim(nSims=10000, size=100)) user system elapsed 0.476 0.008 0.484 > system.time(ParCltSim(cluster=cl, nSims=10000, size=100)) user system elapsed 0.028 0.004 0.375 > system.time(FECltSim(nSims=10000, size=100)) user system elapsed 8.865 0.408 11.309 The SNOW implementation shaves off about 23% of computing time relative to an unparallelized run (time savings get bigger as the number of simulations increase, as we would expect). The foreach attempt actually increases run time by a factor of 20. Additionally, if I change %dopar% to %do% and check the unparallelized version of the loop, it takes over 7 seconds.</
Tags (comma-separated)
Save Edits
Cancel