[CMake] How to determine the number of processors available from CMake code

Michael Wild themiwi at gmail.com
Fri Oct 8 03:24:19 EDT 2010


Would be nice to have this as a module: http://cmake.org/Bug/view.php?id=11302


On 8. Oct, 2010, at 24:41 , David Cole wrote:

> Thanks!
> 
> On Thu, Oct 7, 2010 at 6:08 PM, Mark Moll <mmoll at rice.edu> wrote:
> 
>> On OS X, this command might be easier / faster:
>> 
>> sysctl -n hw.ncpu
>> 
>> On Oct 7, 2010, at 3:50 PM, David Cole wrote:
>> 
>>> I just posted a short blog article demonstrating how to figure out the
>> number of processors available for a "make -j" or a scripted ctest_build
>> call (with the "Unix Makefiles" generator) on Linux, Mac and Windows. Please
>> let me know if you have any ideas for how it might be improved or extended.
>>> 
>>> http://www.kitware.com/blog/home/post/63
>>> 
>>> 
>>> Cheers,
>>> David
>>> 
>>> 
>>> Blog text copied here, too, for search-ability on the mailing list
>> archives:
>>> 
>> ==============================================================================
>>> 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()
>>> 
>>> _______________________________________________
>>> Powered by www.kitware.com
>>> 
>>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>> 
>>> Please keep messages on-topic and check the CMake FAQ at:
>> http://www.cmake.org/Wiki/CMake_FAQ
>>> 
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.cmake.org/mailman/listinfo/cmake
>> 
>> --
>> Mark Moll
>> 
>> 
>> 
>> 
> _______________________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake

--
There is always a well-known solution to every human problem -- neat, plausible, and wrong.
H. L. Mencken

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
URL: <http://www.cmake.org/pipermail/cmake/attachments/20101008/b0fa4a2c/attachment.pgp>


More information about the CMake mailing list