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 function(gmx_suggest_simd _suggested_simd)
59 if (NOT SUGGEST_SIMD_QUIETLY)
60 message(STATUS "Detecting best SIMD instructions for this CPU")
63 # Prepare a default suggestion
64 set(OUTPUT_SIMD "None")
66 # Detect CPU features and place the string in CPU_DETECTION_FEATURES
67 # Note that we are NOT limited to x86.
68 gmx_run_cpu_detection(features)
70 if (DEFINED CPU_DETECTION_FEATURES)
71 # Make a concrete suggestion of SIMD level if a feature flag
72 # matches. Make sure that the match strings below work even if
73 # the feature is first or last.
74 set(CPU_DETECTION_FEATURES " ${CPU_DETECTION_FEATURES} ")
77 if(CPU_DETECTION_FEATURES MATCHES " avx512er ")
78 set(OUTPUT_SIMD "AVX_512_KNL")
79 elseif(CPU_DETECTION_FEATURES MATCHES " avx512f ")
80 set(OUTPUT_SIMD "AVX_512")
81 elseif(CPU_DETECTION_FEATURES MATCHES " avx2 ")
82 if(CPU_DETECTION_FEATURES MATCHES " amd ")
83 set(OUTPUT_SIMD "AVX2_128")
85 set(OUTPUT_SIMD "AVX2_256")
87 elseif(CPU_DETECTION_FEATURES MATCHES " avx ")
88 if(CPU_DETECTION_FEATURES MATCHES " fma4 ")
89 # AMD that works better with avx-128-fma
90 set(OUTPUT_SIMD "AVX_128_FMA")
93 set(OUTPUT_SIMD "AVX_256")
95 elseif(CPU_DETECTION_FEATURES MATCHES " sse4.1 ")
96 set(OUTPUT_SIMD "SSE4.1")
97 elseif(CPU_DETECTION_FEATURES MATCHES " sse2 ")
98 set(OUTPUT_SIMD "SSE2")
101 if(CPU_DETECTION_FEATURES MATCHES " vsx ")
102 set(OUTPUT_SIMD "IBM_VSX")
103 elseif(CPU_DETECTION_FEATURES MATCHES " vmx ")
104 set(OUTPUT_SIMD "IBM_VMX")
105 elseif(CPU_DETECTION_FEATURES MATCHES " qpx ")
106 set(OUTPUT_SIMD "IBM_QPX")
107 elseif(CPU_DETECTION_FEATURES MATCHES " neon_asimd ")
108 set(OUTPUT_SIMD "ARM_NEON_ASIMD")
109 elseif(CPU_DETECTION_FEATURES MATCHES " neon " AND NOT GMX_DOUBLE)
110 set(OUTPUT_SIMD "ARM_NEON")
113 if (NOT SUGGEST_SIMD_QUIETLY)
114 message(STATUS "Detected best SIMD instructions for this CPU - ${OUTPUT_SIMD}")
117 if (NOT SUGGEST_SIMD_QUIETLY)
118 message(STATUS "Detection for best SIMD instructions failed, using SIMD - ${OUTPUT_SIMD}")
122 set(${_suggested_simd} "${OUTPUT_SIMD}" PARENT_SCOPE)
123 set(SUGGEST_SIMD_QUIETLY TRUE CACHE INTERNAL "Be quiet during future construction of SIMD suggestions")
126 function(gmx_detect_simd _suggested_simd)
127 if(GMX_SIMD STREQUAL "AUTO")
129 # BG/Q requires cross-compilation, so needs this
130 # logic. While the qpx feature flag in cpuinfo works, it
131 # can't be returned by cpuinfo running on the build host.
132 set(${_suggested_simd} "IBM_QPX")
133 elseif(GMX_TARGET_FUJITSU_SPARC64)
134 # HPC-ACE is always present. In the future we
135 # should add detection for HPC-ACE2 here.
136 set(${_suggested_simd} "Sparc64_HPC_ACE")
137 elseif(GMX_TARGET_MIC)
138 set(${_suggested_simd} "MIC")
140 gmx_suggest_simd(${_suggested_simd})
143 string(TOUPPER "${${_suggested_simd}}" ${_suggested_simd})
144 set(${_suggested_simd} ${${_suggested_simd}} PARENT_SCOPE)