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

查看:264
本文介绍了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的使用示例:

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天全站免登陆