In the help for detectCores()
it says:
This is not suitable for use directly for the mc.cores argument of mclapply nor specifying the number of cores in makeCluster. First because it may return NA, and second because it does not give the number of allowed cores.
However, I've seen quite a bit of sample code like the following:
library(parallel)
k <- 1000
m <- lapply(1:7, function(X) matrix(rnorm(k^2), nrow=k))
cl <- makeCluster(detectCores() - 1, type = "FORK")
test <- parLapply(cl, m, solve)
stopCluster(cl)
where detectCores()
is used to specify the number of cores in makeCluster
.
My use cases involve running parallel processing both on my own multicore laptop (OSX) and running it on various multicore servers (Linux). So, I wasn't sure whether there is a better way to specify the number of cores or whether perhaps that advice about not using detectCores
was more for package developers where code is meant to run over a wide range of hardware and OS environments.
So in summary:
- Should you use the
detectCores
function in R to specify the number of cores for parallel processing? - What is the distinction mean between detected and allowed cores and when is it relevant?
system('getconf _NPROCESSORS_ONLN')
– rawr