Merge branch 'master' into pygromacs
[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,215, 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.4/4.5 (CUDA 4.0 is not compatible with gcc >v4.4)
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 # Explicitly set the host compiler for nvcc if the current compiler is
55 # supported and it's not an MPI compiler wrapper, otherwise warn the user.
56 #
57 # Note that even though nvcc compiles host code as C++, we use the
58 # CMAKE_C_COMPILER as host compiler. We do this because CUDA versions
59 # preceding 5.0 only recognize icc, but not icpc. However, both gcc and icc
60 # (i.e. all supported compilers) happily compile C++ code.
61 #
62 # Also note that with MSVC nvcc sets the -compiler-bindir option behind the
63 # scenes; to avoid conflicts we don't set -ccbin automatically.
64 #
65 # This will be executed only with cmake <v2.8.10 as later versions set the
66 # host compiler in FindCUDA.
67 if (NOT DEFINED CUDA_HOST_COMPILER AND NOT MSVC)
68     if (NOT CMAKE_COMPILER_IS_GNUCC AND
69         NOT (CMAKE_C_COMPILER_ID MATCHES "Intel" AND UNIX AND NOT APPLE))
70         message(WARNING "
71         Will not set the nvcc host compiler because the current C compiler is not
72         compatible with nvcc:
73         ${CMAKE_C_COMPILER} (ID: ${CMAKE_C_COMPILER_ID})
74         Compatible compilers are: gcc on Linux and Mac OS X, the Intel Compiler on 64-bit
75         Linux and MSVC on Windows. Note that with newer CUDA releases this might change,
76         for up-to-date compatibility information check the NVIDIA documentation.
77         If nothing specified, nvcc will automatically pick the platform-default compiler;
78         Note that mixing compilers can cause errors.
79         To manually set the nvcc host compiler, edit CUDA_NVCC_FLAGS or re-configure
80         setting CUDA_HOST_COMPILER to the full path of a compatible compiler.
81         ")
82     else()
83         # do not use MPI compiler wrappers, as these are prone to brake nvcc
84         if (GMX_MPI AND NOT "${MPI_C_FOUND}") # FindMPI-based detection
85             message(WARNING "
86         Will not set the nvcc host compiler because the current C compiler is an MPI
87         compiler wrapper: ${CMAKE_C_COMPILER}
88         MPI compiler wrappers are prone to not work with nvcc. You might get lucky,
89         but the safest is to use the C compiler that the MPI compiler wrapper uses
90         (if this is compatible).
91         To manually set the nvcc host compiler, edit CUDA_NVCC_FLAGS or re-configure
92         setting CUDA_HOST_COMPILER to the full path of a compatible compiler.
93         ")
94         else()
95             set(CUDA_HOST_COMPILER "${CMAKE_C_COMPILER}")
96             set(CUDA_HOST_COMPILER_AUTOSET TRUE CACHE INTERNAL
97                 "True if CUDA_HOST_COMPILER is automatically set")
98         endif()
99     endif()
100 endif()
101
102 # set up host compiler and its options
103 if(CUDA_HOST_COMPILER_CHANGED)
104     # FindCUDA in CMake 2.8.10 sets the host compiler internally
105     if (CMAKE_VERSION VERSION_LESS "2.8.10")
106         message(STATUS "Setting the nvcc host compiler to: ${CUDA_HOST_COMPILER}")
107         set(CUDA_HOST_COMPILER ${CUDA_HOST_COMPILER}
108             CACHE PATH "Host compiler for nvcc")
109     endif()
110
111     # On *nix force icc in gcc 4.4 compatibility mode with CUDA 3.2/4.0 and
112     # gcc 4.5 compatibility mode with later CUDA versions. This is needed
113     # as even with icc used as host compiler, when icc's gcc compatibility
114     # mode is higher than the max gcc version officially supported by CUDA,
115     # nvcc will freak out.
116     set(CUDA_HOST_COMPILER_OPTIONS "")
117     if (UNIX AND
118             ((CMAKE_C_COMPILER_ID MATCHES "Intel" AND
119               (CUDA_HOST_COMPILER_AUTOSET OR CMAKE_C_COMPILER STREQUAL CUDA_HOST_COMPILER)) OR
120             (CMAKE_CXX_COMPILER_ID MATCHES "Intel" AND CMAKE_CXX_COMPILER STREQUAL CUDA_HOST_COMPILER))
121         )
122         if (CUDA_VERSION VERSION_LESS "4.1")
123             message(STATUS "Setting Intel Compiler compatibity mode to gcc 4.4 for nvcc host compilation")
124             list(APPEND CUDA_HOST_COMPILER_OPTIONS "-Xcompiler;-gcc-version=440")
125         else()
126             message(STATUS "Setting Intel Compiler compatibity mode to gcc 4.5 for nvcc host compilation")
127             list(APPEND CUDA_HOST_COMPILER_OPTIONS "-Xcompiler;-gcc-version=450")
128         endif()
129     endif()
130
131     if(APPLE AND CMAKE_C_COMPILER_ID MATCHES "GNU")
132         # Some versions of gcc-4.8 and gcc-4.9 produce errors (in particular on OS X)
133         # if we do not use -D__STRICT_ANSI__. It is harmless, so we might as well add it for all versions.
134         list(APPEND CUDA_HOST_COMPILER_OPTIONS "-D__STRICT_ANSI__")
135     endif()
136
137     set(CUDA_HOST_COMPILER_OPTIONS "${CUDA_HOST_COMPILER_OPTIONS}"
138         CACHE STRING "Options for nvcc host compiler (do not edit!).")
139
140     mark_as_advanced(CUDA_HOST_COMPILER CUDA_HOST_COMPILER_OPTIONS)
141 endif()
142
143 # the legacy CUDA kernels have been dropped, warn with CUDA 4.0
144 if (CUDA_VERSION VERSION_EQUAL "4.0")
145     message(WARNING "The legacy GPU kernels optimized for older CUDA compilers, including the detected version 4.0, have been removed. To avoid performance loss, we strongly recommend upgrading to a newer CUDA toolkit.
146     ")
147 endif()
148
149 # If any of these manual override variables for target CUDA GPU architectures
150 # or virtual architecture is set, parse the values and assemble the nvcc
151 # command line for these. Otherwise use our defaults.
152 # Note that the manual override variables require a semicolon separated
153 # architectures codes.
154 if (GMX_CUDA_TARGET_SM OR GMX_CUDA_TARGET_COMPUTE)
155     set(GMX_CUDA_NVCC_GENCODE_FLAGS)
156     set(_target_sm_list ${GMX_CUDA_TARGET_SM})
157     foreach(_target ${_target_sm_list})
158         list(APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_${_target},code=sm_${_target}")
159     endforeach()
160     set(_target_compute_list ${GMX_CUDA_TARGET_COMPUTE})
161     foreach(_target ${_target_compute_list})
162         list(APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_${_target},code=compute_${_target}")
163     endforeach()
164 else()
165     # Set the CUDA GPU architectures to compile for:
166     # - with CUDA  <4.2:        compute capability 2.x supported (compiling for sm_2.1 does not help):
167     #     => compile sm_20 cubin, and compute_20 PTX
168     # - with CUDA  =4.2 <5.0:   CC <=3.0 is supported:
169     #     => compile sm_20, sm_30 cubin, and compute_30 PTX
170     # - with CUDA >=5.0 <6.5:   CC <=3.5 is supported
171     #     => compile sm_20, sm_30, sm_35 cubin, and compute_35 PTX
172     # - with CUDA >=6.5:        CC <=3.7 and 5.0 are supported
173     #     => compile sm_20, sm_30, sm_35, sm_37 sm_50, cubin, and compute_50 PTX
174     #
175     #   Note that CUDA 6.5.19 second patch release supports cc 5.2 too, but
176     #   CUDA_VERSION does not contain patch version and having PTX 5.0 JIT-ed is
177     #   equally fast as compiling with sm_5.2 anyway.
178     list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_20,code=sm_20")
179     if(CUDA_VERSION VERSION_GREATER "4.1990") # >= 4.2
180         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_30,code=sm_30")
181     endif()
182     if(CUDA_VERSION VERSION_GREATER "4.999") # >= 5.0
183         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_35,code=sm_35")
184     endif()
185     if(CUDA_VERSION VERSION_GREATER "6.4999") # >= 6.5
186         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_37,code=sm_37")
187         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_50,code=sm_50")
188     endif()
189
190     if(CUDA_VERSION VERSION_LESS "4.2")
191         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_20,code=compute_20")
192     elseif(CUDA_VERSION VERSION_LESS "5.0")
193         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_30,code=compute_30")
194     elseif(CUDA_VERSION VERSION_LESS "6.5")
195         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_35,code=compute_35")
196     else() # version >= 6.5
197         list (APPEND GMX_CUDA_NVCC_GENCODE_FLAGS "-gencode;arch=compute_50,code=compute_50")
198     endif()
199 endif()
200
201 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)
202 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)
203
204 # assemble the CUDA flags
205 list(APPEND GMX_CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_GENCODE_FLAGS}")
206 list(APPEND GMX_CUDA_NVCC_FLAGS "-use_fast_math")
207
208 # assemble the CUDA host compiler flags
209 # with CMake <2.8.10 the host compiler needs to be set on the nvcc command line
210 if (CMAKE_VERSION VERSION_LESS "2.8.10")
211     list(APPEND GMX_CUDA_NVCC_FLAGS "-ccbin=${CUDA_HOST_COMPILER}")
212 endif()
213 list(APPEND GMX_CUDA_NVCC_FLAGS "${CUDA_HOST_COMPILER_OPTIONS}")
214
215 # finally set the damn flags
216 set(CUDA_NVCC_FLAGS "${GMX_CUDA_NVCC_FLAGS}" CACHE STRING "Compiler flags for nvcc." FORCE)