Merge branch release-2016
[alexxy/gromacs.git] / cmake / gmxDetectSimd.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
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.
8 #
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.
13 #
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.
18 #
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.
23 #
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.
31 #
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.
34
35 # - Check the username performing the build, as well as date and time
36 #
37 # gmx_detect_simd(_suggested_simd_)
38 #
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 #
48
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()
54
55 include(gmxDetectCpu)
56 function(gmx_suggest_simd _suggested_simd)
57     if (NOT SUGGEST_SIMD_QUIETLY)
58         message(STATUS "Detecting best SIMD instructions for this CPU")
59     endif()
60
61     # Prepare a default suggestion
62     set(OUTPUT_SIMD "None")
63
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)
67
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} ")
73
74         if(GMX_TARGET_X86)
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")
82                 else()
83                     set(OUTPUT_SIMD "AVX2_256")
84                 endif()
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")
89                 else()
90                     # Intel
91                     set(OUTPUT_SIMD "AVX_256")
92                 endif()
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")
97             endif()
98         else()
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")
109             endif()
110         endif()
111         if (NOT SUGGEST_SIMD_QUIETLY)
112             message(STATUS "Detected best SIMD instructions for this CPU - ${OUTPUT_SIMD}")
113         endif()
114     else()
115         if (NOT SUGGEST_SIMD_QUIETLY)
116             message(STATUS "Detection for best SIMD instructions failed, using SIMD - ${OUTPUT_SIMD}")
117         endif()
118     endif()
119
120     set(${_suggested_simd} "${OUTPUT_SIMD}" PARENT_SCOPE)
121     set(SUGGEST_SIMD_QUIETLY TRUE CACHE INTERNAL "Be quiet during future construction of SIMD suggestions")
122 endfunction()
123
124 function(gmx_detect_simd _suggested_simd)
125     if(GMX_SIMD STREQUAL "AUTO")
126         if(GMX_TARGET_BGQ)
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")
137         else()
138             gmx_suggest_simd(${_suggested_simd})
139         endif()
140
141         set(${_suggested_simd} ${${_suggested_simd}} PARENT_SCOPE)
142     endif()
143 endfunction()