1

Is there a way to check if hyperthreading is enabled from within R?

The best I can think of now is to run the following system call, but this requires root privileges, and has to be parsed.

system("dmidecode -t processor | grep HTT")
Megatron
  • 15,909
  • 12
  • 89
  • 97

1 Answers1

1

Following up on F. Privé's comment, the detectCores function from the parallel package should be able to accomplish what you want with its second argument, logical. From ?parallel::detectCores:

logical: Logical: if possible, use the number of physical CPUs/cores (if FALSE) or logical CPUs (if TRUE). The default is TRUE on Windows and FALSE elsewhere.

For my machine, a 4-core hyperthreaded i-7, I get

# physical cores
parallel::detectCores(logical=FALSE)
[1] 4
# logical cores (threads)
parallel::detectCores(logical=TRUE)
[1] 8
lmo
  • 37,904
  • 9
  • 56
  • 69
  • 1
    Thanks! The default appears to be "TRUE" on all platforms - just tested it on macOS, windows and linux. I'll note that on my linux box, I've got 2 Xeon processors with 6 cores each. Running `detectCores(logical=T)` yields `24` and `detectCores(logical=F)` yields `6`, neither of which is the correct number of physical cores which is `12`. – Megatron Oct 25 '17 at 18:49
  • I'm currently using 3.2.5 (on Windows), so there may have been an update in the default value or in the help file in the meantime. That's interesting that it doesn't notice one CPU with `logical=FALSE`. `logical=TRUE` returns the correct number of threads, however. – lmo Oct 25 '17 at 18:51
  • I don't think this answers the question. `parallel::detectCores` only tells the number of cores in the system. But it does not tell whether hyperthreading is enabled in R. – user3175783 Aug 27 '19 at 17:50
  • @user3175783 you obviously did not read the entire answer. The logical argument tells R whether or not to check for threads. `logical=TRUE` counts the threads, while `logical=FALSE` only counts physical cores. Whether or not your code takes advantage of hyperthreading or multiple cores is another issue. – lmo Aug 31 '19 at 11:34