Search code examples
rforeachparallel.foreachdoparallel

why my R code is not parallel CPU when using foreach


I have a long code, so I wrapped them with "foreach" to parallel multi-CPU. But looks like there is only 1 CPU core busy running. enter image description here

Here is my code:

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
}

Solution

  • Like @coletl said, you need to change %do% to %dopar% if you want foreach to run parallel.

    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
    }