aboutsummaryrefslogtreecommitdiff
path: root/m4/ax_count_cpus.m4
diff options
context:
space:
mode:
Diffstat (limited to 'm4/ax_count_cpus.m4')
-rw-r--r--m4/ax_count_cpus.m4108
1 files changed, 69 insertions, 39 deletions
diff --git a/m4/ax_count_cpus.m4 b/m4/ax_count_cpus.m4
index 39b68d32..2f5ba70a 100644
--- a/m4/ax_count_cpus.m4
+++ b/m4/ax_count_cpus.m4
@@ -4,18 +4,21 @@
4# 4#
5# SYNOPSIS 5# SYNOPSIS
6# 6#
7# AX_COUNT_CPUS 7# AX_COUNT_CPUS([ACTION-IF-DETECTED],[ACTION-IF-NOT-DETECTED])
8# 8#
9# DESCRIPTION 9# DESCRIPTION
10# 10#
11# Attempt to count the number of processors present on the machine. If the 11# Attempt to count the number of logical processor cores (including
12# detection fails, then a value of 1 is assumed. 12# virtual and HT cores) currently available to use on the machine and
13# place detected value in CPU_COUNT variable.
13# 14#
14# The value is placed in the CPU_COUNT variable. 15# On successful detection, ACTION-IF-DETECTED is executed if present. If
16# the detection fails, then ACTION-IF-NOT-DETECTED is triggered. The
17# default ACTION-IF-NOT-DETECTED is to set CPU_COUNT to 1.
15# 18#
16# LICENSE 19# LICENSE
17# 20#
18# Copyright (c) 2014 Karlson2k (Evgeny Grin) <k2k@narod.ru> 21# Copyright (c) 2014,2016 Karlson2k (Evgeny Grin) <k2k@narod.ru>
19# Copyright (c) 2012 Brian Aker <brian@tangent.org> 22# Copyright (c) 2012 Brian Aker <brian@tangent.org>
20# Copyright (c) 2008 Michael Paul Bailey <jinxidoru@byu.net> 23# Copyright (c) 2008 Michael Paul Bailey <jinxidoru@byu.net>
21# Copyright (c) 2008 Christophe Tournayre <turn3r@users.sourceforge.net> 24# Copyright (c) 2008 Christophe Tournayre <turn3r@users.sourceforge.net>
@@ -25,42 +28,69 @@
25# and this notice are preserved. This file is offered as-is, without any 28# and this notice are preserved. This file is offered as-is, without any
26# warranty. 29# warranty.
27 30
28#serial 10 31#serial 17
29 32
30 AC_DEFUN([AX_COUNT_CPUS],[ 33 AC_DEFUN([AX_COUNT_CPUS],[dnl
31 AC_REQUIRE([AC_CANONICAL_HOST]) 34 AC_REQUIRE([AC_CANONICAL_HOST])dnl
32 AC_REQUIRE([AC_PROG_EGREP]) 35 AC_REQUIRE([AC_PROG_EGREP])dnl
33 AC_MSG_CHECKING([the number of available CPUs]) 36 AC_MSG_CHECKING([the number of available CPUs])
34 CPU_COUNT="0" 37 CPU_COUNT="0"
35 38
36 AS_CASE([$host_os],[ 39 # Try generic methods
37 *darwin*],[
38 AS_IF([test -x /usr/sbin/sysctl],[
39 sysctl_a=`/usr/sbin/sysctl -a 2>/dev/null| grep -c hw.cpu`
40 AS_IF([test sysctl_a],[
41 CPU_COUNT=`/usr/sbin/sysctl -n hw.ncpu`
42 ])
43 ])],[
44 *linux*],[
45 AS_IF([test "x$CPU_COUNT" = "x0" -a -e /proc/cpuinfo],[
46 AS_IF([test "x$CPU_COUNT" = "x0" -a -e /proc/cpuinfo],[
47 CPU_COUNT=`$EGREP -c '^processor' /proc/cpuinfo`
48 ])
49 ])],[
50 *mingw*],[
51 AS_IF([test -n "$NUMBER_OF_PROCESSORS"],[
52 CPU_COUNT="$NUMBER_OF_PROCESSORS"
53 ])],[
54 *cygwin*],[
55 AS_IF([test -n "$NUMBER_OF_PROCESSORS"],[
56 CPU_COUNT="$NUMBER_OF_PROCESSORS"
57 ])
58 ])
59 40
60 AS_IF([test "x$CPU_COUNT" = "x0"],[ 41 # 'getconf' is POSIX utility, '_SC_NPROCESSORS_ONLN' is optional
61 CPU_COUNT="1" 42 CPU_COUNT=`getconf _NPROCESSORS_ONLN 2>/dev/null | $EGREP -e '^@<:@0-9@:>@+'` || CPU_COUNT="0"
62 AC_MSG_RESULT( [unable to detect (assuming 1)] ) 43 AS_IF([[test "$CPU_COUNT" -gt "0" 2>/dev/null]],[[# empty]],[dnl
63 ],[ 44 # 'nproc' is part of GNU Coreutils and is widely available
64 AC_MSG_RESULT( $CPU_COUNT ) 45 CPU_COUNT=`OMP_NUM_THREADS='' nproc 2>/dev/null` || CPU_COUNT=`nproc 2>/dev/null` || CPU_COUNT="0"
65 ]) 46 ])dnl
66 ]) 47
48 AS_IF([[test "$CPU_COUNT" -gt "0" 2>/dev/null]],[[# empty]],[dnl
49 # Try platform-specific preferred methods
50 AS_CASE([[$host_os]],dnl
51 [[*linux*]],[[CPU_COUNT=`lscpu -p 2>/dev/null | $EGREP -e '^@<:@0-9@:>@+,' -c` || CPU_COUNT="0"]],dnl
52 [[*darwin*]],[[CPU_COUNT=`sysctl -n hw.logicalcpu 2>/dev/null` || CPU_COUNT="0"]],dnl
53 [[freebsd*]],[[CPU_COUNT=`sysctl -n kern.smp.cpus 2>/dev/null` || CPU_COUNT="0"]],dnl
54 [[solaris*]],[[CPU_COUNT=`psrinfo 2>/dev/null | $EGREP -e '^@<:@0-9@:>@.*on-line' -c 2>/dev/null` || CPU_COUNT="0"]],dnl
55 [[mingw*]],[[CPU_COUNT=`ls -qpU1 /proc/registry/HKEY_LOCAL_MACHINE/HARDWARE/DESCRIPTION/System/CentralProcessor/ 2>/dev/null | $EGREP -e '^@<:@0-9@:>@+/' -c` || CPU_COUNT="0"]],dnl
56 [[msys*]],[[CPU_COUNT=`ls -qpU1 /proc/registry/HKEY_LOCAL_MACHINE/HARDWARE/DESCRIPTION/System/CentralProcessor/ 2>/dev/null | $EGREP -e '^@<:@0-9@:>@+/' -c` || CPU_COUNT="0"]],dnl
57 [[cygwin*]],[[CPU_COUNT=`ls -qpU1 /proc/registry/HKEY_LOCAL_MACHINE/HARDWARE/DESCRIPTION/System/CentralProcessor/ 2>/dev/null | $EGREP -e '^@<:@0-9@:>@+/' -c` || CPU_COUNT="0"]]dnl
58 )dnl
59 ])dnl
60
61 AS_IF([[test "$CPU_COUNT" -gt "0" 2>/dev/null]],[[# empty]],[dnl
62 # Try less preferred generic method
63 # 'hw.ncpu' exist on many platforms, but not on GNU/Linux
64 CPU_COUNT=`sysctl -n hw.ncpu 2>/dev/null` || CPU_COUNT="0"
65 ])dnl
66
67 AS_IF([[test "$CPU_COUNT" -gt "0" 2>/dev/null]],[[# empty]],[dnl
68 # Try platform-specific fallback methods
69 # They can be less accurate and slower then preferred methods
70 AS_CASE([[$host_os]],dnl
71 [[*linux*]],[[CPU_COUNT=`$EGREP -e '^processor' -c /proc/cpuinfo 2>/dev/null` || CPU_COUNT="0"]],dnl
72 [[*darwin*]],[[CPU_COUNT=`system_profiler SPHardwareDataType 2>/dev/null | $EGREP -i -e 'number of cores:'|cut -d : -f 2 -s|tr -d ' '` || CPU_COUNT="0"]],dnl
73 [[freebsd*]],[[CPU_COUNT=`dmesg 2>/dev/null| $EGREP -e '^cpu@<:@0-9@:>@+: '|sort -u|$EGREP -e '^' -c` || CPU_COUNT="0"]],dnl
74 [[solaris*]],[[CPU_COUNT=`kstat -m cpu_info -s state -p 2>/dev/null | $EGREP -c -e 'on-line'` || \
75 CPU_COUNT=`kstat -m cpu_info 2>/dev/null | $EGREP -c -e 'module: cpu_info'` || CPU_COUNT="0"]],dnl
76 [[mingw*]],[AS_IF([[CPU_COUNT=`reg query 'HKLM\\Hardware\\Description\\System\\CentralProcessor' 2>/dev/null | $EGREP -e '\\\\@<:@0-9@:>@+$' -c`]],dnl
77 [[# empty]],[[test "$NUMBER_OF_PROCESSORS" -gt "0" 2>/dev/null && CPU_COUNT="$NUMBER_OF_PROCESSORS"]])],dnl
78 [[msys*]],[[test "$NUMBER_OF_PROCESSORS" -gt "0" 2>/dev/null && CPU_COUNT="$NUMBER_OF_PROCESSORS"]],dnl
79 [[cygwin*]],[[test "$NUMBER_OF_PROCESSORS" -gt "0" 2>/dev/null && CPU_COUNT="$NUMBER_OF_PROCESSORS"]]dnl
80 )dnl
81 ])dnl
82
83 AS_IF([[test "x$CPU_COUNT" != "x0" && test "$CPU_COUNT" -gt 0 2>/dev/null]],[dnl
84 AC_MSG_RESULT([[$CPU_COUNT]])
85 m4_ifvaln([$1],[$1],)dnl
86 ],[dnl
87 m4_ifval([$2],[dnl
88 AS_UNSET([[CPU_COUNT]])
89 AC_MSG_RESULT([[unable to detect]])
90 $2
91 ], [dnl
92 CPU_COUNT="1"
93 AC_MSG_RESULT([[unable to detect (assuming 1)]])
94 ])dnl
95 ])dnl
96 ])dnl