parSapply 和进度条 [英] parSapply and progress bar

查看:26
本文介绍了parSapply 和进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 parSapply 函数在并行环境中运行模拟.这是我的代码:

I am using the function parSapply to run a simulation on the parallel environment. Here is my code:

runpar <- function(i) MonteCarloKfun(i=i)

# Detect number of cores available
ncores <- detectCores(logical=TRUE)

# Set up parallel environment
cl <- makeCluster(ncores, methods=FALSE)

# Export objects to parallel environment
clusterSetRNGStream(cl,1234567) # not necessary since we do not sample
clusterExport(cl, c("kfunctions","frq","dvec","case","control","polygon", "MonteCarloKfun", "khat", 
                    "as.points", "secal"))

# For 1 parameter use parSapply
outpar <- parSapply(cl,i,runpar)

# close parallel environment
stopCluster(cl)

有谁知道是否有可能向 parSapply 函数添加进度条.理想情况下,我想要类似于 pbapply 库的 pbapply 的东西.

Does anyone know if there is a possibility to add a progress bar to the parSapply function. Ideally I would like something similar to pbapply of the pbapply library.

推荐答案

parSapply 函数不支持进度条,我认为没有什么好的实现方法通过向任务函数添加额外的代码,尽管人们为此付出了巨大的努力.

The parSapply function doesn't support a progress bar, and I don't think there is any really good way to implement one by adding extra code to the task function, although people have made valiant efforts to do that.

doSNOW 包支持进度条,因此您可以直接使用它,也可以编写一个与 parSapply 函数类似的包装函数.这是编写此类包装函数的一种方法:

The doSNOW package supports progress bars, so you could either use that directly or write a wrapper function that works like the parSapply function. Here's one way to write such a wrapper function:

# This function is similar to "parSapply", but doesn't preschedule
# tasks and doesn't support "simplify" and "USE.NAMES" options
pbSapply <- function(cl, X, FUN, ...) {
  registerDoSNOW(cl)
  pb <- txtProgressBar(max=length(X))
  on.exit(close(pb))
  progress <- function(n) setTxtProgressBar(pb, n)
  opts <- list(progress=progress)
  foreach(i=X, .combine='c', .options.snow=opts) %dopar% {
    FUN(i, ...)
  }
}

您可以轻松修改此函数以使用 tkProgressBarwinProgressBar 函数.

You can easily modify this function to use either the tkProgressBar or winProgressBar function.

下面是 pbSapply 的一个例子:

Here's an example use of pbSapply:

library(doSNOW)
cl <- makeSOCKcluster(3)
x <- pbSapply(cl, 1:100, function(i, j) {Sys.sleep(1); i + j}, 100)

请注意,这不使用预调度,因此如果您有小任务,性能将不如 parSapply.

Note that this doesn't use prescheduling, so the performance won't be as good as parSapply if you have small tasks.

这篇关于parSapply 和进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆