detectCores {parallel}R Documentation

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). The default is TRUE on Windows and FALSE elsewhere.

Details

This attempts to detect the number of CPU cores in the current machine.

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

Currently logical is supported on Solaris and Windows only.

Value

An integer, NA if the answer is unknown.

Exactly what this represents is OS-dependent: where possible by default it counts physical cores and not logical (e.g., hyperthreaded) CPUs. However, on Linux, OS X, FreeBSD and OpenBSD it currently returns the number of logical CPUs.

On Windows the default is the number of logical CPUs. Only versions of Windows since XP SP3 are supported. Microsoft documents that 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.

Currently logical makes a difference on Sparc Solaris: there logical = FALSE returns the number of physical cores and logical = TRUE returns the number of available hardware threads. (Some Sparc CPUs which do have multiple cores per CPU, others have multiple threads per core and some have both.) For example, the UltraSparc T2 CPU in the CRAN check server is 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 represents the number of CPUs available (or potentially available) to that particular VM.

Note

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.

Author(s)

Simon Urbanek and Brian Ripley

Examples

detectCores()
detectCores(logical = FALSE)

[Package parallel version 3.2.4 Index]