r语言 - foreach with doParallel 不适用于超过 1 个内核



当我启动这个函数时,我遇到了一个问题:

blocs <- split(df, 1 + (1:nrow(df)) %% ncores)
cl <- makeCluster(ncores)
registerDoParallel(cl)
if (mode == "batch"){
res <- foreach(i = blocs, .combine = "cbind", .export = c("batch_gradient_descent", "sampled_df", "add_constant", "sigmoid", "log_loss_function")) %dopar% {
coefs <- batch_gradient_descent(df, colnames(X), colnames(y), learning_rate, max_iter)
}
return(res)
}

当我用1个核心运行它时,它是有效的。当我使用2个或多个核心时,它不会进入我的foreach函数,不会发生任何事情,我没有错误。我可能会错过一些东西,但经过很多小时的搜索,无法找到解决方案!

有人能给我一个关于这个案子的线索吗?

blocs <- split(df, 1 + (1:nrow(df)) %% ncores)将生成包含相同数据的许多批次的ncores(例如,只有3个副本(。试着这样做:

library(tidyverse)
library(doParallel)
ncores <- 3
df <- iris
blocs <-
df %>%
mutate(batch = row_number() %% ncores) %>%
nest(-batch) %>%
pull(data)
cl <- makeCluster(ncores)
registerDoParallel(cl)
res <- foreach(i = blocs, .combine = "rbind") %dopar% {
Sys.sleep(5)
coefs <- mean(i$Sepal.Length)
}

相关内容

最新更新