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