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
In reverse order of the questions you asked:
@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.
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,])}