Hansy Kumaralal
Hansy Kumaralal

Reputation: 169

How to parallelize a function which has more than one parameter?

I have a particular simulation to perform which takes some time to run in R. Since I want to perform 1000 simulations, I decided to use "parallel" package to distribute these simulations over 4 cores. I know that I can get a resulting vector if I have a function like follows.

results_parallel <- parSapply(cl, c(1000,1000,1000,1000), rnorm)

In here rnorm() has only one parameter as the input, so that I can ask to produce 4000 values using 4 cores.

But my simulation has more than one parameter. My question is, since I have more than one parameter as input, how can I tell which parameter should be calculated using 4 cores? If simulation1(A,B,C,m) is my function where m is the number of simulations, I want to ask each core to to do the simulation 250 times, so that finally I can get 1000 simulations. Can anyone give me a hint?

Upvotes: 2

Views: 1286

Answers (2)

rosscova
rosscova

Reputation: 5590

I'm not sure this is exactly what you're after, but you should be able to add the parameters to be passed to rnorm within your function call. I haven't used parSapply before, but here it is with llply:

doMC::registerDoMC( cores = 4 )
results <- plyr::llply( .data = c(1000,1000,1000,1000),
                        .fun = rnorm, mean = 1, sd = 0.4,
                        .parallel = T 
)

Note the parameters mean and sd being passed to rnorm from within the llply call. parSapply should be able to handle this in the same way. For example:

results_parallel <- parSapply( cl, 
                               X = c(1000,1000,1000,1000), 
                               FUN = rnorm, mean = 1, sd = 0.4 
)

Upvotes: 1

BonStats
BonStats

Reputation: 358

You could try creating a function wrapper that takes a single argument rather multiple ones.

rnorm1 <- function(ls){
  rnorm(n = ls$n, mean = ls$mean, sd = ls$sd)
}

cl <- makeCluster(2)

example_list <- list(
      list(n=1000, mean = 0, sd = 1),
      list(n=1000, mean = 1, sd = 2)
)

results_parallel <- parSapply(cl, example_list, rnorm1)

You would just have to specify the required list of arguments so that it is a list of lists.

Upvotes: 6

Related Questions