我正在运行一个函数,该函数使用biganalytics::bigkmeans和xgboost (通过Caret)。如果首先通过执行registerDoMC(cores = 4)注册并行处理,这两种方法都支持并行处理。但是,为了在不增加太多并行开销的情况下使用我可以访问的64核心机器的功能,我想在16个实例中运行以下函数(总共64个进程)。
example = function (x) {
biganalytics:: bigkmeans (matrix(rnorm(10*5,1000,1),ncol=500))
mod <- train(Class ~ ., data = df ,
method = "xgbTree", tuneLength = 50,
trControl = trainControl(search = "random"))
}
set.seed(1)
dat1 <- twoClassSim(1000)
dat2 <- twoClassSim(1001)
dat3 <- twoClassSim(1002)
dat4 <- twoClassSim(1003)
list <- list(dat1, dat2, dat3, dat4)
mclapply(list, example, mc.cores = 16).我坚持使用mclapply是很重要的,因为我需要一个共享内存并行后端,以便在实际使用超过50 my的数据集时不会耗尽ram。
我的问题是,在这种情况下,我会在哪里做registerDoMC呢?
谢谢!
发布于 2018-02-14 16:19:00
使用嵌套并行通常不是一个好主意,但如果外部循环的迭代次数比核心少得多,则可能是这样。
您可以在foreach循环中加载doMC并调用registerDoMC,以便准备工作人员调用train。但是请注意,使用更多的工作人员而不是任务调用mclapply是没有意义的,否则一些员工将没有任何工作要做。
你可以这样做:
example <- function (dat, nw) {
library(doMC)
registerDoMC(nw)
# call train function on dat...
}
# This assumes that length(datlist) is much less than ncores
ncores <- 64
m <- length(datlist)
nw <- ncores %/% m
mclapply(datlist, example, nw, mc.cores=m)如果length(datlist)是4,那么每个“列车”任务将使用16名工人。当然,在“培训”任务中可以使用更少的工人,但您可能不应该使用更多。
https://stackoverflow.com/questions/48763888
复制相似问题