Learn R Programming

parallel (version 3.6.2)

detectCores: Detect the Number of CPU Cores

Description

Attempt to detect the number of CPU cores on the current host.

Usage

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

Arguments

all.tests

Logical: if true apply all known tests.

logical

Logical: if possible, use the number of physical CPUs/cores (if FALSE) or logical CPUs (if TRUE). Currently this is honoured only on macOS, Solaris and Windows.

Value

An integer, NA if the answer is unknown.

Exactly what this represents is OS-dependent: where possible by default it counts logical (e.g., hyperthreaded) CPUs and not physical cores or packages.

Under macOS there is a further distinction between ‘available in the current power management mode’ and ‘could be available this boot’, and this function returns the first.

Only versions of Windows since XP SP3 are supported. Microsoft documents that with logical = FALSE it will report the number of cores on Vista or later, but the number of physical CPU packages on XP or Server 2003: however it reported correctly on the XP systems we tested.

On Sparc Solaris logical = FALSE returns the number of physical cores and logical = TRUE returns the number of available hardware threads. (Some Sparc CPUs have multiple cores per CPU, others have multiple threads per core and some have both.) For example, the UltraSparc T2 CPU in the former CRAN check server was a single physical CPU with 8 cores, and each core supports 8 hardware threads. So detectCores(logical = FALSE) returns 8, and detectCores(logical = TRUE) returns 64.

Where virtual machines are in use, one would hope that the result for logical = TRUE represents the number of CPUs available (or potentially available) to that particular VM.

Details

This attempts to detect the number of available CPU cores.

It has methods to do so for Linux, macOS, FreeBSD, OpenBSD, Solaris, Irix and Windows. detectCores(TRUE) could be tried on other Unix-alike systems.

Examples

Run this code
# NOT RUN {
detectCores()
detectCores(logical = FALSE)
# }

Run the code above in your browser using DataLab