首页 > 解决方案 > 使用 doParallel + foreach 显示 progress_bar

问题描述

我正在使用此处发布的示例代码来显示带有+的progress_bar(来自progress包)。然而,那里的解决方案使用(例如我用于测试的 Dewey Brooke 的代码),它比构建带有 CRAN 标志的包时更过时并返回此注释:doParallelforeachdoSNOWdoParallel

使用被取代的包:'doSNOW (>= 1.0.19)'</p>

改变这似乎并不像预期的那么简单。If onlyregisterDoSNOW替换为,registerDoParallel并且代码将运行,但在第二种情况下根本不会显示任何进度条。.options.snow.options.doparallel

我认为这可能与使用.options.X. 这部分代码对我来说非常模糊,因为.options.snow在 using 时确实有效doSNOW,但是没有foreach关于使用此参数的手册页的文档。因此,不起作用也就不足为奇了.options.doparallel,因为这只是我的一个疯狂猜测。

pb$tick()在循环中包含对的调用foreach也不起作用,实际上会导致结果错误。所以我真的不知道应该在代码中的什么位置包含它。

从哪里来.options.snow?应该去哪里pb$tick(),如何显示这里progress_bar使用的对象doParallel

为方便起见,我粘贴在代码下方(doSNOW替换为doParallel),但再次注明原始来源

library(parallel)
library(doParallel)

numCores<-detectCores()
cl <- makeCluster(numCores)
registerDoParallel(cl)

# progress bar ------------------------------------------------------------
library(progress)

iterations <- 100                               # used for the foreach loop  

pb <- progress_bar$new(
  format = "letter = :letter [:bar] :elapsed | eta: :eta",
  total = iterations,    # 100 
  width = 60)

progress_letter <- rep(LETTERS[1:10], 10)  # token reported in progress bar

# allowing progress bar to be used in foreach -----------------------------
progress <- function(n){
  pb$tick(tokens = list(letter = progress_letter[n]))
} 

opts <- list(progress = progress)

# foreach loop ------------------------------------------------------------
library(foreach)

foreach(i = 1:iterations, .combine = rbind, .options.doparallel = opts) %dopar% {
  summary(rnorm(1e6))[3]
}

stopCluster(cl) 

标签: rforeachprogress-bardoparallel

解决方案


doParallel still uses the .options.snow argument for whatever reason. Found this little tidbit in the doParallel documentation.

The doParallel backend supports both multicore and snow options passed through the foreach function. The supported multicore options are 1st preschedule, set.seed, silent, and cores, which are analogous to the similarly named arguments to mclapply, and are passed using the .options.multicore argument to foreach. The supported snow options are preschedule, which like its multicore analog can be used to chunk the tasks so that each worker gets a prescheduled chunk of tasks, and attachExportEnv, which can be used to attach the export environment in certain cases where R’s lexical scoping is unable to find a needed export. The snow options are passed to foreach using the .options.snow argument.

foreach is powerful package but whoever is maintaining it makes odd decisions.


推荐阅读