12e54cb40efca94399964c7cff008d92dca0dd38
[alexxy/gromacs.git] / cmake / gmxManageNvccConfig.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, 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 CUDA nvcc compilation configuration, try to be smart to ease the users'
37 # pain as much as possible:
38 # - use the CUDA_HOST_COMPILER if defined by the user, otherwise
39 # - check if nvcc works with CUDA_HOST_COMPILER and the generated nvcc and C++ flags
40 #
41 # - (advanced) variables set:
42 #   * CUDA_HOST_COMPILER_OPTIONS    - the full host-compiler related option list passed to nvcc
43 #
44 # Note that from CMake 2.8.10 FindCUDA defines CUDA_HOST_COMPILER internally,
45 # so we won't set it ourselves, but hope that the module does a good job.
46
47 # glibc 2.23 changed string.h in a way that breaks CUDA compilation in
48 # many projects, but which has a trivial workaround. It would be nicer
49 # to compile with nvcc and see that the workaround is necessary and
50 # effective, but it is unclear how to do that. Also, grepping in the
51 # glibc source shows that _FORCE_INLINES is only used in this string.h
52 # feature and performance of memcpy variants is unimportant for CUDA
53 # code in GROMACS. So this workaround is good enough to keep problems
54 # away from users installing GROMACS. See Issue #1942.
55 function(work_around_glibc_2_23)
56     try_compile(IS_GLIBC_2_23_OR_HIGHER ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/cmake/TestGlibcVersion.cpp)
57     if(IS_GLIBC_2_23_OR_HIGHER)
58         message(STATUS "Adding work-around for issue compiling CUDA code with glibc 2.23 string.h")
59         list(APPEND CUDA_HOST_COMPILER_OPTIONS "-D_FORCE_INLINES")
60         set(CUDA_HOST_COMPILER_OPTIONS ${CUDA_HOST_COMPILER_OPTIONS} PARENT_SCOPE)
61     endif()
62 endfunction()
63
64 gmx_check_if_changed(CUDA_HOST_COMPILER_CHANGED CUDA_HOST_COMPILER)
65
66 # set up host compiler and its options
67 if(CUDA_HOST_COMPILER_CHANGED)
68     set(CUDA_HOST_COMPILER_OPTIONS "")
69
70     if(APPLE AND CMAKE_C_COMPILER_ID MATCHES "GNU")
71         # Some versions of gcc-4.8 and gcc-4.9 have produced errors
72         # (in particular on OS X) if we do not use
73         # -D__STRICT_ANSI__. It is harmless, so we might as well add
74         # it for all versions.
75         list(APPEND CUDA_HOST_COMPILER_OPTIONS "-D__STRICT_ANSI__")
76     endif()
77
78     work_around_glibc_2_23()
79
80     set(CUDA_HOST_COMPILER_OPTIONS "${CUDA_HOST_COMPILER_OPTIONS}"
81         CACHE STRING "Options for nvcc host compiler (do not edit!).")
82
83     mark_as_advanced(CUDA_HOST_COMPILER CUDA_HOST_COMPILER_OPTIONS)
84 endif()
85
86 # If any of these manual override variables for target CUDA GPU architectures
87 # or virtual architecture is set, parse the values and assemble the nvcc
88 # command line for these. Otherwise use our defaults.
89 # Note that the manual override variables require a semicolon separating
90 # architecture codes.
91 if (GMX_CUDA_TARGET_SM OR GMX_CUDA_TARGET_COMPUTE)
92     set(GMX_CUDA_NVCC_GENCODE_FLAGS)
93     set(_target_sm_list ${GMX_CUDA_TARGET_SM})
94     foreach(_target ${_target_sm_list})
95         list(APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_${_target},code=sm_${_target}")
96     endforeach()
97     set(_target_compute_list ${GMX_CUDA_TARGET_COMPUTE})
98     foreach(_target ${_target_compute_list})
99         list(APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_${_target},code=compute_${_target}")
100     endforeach()
101 else()
102     # Set the CUDA GPU architectures to compile for:
103     # - with CUDA >=9.0         CC 7.0 is supported and CC 2.0 is no longer supported
104     #     => compile sm_30, sm_35, sm_37, sm_50, sm_52, sm_60, sm_61, sm_70 SASS, and compute_70 PTX
105     # - with CUDA >=10.0        CC 7.5 is supported
106     #     => compile sm_30, sm_35, sm_37, sm_50, sm_52, sm_60, sm_61, sm_70, sm_75 SASS, and compute_75 PTX
107     # - with CUDA >=11.0        CC 8.0 is supported
108     #     => compile sm_35, sm_37, sm_50, sm_52, sm_60, sm_61, sm_70, sm_75, sm_80 SASS, and compute_80 PTX
109
110     # First add flags that trigger SASS (binary) code generation for physical arch
111     if(CUDA_VERSION VERSION_LESS "11.0")
112         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_30,code=sm_30")
113     endif()
114     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_35,code=sm_35")
115     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_37,code=sm_37")
116     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_50,code=sm_50")
117     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_52,code=sm_52")
118     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_60,code=sm_60")
119     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_61,code=sm_61")
120     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_70,code=sm_70")
121     if(NOT CUDA_VERSION VERSION_LESS "11.0")
122         # Requesting sm or compute 35, 37, or 50 triggers deprecation messages with
123         # nvcc 11.0, which we need to suppress for use in CI
124         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-Wno-deprecated-gpu-targets")
125     endif()
126
127     # Next add flags that trigger PTX code generation for the newest supported virtual arch
128     # that's useful to JIT to future architectures
129     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_35,code=compute_35")
130     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_50,code=compute_50")
131     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_52,code=compute_52")
132     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_60,code=compute_60")
133     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_61,code=compute_61")
134     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_70,code=compute_70")
135     if(NOT CUDA_VERSION VERSION_LESS "10.0")
136         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_75,code=compute_75")
137     endif()
138     if(NOT CUDA_VERSION VERSION_LESS "11.0")
139         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_80,code=compute_80")
140     endif()
141 endif()
142
143 if (GMX_CUDA_TARGET_SM)
144     set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY HELPSTRING "List of CUDA GPU architecture codes to compile for (without the sm_ prefix)")
145     set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY TYPE STRING)
146 endif()
147 if (GMX_CUDA_TARGET_COMPUTE)
148     set_property(CACHE GMX_CUDA_TARGET_COMPUTE PROPERTY HELPSTRING "List of CUDA virtual architecture codes to compile for (without the compute_ prefix)")
149     set_property(CACHE GMX_CUDA_TARGET_COMPUTE PROPERTY TYPE STRING)
150 endif()
151
152 # FindCUDA.cmake is unaware of the mechanism used by cmake to embed
153 # the compiler flag for the required C++ standard in the generated
154 # build files, so we have to pass it ourselves
155 if (CUDA_VERSION VERSION_LESS 11.0)
156     # CUDA doesn't formally support C++17 until version 11.0, so for
157     # now host-side code that compiles with CUDA is restricted to
158     # C++14. This needs to be expressed formally for older CUDA
159     # version.
160     list(APPEND GMX_CUDA_NVCC_FLAGS "${CMAKE_CXX14_STANDARD_COMPILE_OPTION}")
161 else()
162     # gcc-7 pre-dated C++17, so uses the -std=c++1z compiler flag for it,
163     # which modern nvcc does not recognize. So we work around that by
164     # compiling in C++14 mode. Clang doesn't have this problem because nvcc
165     # only supports version of clang that already understood -std=c++17
166     if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8)
167         list(APPEND GMX_CUDA_NVCC_FLAGS "${CMAKE_CXX14_STANDARD_COMPILE_OPTION}")
168     else()
169         list(APPEND GMX_CUDA_NVCC_FLAGS "${CMAKE_CXX17_STANDARD_COMPILE_OPTION}")
170     endif()
171 endif()
172
173 # assemble the CUDA flags
174 list(APPEND GMX_CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_GENCODE_FLAGS}")
175 list(APPEND GMX_CUDA_NVCC_FLAGS "-use_fast_math")
176
177 # assemble the CUDA host compiler flags
178 list(APPEND GMX_CUDA_NVCC_FLAGS "${CUDA_HOST_COMPILER_OPTIONS}")
179
180 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
181     # CUDA header cuda_runtime_api.h in at least CUDA 10.1 uses 0
182     # where nullptr would be preferable. GROMACS can't fix these, so
183     # must suppress them.
184     GMX_TEST_CXXFLAG(CXXFLAGS_NO_ZERO_AS_NULL_POINTER_CONSTANT "-Wno-zero-as-null-pointer-constant" NVCC_CLANG_SUPPRESSIONS_CXXFLAGS)
185
186     # CUDA header crt/math_functions.h in at least CUDA 10.x and 11.1
187     # used throw() specifications that are deprecated in more recent
188     # C++ versions. GROMACS can't fix these, so must suppress them.
189     GMX_TEST_CXXFLAG(CXXFLAGS_NO_DEPRECATED_DYNAMIC_EXCEPTION_SPEC "-Wno-deprecated-dynamic-exception-spec" NVCC_CLANG_SUPPRESSIONS_CXXFLAGS)
190
191     # Add these flags to those used for the host compiler. The
192     # "-Xcompiler" prefix directs nvcc to only use them for host
193     # compilation, which is all that is needed in this case.
194     foreach(_flag ${NVCC_CLANG_SUPPRESSIONS_CXXFLAGS})
195         list(APPEND GMX_CUDA_NVCC_FLAGS "-Xcompiler ${_flag}")
196     endforeach()
197 endif()
198
199 string(TOUPPER "${CMAKE_BUILD_TYPE}" _build_type)
200 gmx_check_if_changed(_cuda_nvcc_executable_or_flags_changed CUDA_NVCC_EXECUTABLE CUDA_NVCC_FLAGS CUDA_NVCC_FLAGS_${_build_type})
201
202 # We would like to be helpful and reject the host compiler with a
203 # clear error message at configure time, rather than let nvcc
204 # later reject the host compiler as not supported when the first
205 # CUDA source file is built. We've implemented that for current
206 # nvcc running on Unix-like systems, but e.g. changes to nvcc
207 # will further affect the limited portability of this checking
208 # code. Set the CMake variable GMX_NVCC_WORKS on if you want to
209 # bypass this check.
210 if((_cuda_nvcc_executable_or_flags_changed OR CUDA_HOST_COMPILER_CHANGED OR NOT GMX_NVCC_WORKS) AND NOT WIN32)
211     message(STATUS "Check for working NVCC/C++ compiler combination with nvcc '${CUDA_NVCC_EXECUTABLE}'")
212     execute_process(COMMAND ${CUDA_NVCC_EXECUTABLE} -ccbin ${CUDA_HOST_COMPILER} -c ${CUDA_NVCC_FLAGS} ${CUDA_NVCC_FLAGS_${_build_type}} ${CMAKE_SOURCE_DIR}/cmake/TestCUDA.cu
213         RESULT_VARIABLE _cuda_test_res
214         OUTPUT_VARIABLE _cuda_test_out
215         ERROR_VARIABLE  _cuda_test_err
216         OUTPUT_STRIP_TRAILING_WHITESPACE)
217
218     if(${_cuda_test_res})
219         message(STATUS "Check for working NVCC/C compiler combination - broken")
220         message(STATUS "${CUDA_NVCC_EXECUTABLE} standard output: '${_cuda_test_out}'")
221         message(STATUS "${CUDA_NVCC_EXECUTABLE} standard error:  '${_cuda_test_err}'")
222         if(${_cuda_test_err} MATCHES "nsupported")
223             message(FATAL_ERROR "NVCC/C++ compiler combination does not seem to be supported. CUDA frequently does not support the latest versions of the host compiler, so you might want to try an earlier C++ compiler version and make sure your CUDA compiler and driver are as recent as possible.")
224         else()
225             message(FATAL_ERROR "CUDA compiler does not seem to be functional.")
226         endif()
227     elseif(NOT GMX_CUDA_TEST_COMPILER_QUIETLY)
228         message(STATUS "Check for working NVCC/C++ compiler combination - works")
229         set(GMX_NVCC_WORKS TRUE CACHE INTERNAL "Nvcc can compile a trivial test program")
230     endif()
231 endif() # GMX_CHECK_NVCC
232
233
234 # The flags are set as local variables which shadow the cache variables. The cache variables
235 # (can be set by the user) are appended. This is done in a macro to set the flags when all
236 # host compiler flags are already set.
237 macro(GMX_SET_CUDA_NVCC_FLAGS)
238     set(CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_FLAGS};${CUDA_NVCC_FLAGS}")
239 endmacro()
240
241 # This helper function creates a temporary scope in which we can set
242 # the definitions, include directories and magic host-compiler-flag
243 # variables that have to be set in advance of calling
244 # cuda_add_library(). This is the only way cuda_add_library() can
245 # modify the command line used for host compilation. It is not
246 # possible to use the standard CMake mechanisms like
247 # target_compile_options() to add such things to targets after they
248 # are created.
249 function(gmx_cuda_add_library TARGET)
250     add_definitions(-DHAVE_CONFIG_H)
251     # Source files generated by NVCC can include gmxmpi.h, and so
252     # need access to thread-MPI.
253     include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
254     # Source files can also contain topology related files and need access to
255     # the remaining external headers
256     include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/src/external)
257
258     # Now add all the compilation options
259     gmx_cuda_target_compile_options(CUDA_${TARGET}_CXXFLAGS)
260     list(APPEND CMAKE_CXX_FLAGS ${CUDA_${TARGET}_CXXFLAGS})
261     foreach(build_type ${build_types_with_explicit_flags})
262         list(APPEND CMAKE_CXX_FLAGS_${build_type} ${CUDA_${TARGET}_CXXFLAGS_${build_type}})
263     endforeach()
264
265     cuda_add_library(${TARGET} ${ARGN})
266 endfunction()