Jonathan1234
Jonathan1234

Reputation: 499

Efficient Parallelization of small outer loops and big inner loop in R

I have the following R code

LLL = list()
idx = 1
for(i in 1:3){
  for(j in 1:9){
     for(k in 1:13){
        for(iter in 1:1000000){
       
           if( runif(1,0,1)<0.5 ){
             LLL[[idx]] = rnorm(1,0,1)
             idx = idx + 1
          }
       }
     }
  }
}

Is there a way to parallelize efficiently this code?

What I was thinking is that I have 351 configurations of i,j,k, If I could distribute these configurations to cores and each core would run a for loop for 1000000 iterations, can something similar to that be implemented??

Upvotes: 0

Views: 33

Answers (1)

Till
Till

Reputation: 6663

  1. Instead of calling rnorm() one million times, it would be more efficient to call it once with the argument n = 1000000.
  2. To utilize R's functional programming features we should try to avoid writing for()-loops. We can instead first create an object that represents your 351 configurations and then iterate on that object. See below for an example of how to do that.

Create configurations:

cfgs <-
  expand_grid(i = 1:3,
              j = 1:9,
              k = 1:13)

Code without parallelization.

cfgs |> 
  split(1:nrow(cfgs)) |> 
  lapply(\(x) rnorm(100000, 0, 1))

In order to parallelize the execution of the code we can use the furrr package.

library(furrr)
plan(multisession)
cfgs |> 
  split(1:nrow(cfgs)) |> 
  future_map(\(x) rnorm(100000, 0, 1), .options = furrr_options(seed=TRUE))

Upvotes: 2

Related Questions