如何跟踪并行循环每个实例*内的进度?

问题描述 投票:0回答:1

我正在并行运行多个 MCMC。我如何跟踪每个 MCMC 的进度?我不是问如何跟踪并行循环的进度,即每个 MCMC 何时完成;相反,我想知道嵌套在并行循环中的每个 MCMC 的进度。有没有办法设置多个进度条?或者我需要在嵌套循环中保存一些文件来监视?

#not executed so not sure if actually reprex, but the concept is there
#computer is still running my other code :)
library(foreach)
library(doParallel)

cl <- makeCluster(mc <- getOption("cl.cores", parallel::detectCores()))

registerDoParallel(cl)

reprex <- foreach(1:10) %dopar% { 
  for(ii in 1:100){
    #I want to track this loop's progress within each instance
    Sys.sleep(0.1)
  } 
}

stopCluster(cl)
r parallel-processing progress-bar
1个回答
0
投票

AFAIK,R 中不普遍支持多个进度条,至少不支持 R 控制台中的多行。

除此之外,futureversedoFutureprogressr可以为您提供近乎实时的进度更新。要点如下:

library(foreach)
library(doFuture)
library(progressr)

## Report on progress automatically
handlers(global = TRUE)

## Parallelize on local machine
plan(multisession)

## See https://progressr.futureverse.org/#a-progressor-cannot-be-created-in-the-global-environment
## for why we use local() here
reprex <- local({
  p <- progressor(steps = 10 * 100)
  y <- foreach(hh = 1:10) %dofuture% { 
    for(ii in 1:100){
      Sys.sleep(0.1)
      ## Report on progress thus far with a custom message
      p(sprintf("(hh, ii) = (%d, %d)", hh, ii))
    } 
  }
  y
})

## Stop local cluster
plan(sequential)
© www.soinside.com 2019 - 2024. All rights reserved.