同时使用“多核”并行使用foreach和doMC后端(我使用doMC,因为当我研究它的时候,其他包不允许从日志记录,我想得到一个进度条,使用progress包,但任何进度(这在linux终端上工作,即没有tcltk弹出窗口)可以做到。
考虑到它使用了forking,我可以想象这可能是不可能的,但我不确定。
其预期用途是在我并行加载100个文件(通常在#!Rscript中)时指示进度。
我看过一些像How do you create a progress bar when using the “foreach()” function in R?这样的帖子。很高兴对此悬赏。
编辑
悬赏500点给那些教我如何
使用futile.logger的
Reprex
# load packages
library("futile.logger")
library("data.table")
library("foreach")
# create temp dir
tmp_dir <- tempdir()
# create names for 200 files to be created
nb_files <- 200L
file_names <- file.path(tmp_dir, sprintf("file_%s.txt", 1:nb_files))
# make it reproducible
set.seed(1L)
nb_rows <- 1000L
nb_columns <- 10L
# create those 200 files sequentially
foreach(file_i = file_names) %do%
{
DT <- as.data.table(matrix(data = runif(n = nb_rows * nb_columns), nrow = nb_rows))
fwrite(x = DT, file = file_i)
flog.info("Creating file %s", file_i)
} -> tmp
# Load back the files
foreach(file_i = file_names, .final = rbindlist) %dopar%
{
flog.info("Loading file %s", file_i)
# >>> SOME PROGRESS BAR HERE <<<
fread(file_i)
} -> final_data
# show data
final_data
所需的输出
请注意,进度条不会弄乱打印行)
INFO [2018-07-18 19:03:48] Loading file /tmp/RtmpB13Tko/file_197.txt
INFO [2018-07-18 19:03:48] Loading file /tmp/RtmpB13Tko/file_198.txt
INFO [2018-07-18 19:03:48] Loading file /tmp/RtmpB13Tko/file_199.txt
INFO [2018-07-18 19:03:48] Loading file /tmp/RtmpB13Tko/file_200.txt
[ =======> ] 4%
编辑2个
赏金结束后,没有任何东西接近预期的结果。
进度条中的日志记录会使一切变得混乱。如果有人得到了正确的结果,我会给另一个基于结果的赏金。
发布于 2018-07-10 22:10:01
这里有一个使用自定义函数的解决方案(不完美)。
此函数输出到控制台(使用message
)进度条。
ii
是当前的iteration.N
是迭代到perform.per
的总次数是更新进度条的时间步长(百分比)。我们需要它,因为当执行多次迭代时,进度条更新太频繁,输出混乱。功能:
progBar <- function(ii, N, per = 10) {
if (ii %in% seq(1, N, per)) {
x <- round(ii * 100 / N)
message("[ ",
paste(rep("=", x), collapse = ""),
paste(rep("-", 100 - x), collapse = ""),
" ] ", x, "%", "\r",
appendLF = FALSE)
if (ii == N) cat("\r")
}
}
要测试的代码:
library(doMC)
library(foreach)
registerDoMC(10)
nIteration <- 1e3
foreach(i = 1:nIteration, ii = icount()) %dopar% {
# For progBar ii I'm using icount(), because
# user might iterate over all kind of objects
progBar(ii, nIteration)
Sys.sleep(1 / 10)
}
PS:它并不完美,因为:
如果在foreach
中使用99%
)
100%
,100%
(取决于它可以在输出时停止的迭代次数)混乱(取决于迭代次数和切换频率)-仍然调试此部件发布于 2018-07-15 15:37:41
你可以参考这个链接Progress bar parallel来获得一些见解(可能不是确切的解决方案),这将有助于创建一个并行的进度条。
仅当stype为2或3时,txtProgressBar
才有效
library("foreach")
library("doParallel")
library("progress")
registerDoParallel(parallel::makeCluster(7, outfile = ""))
pb <- progress_bar$new(
format = " [:bar] :percent in :elapsed",
total = 30, clear = FALSE, width = 80, force = T)
a <- foreach (i = 1:30) %dopar% {
pb$tick()
Sys.sleep(0.5)
}
pb <- txtProgressBar(title = "Iterative training", min = 0, max = 30, style = 3)
foreach (i = 1:30) %dopar% {
setTxtProgressBar(pb, i)
Sys.sleep(0.5)
}
请参考此链接Monitoring the function with progress bar,了解根据需要可以实现进度条的不同方式。
使用多核:您可以稍后注册一个不同的并行后端,或者通过调用registerDoSEQ函数注册顺序后端来注销doMC。例如,考虑以下程序
> x <- iris[which(iris[,5] != "setosa"), c(1,5)]
> trials <- 10000
> ptime <- system.time({
+ r <- foreach(icount(trials), .combine=cbind) %dopar% {
+ ind <- sample(100, 100, replace=TRUE)
+ result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
+ coefficients(result1)
+ }
+ })[3]
> ptime
发布于 2018-07-07 03:05:15
我用过的一个并行处理列表的包是pbmcapply
,希望它能对我有所帮助。
https://stackoverflow.com/questions/51213293
复制相似问题