ax_count_cpus.m4 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # ===========================================================================
  2. # https://www.gnu.org/software/autoconf-archive/ax_count_cpus.html
  3. # ===========================================================================
  4. #
  5. # SYNOPSIS
  6. #
  7. # AX_COUNT_CPUS([ACTION-IF-DETECTED],[ACTION-IF-NOT-DETECTED])
  8. #
  9. # DESCRIPTION
  10. #
  11. # Attempt to count the number of logical processor cores (including
  12. # virtual and HT cores) currently available to use on the machine and
  13. # place detected value in CPU_COUNT variable.
  14. #
  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.
  18. #
  19. # LICENSE
  20. #
  21. # Copyright (c) 2014,2016 Karlson2k (Evgeny Grin) <k2k@narod.ru>
  22. # Copyright (c) 2012 Brian Aker <brian@tangent.org>
  23. # Copyright (c) 2008 Michael Paul Bailey <jinxidoru@byu.net>
  24. # Copyright (c) 2008 Christophe Tournayre <turn3r@users.sourceforge.net>
  25. #
  26. # Copying and distribution of this file, with or without modification, are
  27. # permitted in any medium without royalty provided the copyright notice
  28. # and this notice are preserved. This file is offered as-is, without any
  29. # warranty.
  30. #serial 22
  31. AC_DEFUN([AX_COUNT_CPUS],[dnl
  32. AC_REQUIRE([AC_CANONICAL_HOST])dnl
  33. AC_REQUIRE([AC_PROG_EGREP])dnl
  34. AC_MSG_CHECKING([the number of available CPUs])
  35. CPU_COUNT="0"
  36. # Try generic methods
  37. # 'getconf' is POSIX utility, but '_NPROCESSORS_ONLN' and
  38. # 'NPROCESSORS_ONLN' are platform-specific
  39. command -v getconf >/dev/null 2>&1 && \
  40. CPU_COUNT=`getconf _NPROCESSORS_ONLN 2>/dev/null || getconf NPROCESSORS_ONLN 2>/dev/null` || CPU_COUNT="0"
  41. AS_IF([[test "$CPU_COUNT" -gt "0" 2>/dev/null || ! command -v nproc >/dev/null 2>&1]],[[: # empty]],[dnl
  42. # 'nproc' is part of GNU Coreutils and is widely available
  43. CPU_COUNT=`OMP_NUM_THREADS='' nproc 2>/dev/null` || CPU_COUNT=`nproc 2>/dev/null` || CPU_COUNT="0"
  44. ])dnl
  45. AS_IF([[test "$CPU_COUNT" -gt "0" 2>/dev/null]],[[: # empty]],[dnl
  46. # Try platform-specific preferred methods
  47. AS_CASE([[$host_os]],dnl
  48. [[*linux*]],[[CPU_COUNT=`lscpu -p 2>/dev/null | $EGREP -e '^@<:@0-9@:>@+,' -c` || CPU_COUNT="0"]],dnl
  49. [[*darwin*]],[[CPU_COUNT=`sysctl -n hw.logicalcpu 2>/dev/null` || CPU_COUNT="0"]],dnl
  50. [[freebsd*]],[[command -v sysctl >/dev/null 2>&1 && CPU_COUNT=`sysctl -n kern.smp.cpus 2>/dev/null` || CPU_COUNT="0"]],dnl
  51. [[netbsd*]], [[command -v sysctl >/dev/null 2>&1 && CPU_COUNT=`sysctl -n hw.ncpuonline 2>/dev/null` || CPU_COUNT="0"]],dnl
  52. [[solaris*]],[[command -v psrinfo >/dev/null 2>&1 && CPU_COUNT=`psrinfo 2>/dev/null | $EGREP -e '^@<:@0-9@:>@.*on-line' -c 2>/dev/null` || CPU_COUNT="0"]],dnl
  53. [[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
  54. [[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
  55. [[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
  56. )dnl
  57. ])dnl
  58. AS_IF([[test "$CPU_COUNT" -gt "0" 2>/dev/null || ! command -v sysctl >/dev/null 2>&1]],[[: # empty]],[dnl
  59. # Try less preferred generic method
  60. # 'hw.ncpu' exist on many platforms, but not on GNU/Linux
  61. CPU_COUNT=`sysctl -n hw.ncpu 2>/dev/null` || CPU_COUNT="0"
  62. ])dnl
  63. AS_IF([[test "$CPU_COUNT" -gt "0" 2>/dev/null]],[[: # empty]],[dnl
  64. # Try platform-specific fallback methods
  65. # They can be less accurate and slower then preferred methods
  66. AS_CASE([[$host_os]],dnl
  67. [[*linux*]],[[CPU_COUNT=`$EGREP -e '^processor' -c /proc/cpuinfo 2>/dev/null` || CPU_COUNT="0"]],dnl
  68. [[*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
  69. [[freebsd*]],[[CPU_COUNT=`dmesg 2>/dev/null| $EGREP -e '^cpu@<:@0-9@:>@+: '|sort -u|$EGREP -e '^' -c` || CPU_COUNT="0"]],dnl
  70. [[netbsd*]], [[CPU_COUNT=`command -v cpuctl >/dev/null 2>&1 && cpuctl list 2>/dev/null| $EGREP -e '^@<:@0-9@:>@+ .* online ' -c` || \
  71. CPU_COUNT=`dmesg 2>/dev/null| $EGREP -e '^cpu@<:@0-9@:>@+ at'|sort -u|$EGREP -e '^' -c` || CPU_COUNT="0"]],dnl
  72. [[solaris*]],[[command -v kstat >/dev/null 2>&1 && CPU_COUNT=`kstat -m cpu_info -s state -p 2>/dev/null | $EGREP -c -e 'on-line'` || \
  73. CPU_COUNT=`kstat -m cpu_info 2>/dev/null | $EGREP -c -e 'module: cpu_info'` || CPU_COUNT="0"]],dnl
  74. [[mingw*]],[AS_IF([[CPU_COUNT=`reg query 'HKLM\\Hardware\\Description\\System\\CentralProcessor' 2>/dev/null | $EGREP -e '\\\\@<:@0-9@:>@+$' -c`]],dnl
  75. [[: # empty]],[[test "$NUMBER_OF_PROCESSORS" -gt "0" 2>/dev/null && CPU_COUNT="$NUMBER_OF_PROCESSORS"]])],dnl
  76. [[msys*]],[[test "$NUMBER_OF_PROCESSORS" -gt "0" 2>/dev/null && CPU_COUNT="$NUMBER_OF_PROCESSORS"]],dnl
  77. [[cygwin*]],[[test "$NUMBER_OF_PROCESSORS" -gt "0" 2>/dev/null && CPU_COUNT="$NUMBER_OF_PROCESSORS"]]dnl
  78. )dnl
  79. ])dnl
  80. AS_IF([[test "x$CPU_COUNT" != "x0" && test "$CPU_COUNT" -gt 0 2>/dev/null]],[dnl
  81. AC_MSG_RESULT([[$CPU_COUNT]])
  82. m4_ifvaln([$1],[$1],)dnl
  83. ],[dnl
  84. m4_ifval([$2],[dnl
  85. AS_UNSET([[CPU_COUNT]])
  86. AC_MSG_RESULT([[unable to detect]])
  87. $2
  88. ], [dnl
  89. CPU_COUNT="1"
  90. AC_MSG_RESULT([[unable to detect (assuming 1)]])
  91. ])dnl
  92. ])dnl
  93. ])dnl