Parallelization doesn't work with the foreach package
Asked Answered
C

1

9

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.

Changteh answered 6/6, 2015 at 22:43 Comment(3)
Seems like there's nothing to parallelize here. Your for loop states that all five loops run serially.Pebrook
I felt like I misunderstood the vignette. I thought that using %dopar% would cause whatever is within the foreach loop to run in parallel for each value of i. In the first paragraph of page 8 one can read: To make any of the previous examples run in parallel, all you have to do is to replace %do% with %dopar%. What is my mistake?Changteh
Did you get the warning message: "executing %dopar% sequentially: no parallel backend registered"?Pikeperch
S
23

You need to register a parallel backend. Do something like

library(doParallel)
registerDoParallel(cores=4)
Stillborn answered 7/6, 2015 at 2:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.