From 5aff6c228baf2aa3c4f0b82b34d1389bb33c0984 Mon Sep 17 00:00:00 2001 From: Michael Wild Date: Fri, 8 Oct 2010 09:16:04 +0200 Subject: [PATCH] ENH: Added module ProcessorCount.cmake Credit goes to David Cole (http://www.kitware.com/blog/home/post/63). Signed-off-by: Michael Wild --- Modules/ProcessorCount.cmake | 60 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 60 insertions(+), 0 deletions(-) create mode 100644 Modules/ProcessorCount.cmake diff --git a/Modules/ProcessorCount.cmake b/Modules/ProcessorCount.cmake new file mode 100644 index 0000000..b6ba4f4 --- /dev/null +++ b/Modules/ProcessorCount.cmake @@ -0,0 +1,60 @@ +# - Determine the number of processors/cores +# +# Sets the variable PROCESSOR_COUNT to the number of physical cores available +# on the machine if the information can be determined. Otherwise it is set to +# 0. Currently this functionality is only implemented for Windows, Mac OS X and +# Unix system providing the /proc/cpuinfo interface (e.g. Linux). + +# A more reliable way might be to compile a small C program that uses the CPUID +# instruction, but that again requires compiler support or compiling assembler +# code. + +#============================================================================= +# Copyright 2002-2009 Kitware, Inc. +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distributed this file outside of CMake, substitute the full +# License text for the above reference.) + +if(NOT DEFINED PROCESSOR_COUNT) + # Unknown: + set(PROCESSOR_COUNT 0) + + # Windows: + if(WIN32) + set(PROCESSOR_COUNT "$ENV{NUMBER_OF_PROCESSORS}") + return() + endif() + + # Mac: + if(APPLE) + find_program(_pc_cmd_sysctl sysctl) + if(_pc_cmd_sysctl) + execute_process(COMMAND ${_pc_cmd_sysctl} -n hw.ncpu + OUTPUT_VARIABLE PROCESSOR_COUNT) + endif() + return() + endif() + + find_program(_pc_cmd_getconf getconf) + if(_pc_cmd_getconf) + # Linux and other systems with getconf: + execute_process(COMMAND ${_pc_cmd_getconf} _NPROCESSORS_ONLN + OUTPUT_VARIABLE PROCESSOR_COUNT) + return() + else() + # Linux and other systems with /proc/cpuinfo: + set(_pc_cpuinfo_file /proc/cpuinfo) + if(EXISTS "${_pc_cpuinfo_file}") + file(STRINGS "${_pc_cpuinfo_file}" _pc_procs REGEX "^processor.: [0-9]+$") + list(LENGTH _pc_procs PROCESSOR_COUNT) + return() + endif() + endif() +endif() -- 1.7.2.3