An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-hpc/attachments/20130108/0804dcf2/attachment.pl>
programmatically shift between %do% and %dopar% in foreach?
3 messages · Max Kuhn, Simon Urbanek, Brian G. Peterson
On Jan 8, 2013, at 11:40 AM, Max Kuhn wrote:
I'd like to have the option of using foreach but be able to restrict processing to be parallel in one of my packages. I'd rather not have x <- if(goParallel) foreach(stuff) %dopar% foo else foreach(stuff) %do% foo Also, the function is nested within layers that conduct parallel processing (i.e. the foo above might also use dopar inside) and my impression is that registerDoSEQ would shut down parallel processing for both functions. It looks like %do% and %dopar% are not existing objects:
str(%do%)
Error: unexpected SPECIAL in "str(%do%"
They are simply functions - you just forgot to quote it:
str(`%do%`)
function (obj, ex) %...% are binary infix operators so if you use them as a symbol, you have to quote them.
Otherwise I would add some conditional logic to switch between them such as operator <- if(goParallel) %dopar% else %do% x <- foreach(stuff) operator foo
It works, but if you want to keep the infix notation you have to use something like `%op%` <- if (goParallel) `%dopar%` else `%do%` otherwise for your suggestion you'd have to use the functional form: operator(foreach(stuff), foo) Cheers, Simon
Thanks, Max
sessionInfo()
R version 2.15.2 (2012-10-26) Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] foreach_1.4.0 loaded via a namespace (and not attached): [1] codetools_0.2-8 iterators_1.0.6 [[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
On 01/08/2013 10:40 AM, Max Kuhn wrote:
I'd like to have the option of using foreach but be able to restrict
processing to be parallel in one of my packages.
I'd rather not have
x <- if(goParallel) foreach(stuff) %dopar% foo else foreach(stuff) %do%
foo
Also, the function is nested within layers that conduct parallel processing
(i.e. the foo above might also use dopar inside) and my impression is
that registerDoSEQ would shut down parallel processing for both functions.
It looks like %do% and %dopar% are not existing objects:
> str(%do%)
Error: unexpected SPECIAL in "str(%do%"
Otherwise I would add some conditional logic to switch between them such as
operator <- if(goParallel) %dopar% else %do%
x <- foreach(stuff) operator foo
I always use %dopar%, and if necessary, e.g. in nested foreach loops, turn off parallelization on the nested workers after you're inside the worker loop, using a simple if construct as you describe.
Brian