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 Linux, OS X, Sparc Solaris and Windows.
Details
This attempts to detect the number of available CPU cores.
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.
Prior to R 3.3.0 the default was logical = FALSE except on
Windows, but logical = TRUE had an effect only on Sparc Solaris
and Windows (where it was the default).
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 OS X 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.
On Sparc Solaris 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, second
because it does not give the number of allowed cores, and third
because on Sparc Solaris and some Windows boxes it is not reasonable
to try to use all the logical CPUs at once.
Author(s)
Simon Urbanek and Brian Ripley
Examples
detectCores()
detectCores(logical = FALSE)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(parallel)
> png(filename="/home/ddbj/snapshot/RGM3/R_rel/result/parallel/detectCores.Rd_%03d_medium.png", width=480, height=480)
> ### Name: detectCores
> ### Title: Detect the Number of CPU Cores
> ### Aliases: detectCores
>
> ### ** Examples
>
> detectCores()
[1] 4
> detectCores(logical = FALSE)
[1] 2
>
>
>
>
>
> dev.off()
null device
1
>