嵌套循环的并行化

时间:2014-03-04 21:00:46

标签: r foreach parallel-processing apply

提前致谢。

我有三个嵌套的for循环,其中最低的包含条件if语句评估。

代码运行成功,尽管使用1个foreach循环和2个嵌套for循环非常缓慢。对于我的“真实”数据,我可以在大约16小时内运行〜 1000 x 2000 x 160 迭代。我想将其缩放到〜 10000 x 2000 x 160 ,这将花费太长时间来运行(> 100小时)。理想情况下,我想为应用循环转换嵌套for循环,因为我知道它们运行得更快。我按如下方式构建了示例代码:

library("foreach") 
library("doMC") 
registerDoMC()

R = 10
N = 5
M = 10

data_matrix <- matrix(data=runif(50,N,M),ncol=10)

cos_function<- function(x,y){
  res<-runif(1,3,5)
  res<-res + (3/x) + (4/y)
  return(res)
}


results<-foreach(k=1:R,combine=rbind) %dopar% {
  dummy<-matrix(0, N, M)
    for (i in 1:N) {
      for (j in 1:M) {
        if (cos_function(i, j)  <= data_matrix[i,j]) {
          dummy[i,j] = 1
              }
        }
  return(dummy)
}

我在提出加速此过程的方法方面遇到了很多麻烦:即包括嵌套循环的并行化,切换到嵌套的apply函数,将嵌套循环组合到apply循环中。你可能会说,我对R和并行化很新,所以任何指导都会非常有用。

2 个答案:

答案 0 :(得分:1)

我知道您的数据只是玩具数据而且这个答案没有回答您的原始问题,但您是否考虑优化矩阵计算?

以下代码在我的笔记本电脑上运行~90秒(而不是16小时):

R <- 160
N <- 1000
M <- 2000

## construct your data_matrix and replicate it R-times to get the same size
## like the cosmatrix
data_matrix <- matrix(data=rep(runif(N*M, N, M), R), byrow=TRUE, ncol=M)

## construct whole cosmatrix (inclusive replications R)
cosmatrix <- matrix(data=runif(N*M*R, 3, 5), ncol=M)

## perform your cos_fun
cosmatrix <- t(t(cosmatrix+3/(1:N)) + 4/(1:M))

## comparison
cosmatrix <= data_matrix

答案 1 :(得分:0)

我对并行化不太熟悉,但也许更改以下内容会更快

dat<- matrix(data=runif(50,N,M),ncol=10)
#defining a new version of cos which takes a matrix
new_cos<-function(m) (3/m$i) + (4/m$j)
#create a data.frame ij which holds i and j combinations as each row.
ij<-expand.grid(i=seq(N),j=seq(M))
#now use this for your res. This will return a vector, where res[1] is the result of i=1 and j=1,
#res[2] is the result of i=2, j=1.
res<-runif(M*N,3,5)+new_cos(ij)
##now this will compare as appropriate, since the comparisons will be done column-wise along dat
##multiply booleans by 1 to get your 1/0 matrix
1*(res<=dat)
相关问题