Add initial support for python bindings
[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, 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 if (NOT DEFINED CUDA_NVCC_FLAGS_SET)
48     set(CUDA_NVCC_FLAGS_SET TRUE CACHE INTERNAL "True if NVCC flags have been set" FORCE)
49
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.
52     #
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.
57     #
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.
60
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))
64             message(WARNING "
65             Will not set the nvcc host compiler because the current C compiler is not
66             compatible with nvcc:
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.
75             ")
76         else()
77             # do not use MPI compiler wrappers, as these are prone to brake nvcc
78             if (GMX_MPI AND
79                 NOT "${${MPI_PREFIX}_FOUND}" AND # FindMPI-based detection
80                 NOT GMX_THREAD_MPI)
81                 message(WARNING "
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.
89             ")
90             else()
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)
94             endif()
95         endif()
96     endif()
97
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};")
105         endif()
106
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;")
117             else()
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;")
120             endif()
121         endif()
122         set(CUDA_HOST_COMPILER_OPTIONS "${CUDA_HOST_COMPILER_OPTIONS}"
123             CACHE STRING "Options for nvcc host compiler (do not edit!)." FORCE)
124
125         mark_as_advanced(CUDA_HOST_COMPILER CUDA_HOST_COMPILER_OPTIONS)
126     endif()
127
128     if(APPLE AND CMAKE_C_COMPILER_ID MATCHES "GNU")
129         # Some versions of gcc-4.8 and gcc-4.9 produce errors (in particular on OS X)
130         # if we do not use -D__STRICT_ANSI__. It is harmless, so we might as well add it for all versions.
131         set(CUDA_HOST_COMPILER_OPTIONS "${CUDA_HOST_COMPILER_OPTIONS}-D__STRICT_ANSI__;")
132     endif()
133
134     # the legacy CUDA kernels have been dropped, warn with CUDA 4.0
135     if (CUDA_VERSION VERSION_EQUAL "4.0")
136         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.
137         ")
138     endif()
139
140     # Set the CUDA GPU architectures to compile for:
141     # - with CUDA >v4.2 compute capability 2.0, 2.1 is, but 3.0 is not supported:
142     #     => compile sm_20, sm_21 cubin, and compute_20 PTX
143     # - with CUDA >=4.2 compute capability <=3.0 is supported:
144     #     => compile sm_20, sm_21, sm_30 cubin, and compute_30 PTX
145     # - with CUDA 5.0 and later compute capability 3.5 is supported
146     #     => compile sm_20, sm_21, sm_30, sm_35 cubin, and compute_35 PTX
147     if(CUDA_VERSION VERSION_LESS "4.2")
148         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")
149     elseif(CUDA_VERSION VERSION_LESS "5.0")
150         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")
151     else()
152         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")
153     endif()
154
155     # finally set the damn flags
156     set(CUDA_NVCC_FLAGS
157         "${_CUDA_ARCH_STR};-use_fast_math;${_HOST_COMPILER_OPTION_STRING}${CUDA_HOST_COMPILER_OPTIONS}"
158         CACHE STRING "Compiler flags for nvcc." FORCE)
159 endif()