[R] dopar parallel assignments

David M Smith david at revolution-computing.com
Fri Apr 30 00:44:34 CEST 2010


[This relates to the foreach function in library(foreach)]

Vivek,

In the %dopar% example, the assignments are being made into "child" R
sessions in parallel. foreach is generally pretty good about detecting
variables you reference in the parallel loops and making sure the
objects are copied over to the parent, but in this case where you're
using "assign" directly, it doesn't detect that test_1.25 and
tets_1.50 are variables you're modifying in parallel. (In the %do%
case everything is run in the same R session, so the assignments are
naturally preserved.)

Also, in general, think of foreach more of an analogue to "lapply"
than to "for", in the sense that you're running it to collect the
values of the body of the loop, not for their side effects.

So what you probably really want is code like this:

test <- foreach(i = c(1.25, 1.50)) %dopar% some_timeconsuming_operation(i)

and then (if you really need the variables named as specified)

v <- c(1,25,1.50)
for (i in seq(along=v)) assign(paste("test_",v[i],sep=""),test[[i]])

In your specific example,

 some_timeconsuming_function <- function(i) i

but that's not timeconsuming, and so you're not going to get any
benefit from parallelization.

# David Smith


On Thu, Apr 29, 2010 at 2:07 PM, Vivek Ayer <vivek.ayer at gmail.com> wrote:
> Hi guys,
>
> I was wondering why this piece of code doesn't work:
>
> foreach (i = c(1.25,1.50)) %dopar% {
> assign(paste("test_",i,sep=""),i)
> }
>
> but, this does:
>
> foreach (i = c(1.25,1.50)) %do% {
> assign(paste("test_",i,sep=""),i)
> }
>
> Obviously, the difference is %dopar% vs. %do%. If I use %do%, I get
> objects test_1.25 and test_1.50, but I don't get these objects if I
> use %dopar% even though it seemed to run through the loop in parallel.
>
> Thanks in advance,
> Vivek
>
> ______________________________________________
> 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.
>



-- 
David M Smith <david at revolution-computing.com>
VP of Marketing, REvolution Computing  http://blog.revolution-computing.com
Tel: +1 (650) 330-0553 x205 (Palo Alto, CA, USA)

Download REvolution R free:
www.revolution-computing.com/downloads/revolution-r.php



More information about the R-help mailing list