[R-sig-hpc] .combine in nested foreach loops !

Maas James Dr (MED) J.Maas at uea.ac.uk
Thu Mar 31 15:34:23 CEST 2011


I'm attempting to use nested foreach loops.  My problem is trying to combine the results correctly from the outer foreach loop.  Stephen Weston very graciously worked up an example for me of how to get outputs, in the form of matrices, back from a single foreach loop.  I've changed the example to try to get it to work correctly for nested foreach loops.  In this example the "results" object contains 6 lists however what I would like to get is a result containing only 3 lists, corresponding to the length of cvec, each list would be a 3x3x2 array.  I've tried several things to combine the results in the outer loop, including functions using "abind",  and not got it to work out, perhaps it is not possible directly from nested foreach loops?  

BTW, is there a location/way to search the R-sig-hpc listserv?

Thanks a bunch,

Jim

library(doMPI)
cl <- startMPIcluster()
registerDoMPI(cl)
library(foreach)

cvec <- c(1,2,3)

n <- 2
s <- seq(length=n)
a <- lapply(s, function(i) matrix(rnorm(9), 3))
b <- lapply(s, function(i) matrix(rnorm(9), 3))

init <- list(a=list(), b=list())

comb <- function(accum, ...) {
  s <- seq(along=accum)
  names(s) <- names(accum)
  lapply(s, function(i) c(accum[[i]], lapply(list(...), function(a) a[[i]])))
}

results <- foreach (c=cvec, .combine="c") %:%
    foreach(i=icount(length(a)), .combine=comb, .init=init,
                  .multicombine=TRUE) %dopar% {
  list(a=a[[i]]*c, b=b[[i]]*c)
}

results
names(results)

closeCluster(cl)
mpi.quit()

===============================
Dr. Jim Maas
University of East Anglia


More information about the R-sig-hpc mailing list