Re-enable accidentally lost DD warning
[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, 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 # - auto-detect compatible nvcc host compiler and set nvcc -ccbin (if not MPI wrapper)
39 # - set icc compatibility mode to gcc 4.6
40 # - (advanced) variables set:
41 #   * CUDA_HOST_COMPILER            - the host compiler for nvcc (only with cmake <2.8.10)
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 gmx_check_if_changed(CUDA_HOST_COMPILER_CHANGED CUDA_HOST_COMPILER)
48
49 # CUDA_HOST_COMPILER changed hence it is not auto-set anymore
50 if (CUDA_HOST_COMPILER_CHANGED AND CUDA_HOST_COMPILER_AUTOSET)
51     unset(CUDA_HOST_COMPILER_AUTOSET CACHE)
52 endif()
53
54 # Set the host compiler for nvcc if this is not set by CMake (v<=2.8.9)
55 #
56 # Note that even though nvcc compiles host code as C++, we use the
57 # CMAKE_C_COMPILER as host compiler. We do this because CUDA versions
58 # preceding 5.0 only recognize icc, but not icpc. However, both gcc and icc
59 # (i.e. all supported compilers) happily compile C++ code.
60 #
61 # Also note that with MSVC nvcc sets the -compiler-bindir option behind the
62 # scenes; to avoid conflicts we don't set -ccbin automatically.
63 #
64 # TODO: remove this when CMAke >=v2.8.10 is required.
65 if (NOT DEFINED CUDA_HOST_COMPILER AND NOT MSVC)
66     set(CUDA_HOST_COMPILER "${CMAKE_C_COMPILER}")
67     set(CUDA_HOST_COMPILER_AUTOSET TRUE CACHE INTERNAL
68         "True if CUDA_HOST_COMPILER is automatically set")
69 endif()
70
71 # glibc 2.23 changed string.h in a way that breaks CUDA compilation in
72 # many projects, but which has a trivial workaround. It would be nicer
73 # to compile with nvcc and see that the workaround is necessary and
74 # effective, but it is unclear how to do that. Also, grepping in the
75 # glibc source shows that _FORCE_INLINES is only used in this string.h
76 # feature and performance of memcpy variants is unimportant for CUDA
77 # code in GROMACS. So this workaround is good enough to keep problems
78 # away from users installing GROMACS. See Redmine 1942.
79 function(work_around_glibc_2_23)
80     try_compile(IS_GLIBC_2_23_OR_HIGHER ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/cmake/TestGlibcVersion.cpp)
81     if(IS_GLIBC_2_23_OR_HIGHER)
82         message(STATUS "Adding work-around for issue compiling CUDA code with glibc 2.23 string.h")
83         list(APPEND CUDA_HOST_COMPILER_OPTIONS "-D_FORCE_INLINES")
84         set(CUDA_HOST_COMPILER_OPTIONS ${CUDA_HOST_COMPILER_OPTIONS} PARENT_SCOPE)
85     endif()
86 endfunction()
87
88 # set up host compiler and its options
89 if(CUDA_HOST_COMPILER_CHANGED)
90     # FindCUDA in CMake 2.8.10 sets the host compiler internally
91     if (CMAKE_VERSION VERSION_LESS "2.8.10")
92         set(CUDA_HOST_COMPILER ${CUDA_HOST_COMPILER}
93             CACHE PATH "Host compiler for nvcc")
94     endif()
95
96     # On *nix force icc in gcc 4.6 compatibility mode. This is needed
97     # as even with icc used as host compiler, when icc's gcc compatibility
98     # mode is higher than the max gcc version officially supported by CUDA,
99     # nvcc will freak out.
100     set(CUDA_HOST_COMPILER_OPTIONS "")
101     if (UNIX AND
102             ((CMAKE_C_COMPILER_ID MATCHES "Intel" AND
103               (CUDA_HOST_COMPILER_AUTOSET OR CMAKE_C_COMPILER STREQUAL CUDA_HOST_COMPILER)) OR
104             (CMAKE_CXX_COMPILER_ID MATCHES "Intel" AND CMAKE_CXX_COMPILER STREQUAL CUDA_HOST_COMPILER))
105         )
106         message(STATUS "Setting Intel Compiler compatibity mode to gcc 4.6 for nvcc host compilation")
107         list(APPEND CUDA_HOST_COMPILER_OPTIONS "-Xcompiler;-gcc-version=460")
108     endif()
109
110     if(APPLE AND CMAKE_C_COMPILER_ID MATCHES "GNU")
111         # Some versions of gcc-4.8 and gcc-4.9 produce errors (in particular on OS X)
112         # if we do not use -D__STRICT_ANSI__. It is harmless, so we might as well add it for all versions.
113         list(APPEND CUDA_HOST_COMPILER_OPTIONS "-D__STRICT_ANSI__")
114     endif()
115
116     work_around_glibc_2_23()
117
118     set(CUDA_HOST_COMPILER_OPTIONS "${CUDA_HOST_COMPILER_OPTIONS}"
119         CACHE STRING "Options for nvcc host compiler (do not edit!).")
120
121     mark_as_advanced(CUDA_HOST_COMPILER CUDA_HOST_COMPILER_OPTIONS)
122 endif()
123
124 # If any of these manual override variables for target CUDA GPU architectures
125 # or virtual architecture is set, parse the values and assemble the nvcc
126 # command line for these. Otherwise use our defaults.
127 # Note that the manual override variables require a semicolon separated
128 # architectures codes.
129 if (GMX_CUDA_TARGET_SM OR GMX_CUDA_TARGET_COMPUTE)
130     set(GMX_CUDA_NVCC_GENCODE_FLAGS)
131     set(_target_sm_list ${GMX_CUDA_TARGET_SM})
132     foreach(_target ${_target_sm_list})
133         list(APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_${_target},code=sm_${_target}")
134     endforeach()
135     set(_target_compute_list ${GMX_CUDA_TARGET_COMPUTE})
136     foreach(_target ${_target_compute_list})
137         list(APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_${_target},code=compute_${_target}")
138     endforeach()
139 else()
140     # Set the CUDA GPU architectures to compile for:
141     # - with CUDA >=5.0 <6.5:   CC <=3.5 is supported
142     #     => compile sm_20, sm_30, sm_35 SASS, and compute_35 PTX
143     # - with CUDA ==6.5:        CC <=3.7 and 5.0 are supported
144     #     => compile sm_20, sm_30, sm_35, sm_37 sm_50, SASS, and compute_50 PTX
145     # - with CUDA >=7.0         CC 5.2 is supported (5.3, Tegra X1 we don't generate code for)
146     #     => compile sm_20, sm_30, sm_35, sm_37, sm_50, & sm_52 SASS, and compute_52 PTX
147     # - with CUDA >=8.0         CC 6.0-6.2 is supported (but we know nothing about CC 6.2, so we won't generate code or it)
148     #     => compile sm_20, sm_30, sm_35, sm_37, sm_50, sm_52, sm_60, sm_61 SASS, and compute_60 and compute_61 PTX
149     #
150     #
151     #   Note that CUDA 6.5.19 second patch release supports cc 5.2 too, but
152     #   CUDA_VERSION does not contain patch version and having PTX 5.0 JIT-ed is
153     #   equally fast as compiling with sm_5.2 anyway.
154
155     # First add flags that trigger SASS (binary) code generation for physical arch
156     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_20,code=sm_20")
157     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_30,code=sm_30")
158     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_35,code=sm_35")
159
160     if(NOT CUDA_VERSION VERSION_LESS "6.5") # >= 6.5
161         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_37,code=sm_37")
162         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_50,code=sm_50")
163     endif()
164     if(NOT CUDA_VERSION VERSION_LESS "7.0") # >= 7.0
165         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_52,code=sm_52")
166     endif()
167     if(NOT CUDA_VERSION VERSION_LESS "8.0") # >= 8.0
168         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_60,code=sm_60")
169         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_61,code=sm_61")
170     endif()
171
172     # Next add flags that trigger PTX code generation for the newest supported virtual arch
173     # that's useful to JIT to future architectures
174     if(CUDA_VERSION VERSION_LESS "6.5")
175         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_35,code=compute_35")
176     elseif(CUDA_VERSION VERSION_LESS "7.0")
177         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_50,code=compute_50")
178     elseif(CUDA_VERSION VERSION_LESS "8.0")
179         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_52,code=compute_52")
180     else() # version >= 8.0
181         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_60,code=compute_60")
182         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_61,code=compute_61")
183     endif()
184 endif()
185
186 gmx_dependent_cache_variable(GMX_CUDA_TARGET_SM "List of CUDA GPU architecture codes to compile for (without the sm_ prefix)" STRING "" GMX_CUDA_TARGET_SM)
187 gmx_dependent_cache_variable(GMX_CUDA_TARGET_COMPUTE "List of CUDA virtual architecture codes to compile for (without the compute_ prefix)" STRING "" GMX_CUDA_TARGET_COMPUTE)
188
189 # assemble the CUDA flags
190 list(APPEND GMX_CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_GENCODE_FLAGS}")
191 list(APPEND GMX_CUDA_NVCC_FLAGS "-use_fast_math")
192 if (CUDA_VERSION VERSION_EQUAL "8.0")
193     # requesting sm_20 triggers deprecation messages with nvcc 8.0 which we better avoid
194     list(APPEND GMX_CUDA_NVCC_FLAGS "-Wno-deprecated-gpu-targets")
195 endif()
196
197 # assemble the CUDA host compiler flags
198 # with CMake <2.8.10 the host compiler needs to be set on the nvcc command line
199 if (CMAKE_VERSION VERSION_LESS "2.8.10")
200     list(APPEND GMX_CUDA_NVCC_FLAGS "-ccbin=${CUDA_HOST_COMPILER}")
201 endif()
202 list(APPEND GMX_CUDA_NVCC_FLAGS "${CUDA_HOST_COMPILER_OPTIONS}")
203
204 # The flags are set as local variables which shadow the cache variables. The cache variables
205 # (can be set by the user) are appended. This is done in a macro to set the flags when all
206 # host compiler flags are already set.
207 macro(GMX_SET_CUDA_NVCC_FLAGS)
208     if(CUDA_PROPAGATE_HOST_FLAGS)
209         set(CUDA_PROPAGATE_HOST_FLAGS OFF)
210
211         # When CUDA 6.5 is required we should use C++11 also for CUDA and also propagate
212         # the C++11 flag to CUDA. Then we can use the solution implemented in FindCUDA
213         # (starting with 3.3 - can be backported). For now we need to remove the C++11
214         # flag which means we need to manually propagate all other flags.
215         string(REGEX REPLACE "[-]+std=c\\+\\+0x" "" _CMAKE_CXX_FLAGS_SANITIZED "${CMAKE_CXX_FLAGS}")
216
217         # The IBM xlc compiler chokes if we use both altivec and Cuda. Solve
218         # this by not propagating the flag in this case.
219         if(CMAKE_CXX_COMPILER_ID MATCHES "XL")
220             string(REGEX REPLACE "-qaltivec" "" _CMAKE_CXX_FLAGS_SANITIZED "${_CMAKE_CXX_FLAGS_SANITIZED}")
221         endif()
222
223         # CUDA versions prior to 7.5 come with a header (math_functions.h) which uses the _MSC_VER macro
224         # unconditionally, so we strip -Wundef from the propagatest flags for earlier CUDA versions.
225         if (CUDA_VERSION VERSION_LESS "7.5")
226             string(REGEX REPLACE "-Wundef" "" _CMAKE_CXX_FLAGS_SANITIZED "${_CMAKE_CXX_FLAGS_SANITIZED}")
227         endif()
228
229         string(REPLACE " " "," _flags "${_CMAKE_CXX_FLAGS_SANITIZED}")
230         set(CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_FLAGS};${CUDA_NVCC_FLAGS};-Xcompiler;${_flags}")
231
232         # Create list of all possible configurations. For multi-configuration this is CMAKE_CONFIGURATION_TYPES
233         # and for single configuration CMAKE_BUILD_TYPE. Not sure why to add the default ones, but FindCUDA
234         # claims one should.
235         set(CUDA_configuration_types ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE} Debug MinSizeRel Release RelWithDebInfo)
236         list(REMOVE_DUPLICATES CUDA_configuration_types)
237
238         foreach(_config ${CUDA_configuration_types})
239             string(TOUPPER ${_config} _config_upper)
240             string(REPLACE " " "," _flags "${CMAKE_CXX_FLAGS_${_config_upper}}")
241             set(CUDA_NVCC_FLAGS_${_config_upper} "${CUDA_NVCC_FLAGS_${_config_upper}};-Xcompiler;${_flags}")
242         endforeach()
243     else()
244         set(CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_FLAGS};${CUDA_NVCC_FLAGS}")
245     endif()
246 endmacro()