0

I am working on a Monte Carlo simulation. I have three different designs, and for each of those I want to replicate some analysis R times.

To speed things up, I decided to parallelize the job across the designs (hopefully this makes sense, I am new to parallel computing). So, the idea is to have a foreach loop that uses the %dopar% operator iterating over the different designs.

Within this, I want to nest a "standard" for loop that repeats some code R times. What I would like to do is to keep track of the progress of the program. What I generally do is to cat the number of the current replication, but also a progress bar would be okay.

However, I do not know how to do this within the foreach loop. It follows my attempt.

pkgs <- c("parallel", "foreach", "doSNOW") 
inst <- lapply(pkgs, library, character.only = TRUE)

n_designs <- 3

n_cores <- detectCores() - 1L
my_cluster <- makeCluster(n_cores)
registerDoSNOW(cl = my_cluster)

results <- foreach(i = seq_len(n_designs)) %dopar% {
  R <- 100

  for (r in R) {
    cat(r, "\n")
  }
}

Searching on SO, I found some posts on how to create a progress bar that keeps track of the foreach loop (example), but this is slightly different from what I am looking for.

EDIT

The post Print outputs in foreach loop in R does not really answer my question for two reasons:

  • That post concerns printing the status of the outer foreach loop, which in my case is pointless as this iterates over three different designs. What I am interested in is displaying the status of the inner loop, which draws a new sample and performs the analysis R times;
  • The solution for that post is to write in an external log file, while my aim is to display the status in the console.
riccardo-df
  • 512
  • 4
  • 9

0 Answers0