Prepare branch for 2021.2 point release
[alexxy/gromacs.git] / cmake / gmxManageFFTLibraries.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5 # Copyright (c) 2017,2018,2019,2020,2021, 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.
9 #
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.
14 #
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.
19 #
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.
24 #
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.
32 #
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.
35
36 # Manage setup of the different FFT libraries we can use in Gromacs.
37 set(PKG_FFT "")
38 set(PKG_FFT_LIBS "")
39 # Intel 11 and up makes life somewhat easy if you just want to use
40 # all their stuff. It's not easy if you only want some of their
41 # stuff...
42 set(MKL_MANUALLY FALSE)
43 if (GMX_FFT_LIBRARY STREQUAL "MKL" AND
44     NOT ((CMAKE_C_COMPILER_ID MATCHES "Intel" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "11")
45          OR GMX_INTEL_LLVM))
46     # The user will have to provide the set of magic libraries in
47     # MKL_LIBRARIES (see below), which we cache (non-advanced), so that they
48     # don't have to keep specifying it, and can easily see that
49     # CMake is still using that information.
50     set(MKL_MANUALLY TRUE)
51 endif()
52 set(MKL_LIBRARIES_FORMAT_DESCRIPTION "Use full paths to library files, in the right order, and separated by semicolons.")
53 gmx_dependent_cache_variable(
54     MKL_LIBRARIES
55     "List of libraries for linking to MKL. ${MKL_LIBRARIES_FORMAT_DESCRIPTION}"
56     STRING ""
57     MKL_MANUALLY)
58 gmx_dependent_cache_variable(
59     MKL_INCLUDE_DIR
60     "Path to mkl.h (non-inclusive)."
61     PATH ""
62     MKL_MANUALLY)
63 if(${GMX_FFT_LIBRARY} STREQUAL "FFTW3")
64     # ${FFTW} must be in upper case
65     if(GMX_DOUBLE)
66         set(FFTW "FFTW")
67     else()
68         set(FFTW "FFTWF")
69     endif()
70
71     if(GMX_BUILD_OWN_FFTW)
72
73         if(MSVC)
74             message(FATAL_ERROR "Cannot build FFTW3 automatically (GMX_BUILD_OWN_FFTW=ON) in Visual Studio")
75         endif()
76         if(CMAKE_GENERATOR STREQUAL "Ninja")
77             message(FATAL_ERROR "Cannot build FFTW3 automatically (GMX_BUILD_OWN_FFTW=ON) with ninja")
78         endif()
79
80         add_subdirectory(src/external/build-fftw)
81         include_directories(BEFORE ${${FFTW}_INCLUDE_DIRS})
82         # libgmxfftw is always built static, so libgromacs does not
83         # have a dependency on anything, so PKG_FFT should be empty
84         set(PKG_FFT "")
85         set(FFT_STATUS_MESSAGE "Using external FFT library - FFTW3 build managed by GROMACS")
86     else()
87         string(TOLOWER "${FFTW}" LOWERFFTW)
88         find_package(FFTW COMPONENTS ${LOWERFFTW})
89
90         if(NOT ${FFTW}_FOUND)
91             MESSAGE(FATAL_ERROR "Cannot find FFTW 3 (with correct precision - libfftw3f for mixed-precision GROMACS or libfftw3 for double-precision GROMACS). Either choose the right precision, choose another FFT(W) library (-DGMX_FFT_LIBRARY), enable the advanced option to let GROMACS build FFTW 3 for you (-DGMX_BUILD_OWN_FFTW=ON), or use the really slow GROMACS built-in fftpack library (-DGMX_FFT_LIBRARY=fftpack).")
92         endif()
93
94         set(PKG_FFT "${${FFTW}_PKG}")
95         include_directories(SYSTEM ${${FFTW}_INCLUDE_DIRS})
96
97         if(NOT WIN32) # Detection doesn't work on Windows
98           if ((${GMX_SIMD_ACTIVE} MATCHES "SSE" OR ${GMX_SIMD_ACTIVE} MATCHES "AVX") AND NOT ${FFTW}_HAVE_SIMD)
99               set(FFT_WARNING_MESSAGE "The fftw library found is compiled without SIMD support, which makes it slow. Consider recompiling it or contact your admin")
100           else()
101               if(${GMX_SIMD_ACTIVE} MATCHES "AVX" AND NOT (${FFTW}_HAVE_SSE OR ${FFTW}_HAVE_SSE2))
102                   # If we end up here we have an AVX Gromacs build, and
103                   # FFTW with SIMD.
104                   set(FFT_WARNING_MESSAGE "The FFTW library was compiled with neither --enable-sse nor --enable-sse2; those would have enabled SSE(2) SIMD instructions. This will give suboptimal performance. You should (re)compile the FFTW library with --enable-sse2 and --enable-avx (and --enable-avx2 or --enable-avx512 if supported).")
105               endif()
106           endif()
107         endif()
108
109         find_path(ARMPL_INCLUDE_DIR "armpl.h" HINTS ${${FFTW}_INCLUDE_DIRS}
110             NO_DEFAULT_PATH
111             NO_CMAKE_ENVIRONMENT_PATH
112             NO_CMAKE_PATH
113             NO_SYSTEM_ENVIRONMENT_PATH
114             NO_CMAKE_SYSTEM_PATH)
115         mark_as_advanced(ARMPL_INCLUDE_DIR)
116         if (ARMPL_INCLUDE_DIR)
117             set(GMX_FFT_ARMPL_FFTW3 1)
118             set(FFT_STATUS_MESSAGE "Using external FFT library - ARM Performance Library (FFTW3 compatibility mode)")
119         else()
120             set(FFT_STATUS_MESSAGE "Using external FFT library - FFTW3")
121         endif()
122     endif()
123     if (NOT GMX_FFT_ARMPL_FFTW3)
124         set(GMX_FFT_FFTW3 1)
125     endif()
126
127     set(FFT_LIBRARIES ${${FFTW}_LIBRARIES})
128 elseif(${GMX_FFT_LIBRARY} STREQUAL "MKL")
129     # Intel 11 and up makes life somewhat easy if you just want to use
130     # all their stuff. It's not easy if you only want some of their
131     # stuff...
132     if (NOT MKL_MANUALLY)
133         # The next line takes care of everything for MKL
134         if (WIN32)
135             # This works according to the Intel MKL 10.3 for Windows
136             # docs, but on Jenkins Win2k8, icl tries to interpret it
137             # as a file. Shrug.
138             set(FFT_LINKER_FLAGS "/Qmkl:sequential")
139         elseif(GMX_INTEL_LLVM AND GMX_INTEL_LLVM_VERSION GREATER_EQUAL 2021020)
140             set(FFT_LINKER_FLAGS "-qmkl=sequential")
141         else()
142             set(FFT_LINKER_FLAGS "-mkl=sequential")
143         endif()
144         # Some versions of icc require this in order that mkl.h can be
145         # found at compile time.
146         list(APPEND EXTRA_C_FLAGS ${FFT_LINKER_FLAGS})
147         list(APPEND EXTRA_CXX_FLAGS ${FFT_LINKER_FLAGS})
148
149         set(MKL_ERROR_MESSAGE "Make sure you have configured your compiler so that ${FFT_LINKER_FLAGS} will work.")
150     else()
151         include_directories(SYSTEM ${MKL_INCLUDE_DIR})
152         set(FFT_LIBRARIES "${MKL_LIBRARIES}")
153         set(MKL_ERROR_MESSAGE "The include path to mkl.h in MKL_INCLUDE_DIR, and the link libraries in MKL_LIBRARIES=${MKL_LIBRARIES} need to match what the MKL documentation says you need for your system: ${MKL_LIBRARIES_FORMAT_DESCRIPTION}")
154         # Convert the semi-colon separated list to a list of
155         # command-line linker arguments so that code using our
156         # pkgconfig setup can use it.
157         string(REGEX REPLACE ";" " " PKG_FFT_LIBS "${MKL_LIBRARIES}")
158     endif()
159
160     # Check MKL works. If we were in a non-global scope, we wouldn't
161     # have to play nicely.
162     set(old_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
163     set(CMAKE_REQUIRED_FLAGS "${FFT_LINKER_FLAGS}")
164     set(old_CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
165     set(CMAKE_REQUIRED_LIBRARIES "${FFT_LIBRARIES}")
166
167     check_function_exists(DftiCreateDescriptor TEST_MKL)
168
169     set(CMAKE_REQUIRED_FLAGS "${old_CMAKE_REQUIRED_FLAGS}")
170     set(CMAKE_REQUIRED_LIBRARIES "${old_CMAKE_REQUIRED_LIBRARIES}")
171
172     if(NOT TEST_MKL)
173         # Hack to help the user vary MKL settings until they work.
174         # TODO Make this logic more useful.
175         unset(TEST_MKL CACHE)
176         message(FATAL_ERROR "Linking with MKL was requested, but was not successful: ${MKL_ERROR_MESSAGE}")
177     endif()
178
179     # Set variables to signal that we have MKL available and should use it for FFTs.
180     set(GMX_FFT_MKL 1)
181     set(HAVE_LIBMKL 1)
182
183     set(FFT_STATUS_MESSAGE "Using external FFT library - Intel MKL")
184 elseif(${GMX_FFT_LIBRARY} STREQUAL "FFTPACK")
185     set(GMX_FFT_FFTPACK 1)
186     set(FFT_STATUS_MESSAGE "Using internal FFT library - fftpack")
187 else()
188     gmx_invalid_option_value(GMX_FFT_LIBRARY)
189 endif()
190 gmx_check_if_changed(FFT_CHANGED GMX_FFT_LIBRARY)
191 if (FFT_CHANGED)
192     if(FFT_WARNING_MESSAGE)
193         message(WARNING "${FFT_WARNING_MESSAGE}")
194     endif()
195     message(STATUS "${FFT_STATUS_MESSAGE}")
196 endif()
197
198 # enable threaded fftw3 if we've found it
199 if(FFTW3_THREADS OR FFTW3F_THREADS)
200     add_definitions(-DFFT5D_FFTW_THREADS)
201 endif()
202