[R-sig-hpc] foreach: using either %do% or %dopar% depending on condition

Brian G. Peterson brian at braverock.com
Mon May 3 13:19:34 CEST 2010


I think you're over-complicating this.

I only use %do% while testing something, to make certain that I can debug 
cleanly, and often not even that.

I then convert *all* code to %dopar%

The user can registerDoSeq() to do sequentially on one core, and this is the 
default behavior if no parallel registerDo* engine is registered (e.g. foreach 
is installed, but none of the parallel backends, or the user has not registered 
a backend).

Regards,

    - Brian

mat wrote:
> Hi
> 
> I wish to be able to substitute %do% or %dopar% depending on whether the 
> user wants or not to use the parallel feature. For this, I would do 
> something like:
> 
> if(hpc) foreach(icount(5), .combine = "rbind")  %dopar%
> else foreach(icount(5), .combine = "rbind")  %do%
>    statement of function...
> 
> I did not succeed in doing it, I tried:
> 
> foreach(icount(5), .combine = "rbind")  %do% else  %dopar% {
> a<-runif(1)
> b<-runif(1)
> c(a,b)
> }
> 
> Or tried to assign outside the operator:
> 
> "%my%" <-function(obj,ex) if(hpc=="none") "%dopar%" else "%dopar%"
> 
> foreach(icount(5), .combine = "rbind")  %my% {
> a<-runif(1)
> b<-runif(1)
> c(a,b)
> }
> 
> But it fails... any idea?
> 
> Thanks a lot!!
> 
> Matthieu
> 
> _______________________________________________
> R-sig-hpc mailing list
> R-sig-hpc at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-hpc


-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-sig-hpc mailing list