Using foreach and doMC, is there a way to get cat
and/or message
to print to the screen when running in parallel? For example:
> library(foreach)
>
> tmp <- foreach(i=1:3) %do%
+ cat("sqrt(i) =", sqrt(i), "\n")
sqrt(i) = 1
sqrt(i) = 1.414214
sqrt(i) = 1.732051
>
> tmp <- foreach(i=1:3) %do%
+ message("i^2 =", i^2, "\n")
i^2 =1
i^2 =4
i^2 =9
>
> library(doMC)
> registerDoMC(2)
>
> tmp <- foreach(i=1:3) %dopar%
+ cat("sqrt(i) =", sqrt(i), "\n")
>
> tmp <- foreach(i=1:3) %dopar%
+ message("i^2 =", i^2, "\n")
>
> sessionInfo()
R Under development (unstable) (2014-01-29 r64898)
Platform: x86_64-apple-darwin10.8.0 (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] parallel stats graphics grDevices utils datasets
[7] methods base
other attached packages:
[1] doMC_1.3.2 iterators_1.0.6 foreach_1.4.1
loaded via a namespace (and not attached):
[1] codetools_0.2-8 compiler_3.1.0 tools_3.1.0
Thanks,
Max
By default, stdout from the workers isn't redirected when using foreach with the doMC backend, so output should be displayed unless you're using a GUI such as R.app on Mac OS X or Rgui on Windows. I just tried a quick test on my Mac, and it appears that RStudio also throws away stdout from the workers created by doMC/mclapply.
When executing R from a terminal, I see worker output on Mac OS X 10.7.5 using R 3.0.2 and "R Under development (unstable) (2014-02-13 r64986)". Since you're using a slightly older development build, it's possible that there was a problem that has been subsequently fixed.
You could try explicitly turning on output with the doMC-specific "silent" option:
> opts <- list(silent=FALSE)
> tmp <- foreach(i=1:3, .options.multicore=opts) %dopar% message('i^2 = ', i^2)
i^2 = 1
i^2 = 4
i^2 = 9
This causes mclapply to be called with mc.silent=FALSE
. If that doesn't help, I suggest trying the latest development build which works for me.