2 # This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
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.8.1
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
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.
47 gmx_check_if_changed(CUDA_HOST_COMPILER_CHANGED CUDA_HOST_COMPILER)
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)
54 # glibc 2.23 changed string.h in a way that breaks CUDA compilation in
55 # many projects, but which has a trivial workaround. It would be nicer
56 # to compile with nvcc and see that the workaround is necessary and
57 # effective, but it is unclear how to do that. Also, grepping in the
58 # glibc source shows that _FORCE_INLINES is only used in this string.h
59 # feature and performance of memcpy variants is unimportant for CUDA
60 # code in GROMACS. So this workaround is good enough to keep problems
61 # away from users installing GROMACS. See Redmine 1942.
62 function(work_around_glibc_2_23)
63 try_compile(IS_GLIBC_2_23_OR_HIGHER ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/cmake/TestGlibcVersion.cpp)
64 if(IS_GLIBC_2_23_OR_HIGHER)
65 message(STATUS "Adding work-around for issue compiling CUDA code with glibc 2.23 string.h")
66 list(APPEND CUDA_HOST_COMPILER_OPTIONS "-D_FORCE_INLINES")
67 set(CUDA_HOST_COMPILER_OPTIONS ${CUDA_HOST_COMPILER_OPTIONS} PARENT_SCOPE)
71 # set up host compiler and its options
72 if(CUDA_HOST_COMPILER_CHANGED)
73 set(CUDA_HOST_COMPILER_OPTIONS "")
75 if(APPLE AND CMAKE_C_COMPILER_ID MATCHES "GNU")
76 # Some versions of gcc-4.8 and gcc-4.9 produce errors (in particular on OS X)
77 # if we do not use -D__STRICT_ANSI__. It is harmless, so we might as well add it for all versions.
78 list(APPEND CUDA_HOST_COMPILER_OPTIONS "-D__STRICT_ANSI__")
81 work_around_glibc_2_23()
83 set(CUDA_HOST_COMPILER_OPTIONS "${CUDA_HOST_COMPILER_OPTIONS}"
84 CACHE STRING "Options for nvcc host compiler (do not edit!).")
86 mark_as_advanced(CUDA_HOST_COMPILER CUDA_HOST_COMPILER_OPTIONS)
89 # If any of these manual override variables for target CUDA GPU architectures
90 # or virtual architecture is set, parse the values and assemble the nvcc
91 # command line for these. Otherwise use our defaults.
92 # Note that the manual override variables require a semicolon separated
93 # architectures codes.
94 if (GMX_CUDA_TARGET_SM OR GMX_CUDA_TARGET_COMPUTE)
95 set(GMX_CUDA_NVCC_GENCODE_FLAGS)
96 set(_target_sm_list ${GMX_CUDA_TARGET_SM})
97 foreach(_target ${_target_sm_list})
98 list(APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_${_target},code=sm_${_target}")
100 set(_target_compute_list ${GMX_CUDA_TARGET_COMPUTE})
101 foreach(_target ${_target_compute_list})
102 list(APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_${_target},code=compute_${_target}")
105 # Set the CUDA GPU architectures to compile for:
106 # - with CUDA >=5.0 <6.5: CC <=3.5 is supported
107 # => compile sm_20, sm_30, sm_35 SASS, and compute_35 PTX
108 # - with CUDA ==6.5: CC <=3.7 and 5.0 are supported
109 # => compile sm_20, sm_30, sm_35, sm_37 sm_50, SASS, and compute_50 PTX
110 # - with CUDA >=7.0 CC 5.2 is supported (5.3, Tegra X1 we don't generate code for)
111 # => compile sm_20, sm_30, sm_35, sm_37, sm_50, & sm_52 SASS, and compute_52 PTX
112 # - 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)
113 # => 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
114 # - with CUDA >=9.0 CC 7.0 is supported and CC 2.0 is no longer supported
115 # => compile sm_30, sm_35, sm_37, sm_50, sm_52, sm_60, sm_61, sm_70 SASS, and compute_70 PTX
117 # Note that CUDA 6.5.19 second patch release supports cc 5.2 too, but
118 # CUDA_VERSION does not contain patch version and having PTX 5.0 JIT-ed is
119 # equally fast as compiling with sm_5.2 anyway.
121 # First add flags that trigger SASS (binary) code generation for physical arch
122 if(CUDA_VERSION VERSION_LESS "9.00") # < 9.0
123 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_20,code=sm_20")
125 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_30,code=sm_30")
126 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_35,code=sm_35")
128 if(NOT CUDA_VERSION VERSION_LESS "6.5") # >= 6.5
129 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_37,code=sm_37")
130 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_50,code=sm_50")
132 if(NOT CUDA_VERSION VERSION_LESS "7.0") # >= 7.0
133 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_52,code=sm_52")
135 if(NOT CUDA_VERSION VERSION_LESS "8.0") # >= 8.0
136 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_60,code=sm_60")
137 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_61,code=sm_61")
139 if(NOT CUDA_VERSION VERSION_LESS "9.0") # >= 9.0
140 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_70,code=sm_70")
143 # Next add flags that trigger PTX code generation for the newest supported virtual arch
144 # that's useful to JIT to future architectures
145 if(CUDA_VERSION VERSION_LESS "6.5")
146 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_35,code=compute_35")
147 elseif(CUDA_VERSION VERSION_LESS "7.0")
148 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_50,code=compute_50")
149 elseif(CUDA_VERSION VERSION_LESS "8.0")
150 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_52,code=compute_52")
151 elseif(CUDA_VERSION VERSION_LESS "9.0")
152 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_60,code=compute_60")
153 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_61,code=compute_61")
154 else() # version >= 9.0
155 list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_70,code=compute_70")
159 if (GMX_CUDA_TARGET_SM)
160 set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY HELPSTRING "List of CUDA GPU architecture codes to compile for (without the sm_ prefix)")
161 set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY TYPE STRING)
163 if (GMX_CUDA_TARGET_COMPUTE)
164 set_property(CACHE GMX_CUDA_TARGET_COMPUTE PROPERTY HELPSTRING "List of CUDA virtual architecture codes to compile for (without the compute_ prefix)")
165 set_property(CACHE GMX_CUDA_TARGET_COMPUTE PROPERTY TYPE STRING)
168 # assemble the CUDA flags
169 list(APPEND GMX_CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_GENCODE_FLAGS}")
170 list(APPEND GMX_CUDA_NVCC_FLAGS "-use_fast_math")
171 if (CUDA_VERSION VERSION_EQUAL "8.0")
172 # requesting sm_20 triggers deprecation messages with nvcc 8.0 which we better avoid
173 list(APPEND GMX_CUDA_NVCC_FLAGS "-Wno-deprecated-gpu-targets")
176 # assemble the CUDA host compiler flags
177 list(APPEND GMX_CUDA_NVCC_FLAGS "${CUDA_HOST_COMPILER_OPTIONS}")
179 # The flags are set as local variables which shadow the cache variables. The cache variables
180 # (can be set by the user) are appended. This is done in a macro to set the flags when all
181 # host compiler flags are already set.
182 macro(GMX_SET_CUDA_NVCC_FLAGS)
183 set(CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_FLAGS};${CUDA_NVCC_FLAGS}")