Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallelization doesn't work with the foreach package

Using the foreach package, I was expecting the following line to run in about 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 following line to run in 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 it doesn't work.

I am on a Mac OSX, my machine has 16 processors and nothing heavy is currently running. I don't get any error or warning message.

like image 654
Remi.b Avatar asked Jun 06 '15 22:06

Remi.b


1 Answers

You need to register a parallel backend. Do something like

library(doParallel)
registerDoParallel(cores=4)
like image 86
user12912834 Avatar answered Oct 03 '22 00:10

user12912834