ProcessorCount¶
This module provides the following function to determine the number of processors/cores:
- ProcessorCount¶
ProcessorCount(<variable>)
Sets a local variable named
<variable>
to the number of logical CPU cores available on the machine, if the information can be determined. If successful, the variable is guaranteed to be set to a positive integer (>=1). If the processor count cannot be determined, it is set to 0.Currently, this functionality is implemented for AIX, Cygwin, FreeBSD, Haiku, HPUX, Linux, macOS, QNX, Sun and Windows.
This function provides an approximation of the number of compute cores available on the current machine, making it useful for parallel building and testing. It is meant to help utilize as much of the machine as seems reasonable, though users should consider other workloads running on the machine before using its full capacity for parallel tasks.
Changed in version 3.15: On Linux, returns the container CPU count instead of the host CPU count.
Note
This module relies on system-dependent commands to determine the number of
processors, which may not always provide accurate information in certain
environments. A more generally accurate logical CPU count can be also
obtained with the cmake_host_system_information()
:
cmake_host_system_information(RESULT n QUERY NUMBER_OF_LOGICAL_CORES)
Examples¶
Using ProcessorCount
module in a ctest -S
dashboard script:
include(ProcessorCount)
ProcessorCount(n)
if(NOT n EQUAL 0)
set(CTEST_BUILD_FLAGS -j${n})
set(ctest_test_args ${ctest_test_args} PARALLEL_LEVEL ${n})
endif()