[R-sig-hpc] trouble with saving object

Stephen Weston stephen.b.weston at gmail.com
Tue Jan 5 23:08:35 CET 2010


Hi Erin,

The problem is that you're trying to use foreach just like
a for loop.  It's more of a hybrid between a for loop and
lapply.  Basically, side effects in the loop don't work.  It's
the return value that counts.

Try something more like this:

    library(doMPI)
    cl <- startMPIcluster()
    registerDoMPI(cl)
    zz <- foreach(i=1:10000, .combine='c') %dopar% {
      x <- arima.sim(list(order=c(1,0,0), ar=-0.9), n=120)
      arima(x, order=c(1,0,0))$model$phi
    }
    print(zz)
    closeCluster(cl)
    mpi.quit()

Note that the standard 'c' function is used to combine
the "phi" elements together into a vector, which is returned
in the variable "zz".

Hope this helps,

- Steve


On Tue, Jan 5, 2010 at 4:32 PM, Hodgess, Erin <HodgessE at uhd.edu> wrote:
> Dear R HPC People:
>
> Here is a function that I have put together:
>
> library(foreach)
> library(doMPI)
> cl <- startMPIcluster()
> registerDoMPI(cl)
> zz <- numeric(length=10000)
> foreach(i=1:10000,.combine=cbind) %dopar% {
>  x <- arima.sim(list(order=c(1,0,0),ar=-0.9),n=120)
>  z[i] <- arima(x,order=c(1,0,0))$model$phi
>  }
> assign("zz",z,envir=.GlobalEnv)
>     # and shut it down
>     closeCluster(cl)
>
> but it doesn't save the zz variable.
>
> Does anything look out of whack, please?
>
> Thanks,
> Erin
>
>
>
> Erin M. Hodgess, PhD
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: hodgesse at uhd.edu
>
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-hpc mailing list
> R-sig-hpc at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-hpc
>



More information about the R-sig-hpc mailing list