R语言 如何跟踪并行化循环的每个示例中的进度?

cu6pst1q  于 2023-09-27  发布在  其他
关注(0)|答案(2)|浏览(216)

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

  1. #not executed so not sure if actually reprex, but the concept is there
  2. #computer is still running my other code :)
  3. library(foreach)
  4. library(doParallel)
  5. cl <- makeCluster(mc <- getOption("cl.cores", parallel::detectCores()))
  6. registerDoParallel(cl)
  7. reprex <- foreach(1:10) %dopar% {
  8. for(ii in 1:100){
  9. #I want to track this loop's progress within each instance
  10. Sys.sleep(0.1)
  11. }
  12. }
  13. stopCluster(cl)
csbfibhn

csbfibhn1#

当然,在R中没有对多个进度条的一般支持,至少在R控制台中没有跨多行的支持。
除此之外,futureversedoFutureprogressr可以为您提供近乎实时的进度更新。要点如下:

  1. library(foreach)
  2. library(doFuture)
  3. library(progressr)
  4. ## Report on progress automatically
  5. handlers(global = TRUE)
  6. ## Parallelize on local machine
  7. plan(multisession)
  8. ## See https://progressr.futureverse.org/#a-progressor-cannot-be-created-in-the-global-environment
  9. ## for why we use local() here
  10. reprex <- local({
  11. p <- progressor(steps = 10 * 100)
  12. y <- foreach(hh = 1:10) %dofuture% {
  13. for(ii in 1:100){
  14. Sys.sleep(0.1)
  15. ## Report on progress thus far with a custom message
  16. p(sprintf("(hh, ii) = (%d, %d)", hh, ii))
  17. }
  18. }
  19. y
  20. })
  21. ## Stop local cluster
  22. plan(sequential)
展开查看全部
fnvucqvd

fnvucqvd2#

谢谢你的代码,它帮助了很多。
我发现的一个额外的特性是,通过设置class =“sticky”,我可以打印链的一些值,并且它们保留在控制台中。

  1. p(sprintf("precision %.*f, width '%*.3f'", 3, pi, 8, pi), class = "sticky")

相关问题