[英]why my R code is not parallel CPU when using foreach
我的代碼很長,所以我用“foreach”將它們包裝到並行多 CPU。 但看起來只有 1 個 CPU 核心在忙着運行。
這是我的代碼:
library(doParallel)
library(foreach)
no_cores <- detectCores() - 2 # leave 2 core2 for the system
registerDoParallel(cores = no_cores)
ReadList <- read_excel("E:xxxx")
foreach(Index = 1:nrow(ReadList)) %do% {
# code body part is huge
write.table(D.results, quote = FALSE, sep = " ", paste(outputpath, "Daily_", List$Gid[Index], ".txt", sep="")) # I output result for each Index within the loop
}
就像@coletl 所說,如果您希望 foreach 並行運行,您需要將%do%
更改為%dopar%
。
library(doParallel)
library(foreach)
no_cores <- detectCores() - 2 # leave 2 core2 for the system
registerDoParallel(cores = no_cores)
ReadList <- read_excel("E:xxxx")
foreach(Index = 1:nrow(ReadList)) %dopar% {
# code body part is huge
write.table(D.results, quote = FALSE, sep = " ", paste(outputpath, "Daily_", List$Gid[Index], ".txt", sep="")) # I output result for each Index within the loop
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.