Remove cycle suppression
[gromacs.git] / cmake / gmxDetectSimd.cmake
blob4231cb15eaaaa9b9289d227f58c6c9098f06fdb2
2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2012,2013,2014,2015,2016,2017,2018, 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.
44
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()
57 include(gmxDetectCpu)
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")
63     endif()
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} ")
78         if(GMX_TARGET_X86)
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                     if (NOT SUGGEST_SIMD_QUIETLY)
87                         message(STATUS "This host supports AVX-512, but only has 1 AVX-512 FMA unit, so AVX2 will be faster.")
88                     endif()
89                     set(OUTPUT_SIMD "AVX2_256")
90                 else()
91                     if (NOT SUGGEST_SIMD_QUIETLY)
92                         message(STATUS "Could not run code to detect number of AVX-512 FMA units - assuming 2.")
93                     endif()
94                     set(OUTPUT_SIMD "AVX_512")
95                 endif()
96             elseif(CPU_DETECTION_FEATURES MATCHES " avx2 ")
97                 if(CPU_DETECTION_FEATURES MATCHES " amd ")
98                     set(OUTPUT_SIMD "AVX2_128")
99                 else()
100                     set(OUTPUT_SIMD "AVX2_256")
101                 endif()
102             elseif(CPU_DETECTION_FEATURES MATCHES " avx ")
103                 if(CPU_DETECTION_FEATURES MATCHES " fma4 ")
104                     # AMD that works better with avx-128-fma
105                     set(OUTPUT_SIMD "AVX_128_FMA")
106                 else()
107                     # Intel
108                     set(OUTPUT_SIMD "AVX_256")
109                 endif()
110             elseif(CPU_DETECTION_FEATURES MATCHES " sse4.1 ")
111                 set(OUTPUT_SIMD "SSE4.1")
112             elseif(CPU_DETECTION_FEATURES MATCHES " sse2 ")
113                 set(OUTPUT_SIMD "SSE2")
114             endif()
115         else()
116             if(CPU_DETECTION_FEATURES MATCHES " vsx ")
117                 set(OUTPUT_SIMD "IBM_VSX")
118             elseif(CPU_DETECTION_FEATURES MATCHES " vmx ")
119                 set(OUTPUT_SIMD "IBM_VMX")
120             elseif(CPU_DETECTION_FEATURES MATCHES " neon_asimd ")
121                 set(OUTPUT_SIMD "ARM_NEON_ASIMD")
122             elseif(CPU_DETECTION_FEATURES MATCHES " neon " AND NOT GMX_DOUBLE)
123                 set(OUTPUT_SIMD "ARM_NEON")
124             endif()
125         endif()
126         if (NOT SUGGEST_SIMD_QUIETLY)
127             message(STATUS "Detected best SIMD instructions for this CPU - ${OUTPUT_SIMD}")
128         endif()
129     else()
130         if (NOT SUGGEST_SIMD_QUIETLY)
131             message(STATUS "Detection for best SIMD instructions failed, using SIMD - ${OUTPUT_SIMD}")
132         endif()
133     endif()
135     set(${_suggested_simd} "${OUTPUT_SIMD}" PARENT_SCOPE)
136     set(SUGGEST_SIMD_QUIETLY TRUE CACHE INTERNAL "Be quiet during future construction of SIMD suggestions")
137 endfunction()
139 function(gmx_detect_simd _suggested_simd)
140     if(GMX_SIMD STREQUAL "AUTO")
141         if(GMX_TARGET_FUJITSU_SPARC64)
142             # HPC-ACE is always present. In the future we
143             # should add detection for HPC-ACE2 here.
144             set(${_suggested_simd} "Sparc64_HPC_ACE")
145         elseif(GMX_TARGET_MIC)
146             set(${_suggested_simd} "MIC")
147         else()
148             gmx_suggest_simd(${_suggested_simd})
149         endif()
151         string(TOUPPER "${${_suggested_simd}}" ${_suggested_simd})
152         set(${_suggested_simd} ${${_suggested_simd}} PARENT_SCOPE)
153     endif()
154 endfunction()