R doMC 库 detectCores 误报核心数

R doMC library detectCores misreporting number of cores

一位 R 讲师提供了这段代码,以确保学生在并行处理中只使用 PCs/Laptops 上的一半内核。但是,如下所示,我的 mac 的系统报告说我有 4 个,而这个库似乎检测到 8 个。为确保至少有 1 或 2 个核心空闲,此代码设置是否正确?或者我是否应该用 ifelse() 语句更改最后一行,告诉它当系统规格不同意其检测时该怎么做?

R代码:

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

输出(它看到多少核):

[1] 8

"About this Mac" -> "System Report" 报告的规格包括内核:

  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

这里的问题是线程数与内核数。内核是物理的,而线程(或多或少)拆分一个内核并由软件驱动。这种参数组合应该让你只是核心:

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