Hello,
I am trying to run a small example with foreach, but I am having some
problems. Here is the code:
*library(doMC)
registerDoMC()
zappa = list()
frank = list()
foreach (i = 1:4) %dopar% {
zappa[[i]] = kmeans (iris[-5],4)
frank[[i]] = warnings()
}*
The code runs without error. However the zappa and frank will be empty
lists.
If I use regular *for *instead, the list will be filled up with kmeans and
warnings results. However, it will not be processed distributed.
What am I doing wrong?
What am I not understanding?
Thanks
Rui
[[alternative HTML version deleted]]
Rui,
I suggest you read the following tutorial to give you an introduction to
foreach:
http://cran.r-project.org/web/packages/doMC/vignettes/gettingstartedMC.pdf
Regarding your question I could would suspect that you can not assign values to
some variable inside foreach and use them "outside" in the global
environment. It is analog to using 'loacal' variables inside ordinary
dfunctions.
Try running:
zappa <- foreach (i = 1:4) %dopar% {
kmeans(iris[-5],4)$cluster
}
You need to define the .combine function to combine the results from your
calculation. I would suggest you try to restrict yourself to saving single
vectors from each foreach loop for the sake of simplicity. I myself tried to
save different types of R objects from foreach iterations once and it was a hard
fight against the code....
HTH
Jannis
P.S. Is there any particular reason why there are so seldom answers to posts
regarding foreach and all these doMC/doSMP packages ? Do so few people use these
packages or does this have anything to do with the commercial origin of these
packages?
--- Rui Esteves <ruimaximo at gmail.com> schrieb am Mo, 17.10.2011:
> Von: Rui Esteves <ruimaximo at gmail.com>
> Betreff: [R] Foreach (doMC)
> An: r-help at r-project.org
> Datum: Montag, 17. Oktober, 2011 19:31 Uhr
> Hello,
>
> I am trying to run a small example with foreach, but I am
> having some
> problems. Here is the code:
>
> *library(doMC)
> registerDoMC()
>
> zappa = list()
> frank = list()
>
> foreach (i = 1:4) %dopar% {
> zappa[[i]] = kmeans (iris[-5],4)
> frank[[i]] = warnings()
> }*
>
> The code runs without error. However the zappa and frank
> will be empty
> lists.
> If I use regular *for *instead, the list will be filled up
> with kmeans and
> warnings results. However, it will not be processed
> distributed.
>
> What am I doing wrong?
> What am I not understanding?
>
> Thanks
>
> Rui
>
> ??? [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org
> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained,
> reproducible code.
>
> P.S. Is there any particular reason why there are so seldom answers to posts regarding foreach and all these doMC/doSMP packages ? Do so few people use these packages or does this have anything to do with the commercial origin of these packages?Jannis, An interesting question. I'm a huge fan of foreach and the parallel backends, and have used foreach in some of my packages. It leaves the choice of backend to the user, rather than forcing some environment. If you like multicore, great -- the package doesn't care. Someone else may use doSNOW. No problem. To answer your question, foreach was originally written by (primarily, at least) Steve Weston, previously of REvolution Computing. It, along with some of the parallel backends (perhaps all at this point, I'm out of touch) are available open-source. Hence, I'd argue that the "commercial origin" is a moot point -- it doesn't matter, it will always be available, and it's really useful. Steve is no longer with REvolution, however, and I can't speak for the responsiveness/interest of current REvolution folks on this point. Scanning R-help daily for things relating to my own packages is something I try to do, but it doesn't always happen. I would like to think foreach is widely used -- it does have a growing list of reverse depends/suggests. And was updated as recently as last May, I just noticed. http://cran.r-project.org/web/packages/foreach/index.html Jay -- John W. Emerson (Jay) Associate Professor of Statistics Department of Statistics Yale University http://www.stat.yale.edu/~jay