Search code examples
rforeachdataframediagonaldoparallel

Upper diagonal calculation in parallel in R


I use the following code:

  library(foreach)
  library(doParallel)
  N<-5
  cl<-makeCluster(8)
  registerDoParallel(cl)

  #loop
  s8 <- foreach(i=1:N, .combine='rbind') %:%
    foreach(j=1:N, .combine='c') %dopar% {

        dis <-as.numeric (i+j)  } ## In reality there something more complicated

  stopCluster(cl) 

I get the following result:

> s8
         [,1] [,2] [,3] [,4] [,5]
result.1    2    3    4    5    6
result.2    3    4    5    6    7
result.3    4    5    6    7    8
result.4    5    6    7    8    9
result.5    6    7    8    9   10 

I would like to get the upper diagonal matrix as followed:

> s8
         [,1] [,2] [,3] [,4] [,5]
result.1    2    3    4    5    6
result.2    0    4    5    6    7
result.3    0    0    6    7    8
result.4    0    0    0    8    9
result.5    0    0    0    0   10

If I change the iterator of the inner loop to

foreach(j=i:N, .combine='c') %dopar% {

I don't get the required results.


Solution

  • Maybe this (although I would probably only parallelize the outer loop):

    library(foreach)
    library(doParallel)
    N<-5
    cl<-makeCluster(4)
    registerDoParallel(cl)
    
    #loop
    #result is a list of vectors
    s8 <- foreach(i=1:N) %:%
      foreach(j=i:N, .combine='c') %dopar% {
    
        as.numeric (i+j)  } ## In reality there something more complicated
    
    stopCluster(cl) 
    

    Post-processing to get same lengths:

    s8 <- t(vapply(s8, function(x) {
      x <- rev(x)
      length(x) <- N
      rev(x)
    }, FUN.VALUE = numeric(N)))
    
    s8[lower.tri(s8)] <- 0
    
    #     [,1] [,2] [,3] [,4] [,5]
    #[1,]    2    3    4    5    6
    #[2,]    0    4    5    6    7
    #[3,]    0    0    6    7    8
    #[4,]    0    0    0    8    9
    #[5,]    0    0    0    0   10