3b5da4e575558da5a70fc76731ea26b144cf29d2
[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 Redmine 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 separated
90 # architectures 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
108     # First add flags that trigger SASS (binary) code generation for physical arch
109     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_30,code=sm_30")
110     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_35,code=sm_35")
111     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_37,code=sm_37")
112     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_50,code=sm_50")
113     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_52,code=sm_52")
114     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_60,code=sm_60")
115     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_61,code=sm_61")
116     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_70,code=sm_70")
117
118     # Next add flags that trigger PTX code generation for the newest supported virtual arch
119     # that's useful to JIT to future architectures
120     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_35,code=compute_35")
121     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_50,code=compute_50")
122     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_52,code=compute_52")
123     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_60,code=compute_60")
124     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_61,code=compute_61")
125     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_70,code=compute_70")
126     if(NOT CUDA_VERSION VERSION_LESS "10.0")
127         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_75,code=compute_75")
128     endif()
129 endif()
130
131 if (GMX_CUDA_TARGET_SM)
132     set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY HELPSTRING "List of CUDA GPU architecture codes to compile for (without the sm_ prefix)")
133     set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY TYPE STRING)
134 endif()
135 if (GMX_CUDA_TARGET_COMPUTE)
136     set_property(CACHE GMX_CUDA_TARGET_COMPUTE PROPERTY HELPSTRING "List of CUDA virtual architecture codes to compile for (without the compute_ prefix)")
137     set_property(CACHE GMX_CUDA_TARGET_COMPUTE PROPERTY TYPE STRING)
138 endif()
139
140 # FindCUDA.cmake is unaware of the mechanism used by cmake to embed
141 # the compiler flag for the required C++ standard in the generated
142 # build files, so we have to pass it ourselves
143 if (MSVC)
144     # We use C++14 on MSVC, but cmake does not understand the
145     # necessary compilation option for that until version 3.10, so we
146     # can remove this after we require that version.
147     if (NOT CMAKE_CXX14_STANDARD_COMPILE_OPTION)
148         set(GMX_CXX_STANDARD_COMPILE_OPTION "-std:c++14")
149     else()
150         set(GMX_CXX_STANDARD_COMPILE_OPTION "${CMAKE_CXX14_STANDARD_COMPILE_OPTION}")
151     endif()
152 else()
153     set(GMX_CXX_STANDARD_COMPILE_OPTION "${CMAKE_CXX14_STANDARD_COMPILE_OPTION}")
154 endif()
155 list(APPEND GMX_CUDA_NVCC_FLAGS "${GMX_CXX_STANDARD_COMPILE_OPTION}")
156
157 # assemble the CUDA flags
158 list(APPEND GMX_CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_GENCODE_FLAGS}")
159 list(APPEND GMX_CUDA_NVCC_FLAGS "-use_fast_math")
160
161 # assemble the CUDA host compiler flags
162 list(APPEND GMX_CUDA_NVCC_FLAGS "${CUDA_HOST_COMPILER_OPTIONS}")
163
164 string(TOUPPER "${CMAKE_BUILD_TYPE}" _build_type)
165 gmx_check_if_changed(_cuda_nvcc_executable_or_flags_changed CUDA_NVCC_EXECUTABLE CUDA_NVCC_FLAGS CUDA_NVCC_FLAGS_${_build_type})
166
167 # We would like to be helpful and reject the host compiler with a
168 # clear error message at configure time, rather than let nvcc
169 # later reject the host compiler as not supported when the first
170 # CUDA source file is built. We've implemented that for current
171 # nvcc running on Unix-like systems, but e.g. changes to nvcc
172 # will further affect the limited portability of this checking
173 # code. Set the CMake variable GMX_NVCC_WORKS on if you want to
174 # bypass this check.
175 if((_cuda_nvcc_executable_or_flags_changed OR CUDA_HOST_COMPILER_CHANGED OR NOT GMX_NVCC_WORKS) AND NOT WIN32)
176     message(STATUS "Check for working NVCC/C++ compiler combination with nvcc '${CUDA_NVCC_EXECUTABLE}'")
177     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
178         RESULT_VARIABLE _cuda_test_res
179         OUTPUT_VARIABLE _cuda_test_out
180         ERROR_VARIABLE  _cuda_test_err
181         OUTPUT_STRIP_TRAILING_WHITESPACE)
182
183     if(${_cuda_test_res})
184         message(STATUS "Check for working NVCC/C compiler combination - broken")
185         message(STATUS "${CUDA_NVCC_EXECUTABLE} standard output: '${_cuda_test_out}'")
186         message(STATUS "${CUDA_NVCC_EXECUTABLE} standard error:  '${_cuda_test_err}'")
187         if(${_cuda_test_err} MATCHES "nsupported")
188             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.")
189         else()
190             message(FATAL_ERROR "CUDA compiler does not seem to be functional.")
191         endif()
192     elseif(NOT GMX_CUDA_TEST_COMPILER_QUIETLY)
193         message(STATUS "Check for working NVCC/C++ compiler combination - works")
194         set(GMX_NVCC_WORKS TRUE CACHE INTERNAL "Nvcc can compile a trivial test program")
195     endif()
196 endif() # GMX_CHECK_NVCC
197
198
199 # The flags are set as local variables which shadow the cache variables. The cache variables
200 # (can be set by the user) are appended. This is done in a macro to set the flags when all
201 # host compiler flags are already set.
202 macro(GMX_SET_CUDA_NVCC_FLAGS)
203     set(CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_FLAGS};${CUDA_NVCC_FLAGS}")
204 endmacro()
205
206 # This helper function creates a temporary scope in which we can set
207 # the definitions, include directories and magic host-compiler-flag
208 # variables that have to be set in advance of calling
209 # cuda_add_library(). This is the only way cuda_add_library() can
210 # modify the command line used for host compilation. It is not
211 # possible to use the standard CMake mechanisms like
212 # target_compile_options() to add such things to targets after they
213 # are created.
214 function(gmx_cuda_add_library TARGET)
215     add_definitions(-DHAVE_CONFIG_H)
216     # Source files generated by NVCC can include gmxmpi.h, and so
217     # need access to thread-MPI.
218     include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
219     # Source files can also contain topology related files and need access to
220     # the remaining external headers
221     include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/src/external)
222
223     # Now add all the compilation options
224     gmx_cuda_target_compile_options(CUDA_${TARGET}_CXXFLAGS)
225     list(APPEND CMAKE_CXX_FLAGS ${CUDA_${TARGET}_CXXFLAGS})
226     foreach(build_type ${build_types_with_explicit_flags})
227         list(APPEND CMAKE_CXX_FLAGS_${build_type} ${CUDA_${TARGET}_CXXFLAGS_${build_type}})
228     endforeach()
229
230     cuda_add_library(${TARGET} ${ARGN})
231 endfunction()