Execute foreach loop in parallel or sequentially given a condition

后端 未结 3 1210
别跟我提以往
别跟我提以往 2021-02-03 10:15

I often end up with several nested foreach loops and sometimes when writing general functions (e.g. for a package) there is no level which is obvious to parallelize

3条回答
  •  猫巷女王i
    2021-02-03 11:04

    In reverse order of the questions you asked:

    1. @Joris is correct regarding checking for a registered parallel backend. However, note that there is a difference between a machine being single core and whether or not a parallel backend is registered. Checking the # of cores is a very platform (operating system) specific task. On Linux, this may work for you:

      CountUnixCPUs  <- function(cpuinfo = "/proc/cpuinfo"){
      tmpCmd  <- paste("grep processor ", cpuinfo, " | wc -l", sep = "")
      numCPU  <- as.numeric(system(tmpCmd, intern = TRUE))
      return(numCPU)
      }
      

      Edit: See @Joris's link to another page, below, which gives advice for Windows and Linux. I will likely rewrite my own code, at least to include more options for counting cores.

    2. Regarding the nested loops, I take a different tack: I prepare a table of parameters and then iterate over rows. A very simple way is, e.g.:

      library(Matrix)
      ptable <- which(triu(matrix(1, ncol = 20, nrow = 20))==1, arr.ind = TRUE)
      foreach(ix_row = 1:nrow(ptable)) %dopar% { myFunction(ptable[ix_row,])}
      

提交回复
热议问题