3

I have been trying to get some output displayed from the foreach loop R. A reproducible example is

cl <- makeCluster(2)
registerDoParallel(cl)


ptm1 <- proc.time()
foreach (i = 1:50, .packages = c("MASS"), .combine='+') %dopar% {
  ginv(matrix(rexp(1000000, rate=.001), ncol=1000))
  if (i >49){
    cat("Time taken", proc.time() - ptm1)
  }
}

I expect the time taken to be displayed. But, this does not display anything. Can you please suggest ways of capturing the messages in the foreach loop and displaying at the end of the loop.

honeybadger
  • 1,465
  • 1
  • 19
  • 32
  • 2
    Possible duplicate of [How can I print when using %dopar%](https://stackoverflow.com/questions/10903787/how-can-i-print-when-using-dopar) – pieca Jun 05 '18 at 06:57

1 Answers1

2

I'm not sure if there's a way to output to the screen, but you can easily output to a log file using the sink function like so

ptm1 <- proc.time()
foreach (i = 1:50, .packages = c("MASS"), .combine='+') %dopar% {
  ginv(matrix(rexp(1000000, rate=.001), ncol=1000))
  if (i >49){

    sink("Report.txt", append=TRUE) #open sink file and add output

    cat("Time taken", proc.time() - ptm1)

  }
}

EDIT : As @Roland points out, this can be dangerous if you want to capture output from every iteration and not just the final one, because you don't want the workers to clobber each other. He links to a better alternative for this scenario in his comment.

Esther
  • 1,115
  • 1
  • 10
  • 15
  • 3
    I think this is dangerous. If you follow this approach, you should create a separate log for each worker, similar to [this](https://stackoverflow.com/a/42250632/1412059) – Roland Jun 05 '18 at 07:07
  • That's a good point. However in this case the OP is only calling sink once on the final process. I agree that if you want the output from each process multiple logs is the way to go. – Esther Jun 05 '18 at 07:22
  • "calling sink once on the final process" No, they want output from each iteration, which means calling sink within the loop and thus on the workers (as you indeed show in your code). – Roland Jun 05 '18 at 07:25
  • I interpreted their inclusion of `if (i > 49) { cat(...)}` to mean that they only desired output from the final iteration. – Esther Jun 05 '18 at 07:30
  • Ah, I see. That seems strange (and useless). – Roland Jun 05 '18 at 07:36