rsnowfall

Running functions in parallel in R


I have two functions fun1 and fun2. I would like to run them in parallel in my R script. When they complete I'd like to use their output in further processing sequentially. I guess this means that I need to wait for them to complete.

I would very much appreciate any tips or pointers.

Thank You.


Solution

  • The parallel packages for R are mostly designed to run the same function or expression in parallel with possibly different data. They are not designed out of the box to run different functions in a simple way.

    It would be nice to have a function like DAGapply that you could specify a Directed Acyclic Graph (what pieces depend on which other pieces) and all the pieces that could be run in parallel would be, then as the initial pieces returned, the pieces whose dependencies have all completed would then be submitted to run in parallel, but I don't see that functionality anywhere yet. I have thought of writing it myself and can see the main pieces to call (but they are non-exported and could change at any time) but have not found the time yet.

    A work around that you could use now would be to write your own function that take a single argument and if that argument is 1 then it calls your first function and if the argument in 2 it calls your second function. This new function could then be passed to a parallel function (such as parLapply in the parallel package) and it would in turn run your functions in parallel and wait for both to finish.