[R] R parallel / foreach - aggregation of results

Martin Spindler Martin.Spindler at gmx.de
Sat Aug 1 21:19:26 CEST 2015


Dear Jim,

Thank you very much for your response. It seems to work now, but the return value is not the required matrix but a list of matrices (one for each repition j).
Any idea how it is possible to return only the last matrix and not all?

Thanks and best,

Martin
 
 

Gesendet: Freitag, 31. Juli 2015 um 18:22 Uhr
Von: "jim holtman" <jholtman at gmail.com>
An: "Martin Spindler" <Martin.Spindler at gmx.de>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Betreff: Re: [R] R parallel / foreach - aggregation of results

Try this chance to actually return values:
 
 
library(doParallel)
Simpar3 <- function(n1) {
   L2distance <- matrix(NA, ncol=n1, nrow=n1)
   data <- rnorm(n1)
   diag(L2distance)=0
   cl <- makeCluster(4)
   registerDoParallel(cl)
   x <- foreach(j=1:n1)  %dopar% {
     library(np)
     datj <- data[j]
     for(k in j:n1) {
       L2distance[j,k] <- k*datj
     }
     L2distance  # return the value
   }
   stopCluster(cl)
   return(x)
 }
 Res <- Simpar3(100)
 

Jim Holtman
Data Munger Guru
 
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it. 
On Fri, Jul 31, 2015 at 8:39 AM, Martin Spindler <Martin.Spindler at gmx.de> wrote:Dear all,

when I am running the code attached below, it seems that no results are returned, only the predefined NAs. What mistake do I make?
Any comments and help is highly appreciated.

Thanks and best,

Martin


Simpar3 <- function(n1) {
  L2distance <- matrix(NA, ncol=n1, nrow=n1)
  data <- rnorm(n1)
  diag(L2distance)=0
  cl <- makeCluster(4)
  registerDoParallel(cl)
  foreach(j=1:n1)  %dopar% {
    library(np)
    datj <- data[j]
    for(k in j:n1) {
      L2distance[j,k] <- k*datj
    }
  }
  stopCluster(cl)
  return(L2distance)
}

Res <- Simpar3(100)

______________________________________________
R-help at r-project.org[R-help at r-project.org] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help[https://stat.ethz.ch/mailman/listinfo/r-help]
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html[http://www.R-project.org/posting-guide.html]
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list