繁体   English   中英

如何在并行包中跟踪R中mclapply的进度

[英]how to track progress in mclapply in R in parallel package

我的问题与这个问题有关 但是,上面引用的问题使用了parallel替换的multicore包。 响应中的大多数函数都无法在parallel包中复制。 有没有办法跟踪mclapply进度。 在查看mclapply文档时,有一个名为mc.silent的参数,我不确定这是否能跟踪进度,如果可以,我们如何以及在哪里可以看到日志文件? 我在ubuntu linux OS上运行。 请参阅下面的重复性示例,我想对其进行处理。

require(parallel) 

wait.then.square <- function(xx){
  # Wait for one second
  Sys.sleep(2);
  # Square the argument 
  xx^2 } 

output <- mclapply( 1:10, wait.then.square, mc.cores=4,mc.silent=FALSE)

任何帮助将不胜感激。

感谢pbmcapply包,您现在可以轻松跟踪mclapplymcmapply作业的进度。 只需用mclapply替换pbmclapply

wait.then.square <- function(xx) {
    Sys.sleep(2)
    xx^2 
} 

library(pbmcapply)
output <- pbmclapply(1:10, wait.then.square, mc.cores = 4)

...这将显示一个漂亮的进度条。

笔者对技术细节和性能基准一篇好的博客贴子在这里

这是我相关答案的更新。

library(parallel)

finalResult <- local({
  f <- fifo(tempfile(), open="w+b", blocking=T)
  if (inherits(parallel:::mcfork(), "masterProcess")) {
    # Child
    progress <- 0.0
    while (progress < 1 && !isIncomplete(f)) {
      msg <- readBin(f, "double")
      progress <- progress + as.numeric(msg)
      cat(sprintf("Progress: %.2f%%\n", progress * 100))
    } 
    parallel:::mcexit()
  }
  numJobs <- 100
  result <- mclapply(1:numJobs, function(...) {
    # Do something fancy here... For this example, just sleep
    Sys.sleep(0.05)
    # Send progress update
    writeBin(1/numJobs, f)
    # Some arbitrary result
    sample(1000, 1)
  })
  close(f)
  result
})

cat("Done\n")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM