Theoden
Theoden

Reputation: 297

Why the parallelization of the following code takes too much to run?

The following piece of code is supposed to be very easy to run. In sequential mode it is executed in less than a second. However when I make it parallel it seemingly takes forever to run. If I set the vector lengths to 100 the code runs fine so I don't think it's a structural issue.

  library(foreach)
    library(doParallel)
    cl <- makeCluster(4)
    registerDoParallel(cl)
    bvec <-as.vector(1:1e3)
    avec <-as.vector(1:1e3)
    sim= function(x,y) 10*x+y
    system.time(x <-
       foreach(b=bvec, .combine='cbind') %:%
       foreach(a=avec, .combine='c') %dopar% {
         sim(a, b)
         })

What is the catch? Appreciated.

Upvotes: 0

Views: 51

Answers (1)

spritt
spritt

Reputation: 11

Parallelism requires quite a bit of overhead. Since your 'sim' function is computationally cheap, the cost of the overhead overwhelms the performance benefits from executing 'sim' in parallel. That's why you get the expected results with larger vector lengths.

Upvotes: 1

Related Questions