2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2012,2013,2014,2015,2016,2017, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
35 # - Check the username performing the build, as well as date and time
37 # gmx_detect_simd(_suggested_simd)
39 # Try to detect CPU features and suggest a SIMD instruction set
40 # that fits the current CPU. This should work on all architectures
41 # where we are not cross-compiling; depending on the architecture the
42 # detection will either use special assembly instructions (like cpuid),
43 # preprocessor defines, or probing /proc/cpuinfo on Linux.
45 # Sets ${_suggested_simd} in the parent scope if GMX_SIMD is not set
46 # (e.g. by the user, or a previous run of CMake).
47 # The string is converted to uppercase for compatibility with
48 # gmx_option_multichoice() user input parsing.
51 # we rely on inline asm support for GNU!
52 include(gmxTestInlineASM)
53 # Ensure things like GMX_TARGET_X86 are available
54 include(gmxDetectTargetArchitecture)
55 gmx_detect_target_architecture()
58 include(gmxDetectAvx512FmaUnits)
60 function(gmx_suggest_simd _suggested_simd)
61 if (NOT SUGGEST_SIMD_QUIETLY)
62 message(STATUS "Detecting best SIMD instructions for this CPU")
65 # Prepare a default suggestion
66 set(OUTPUT_SIMD "None")
68 # Detect CPU features and place the string in CPU_DETECTION_FEATURES
69 # Note that we are NOT limited to x86.
70 gmx_run_cpu_detection(features)
72 if (DEFINED CPU_DETECTION_FEATURES)
73 # Make a concrete suggestion of SIMD level if a feature flag
74 # matches. Make sure that the match strings below work even if
75 # the feature is first or last.
76 set(CPU_DETECTION_FEATURES " ${CPU_DETECTION_FEATURES} ")
79 if(CPU_DETECTION_FEATURES MATCHES " avx512er ")
80 set(OUTPUT_SIMD "AVX_512_KNL")
81 elseif(CPU_DETECTION_FEATURES MATCHES " avx512f ")
82 gmx_detect_avx_512_fma_units(NUMBER_OF_AVX_512_FMA_UNITS)
83 if(NUMBER_OF_AVX_512_FMA_UNITS EQUAL 2)
84 set(OUTPUT_SIMD "AVX_512")
85 elseif(NUMBER_OF_AVX_512_FMA_UNITS EQUAL 1)
86 message(STATUS "This host supports AVX-512, but only has 1 AVX-512 FMA unit, so AVX2 will be faster.")
87 set(OUTPUT_SIMD "AVX2_256")
89 message(WARNING "Could not run code to detect number of AVX-512 FMA units - assuming 2.")
90 set(OUTPUT_SIMD "AVX_512")
92 elseif(CPU_DETECTION_FEATURES MATCHES " avx2 ")
93 if(CPU_DETECTION_FEATURES MATCHES " amd ")
94 set(OUTPUT_SIMD "AVX2_128")
96 set(OUTPUT_SIMD "AVX2_256")
98 elseif(CPU_DETECTION_FEATURES MATCHES " avx ")
99 if(CPU_DETECTION_FEATURES MATCHES " fma4 ")
100 # AMD that works better with avx-128-fma
101 set(OUTPUT_SIMD "AVX_128_FMA")
104 set(OUTPUT_SIMD "AVX_256")
106 elseif(CPU_DETECTION_FEATURES MATCHES " sse4.1 ")
107 set(OUTPUT_SIMD "SSE4.1")
108 elseif(CPU_DETECTION_FEATURES MATCHES " sse2 ")
109 set(OUTPUT_SIMD "SSE2")
112 if(CPU_DETECTION_FEATURES MATCHES " vsx ")
113 set(OUTPUT_SIMD "IBM_VSX")
114 elseif(CPU_DETECTION_FEATURES MATCHES " vmx ")
115 set(OUTPUT_SIMD "IBM_VMX")
116 elseif(CPU_DETECTION_FEATURES MATCHES " qpx ")
117 set(OUTPUT_SIMD "IBM_QPX")
118 elseif(CPU_DETECTION_FEATURES MATCHES " neon_asimd ")
119 set(OUTPUT_SIMD "ARM_NEON_ASIMD")
120 elseif(CPU_DETECTION_FEATURES MATCHES " neon " AND NOT GMX_DOUBLE)
121 set(OUTPUT_SIMD "ARM_NEON")
124 if (NOT SUGGEST_SIMD_QUIETLY)
125 message(STATUS "Detected best SIMD instructions for this CPU - ${OUTPUT_SIMD}")
128 if (NOT SUGGEST_SIMD_QUIETLY)
129 message(STATUS "Detection for best SIMD instructions failed, using SIMD - ${OUTPUT_SIMD}")
133 set(${_suggested_simd} "${OUTPUT_SIMD}" PARENT_SCOPE)
134 set(SUGGEST_SIMD_QUIETLY TRUE CACHE INTERNAL "Be quiet during future construction of SIMD suggestions")
137 function(gmx_detect_simd _suggested_simd)
138 if(GMX_SIMD STREQUAL "AUTO")
140 # BG/Q requires cross-compilation, so needs this
141 # logic. While the qpx feature flag in cpuinfo works, it
142 # can't be returned by cpuinfo running on the build host.
143 set(${_suggested_simd} "IBM_QPX")
144 elseif(GMX_TARGET_FUJITSU_SPARC64)
145 # HPC-ACE is always present. In the future we
146 # should add detection for HPC-ACE2 here.
147 set(${_suggested_simd} "Sparc64_HPC_ACE")
148 elseif(GMX_TARGET_MIC)
149 set(${_suggested_simd} "MIC")
151 gmx_suggest_simd(${_suggested_simd})
154 string(TOUPPER "${${_suggested_simd}}" ${_suggested_simd})
155 set(${_suggested_simd} ${${_suggested_simd}} PARENT_SCOPE)