2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2012,2013,2014, 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.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 used 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 # the legacy CUDA kernels have been dropped, warn with CUDA 4.0
138 if (CUDA_VERSION VERSION_EQUAL "4.0")
139 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.
143 # Set the CUDA GPU architectures to compile for:
144 # - with CUDA >v4.2 compute capability 2.0, 2.1 is, but 3.0 is not supported:
145 # => compile sm_20, sm_21 cubin, and compute_20 PTX
146 # - with CUDA >=4.2 compute capability <=3.0 is supported:
147 # => compile sm_20, sm_21, sm_30 cubin, and compute_30 PTX
148 # - with CUDA 5.0 and later compute capability 3.5 is supported
149 # => compile sm_20, sm_21, sm_30, sm_35 cubin, and compute_35 PTX
150 if(CUDA_VERSION VERSION_LESS "4.2")
151 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")
152 elseif(CUDA_VERSION VERSION_LESS "5.0")
153 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")
155 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_35,code=sm_35;-gencode;arch=compute_35,code=compute_35")
158 # finally set the damn flags
160 "${_CUDA_ARCH_STR};-use_fast_math;${_HOST_COMPILER_OPTION_STRING}${CUDA_HOST_COMPILER_OPTIONS}"
161 CACHE STRING "Compiler flags for nvcc." FORCE)