Use CMake to propagate versions and hashes to gitlab jobs
[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,2021, 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 # The earliest version of the CUDA toolkit that supports c++17 is 11.0
37 set(REQUIRED_CUDA_VERSION 11.0)
38 set(REQUIRED_CUDA_COMPUTE_CAPABILITY 3.0)
39
40 set(GMX_GPU_CUDA ON)
41
42 option(GMX_CLANG_CUDA "Use clang for CUDA" OFF)
43
44 if(GMX_DOUBLE)
45     message(FATAL_ERROR "CUDA acceleration is not available in double precision")
46 endif()
47
48 find_package(CUDA ${REQUIRED_CUDA_VERSION} REQUIRED)
49
50 # Try to execute ${CUDA_NVCC_EXECUTABLE} --version and set the output
51 # (or an error string) in the argument variable.
52 # Note that semicolon is used as separator for nvcc.
53 #
54 # Parameters:
55 #   COMPILER_INFO         - [output variable] string with compiler path, ID and
56 #                           some compiler-provided information
57 #   DEVICE_COMPILER_FLAGS - [output variable] device flags for the compiler
58 #   HOST_COMPILER_FLAGS   - [output variable] host flags for the compiler, if propagated
59 #
60 macro(get_cuda_compiler_info COMPILER_INFO DEVICE_COMPILER_FLAGS HOST_COMPILER_FLAGS)
61     if(NOT GMX_CLANG_CUDA)
62         if(CUDA_NVCC_EXECUTABLE)
63
64             # Get the nvcc version string. This is multi-line, but since it is only 4 lines
65             # and might change in the future it is better to store than trying to parse out
66             # the version from the current format.
67             execute_process(COMMAND ${CUDA_NVCC_EXECUTABLE} --version
68                 RESULT_VARIABLE _nvcc_version_res
69                 OUTPUT_VARIABLE _nvcc_version_out
70                 ERROR_VARIABLE  _nvcc_version_err
71                 OUTPUT_STRIP_TRAILING_WHITESPACE)
72             if (${_nvcc_version_res} EQUAL 0)
73                 # Fix multi-line mess: Replace newline with ";" so we can use it in a define
74                 string(REPLACE "\n" ";" _nvcc_info_singleline ${_nvcc_version_out})
75                 SET(${COMPILER_INFO} "${CUDA_NVCC_EXECUTABLE} ${_nvcc_info_singleline}")
76                 string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
77                 SET(_compiler_flags "${CUDA_NVCC_FLAGS_${_build_type}}")
78                 if(CUDA_PROPAGATE_HOST_FLAGS)
79                     set(${HOST_COMPILER_FLAGS} BUILD_CXXFLAGS)
80                 else()
81                     set(${HOST_COMPILER_FLAGS} "")
82                 endif()
83                 SET(${DEVICE_COMPILER_FLAGS} "${CUDA_NVCC_FLAGS}${CUDA_NVCC_FLAGS_${_build_type}}")
84             else()
85                 SET(${COMPILER_INFO} "N/A")
86                 SET(${COMPILER_FLAGS} "N/A")
87             endif()
88         endif()
89     else()
90         # CXX compiler is the CUDA compiler
91         set(${COMPILER_INFO} "${CMAKE_CXX_COMPILER}  ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
92         # there are some extra flags
93         set(${COMPILER_FLAGS} "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${_build_type}} ${GMX_CUDA_CLANG_FLAGS}")
94     endif()
95 endmacro ()
96
97 if(GMX_CLANG_CUDA)
98     include(gmxManageClangCudaConfig)
99     list(APPEND GMX_EXTRA_LIBRARIES ${GMX_CUDA_CLANG_LINK_LIBS})
100     link_directories("${GMX_CUDA_CLANG_LINK_DIRS}")
101 else()
102     # Using NVIDIA compiler
103     if(NOT CUDA_NVCC_EXECUTABLE)
104         message(FATAL_ERROR "nvcc is required for a CUDA build, please set CUDA_TOOLKIT_ROOT_DIR appropriately")
105     endif()
106     # set up nvcc options
107     include(gmxManageNvccConfig)
108 endif()
109
110 option(GMX_CUDA_NB_SINGLE_COMPILATION_UNIT "Whether to compile the CUDA non-bonded module using a single compilation unit." OFF)
111 mark_as_advanced(GMX_CUDA_NB_SINGLE_COMPILATION_UNIT)