Parallelization doesn't work with foreach package

Using the foreach package, I expected the next line to run in 10 seconds

system.time(foreach (i=1:5, .combine='c') %do% {Sys.sleep(2);i})
   user  system elapsed 
  0.053   0.011  10.012 

      

and the next line to run after about 2 seconds

system.time(foreach (i=1:5, .combine='c') %dopar% {Sys.sleep(2);i})
   user  system elapsed 
  0.069   0.017  10.019 

      

but that won't work.

I am on Mac OSX, my machine has 16 processors and nothing heavy is currently running. I am not getting any error or warning message.

+3


source to share


1 answer


You need to register a parallel server. Do something like



library(doParallel)
registerDoParallel(cores=4)

      

+6


source







All Articles