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).
49 # we rely on inline asm support for GNU!
50 include(gmxTestInlineASM)
51 # Ensure things like GMX_TARGET_X86 are available
52 include(gmxDetectTargetArchitecture)
53 gmx_detect_target_architecture()
56 function(gmx_suggest_simd _suggested_simd)
57 if (NOT SUGGEST_SIMD_QUIETLY)
58 message(STATUS "Detecting best SIMD instructions for this CPU")
61 # Prepare a default suggestion
62 set(OUTPUT_SIMD "None")
64 # Detect CPU features and place the string in CPU_DETECTION_FEATURES
65 # Note that we are NOT limited to x86.
66 gmx_run_cpu_detection(features)
68 if (DEFINED CPU_DETECTION_FEATURES)
69 # Make a concrete suggestion of SIMD level if a feature flag
70 # matches. Make sure that the match strings below work even if
71 # the feature is first or last.
72 set(CPU_DETECTION_FEATURES " ${CPU_DETECTION_FEATURES} ")
75 if(CPU_DETECTION_FEATURES MATCHES " avx512er ")
76 set(OUTPUT_SIMD "AVX_512_KNL")
77 elseif(CPU_DETECTION_FEATURES MATCHES " avx512f ")
78 set(OUTPUT_SIMD "AVX_512")
79 elseif(CPU_DETECTION_FEATURES MATCHES " avx2 ")
80 if(CPU_DETECTION_FEATURES MATCHES " amd ")
81 set(OUTPUT_SIMD "AVX2_128")
83 set(OUTPUT_SIMD "AVX2_256")
85 elseif(CPU_DETECTION_FEATURES MATCHES " avx ")
86 if(CPU_DETECTION_FEATURES MATCHES " fma4 ")
87 # AMD that works better with avx-128-fma
88 set(OUTPUT_SIMD "AVX_128_FMA")
91 set(OUTPUT_SIMD "AVX_256")
93 elseif(CPU_DETECTION_FEATURES MATCHES " sse4.1 ")
94 set(OUTPUT_SIMD "SSE4.1")
95 elseif(CPU_DETECTION_FEATURES MATCHES " sse2 ")
96 set(OUTPUT_SIMD "SSE2")
99 if(CPU_DETECTION_FEATURES MATCHES " vsx ")
100 set(OUTPUT_SIMD "IBM_VSX")
101 elseif(CPU_DETECTION_FEATURES MATCHES " vmx ")
102 set(OUTPUT_SIMD "IBM_VMX")
103 elseif(CPU_DETECTION_FEATURES MATCHES " qpx ")
104 set(OUTPUT_SIMD "IBM_QPX")
105 elseif(CPU_DETECTION_FEATURES MATCHES " neon_asimd ")
106 set(OUTPUT_SIMD "ARM_NEON_ASIMD")
107 elseif(CPU_DETECTION_FEATURES MATCHES " neon " AND NOT GMX_DOUBLE)
108 set(OUTPUT_SIMD "ARM_NEON")
111 if (NOT SUGGEST_SIMD_QUIETLY)
112 message(STATUS "Detected best SIMD instructions for this CPU - ${OUTPUT_SIMD}")
115 if (NOT SUGGEST_SIMD_QUIETLY)
116 message(STATUS "Detection for best SIMD instructions failed, using SIMD - ${OUTPUT_SIMD}")
120 set(${_suggested_simd} "${OUTPUT_SIMD}" PARENT_SCOPE)
121 set(SUGGEST_SIMD_QUIETLY TRUE CACHE INTERNAL "Be quiet during future construction of SIMD suggestions")
124 function(gmx_detect_simd _suggested_simd)
125 if(GMX_SIMD STREQUAL "AUTO")
127 # BG/Q requires cross-compilation, so needs this
128 # logic. While the qpx feature flag in cpuinfo works, it
129 # can't be returned by cpuinfo running on the build host.
130 set(${_suggested_simd} "IBM_QPX")
131 elseif(GMX_TARGET_FUJITSU_SPARC64)
132 # HPC-ACE is always present. In the future we
133 # should add detection for HPC-ACE2 here.
134 set(${_suggested_simd} "Sparc64_HPC_ACE")
135 elseif(GMX_TARGET_MIC)
136 set(${_suggested_simd} "MIC")
138 gmx_suggest_simd(${_suggested_simd})
141 set(${_suggested_simd} ${${_suggested_simd}} PARENT_SCOPE)