Improve CUDA codegen flags
[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,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 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_35, 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_35, 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_35, 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 "10.0")
122         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_75,code=sm_75")
123     endif()
124     if(NOT CUDA_VERSION VERSION_LESS "11.0")
125         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_80,code=sm_80")
126         # Requesting sm or compute 35, 37, or 50 triggers deprecation messages with
127         # nvcc 11.0, which we need to suppress for use in CI
128         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-Wno-deprecated-gpu-targets")
129     endif()
130     if(NOT CUDA_VERSION VERSION_LESS "11.1")
131         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_86,code=sm_86")
132     endif()
133
134     # Next add flags that trigger PTX code generation for the
135     # newest supported virtual arch that's useful to JIT to future architectures
136     # as well as an older one suitable for JIT-ing to any rare intermediate arch
137     # (like that of Jetson / Drive PX devices)
138     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_35,code=compute_35")
139     if(CUDA_VERSION VERSION_LESS "11.0")
140         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_32,code=compute_32")
141     else()
142         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_53,code=compute_53")
143     endif()
144     if(NOT CUDA_VERSION VERSION_LESS "11.0")
145         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_80,code=compute_80")
146     endif()
147 endif()
148
149 if (GMX_CUDA_TARGET_SM)
150     set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY HELPSTRING "List of CUDA GPU architecture codes to compile for (without the sm_ prefix)")
151     set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY TYPE STRING)
152 endif()
153 if (GMX_CUDA_TARGET_COMPUTE)
154     set_property(CACHE GMX_CUDA_TARGET_COMPUTE PROPERTY HELPSTRING "List of CUDA virtual architecture codes to compile for (without the compute_ prefix)")
155     set_property(CACHE GMX_CUDA_TARGET_COMPUTE PROPERTY TYPE STRING)
156 endif()
157
158 # FindCUDA.cmake is unaware of the mechanism used by cmake to embed
159 # the compiler flag for the required C++ standard in the generated
160 # build files, so we have to pass it ourselves
161 if (CUDA_VERSION VERSION_LESS 11.0)
162     # CUDA doesn't formally support C++17 until version 11.0, so for
163     # now host-side code that compiles with CUDA is restricted to
164     # C++14. This needs to be expressed formally for older CUDA
165     # version.
166     list(APPEND GMX_CUDA_NVCC_FLAGS "${CMAKE_CXX14_STANDARD_COMPILE_OPTION}")
167 else()
168     # gcc-7 pre-dated C++17, so uses the -std=c++1z compiler flag for it,
169     # which modern nvcc does not recognize. So we work around that by
170     # compiling in C++14 mode. Clang doesn't have this problem because nvcc
171     # only supports version of clang that already understood -std=c++17
172     if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8)
173         list(APPEND GMX_CUDA_NVCC_FLAGS "${CMAKE_CXX14_STANDARD_COMPILE_OPTION}")
174     else()
175         list(APPEND GMX_CUDA_NVCC_FLAGS "${CMAKE_CXX17_STANDARD_COMPILE_OPTION}")
176     endif()
177 endif()
178
179 # assemble the CUDA flags
180 list(APPEND GMX_CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_GENCODE_FLAGS}")
181 list(APPEND GMX_CUDA_NVCC_FLAGS "-use_fast_math")
182
183 # assemble the CUDA host compiler flags
184 list(APPEND GMX_CUDA_NVCC_FLAGS "${CUDA_HOST_COMPILER_OPTIONS}")
185
186 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
187     # CUDA header cuda_runtime_api.h in at least CUDA 10.1 uses 0
188     # where nullptr would be preferable. GROMACS can't fix these, so
189     # must suppress them.
190     GMX_TEST_CXXFLAG(CXXFLAGS_NO_ZERO_AS_NULL_POINTER_CONSTANT "-Wno-zero-as-null-pointer-constant" NVCC_CLANG_SUPPRESSIONS_CXXFLAGS)
191
192     # CUDA header crt/math_functions.h in at least CUDA 10.x and 11.1
193     # used throw() specifications that are deprecated in more recent
194     # C++ versions. GROMACS can't fix these, so must suppress them.
195     GMX_TEST_CXXFLAG(CXXFLAGS_NO_DEPRECATED_DYNAMIC_EXCEPTION_SPEC "-Wno-deprecated-dynamic-exception-spec" NVCC_CLANG_SUPPRESSIONS_CXXFLAGS)
196
197     # Add these flags to those used for the host compiler. The
198     # "-Xcompiler" prefix directs nvcc to only use them for host
199     # compilation, which is all that is needed in this case.
200     foreach(_flag ${NVCC_CLANG_SUPPRESSIONS_CXXFLAGS})
201         list(APPEND GMX_CUDA_NVCC_FLAGS "-Xcompiler ${_flag}")
202     endforeach()
203 endif()
204
205 string(TOUPPER "${CMAKE_BUILD_TYPE}" _build_type)
206 gmx_check_if_changed(_cuda_nvcc_executable_or_flags_changed CUDA_NVCC_EXECUTABLE CUDA_NVCC_FLAGS CUDA_NVCC_FLAGS_${_build_type})
207
208 # We would like to be helpful and reject the host compiler with a
209 # clear error message at configure time, rather than let nvcc
210 # later reject the host compiler as not supported when the first
211 # CUDA source file is built. We've implemented that for current
212 # nvcc running on Unix-like systems, but e.g. changes to nvcc
213 # will further affect the limited portability of this checking
214 # code. Set the CMake variable GMX_NVCC_WORKS on if you want to
215 # bypass this check.
216 if((_cuda_nvcc_executable_or_flags_changed OR CUDA_HOST_COMPILER_CHANGED OR NOT GMX_NVCC_WORKS) AND NOT WIN32)
217     message(STATUS "Check for working NVCC/C++ compiler combination with nvcc '${CUDA_NVCC_EXECUTABLE}'")
218     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
219         RESULT_VARIABLE _cuda_test_res
220         OUTPUT_VARIABLE _cuda_test_out
221         ERROR_VARIABLE  _cuda_test_err
222         OUTPUT_STRIP_TRAILING_WHITESPACE)
223
224     if(${_cuda_test_res})
225         message(STATUS "Check for working NVCC/C compiler combination - broken")
226         message(STATUS "${CUDA_NVCC_EXECUTABLE} standard output: '${_cuda_test_out}'")
227         message(STATUS "${CUDA_NVCC_EXECUTABLE} standard error:  '${_cuda_test_err}'")
228         if(${_cuda_test_err} MATCHES "nsupported")
229             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.")
230         else()
231             message(FATAL_ERROR "CUDA compiler does not seem to be functional.")
232         endif()
233     elseif(NOT GMX_CUDA_TEST_COMPILER_QUIETLY)
234         message(STATUS "Check for working NVCC/C++ compiler combination - works")
235         set(GMX_NVCC_WORKS TRUE CACHE INTERNAL "Nvcc can compile a trivial test program")
236     endif()
237 endif() # GMX_CHECK_NVCC
238
239
240 # The flags are set as local variables which shadow the cache variables. The cache variables
241 # (can be set by the user) are appended. This is done in a macro to set the flags when all
242 # host compiler flags are already set.
243 macro(GMX_SET_CUDA_NVCC_FLAGS)
244     set(CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_FLAGS};${CUDA_NVCC_FLAGS}")
245 endmacro()
246
247 # This helper function creates a temporary scope in which we can set
248 # the definitions, include directories and magic host-compiler-flag
249 # variables that have to be set in advance of calling
250 # cuda_add_library(). This is the only way cuda_add_library() can
251 # modify the command line used for host compilation. It is not
252 # possible to use the standard CMake mechanisms like
253 # target_compile_options() to add such things to targets after they
254 # are created.
255 function(gmx_cuda_add_library TARGET)
256     add_definitions(-DHAVE_CONFIG_H)
257     # Source files generated by NVCC can include gmxmpi.h, and so
258     # need access to thread-MPI.
259     include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
260     # Source files can also contain topology related files and need access to
261     # the remaining external headers
262     include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/src/external)
263
264     # Now add all the compilation options
265     gmx_cuda_target_compile_options(CUDA_${TARGET}_CXXFLAGS)
266     list(APPEND CMAKE_CXX_FLAGS ${CUDA_${TARGET}_CXXFLAGS})
267     foreach(build_type ${build_types_with_explicit_flags})
268         list(APPEND CMAKE_CXX_FLAGS_${build_type} ${CUDA_${TARGET}_CXXFLAGS_${build_type}})
269     endforeach()
270
271     cuda_add_library(${TARGET} ${ARGN})
272 endfunction()