2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2012, by the GROMACS development team, led by
5 # David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 # others, as listed in the AUTHORS file in the top-level source
7 # 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.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
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 if (NOT DEFINED CUDA_NVCC_FLAGS_SET)
48 set(CUDA_NVCC_FLAGS_SET TRUE CACHE INTERNAL "True if NVCC flags have been set" FORCE)
50 # Explicitly set the host compiler for nvcc if the current compiler is
51 # supported and it's not an MPI compiler wrapper, otherwise warn the user.
53 # Note that even though nvcc compiles host code as C++, we use the
54 # CMAKE_C_COMPILER as host compiler. We do this because CUDA versions
55 # preceding 5.0 only recognize icc, but not icpc. However, both gcc and icc
56 # (i.e. all supported compilers) happily compile C++ code.
58 # Also note that with MSVC nvcc sets the -compiler-bindir option behind the
59 # scenes; to avoid conflicts we don't set -ccbin automatically.
61 if (NOT DEFINED CUDA_HOST_COMPILER AND NOT MSVC)
62 if (NOT CMAKE_COMPILER_IS_GNUCC AND
63 NOT (CMAKE_C_COMPILER_ID MATCHES "Intel" AND UNIX AND NOT APPLE))
65 Will not set the nvcc host compiler because the current C compiler is not
67 ${CMAKE_C_COMPILER} (ID: ${CMAKE_C_COMPILER_ID})
68 Compatible compilers are: gcc on Linux and Mac OS X, the Intel Compiler on 64-bit
69 Linux and MSVC on Windows. Note that with newer CUDA releases this might change,
70 for up-to-date compatibility information check the NVIDIA documentation.
71 If nothing specified, nvcc will automatically pick the platform-default compiler;
72 Note that mixing compilers can cause errors.
73 To manually set the nvcc host compiler, edit CUDA_NVCC_FLAGS or re-configure
74 setting CUDA_HOST_COMPILER to the full path of a compatible compiler.
77 # do not use MPI compiler wrappers, as these are prone to brake nvcc
79 NOT "${${MPI_PREFIX}_FOUND}" AND # FindMPI-based detection
82 Will not set the nvcc host compiler because the current C compiler is an MPI
83 compiler wrapper: ${CMAKE_C_COMPILER}
84 MPI compiler wrappers are prone to not work with nvcc. You might get lucky,
85 but the safest is to use the C compiler that the MPI compiler wrapper uses
86 (if this is compatible).
87 To manually set the nvcc host compiler, edit CUDA_NVCC_FLAGS or re-configure
88 setting CUDA_HOST_COMPILER to the full path of a compatible compiler.
91 set(CUDA_HOST_COMPILER "${CMAKE_C_COMPILER}")
92 set(CUDA_HOST_COMPILER_AUTOSET TRUE CACHE INTERNAL
93 "True if CUDA_HOST_COMPILER is automatically set" FORCE)
98 if(DEFINED CUDA_HOST_COMPILER)
99 # FindCUDA in CMake 2.8.10 sets the host compiler internally
100 if (CMAKE_VERSION VERSION_LESS "2.8.10")
101 message(STATUS "Setting the nvcc host compiler to: ${CUDA_HOST_COMPILER}")
102 set(CUDA_HOST_COMPILER ${CUDA_HOST_COMPILER}
103 CACHE PATH "Host compiler for nvcc (do not edit!)" FORCE)
104 set(_HOST_COMPILER_OPTION_STRING "-ccbin=${CUDA_HOST_COMPILER};")
107 # On *nix force icc in gcc 4.4 compatibility mode with CUDA 3.2/4.0 and
108 # gcc 4.5 compatibility mode with later CUDA versions. This is needed
109 # as even with icc use as host compiler, when icc's gcc compatibility
110 # mode is higher than the max gcc version officially supported by CUDA,
111 # nvcc will freak out.
112 if (UNIX AND CMAKE_C_COMPILER_ID MATCHES "Intel" AND
113 CUDA_HOST_COMPILER_AUTOSET)
114 if (CUDA_VERSION VERSION_LESS "4.1")
115 message(STATUS "Setting Intel Compiler compatibity mode to gcc 4.4 for nvcc host compilation")
116 set(CUDA_HOST_COMPILER_OPTIONS "${CUDA_HOST_COMPILER_OPTIONS};-Xcompiler;-gcc-version=440;")
118 message(STATUS "Setting Intel Compiler compatibity mode to gcc 4.5 for nvcc host compilation")
119 set(CUDA_HOST_COMPILER_OPTIONS "${CUDA_HOST_COMPILER_OPTIONS};-Xcompiler;-gcc-version=450;")
122 set(CUDA_HOST_COMPILER_OPTIONS "${CUDA_HOST_COMPILER_OPTIONS}"
123 CACHE STRING "Options for nvcc host compiler (do not edit!)." FORCE)
125 mark_as_advanced(CUDA_HOST_COMPILER CUDA_HOST_COMPILER_OPTIONS)
128 # on Linux we need to add -fPIC when building shared gmx libs
129 # Note: will add -fPIC for any compiler that supports it as it shouldn't hurt
130 if(BUILD_SHARED_LIBS)
131 GMX_TEST_CXXFLAG(CXXFLAG_FPIC "-fPIC" _FPIC_NVCC_FLAG)
133 set(CUDA_HOST_COMPILER_OPTIONS "${CUDA_HOST_COMPILER_OPTIONS}-Xcompiler;${_FPIC_NVCC_FLAG}")
137 # Set the CUDA GPU architectures to compile for:
138 # - with CUDA >v4.2 compute capability 2.0, 2.1 is, but 3.0 is not supported:
139 # => compile sm_20, sm_21 cubin, and compute_20 PTX
140 # - with CUDA >=4.2 compute capabity <=3.0 is supported:
141 # => compile sm_20, sm_21, sm_30 cubin, and compute_30 PTX
142 # - with CUDA 5.0 compute capabity 3.5 is supported, but generating code
143 # optimized for sm_35 results in lower performance than with sm_30.
144 if(CUDA_VERSION VERSION_LESS "4.2")
145 set(_CUDA_ARCH_STR "-gencode;arch=compute_20,code=sm_20;-gencode;arch=compute_20,code=sm_21;-gencode;arch=compute_20,code=compute_20")
147 set(_CUDA_ARCH_STR "-gencode;arch=compute_20,code=sm_20;-gencode;arch=compute_20,code=sm_21;-gencode;arch=compute_30,code=sm_30;-gencode;arch=compute_30,code=compute_30")
150 # finally set the damn flags
152 "${_CUDA_ARCH_STR};-use_fast_math;${_HOST_COMPILER_OPTION_STRING}${CUDA_HOST_COMPILER_OPTIONS}"
153 CACHE STRING "Compiler flags for nvcc." FORCE)
157 # Try to execute ${CUDA_NVCC_EXECUTABLE} --version and set the output
158 # (or an error string) in the argument variable.
160 # returned in argument: CUDA nvcc compiler version string
162 macro(get_nvcc_version_info)
163 if(CUDA_NVCC_EXECUTABLE AND NOT CUDA_NVCC_COMPILER_INFO)
165 # Get the nvcc version string. This is multi-line, but since it is only 4 lines
166 # and might change in the future it is better to store than trying to parse out
167 # the version from the current format.
168 execute_process(COMMAND ${CUDA_NVCC_EXECUTABLE} --version
169 RESULT_VARIABLE _nvcc_version_res
170 OUTPUT_VARIABLE _nvcc_version_out
171 ERROR_VARIABLE _nvcc_version_err
172 OUTPUT_STRIP_TRAILING_WHITESPACE)
173 if (${_nvcc_version_res} EQUAL 0)
174 # Fix multi-line mess: Replace newline with ";" so we can use it in a define
175 string(REPLACE "\n" ";" _nvcc_info_singleline ${_nvcc_version_out})
176 SET(CUDA_NVCC_COMPILER_INFO ${_nvcc_info_singleline}
177 CACHE STRING "CUDA nvcc compiler version string" FORCE)
179 SET(CUDA_NVCC_COMPILER_INFO ""
180 CACHE STRING "CUDA nvcc compiler version string not available" FORCE)
183 mark_as_advanced(CUDA_NVCC_COMPILER_INFO)