I have the following code that does serial processing with purr::pmap
library(tidyverse)
set.seed(1)
params <- tribble(
~mean, ~sd, ~n,
5, 1, 1,
10, 5, 3,
-3, 10, 5
)
params %>%
pmap(rnorm)
#> [[1]]
#> [1] 4.373546
#>
#> [[2]]
#> [1] 10.918217 5.821857 17.976404
#>
#> [[3]]
#> [1] 0.2950777 -11.2046838 1.8742905 4.3832471 2.7578135
How can I parallelize (fork) the process above so that it runs faster and produces identical result?
Here, I use rnorm
for illustration purpose, in reality I have a function that does heavy duty work. It needs parallelizing.
I'm open to non-purrr (non-tidyverse) solution, as long as it produces identical result given the rnorm
function and params
as input.
In short: a "parallel
pmap()
", allowing a similar syntax topmap()
, could look like:lift(mcmapply)()
orlift(clusterMap)()
.If you're not on Windows, you could:
Edit
Here is a "cleaner" option, that should feel more like using
pmap
:If you're on Windows (or any other OS), you could:
In case you're more inclined to use
foreach
, you could: