rparallel-processingdomc

R doMC library detectCores misreporting number of cores


An R instructor provided this code to ensure that students only use half the cores on their PCs/Laptops in parallel processing. However, as shown below, the system report from my mac says I have 4 while this library seems to detect 8. To make sure at least 1 or 2 cores are free, is this code set correctly? Or should I alter the last line maybe with an ifelse() statement that tells it what to do when system specs don't agree with its detection?

R code:

# Setting Parallel processing
library(doMC)
library(parallel)
number_of_cores <- detectCores()
registerDoMC(cores = number_of_cores/2)

Output (how many cores it sees):

[1] 8

What "About this Mac" -> "System Report" reports for specs including cores:

  Model Name:   MacBook Pro
  Model Identifier: MacBookPro14,3
  Processor Name:   Intel Core i7
  Processor Speed:  2.8 GHz
  Number of Processors: 1
  Total Number of Cores:    4
  L2 Cache (per Core):  256 KB
  L3 Cache: 6 MB
  Memory:   16 GB
  Boot ROM Version: MBP143.0160.B02

Solution

  • The issue here is number of threads versus number of cores. Cores are physical, while threads (more or less) split a core and are software driven. This combination of arguments should get you just the cores:

    detectCores(all.tests = FALSE, logical = FALSE)