Simplified uniform GPU selection in CMake
[alexxy/gromacs.git] / cmake / gmxManageCuda.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5 # Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by
6 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 # and including many others, as listed in the AUTHORS file in the
8 # top-level source directory and at http://www.gromacs.org.
9 #
10 # GROMACS is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public License
12 # as published by the Free Software Foundation; either version 2.1
13 # of the License, or (at your option) any later version.
14 #
15 # GROMACS is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # Lesser General Public License for more details.
19 #
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with GROMACS; if not, see
22 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24 #
25 # If you want to redistribute modifications to GROMACS, please
26 # consider that scientific software is very special. Version
27 # control is crucial - bugs must be traceable. We will be happy to
28 # consider code for inclusion in the official distribution, but
29 # derived work must not be called official GROMACS. Details are found
30 # in the README & COPYING files - if they are missing, get the
31 # official version at http://www.gromacs.org.
32 #
33 # To help us fund GROMACS development, we humbly ask that you cite
34 # the research papers on the package. Check out http://www.gromacs.org.
35
36
37 # The earliest version of the CUDA toolkit that supports c++14 is 9.0
38 set(REQUIRED_CUDA_VERSION 9.0)
39 set(REQUIRED_CUDA_COMPUTE_CAPABILITY 3.0)
40
41 set(GMX_USE_CUDA ON)
42
43 option(GMX_CLANG_CUDA "Use clang for CUDA" OFF)
44
45 if(GMX_DOUBLE)
46     message(FATAL_ERROR "CUDA acceleration is not available in double precision")
47 endif()
48
49 find_package(CUDA ${REQUIRED_CUDA_VERSION} REQUIRED)
50
51 # Try to execute ${CUDA_NVCC_EXECUTABLE} --version and set the output
52 # (or an error string) in the argument variable.
53 # Note that semicolon is used as separator for nvcc.
54 #
55 # Parameters:
56 #   COMPILER_INFO         - [output variable] string with compiler path, ID and
57 #                           some compiler-provided information
58 #   DEVICE_COMPILER_FLAGS - [output variable] device flags for the compiler
59 #   HOST_COMPILER_FLAGS   - [output variable] host flags for the compiler, if propagated
60 #
61 macro(get_cuda_compiler_info COMPILER_INFO DEVICE_COMPILER_FLAGS HOST_COMPILER_FLAGS)
62     if(NOT GMX_CLANG_CUDA)
63         if(CUDA_NVCC_EXECUTABLE)
64
65             # Get the nvcc version string. This is multi-line, but since it is only 4 lines
66             # and might change in the future it is better to store than trying to parse out
67             # the version from the current format.
68             execute_process(COMMAND ${CUDA_NVCC_EXECUTABLE} --version
69                 RESULT_VARIABLE _nvcc_version_res
70                 OUTPUT_VARIABLE _nvcc_version_out
71                 ERROR_VARIABLE  _nvcc_version_err
72                 OUTPUT_STRIP_TRAILING_WHITESPACE)
73             if (${_nvcc_version_res} EQUAL 0)
74                 # Fix multi-line mess: Replace newline with ";" so we can use it in a define
75                 string(REPLACE "\n" ";" _nvcc_info_singleline ${_nvcc_version_out})
76                 SET(${COMPILER_INFO} "${CUDA_NVCC_EXECUTABLE} ${_nvcc_info_singleline}")
77                 string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
78                 SET(_compiler_flags "${CUDA_NVCC_FLAGS_${_build_type}}")
79                 if(CUDA_PROPAGATE_HOST_FLAGS)
80                     set(${HOST_COMPILER_FLAGS} BUILD_CXXFLAGS)
81                 else()
82                     set(${HOST_COMPILER_FLAGS} "")
83                 endif()
84                 SET(${DEVICE_COMPILER_FLAGS} "${CUDA_NVCC_FLAGS}${CUDA_NVCC_FLAGS_${_build_type}}")
85             else()
86                 SET(${COMPILER_INFO} "N/A")
87                 SET(${COMPILER_FLAGS} "N/A")
88             endif()
89         endif()
90     else()
91         # CXX compiler is the CUDA compiler
92         set(${COMPILER_INFO} "${CMAKE_CXX_COMPILER}  ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
93         # there are some extra flags
94         set(${COMPILER_FLAGS} "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${_build_type}} ${GMX_CUDA_CLANG_FLAGS}")
95     endif()
96 endmacro ()
97
98 if(GMX_CLANG_CUDA)
99     include(gmxManageClangCudaConfig)
100     list(APPEND GMX_EXTRA_LIBRARIES ${GMX_CUDA_CLANG_LINK_LIBS})
101     link_directories("${GMX_CUDA_CLANG_LINK_DIRS}")
102 else()
103     # Using NVIDIA compiler
104     if(NOT CUDA_NVCC_EXECUTABLE)
105         message(FATAL_ERROR "nvcc is required for a CUDA build, please set CUDA_TOOLKIT_ROOT_DIR appropriately")
106     endif()
107     # set up nvcc options
108     include(gmxManageNvccConfig)
109 endif()
110
111 option(GMX_CUDA_NB_SINGLE_COMPILATION_UNIT "Whether to compile the CUDA non-bonded module using a single compilation unit." OFF)
112 mark_as_advanced(GMX_CUDA_NB_SINGLE_COMPILATION_UNIT)