Search code examples
rparallel-processingmclapply

Is mclapply() with mc.cores = 1 the same as lapply()?


I know that I should not nest parallel operators, but I am in a situation where I have to basically nest two mclapply() calls in my code. This is because in my code I have a function (let's call it foo()) that is already using mclapply() inside it. What I want is basically to run parallel threads of a sequential version of foo(), doing something like this:

mclapply(X, function(x) {
  foo(x, mc.cores = 1)
},
mc.cores = 4)

Can I just run the internal mclapply() with mc.cores = 1 or I should implement a sequential version of foo()?

In other words: is mclapply(..., mc.cores = 1) behaving exactly as lapply(...)? Is there a parallel overhead slowing down the program in this case?


Solution

  • The source code of parallel::mclapply contains this bit of code:

     ... 
     if (cores < 2L) 
            return(lapply(X = X, FUN = FUN, ...))
     ... 
    

    So I believe the answer is yes, you should get the same results as using lapply directly, but there is also some additional overhead. I doubt that this will affect the runtime very significantly.

    The documentation also states that:

    Details

    mclapply is a parallelized version of lapply, provided mc.cores > 1: for mc.cores == 1 it simply calls lapply.