Remove unused environment variables from docs
[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 #1982.
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 # We would like to be helpful and reject the host compiler with a
87 # clear error message at configure time, rather than let nvcc
88 # later reject the host compiler as not supported when the first
89 # CUDA source file is built. We've implemented that for current
90 # nvcc running on Unix-like systems, but e.g. changes to nvcc
91 # will further affect the limited portability of this checking
92 # code. Set the CMake variable GMX_NVCC_WORKS on if you want to
93 # bypass this check.
94 if((_cuda_nvcc_executable_or_flags_changed OR CUDA_HOST_COMPILER_CHANGED OR NOT GMX_NVCC_WORKS) AND NOT WIN32)
95     message(STATUS "Check for working NVCC/C++ compiler combination with nvcc '${CUDA_NVCC_EXECUTABLE}'")
96     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
97         RESULT_VARIABLE _cuda_test_res
98         OUTPUT_VARIABLE _cuda_test_out
99         ERROR_VARIABLE  _cuda_test_err
100         OUTPUT_STRIP_TRAILING_WHITESPACE)
101
102     if(${_cuda_test_res})
103         message(STATUS "Check for working NVCC/C compiler combination - broken")
104         message(STATUS "${CUDA_NVCC_EXECUTABLE} standard output: '${_cuda_test_out}'")
105         message(STATUS "${CUDA_NVCC_EXECUTABLE} standard error:  '${_cuda_test_err}'")
106         if(${_cuda_test_err} MATCHES "nsupported")
107             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. Set the GMX_NVCC_WORKS CMake cache variable to bypass this check if you know what you are doing.")
108         else()
109             message(FATAL_ERROR "CUDA compiler does not seem to be functional. Set the GMX_NVCC_WORKS CMake cache variable to bypass this check if you know what you are doing.")
110         endif()
111     elseif(NOT GMX_CUDA_TEST_COMPILER_QUIETLY)
112         message(STATUS "Check for working NVCC/C++ compiler combination - works")
113         set(GMX_NVCC_WORKS TRUE CACHE INTERNAL "Nvcc can compile a trivial test program")
114     endif()
115 endif() # GMX_CHECK_NVCC
116
117 # Tests a single set of one or more flags to use with nvcc.
118 #
119 # If the flags are accepted, they are appended to the variable named
120 # in the first argument. The cache variable named in the second
121 # argument is used to avoid rerunning the check in future invocations
122 # of cmake. The list of flags to check follows these two required
123 # arguments.
124 #
125 # As this code is not yet tested on Windows, it always accepts the
126 # flags in that case.
127 function(gmx_add_nvcc_flag_if_supported _output_variable_name_to_append_to _flags_cache_variable_name)
128     # If the check has already been run, do not re-run it
129     if (NOT ${_flags_cache_variable_name} AND NOT WIN32)
130         message(STATUS "Checking if nvcc accepts flags ${ARGN}")
131         execute_process(
132             COMMAND ${CUDA_NVCC_EXECUTABLE} ${ARGN} "${CMAKE_SOURCE_DIR}/cmake/TestCUDA.cu"
133             RESULT_VARIABLE _cuda_success
134             OUTPUT_QUIET
135             ERROR_QUIET
136             )
137         # Convert the success value to a boolean and report status
138         if (_cuda_success EQUAL 0)
139             set(_cache_variable_value TRUE)
140             message(STATUS "Checking if nvcc accepts flags ${ARGN} - Success")
141         else()
142             set(_cache_variable_value FALSE)
143             message(STATUS "Checking if nvcc accepts flags ${ARGN} - Failed")
144         endif()
145         set(${_flags_cache_variable_name} ${_cache_variable_value} CACHE BOOL "Whether NVCC supports flag(s) ${ARGN}")
146     endif()
147     # Append the flags to the output variable if they have been tested to work
148     if (${_flags_cache_variable_name} OR WIN32)
149         list(APPEND ${_output_variable_name_to_append_to} ${ARGN})
150         set(${_output_variable_name_to_append_to} ${${_output_variable_name_to_append_to}} PARENT_SCOPE)
151     endif()
152 endfunction()
153
154 # If any of these manual override variables for target CUDA GPU architectures
155 # or virtual architecture is set, parse the values and assemble the nvcc
156 # command line for these. Otherwise use our defaults.
157 # Note that the manual override variables require a semicolon separating
158 # architecture codes.
159 set(GMX_CUDA_NVCC_GENCODE_FLAGS)
160 if (GMX_CUDA_TARGET_SM OR GMX_CUDA_TARGET_COMPUTE)
161     set(_target_sm_list ${GMX_CUDA_TARGET_SM})
162     foreach(_target ${_target_sm_list})
163         gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_AND_SM_${_target} -gencode arch=compute_${_target},code=sm_${_target})
164         if (NOT NVCC_HAS_GENCODE_COMPUTE_AND_SM_${_target} AND NOT WIN32)
165             message(FATAL_ERROR "Your choice of ${_target} in GMX_CUDA_TARGET_SM was not accepted by nvcc, please choose a target that it accepts")
166         endif()
167     endforeach()
168     set(_target_compute_list ${GMX_CUDA_TARGET_COMPUTE})
169     foreach(_target ${_target_compute_list})
170         gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_${_target} -gencode arch=compute_${_target},code=compute_${_target})
171         if (NOT NVCC_HAS_GENCODE_COMPUTE_${_target} AND NOT WIN32)
172             message(FATAL_ERROR "Your choice of ${_target} in GMX_CUDA_TARGET_COMPUTE was not accepted by nvcc, please choose a target that it accepts")
173         endif()
174     endforeach()
175 else()
176     # Set the CUDA GPU architectures to compile for:
177     # - with CUDA >=11.0        CC 8.0 is supported
178     #     => 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
179
180     # First add flags that trigger SASS (binary) code generation for physical arch
181     gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_AND_SM_35 -gencode arch=compute_35,code=sm_35)
182     gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_AND_SM_37 -gencode arch=compute_37,code=sm_37)
183     gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_AND_SM_50 -gencode arch=compute_50,code=sm_50)
184     gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_AND_SM_52 -gencode arch=compute_52,code=sm_52)
185     gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_AND_SM_60 -gencode arch=compute_60,code=sm_60)
186     gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_AND_SM_61 -gencode arch=compute_61,code=sm_61)
187     gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_AND_SM_70 -gencode arch=compute_70,code=sm_70)
188     gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_AND_SM_75 -gencode arch=compute_75,code=sm_75)
189     gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_AND_SM_80 -gencode arch=compute_80,code=sm_80)
190     gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_AND_SM_86 -gencode arch=compute_86,code=sm_86)
191     # Requesting sm or compute 35, 37, or 50 triggers deprecation messages with
192     # nvcc 11.0, which we need to suppress for use in CI
193     gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_WARNING_NO_DEPRECATED_GPU_TARGETS -Wno-deprecated-gpu-targets)
194
195     # Next add flags that trigger PTX code generation for the
196     # newest supported virtual arch that's useful to JIT to future architectures
197     # as well as an older one suitable for JIT-ing to any rare intermediate arch
198     # (like that of Jetson / Drive PX devices)
199     gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_53 -gencode arch=compute_53,code=sm_53)
200     gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_GENCODE_FLAGS NVCC_HAS_GENCODE_COMPUTE_80 -gencode arch=compute_80,code=sm_80)
201 endif()
202
203 if (GMX_CUDA_TARGET_SM)
204     set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY HELPSTRING "List of CUDA GPU architecture codes to compile for (without the sm_ prefix)")
205     set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY TYPE STRING)
206 endif()
207 if (GMX_CUDA_TARGET_COMPUTE)
208     set_property(CACHE GMX_CUDA_TARGET_COMPUTE PROPERTY HELPSTRING "List of CUDA virtual architecture codes to compile for (without the compute_ prefix)")
209     set_property(CACHE GMX_CUDA_TARGET_COMPUTE PROPERTY TYPE STRING)
210 endif()
211
212 # FindCUDA.cmake is unaware of the mechanism used by cmake to embed
213 # the compiler flag for the required C++ standard in the generated
214 # build files, so we have to pass it ourselves
215
216 # gcc-7 pre-dated C++17, so uses the -std=c++1z compiler flag for it,
217 # which modern nvcc does not recognize. So we work around that by
218 # compiling in C++14 mode. Clang doesn't have this problem because nvcc
219 # only supports version of clang that already understood -std=c++17
220 if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8)
221     list(APPEND GMX_CUDA_NVCC_FLAGS "${CMAKE_CXX14_STANDARD_COMPILE_OPTION}")
222 else()
223     list(APPEND GMX_CUDA_NVCC_FLAGS "${CMAKE_CXX17_STANDARD_COMPILE_OPTION}")
224 endif()
225
226 # assemble the CUDA flags
227 list(APPEND GMX_CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_GENCODE_FLAGS}")
228 gmx_add_nvcc_flag_if_supported(GMX_CUDA_NVCC_FLAGS NVCC_HAS_USE_FAST_MATH -use_fast_math)
229
230 # assemble the CUDA host compiler flags
231 list(APPEND GMX_CUDA_NVCC_FLAGS "${CUDA_HOST_COMPILER_OPTIONS}")
232
233 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
234     # CUDA header cuda_runtime_api.h in at least CUDA 10.1 uses 0
235     # where nullptr would be preferable. GROMACS can't fix these, so
236     # must suppress them.
237     GMX_TEST_CXXFLAG(HAS_WARNING_NO_ZERO_AS_NULL_POINTER_CONSTANT "-Wno-zero-as-null-pointer-constant" NVCC_CLANG_SUPPRESSIONS_CXXFLAGS)
238
239     # CUDA header crt/math_functions.h in at least CUDA 10.x and 11.1
240     # used throw() specifications that are deprecated in more recent
241     # C++ versions. GROMACS can't fix these, so must suppress them.
242     GMX_TEST_CXXFLAG(HAS_WARNING_NO_DEPRECATED_DYNAMIC_EXCEPTION_SPEC "-Wno-deprecated-dynamic-exception-spec" NVCC_CLANG_SUPPRESSIONS_CXXFLAGS)
243
244     # CUDA headers cuda_runtime.h and channel_descriptor.h in at least
245     # CUDA 11.0 uses many C-style casts, which are ncessary for this
246     # header to work for C. GROMACS can't fix these, so must suppress
247     # the warnings they generate
248     GMX_TEST_CXXFLAG(HAS_WARNING_NO_OLD_STYLE_CAST "-Wno-old-style-cast" NVCC_CLANG_SUPPRESSIONS_CXXFLAGS)
249
250     # Add these flags to those used for the host compiler. The
251     # "-Xcompiler" prefix directs nvcc to only use them for host
252     # compilation, which is all that is needed in this case.
253     foreach(_flag ${NVCC_CLANG_SUPPRESSIONS_CXXFLAGS})
254         list(APPEND GMX_CUDA_NVCC_FLAGS "-Xcompiler ${_flag}")
255     endforeach()
256 endif()
257
258 string(TOUPPER "${CMAKE_BUILD_TYPE}" _build_type)
259 gmx_check_if_changed(_cuda_nvcc_executable_or_flags_changed CUDA_NVCC_EXECUTABLE CUDA_NVCC_FLAGS CUDA_NVCC_FLAGS_${_build_type})
260
261
262 # The flags are set as local variables which shadow the cache variables. The cache variables
263 # (can be set by the user) are appended. This is done in a macro to set the flags when all
264 # host compiler flags are already set.
265 macro(GMX_SET_CUDA_NVCC_FLAGS)
266     set(CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_FLAGS};${CUDA_NVCC_FLAGS}")
267 endmacro()
268
269 # This helper function creates a temporary scope in which we can set
270 # the definitions, include directories and magic host-compiler-flag
271 # variables that have to be set in advance of calling
272 # cuda_add_library(). This is the only way cuda_add_library() can
273 # modify the command line used for host compilation. It is not
274 # possible to use the standard CMake mechanisms like
275 # target_compile_options() to add such things to targets after they
276 # are created.
277 function(gmx_cuda_add_library TARGET)
278     add_definitions(-DHAVE_CONFIG_H)
279     # Source files generated by NVCC can include gmxmpi.h, and so
280     # need access to thread-MPI.
281     include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
282     # Source files can also contain topology related files and need access to
283     # the remaining external headers
284     include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/src/external)
285
286     # Now add all the compilation options
287     gmx_cuda_target_compile_options(CUDA_${TARGET}_CXXFLAGS)
288     list(APPEND CMAKE_CXX_FLAGS ${CUDA_${TARGET}_CXXFLAGS})
289     foreach(build_type ${build_types_with_explicit_flags})
290         list(APPEND CMAKE_CXX_FLAGS_${build_type} ${CUDA_${TARGET}_CXXFLAGS_${build_type}})
291     endforeach()
292
293     cuda_add_library(${TARGET} ${ARGN})
294 endfunction()