67f42e828ef61f09312832b5d1f3fba078429aee
[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,2017,2018,2019, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34
35 # Manage CUDA nvcc compilation configuration, try to be smart to ease the users'
36 # pain as much as possible:
37 # - use the CUDA_HOST_COMPILER if defined by the user, otherwise
38 # - check if nvcc works with CUDA_HOST_COMPILER and the generated nvcc and C++ flags
39 #
40 # - (advanced) variables set:
41 #   * CUDA_HOST_COMPILER_OPTIONS    - the full host-compiler related option list passed to nvcc
42 #
43 # Note that from CMake 2.8.10 FindCUDA defines CUDA_HOST_COMPILER internally,
44 # so we won't set it ourselves, but hope that the module does a good job.
45
46 # glibc 2.23 changed string.h in a way that breaks CUDA compilation in
47 # many projects, but which has a trivial workaround. It would be nicer
48 # to compile with nvcc and see that the workaround is necessary and
49 # effective, but it is unclear how to do that. Also, grepping in the
50 # glibc source shows that _FORCE_INLINES is only used in this string.h
51 # feature and performance of memcpy variants is unimportant for CUDA
52 # code in GROMACS. So this workaround is good enough to keep problems
53 # away from users installing GROMACS. See Redmine 1942.
54 function(work_around_glibc_2_23)
55     try_compile(IS_GLIBC_2_23_OR_HIGHER ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/cmake/TestGlibcVersion.cpp)
56     if(IS_GLIBC_2_23_OR_HIGHER)
57         message(STATUS "Adding work-around for issue compiling CUDA code with glibc 2.23 string.h")
58         list(APPEND CUDA_HOST_COMPILER_OPTIONS "-D_FORCE_INLINES")
59         set(CUDA_HOST_COMPILER_OPTIONS ${CUDA_HOST_COMPILER_OPTIONS} PARENT_SCOPE)
60     endif()
61 endfunction()
62
63 gmx_check_if_changed(CUDA_HOST_COMPILER_CHANGED CUDA_HOST_COMPILER)
64
65 # set up host compiler and its options
66 if(CUDA_HOST_COMPILER_CHANGED)
67     set(CUDA_HOST_COMPILER_OPTIONS "")
68
69     if(APPLE AND CMAKE_C_COMPILER_ID MATCHES "GNU")
70         # Some versions of gcc-4.8 and gcc-4.9 have produced errors
71         # (in particular on OS X) if we do not use
72         # -D__STRICT_ANSI__. It is harmless, so we might as well add
73         # it for all versions.
74         list(APPEND CUDA_HOST_COMPILER_OPTIONS "-D__STRICT_ANSI__")
75     endif()
76
77     work_around_glibc_2_23()
78
79     set(CUDA_HOST_COMPILER_OPTIONS "${CUDA_HOST_COMPILER_OPTIONS}"
80         CACHE STRING "Options for nvcc host compiler (do not edit!).")
81
82     mark_as_advanced(CUDA_HOST_COMPILER CUDA_HOST_COMPILER_OPTIONS)
83 endif()
84
85 # If any of these manual override variables for target CUDA GPU architectures
86 # or virtual architecture is set, parse the values and assemble the nvcc
87 # command line for these. Otherwise use our defaults.
88 # Note that the manual override variables require a semicolon separated
89 # architectures codes.
90 if (GMX_CUDA_TARGET_SM OR GMX_CUDA_TARGET_COMPUTE)
91     set(GMX_CUDA_NVCC_GENCODE_FLAGS)
92     set(_target_sm_list ${GMX_CUDA_TARGET_SM})
93     foreach(_target ${_target_sm_list})
94         list(APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_${_target},code=sm_${_target}")
95     endforeach()
96     set(_target_compute_list ${GMX_CUDA_TARGET_COMPUTE})
97     foreach(_target ${_target_compute_list})
98         list(APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_${_target},code=compute_${_target}")
99     endforeach()
100 else()
101     # Set the CUDA GPU architectures to compile for:
102     # - with CUDA >=9.0         CC 7.0 is supported and CC 2.0 is no longer supported
103     #     => compile sm_30, sm_35, sm_37, sm_50, sm_52, sm_60, sm_61, sm_70 SASS, and compute_70 PTX
104     # - with CUDA >=10.0        CC 7.5 is supported
105     #     => compile sm_30, sm_35, sm_37, sm_50, sm_52, sm_60, sm_61, sm_70, sm_75 SASS, and compute_75 PTX
106
107     # First add flags that trigger SASS (binary) code generation for physical arch
108     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_30,code=sm_30")
109     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_35,code=sm_35")
110     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_37,code=sm_37")
111     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_50,code=sm_50")
112     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_52,code=sm_52")
113     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_60,code=sm_60")
114     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_61,code=sm_61")
115     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_70,code=sm_70")
116
117     # Next add flags that trigger PTX code generation for the newest supported virtual arch
118     # that's useful to JIT to future architectures
119     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_35,code=compute_35")
120     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_50,code=compute_50")
121     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_52,code=compute_52")
122     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_60,code=compute_60")
123     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_61,code=compute_61")
124     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_70,code=compute_70")
125     if(NOT CUDA_VERSION VERSION_LESS "10.0")
126         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_75,code=compute_75")
127     endif()
128 endif()
129
130 if (GMX_CUDA_TARGET_SM)
131     set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY HELPSTRING "List of CUDA GPU architecture codes to compile for (without the sm_ prefix)")
132     set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY TYPE STRING)
133 endif()
134 if (GMX_CUDA_TARGET_COMPUTE)
135     set_property(CACHE GMX_CUDA_TARGET_COMPUTE PROPERTY HELPSTRING "List of CUDA virtual architecture codes to compile for (without the compute_ prefix)")
136     set_property(CACHE GMX_CUDA_TARGET_COMPUTE PROPERTY TYPE STRING)
137 endif()
138
139 # FindCUDA.cmake is unaware of the mechanism used by cmake to embed
140 # the compiler flag for the required C++ standard in the generated
141 # build files, so we have to pass it ourselves
142 if (MSVC)
143     # We use C++14 on MSVC, but cmake does not understand the
144     # necessary compilation option for that until version 3.10, so we
145     # can remove this after we require that version.
146     if (NOT CMAKE_CXX14_STANDARD_COMPILE_OPTION)
147         set(GMX_CXX_STANDARD_COMPILE_OPTION "-std:c++14")
148     else()
149         set(GMX_CXX_STANDARD_COMPILE_OPTION "${CMAKE_CXX14_STANDARD_COMPILE_OPTION}")
150     endif()
151 else()
152     set(GMX_CXX_STANDARD_COMPILE_OPTION "${CMAKE_CXX14_STANDARD_COMPILE_OPTION}")
153 endif()
154 list(APPEND GMX_CUDA_NVCC_FLAGS "${GMX_CXX_STANDARD_COMPILE_OPTION}")
155
156 # assemble the CUDA flags
157 list(APPEND GMX_CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_GENCODE_FLAGS}")
158 list(APPEND GMX_CUDA_NVCC_FLAGS "-use_fast_math")
159
160 # assemble the CUDA host compiler flags
161 list(APPEND GMX_CUDA_NVCC_FLAGS "${CUDA_HOST_COMPILER_OPTIONS}")
162
163 string(TOUPPER "${CMAKE_BUILD_TYPE}" _build_type)
164 gmx_check_if_changed(_cuda_nvcc_executable_or_flags_changed CUDA_NVCC_EXECUTABLE CUDA_NVCC_FLAGS CUDA_NVCC_FLAGS_${_build_type})
165
166 # We would like to be helpful and reject the host compiler with a
167 # clear error message at configure time, rather than let nvcc
168 # later reject the host compiler as not supported when the first
169 # CUDA source file is built. We've implemented that for current
170 # nvcc running on Unix-like systems, but e.g. changes to nvcc
171 # will further affect the limited portability of this checking
172 # code. Set the CMake variable GMX_NVCC_WORKS on if you want to
173 # bypass this check.
174 if((_cuda_nvcc_executable_or_flags_changed OR CUDA_HOST_COMPILER_CHANGED OR NOT GMX_NVCC_WORKS) AND NOT WIN32)
175     message(STATUS "Check for working NVCC/C++ compiler combination with nvcc '${CUDA_NVCC_EXECUTABLE}'")
176     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
177         RESULT_VARIABLE _cuda_test_res
178         OUTPUT_VARIABLE _cuda_test_out
179         ERROR_VARIABLE  _cuda_test_err
180         OUTPUT_STRIP_TRAILING_WHITESPACE)
181
182     if(${_cuda_test_res})
183         message(STATUS "Check for working NVCC/C compiler combination - broken")
184         message(STATUS "${CUDA_NVCC_EXECUTABLE} standard output: '${_cuda_test_out}'")
185         message(STATUS "${CUDA_NVCC_EXECUTABLE} standard error:  '${_cuda_test_err}'")
186         if(${_cuda_test_err} MATCHES "nsupported")
187             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.")
188         else()
189             message(FATAL_ERROR "CUDA compiler does not seem to be functional.")
190         endif()
191     elseif(NOT GMX_CUDA_TEST_COMPILER_QUIETLY)
192         message(STATUS "Check for working NVCC/C++ compiler combination - works")
193         set(GMX_NVCC_WORKS TRUE CACHE INTERNAL "Nvcc can compile a trivial test program")
194     endif()
195 endif() # GMX_CHECK_NVCC
196
197
198 # The flags are set as local variables which shadow the cache variables. The cache variables
199 # (can be set by the user) are appended. This is done in a macro to set the flags when all
200 # host compiler flags are already set.
201 macro(GMX_SET_CUDA_NVCC_FLAGS)
202     set(CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_FLAGS};${CUDA_NVCC_FLAGS}")
203 endmacro()
204
205 # This helper function creates a temporary scope in which we can set
206 # the definitions, include directories and magic host-compiler-flag
207 # variables that have to be set in advance of calling
208 # cuda_add_library(). This is the only way cuda_add_library() can
209 # modify the command line used for host compilation. It is not
210 # possible to use the standard CMake mechanisms like
211 # target_compile_options() to add such things to targets after they
212 # are created.
213 function(gmx_cuda_add_library TARGET)
214     add_definitions(-DHAVE_CONFIG_H)
215     # Source files generated by NVCC can include gmxmpi.h, and so
216     # need access to thread-MPI.
217     include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
218
219     # Now add all the compilation options
220     gmx_cuda_target_compile_options(CUDA_${TARGET}_CXXFLAGS)
221     list(APPEND CMAKE_CXX_FLAGS ${CUDA_${TARGET}_CXXFLAGS})
222     foreach(build_type ${build_types_with_explicit_flags})
223         list(APPEND CMAKE_CXX_FLAGS_${build_type} ${CUDA_${TARGET}_CXXFLAGS_${build_type}})
224     endforeach()
225
226     cuda_add_library(${TARGET} ${ARGN})
227 endfunction()