[R-sig-hpc] foreach: using either %do% or %dopar% depending on condition
mat
matthieu.stigler at gmail.com
Mon May 3 13:37:21 CEST 2010
Thanks a lot Brian and Steve for your prompt answer!!
You noth pointed out what I'm doing is a little bit complicated... let
me precise why I do this, and maybe you will agree?
The function for end-user is not on parallel staff, it is a test for a
time series model, whose computations can be alleviated thanks to a
parallel computing. Say the function is (using the names as in package
strucchange, which is to my knowledge the only one to make an explicit
call to foreach):
mytest<-function(x, y, optionA=c("A", "B"), hpc=c("none", "foreach")
So the rationale is that one assumes that by default the user does not
use the parallel option, hence the %do% call (using by default %dopar%
would be adding an unexpected warning, no?). But if the user wants, then
we will call %dopar%
Do you think it then makes sense? Are there any recomandations /
standardization efforts on how one should provide link to foreach or
snow from other packages?
Thanks a lot!!!
Matthieu
Stephen Weston a écrit :
> You could try something like this:
>
> > '%my%' <- if (hpc == "none") get('%do%') else get('%dopar%')
>
> However, why not just let the user decide by either registering a parallel
> backend or not? The main purpose of the "registerDoSEQ" function is
> to allow the user to say that parallel operations should be done sequentially.
> The '%dopar%' function is the programmer's way to declare that the
> foreach loop can be executed in parallel. It's the user's job to declare
> how the '%dopar%' should be executed. That also simplifies the code
> by not having to define and pass around yet another option to your
> functions.
>
> - Steve
>
>
>
>
> On Mon, May 3, 2010 at 6:45 AM, mat <matthieu.stigler at gmail.com> 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
>>
>>
More information about the R-sig-hpc
mailing list