f03a64b7a35bc66e66d4fd3443e8f0e8782c2ee3
[alexxy/gromacs.git] / cmake / gmxManageNvccConfig.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
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.
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_NVCC_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_NVCC_HOST_COMPILER       - the compiler nvcc is forced to use (via -ccbin)
42 #   * CUDA_NVCC_HOST_COMPILER_OPTIONS   - the full host-compiler related option list passed to nvcc
43 if (NOT DEFINED CUDA_NVCC_FLAGS_SET)
44     set(CUDA_NVCC_FLAGS_SET TRUE CACHE INTERNAL "True if NVCC flags have been set" FORCE)
45
46     # Explicitly set the host compiler for nvcc if the current compiler is
47     # supported and it's not an MPI compiler wrapper, otherwise warn the user.
48     #
49     # Note that even though nvcc compiles host code as C++, we use the
50     # CMAKE_C_COMPILER as host compiler. We do this because CUDA versions
51     # preceding 5.0 only recognize icc, but not icpc. However, both gcc and icc
52     # (i.e. all supported compilers) happily compile C++ code.
53     #
54     # Also note that with MSVC nvcc sets the -compiler-bindir option behind the
55     # scenes; to avoid conflicts we don't set -ccbin automatically.
56
57     if (NOT DEFINED CUDA_NVCC_HOST_COMPILER AND NOT MSVC)
58         if (NOT CMAKE_COMPILER_IS_GNUCC AND
59             NOT (CMAKE_C_COMPILER_ID MATCHES "Intel" AND UNIX AND NOT APPLE))
60             message(WARNING "
61             Will not set the nvcc host compiler because the current C compiler is not
62             compatible with nvcc:
63             ${CMAKE_C_COMPILER} (ID: ${CMAKE_C_COMPILER_ID})
64             Compatible compilers are: gcc on Linux and Mac OS X, the Intel Compiler on
65             64-bit Linux and MSVC on Windows. If nothing specified, nvcc will automatically
66             pick the platform-default compiler; However, as mixing compilers can cause errors.
67             To manually set the nvcc host compiler, edit CUDA_NVCC_FLAGS or re-configure
68             setting CUDA_NVCC_HOST_COMPILER to the full path of a compatible compiler.
69             ")
70         else()
71             # do not use MPI compiler wrappers, as these are prone to brake nvcc
72             if (GMX_MPI AND
73                 NOT "${${MPI_PREFIX}_FOUND}" AND # FindMPI-based detection
74                 NOT GMX_THREAD_MPI)
75                 message(WARNING "
76             Will not set the nvcc host compiler because the current C compiler is an MPI
77             compiler wrapper: ${CMAKE_C_COMPILER}
78             MPI compiler wrappers are prone to not work with nvcc. You might get lucky,
79             but the safest is to use the C compiler that the MPI compiler wrapper uses
80             (if this is compatible).
81             To manually set the nvcc host compiler, edit CUDA_NVCC_FLAGS or re-configure
82             setting CUDA_NVCC_HOST_COMPILER to the full path of a compatible compiler.
83             ")
84             else()
85                 set(CUDA_NVCC_HOST_COMPILER "${CMAKE_C_COMPILER}")
86                 set(CUDA_NVCC_HOST_COMPILER_AUTOSET TRUE CACHE INTERNAL
87                     "True if CUDA_NVCC_HOST_COMPILER is automatically set" FORCE)
88             endif()
89         endif()
90     endif()
91
92     if(DEFINED CUDA_NVCC_HOST_COMPILER)
93         message(STATUS "Setting the nvcc host compiler to: ${CUDA_NVCC_HOST_COMPILER}")
94         set(CUDA_NVCC_HOST_COMPILER ${CUDA_NVCC_HOST_COMPILER}
95             CACHE PATH "Host compiler for nvcc (do not edit!)" FORCE)
96
97         set(CUDA_NVCC_HOST_COMPILER_OPTIONS "-ccbin=${CUDA_NVCC_HOST_COMPILER}")
98         # On *nix force icc in gcc 4.4 compatibility with CUDA 3.2/4.0 and
99         # gcc 4.5 compatibility mode with later CUDA versions. This is needed
100         # as even with icc use as host compiler, when icc's gcc compatibility
101         # mode is higher than the max gcc version officially supported by CUDA,
102         # nvcc will freak out.
103         if (UNIX AND CMAKE_C_COMPILER_ID MATCHES "Intel" AND
104             CUDA_NVCC_HOST_COMPILER_AUTOSET)
105             if (CUDA_VERSION VERSION_LESS "4.1")
106                 message(STATUS "Setting Intel Compiler compatibity mode to gcc 4.4 for nvcc host compilation")
107                 set(CUDA_NVCC_HOST_COMPILER_OPTIONS "${CUDA_NVCC_HOST_COMPILER_OPTIONS};-Xcompiler;-gcc-version=440;")
108             else()
109                 message(STATUS "Setting Intel Compiler compatibity mode to gcc 4.5 for nvcc host compilation")
110                 set(CUDA_NVCC_HOST_COMPILER_OPTIONS "${CUDA_NVCC_HOST_COMPILER_OPTIONS};-Xcompiler;-gcc-version=450;")
111             endif()
112         endif()
113         set(CUDA_NVCC_HOST_COMPILER_OPTIONS "${CUDA_NVCC_HOST_COMPILER_OPTIONS}"
114             CACHE STRING "Host-side compiler and options for it (do not edit!)." FORCE)
115
116         mark_as_advanced(CUDA_NVCC_HOST_COMPILER CUDA_NVCC_HOST_COMPILER_OPTIONS)
117     endif()
118
119     # on Linux we need to add -fPIC when building shared gmx libs
120     # Note: will add -fPIC for any compiler that supports it as it shouldn't hurt
121     if(BUILD_SHARED_LIBS)
122         GMX_TEST_CXXFLAG(CXXFLAG_FPIC "-fPIC" _FPIC_NVCC_FLAG)
123         if(_FPIC_NVCC_FLAG)
124             set(_FPIC_NVCC_FLAG "-Xcompiler;${_FPIC_NVCC_FLAG}")
125         endif()
126     endif()
127
128     # Set the CUDA GPU architectures to compile for:
129     # - with CUDA >v4.2 compute capability 2.0, 2.1 is, but 3.0 is not supported:
130     #     => compile sm_20, sm_21 cubin, and compute_20 PTX
131     # - with CUDA >=4.2 compute capabity <=3.0 is supported:
132     #     => compile sm_20, sm_21, sm_30 cubin, and compute_30 PTX
133     # - with CUDA 5.0 compute capabity 3.5 is supported, but generating code
134     #   optimized for sm_35 results in lower performance than with sm_30.
135     if(CUDA_VERSION VERSION_LESS "4.2")
136         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")
137     else()
138         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")
139     endif()
140
141     # finally set the damn flags
142     set(CUDA_NVCC_FLAGS
143         "${_CUDA_ARCH_STR};-use_fast_math;${CUDA_NVCC_HOST_COMPILER_OPTIONS};${_FPIC_NVCC_FLAG}"
144         CACHE STRING "Compiler flags for nvcc." FORCE)
145 endif()
146
147
148 # Try to execute ${CUDA_NVCC_EXECUTABLE} --version and set the output
149 # (or an error string) in the argument variable.
150 #
151 # returned in argument: CUDA nvcc compiler version string
152 #
153 macro(get_nvcc_version_info)
154     if(CUDA_NVCC_EXECUTABLE AND NOT CUDA_NVCC_COMPILER_INFO)
155
156         # Get the nvcc version string. This is multi-line, but since it is only 4 lines
157         # and might change in the future it is better to store than trying to parse out
158         # the version from the current format.
159         execute_process(COMMAND ${CUDA_NVCC_EXECUTABLE} --version
160             RESULT_VARIABLE _nvcc_version_res
161             OUTPUT_VARIABLE _nvcc_version_out
162             ERROR_VARIABLE  _nvcc_version_err
163             OUTPUT_STRIP_TRAILING_WHITESPACE)
164         if (${_nvcc_version_res} EQUAL 0)
165             # Fix multi-line mess: Replace newline with ";" so we can use it in a define
166             string(REPLACE "\n" ";" _nvcc_info_singleline ${_nvcc_version_out})
167             SET(CUDA_NVCC_COMPILER_INFO ${_nvcc_info_singleline}
168                 CACHE STRING "CUDA nvcc compiler version string" FORCE)
169         else ()
170             SET(CUDA_NVCC_COMPILER_INFO ""
171                 CACHE STRING "CUDA nvcc compiler version string not available" FORCE)
172         endif ()
173     endif ()
174     mark_as_advanced(CUDA_NVCC_COMPILER_INFO)
175 endmacro ()