5

我的问题与这个问题有关。然而,上面提到的问题使用multicore包被替换为parallel. 响应中的大多数功能无法在parallel包中复制。有没有办法跟踪进度mclapply。在查看mclapply文档时,有一个名为 的参数mc.silent,我不确定这是否能够跟踪进度,如果可以,我们如何以及在哪里可以看到日志文件?我在ubuntulinux操作系统上运行。请参阅下面的一个可重复的示例,我想对其进行改进。

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)

任何帮助将不胜感激。

4

2 回答 2

10

借助该软件包pbmcapply,您现在可以轻松跟踪mclapply工作进度mcmapply。只需替换mclapplypbmclapply

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

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

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

作者在这里有一篇关于技术细节和性能基准的不错的博客文章

于 2016-11-21T18:30:49.460 回答
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")
于 2015-01-01T10:00:29.350 回答