简体   繁体   English

R中小外循环和大内循环的高效并行化

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

I have the following R code我有以下R代码

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??我在想的是,我有351i,j,k配置,如果我可以将这些配置分配给内核,并且每个内核都可以运行for循环1000000次迭代,是否可以实现类似的东西?

  1. Instead of calling rnorm() one million times, it would be more efficient to call it once with the argument n = 1000000 .与调用rnorm()一百万次不同,使用参数n = 1000000调用一次会更有效。
  2. To utilize R's functional programming features we should try to avoid writing for() -loops.为了利用 R 的函数式编程特性,我们应该尽量避免编写for()循环。 We can instead first create an object that represents your 351 configurations and then iterate on that object.我们可以先创建一个代表您的 351 个配置的对象,然后迭代该对象。 See below for an example of how to do that without.有关如何在没有的情况下执行此操作的示例,请参见下文。

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.为了并行化代码的执行,我们可以使用furrr包。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 外环的并行化在REvolution中起作用,但在正常的R中不起作用 - Parallelization of outer loop works in REvolution, but not in normal R R在外环中嵌套foreach%dopar%,在内环中嵌套%do% - R nested foreach %dopar% in outer loop and %do% in inner loop 如何在 R 的嵌套 foreach 循环的内循环和外循环之间添加代码 - How can I add code between the inner and outer loops of nested foreach loops in R 包含* apply的R循环的并行化/优化 - Parallelization/Optimization of R loops containing *apply 有两个for循环可使用(i,j)创建一个列表,其中外部循环不会覆盖内部循环 - Having two for-loops to create a list with (i,j) where the outer loop doesn't override the inner 如何使用外循环索引的索引作为 R 中增量的基础来实现带有内循环的嵌套循环? - How can I implement nested loop with inner loop using index of outer loop index as base for incrementation in R? 在R中,如何有效地基于小数据框中的行修改大数据框中的列 - In R, how to modify a column in a big dataframe based on rows in a small dataframe in an efficient way 向量化 R for 循环的有效方法 - Efficient way to vectorize R for loops R循环-有没有更有效的方法? - R loops - is there a more efficient way? R和并行化 - R and parallelization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM