How many ya got?
At the end of this script snippet, the CMake variable PROCESSOR_COUNT has a value appropriate for passing to make’s -j for parallel builds.
When used in a ctest -S script, you can call…
if(PROCESSOR_COUNT)
set(CTEST_BUILD_FLAGS “-j${PROCESSOR_COUNT}”)
endif()
…to enable parallel builds with “Unix Makefiles” and the ctest_build command.
Here’s the snippet:
if(NOT DEFINED PROCESSOR_COUNT)
# Unknown:
set(PROCESSOR_COUNT 0)
# Linux:
set(cpuinfo_file “/proc/cpuinfo”)
if(EXISTS “${cpuinfo_file}”)
file(STRINGS “${cpuinfo_file}” procs REGEX “^processor.: [0-9]+$”)
list(LENGTH procs PROCESSOR_COUNT)
endif()
# Mac:
if(APPLE)
find_program(cmd_sys_pro “system_profiler”)
if(cmd_sys_pro)
execute_process(COMMAND ${cmd_sys_pro} OUTPUT_VARIABLE info)
string(REGEX REPLACE “^.*Total Number Of Cores: ([0-9]+).*$” “\\1”
PROCESSOR_COUNT “${info}”)
endif()
endif()
# Windows:
if(WIN32)
set(PROCESSOR_COUNT “$ENV{NUMBER_OF_PROCESSORS}”)
endif()
endif()
Cheers!
So, my buddy Marcus tells me we should add 1 to the PROCESSOR_COUNT to take better advantage of the full machine. Rule of thumb and all that. It’s worked for me for years, I’ve never had a problem…
Well, I generally avoid math where possible in CMake files, because it’s possible, but not exactly pretty. Just my opinion and all that.
But if you were so inclined, here’s the chunk with one line added that you’d need to use -j with N+1:
if(PROCESSOR_COUNT)
math(EXPR PROCESSOR_COUNT “${PROCESSOR_COUNT} + 1”)
set(CTEST_BUILD_FLAGS “-j${PROCESSOR_COUNT}”)
endif()
Very cool, and the required line looks much simpler than I thought 😉
At least linux supports querying processor count using getconf (“getconf _NPROCESSORS_ONLN”) without the hassle of parsing /proc/cpuinfo manually.
Cya, Ed
On a Mac you can get the number of CPUs with this command:
sysctl hw.ncpu
Or on a hyperthreaded machine, you can get the number of physical cores like this:
sysctl hw.physicalcpu
system_profiler is a pretty slow command and parsing its output is kinda gross.
How can I make use of that when I want to build a project without all those CTest stuff. I tried CTEST_BUILD_FLAGS with cmake –build but it built only on one core…
Thanks!
This functionality is now officially in the CMake Module ProcessorCount. It has been in the official release of CMake since version 2.8.5.
You may read details about its inclusion, in the bug tracker: http://public.kitware.com/Bug/view.php?id=11302