簡體   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