[英]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
包,您现在可以轻松跟踪mclapply
和mcmapply
作业的进度。 只需用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.