Use CMake to propagate versions and hashes to gitlab jobs
[alexxy/gromacs.git] / cmake / gmxManageClangCudaConfig.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2017,2018,2019,2020,2021, 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 function (gmx_test_clang_cuda_support)
36
37     if ((NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") OR
38         (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6))
39         message(FATAL_ERROR "clang 6 or later required with GMX_CLANG_CUDA=ON!")
40     endif()
41
42     # NOTE: we'd ideally like to use a compile check here, but the link-stage
43     # fails as the clang invocation generated seems to not handle well some
44     # (GPU code) in the object file generated during compilation.
45     # SET(CMAKE_REQUIRED_FLAGS ${FLAGS})
46     # SET(CMAKE_REQUIRED_LIBRARIES ${LIBS})
47     # CHECK_CXX_SOURCE_COMPILES("int main() { int c; cudaGetDeviceCount(&c); return 0; }" _CLANG_CUDA_COMPILES)
48 endfunction ()
49
50 if (GMX_CUDA_TARGET_COMPUTE)
51     message(WARNING "Values passed in GMX_CUDA_TARGET_COMPUTE will be ignored; clang will by default include PTX in the binary.")
52 endif()
53
54 if (CUDA_VERSION VERSION_GREATER 10.1)
55     # At the time of writing, the latest versions are Clang 11 and CUDA 11.2.
56     if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 11.0)
57         # We don't know about the future Clang versions, but so far Clang 12 docs state that only CUDA versions 7.0-10.1 are supported.
58         set(_support_status "likely incompatible")
59     else()
60         if (CUDA_VERSION VERSION_GREATER 11.2)
61             # No idea about future CUDA versions.
62             set(_support_status "officially incompatible")
63         else()
64             # Our experience and multiple reports on the internet indicate that it works just fine.
65             set(_support_status "officially incompatible (but generally working)")
66         endif()
67     endif()
68     message(NOTICE "Using ${_support_status} version of CUDA with Clang.")
69     message(NOTICE "If Clang fails to recognize CUDA version, consider creating doing "
70       "`echo \"CUDA Version ${CUDA_VERSION}\" | sudo tee \"${CUDA_TOOLKIT_ROOT_DIR}/version.txt\"`")
71     list(APPEND _CUDA_CLANG_FLAGS "-Wno-unknown-cuda-version")
72
73 endif()
74
75 if (GMX_CUDA_TARGET_SM)
76     set(_CUDA_CLANG_GENCODE_FLAGS)
77     set(_target_sm_list ${GMX_CUDA_TARGET_SM})
78     foreach(_target ${_target_sm_list})
79         list(APPEND _CUDA_CLANG_GENCODE_FLAGS "--cuda-gpu-arch=sm_${_target}")
80     endforeach()
81 else()
82     if (CUDA_VERSION VERSION_LESS 11.0)
83         list(APPEND _CUDA_CLANG_GENCODE_FLAGS "--cuda-gpu-arch=sm_30")
84     endif()
85     list(APPEND _CUDA_CLANG_GENCODE_FLAGS "--cuda-gpu-arch=sm_35")
86     # clang 6.0 + CUDA 9.0 seems to have issues generating code for sm_37
87     if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0 OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 6.0.999)
88         list(APPEND _CUDA_CLANG_GENCODE_FLAGS "--cuda-gpu-arch=sm_37")
89     endif()
90     list(APPEND _CUDA_CLANG_GENCODE_FLAGS "--cuda-gpu-arch=sm_50")
91     list(APPEND _CUDA_CLANG_GENCODE_FLAGS "--cuda-gpu-arch=sm_52")
92     list(APPEND _CUDA_CLANG_GENCODE_FLAGS "--cuda-gpu-arch=sm_60")
93     list(APPEND _CUDA_CLANG_GENCODE_FLAGS "--cuda-gpu-arch=sm_61")
94     list(APPEND _CUDA_CLANG_GENCODE_FLAGS "--cuda-gpu-arch=sm_70")
95     if (NOT CUDA_VERSION VERSION_LESS 10.0)
96         list(APPEND _CUDA_CLANG_GENCODE_FLAGS "--cuda-gpu-arch=sm_75")
97     endif()
98     # Enable this when clang (12.0 ?) properly recognizes CUDA 11.0
99     #if(NOT CUDA_VERSION VERSION_LESS 11.0)
100     #    list(APPEND _CUDA_CLANG_GENCODE_FLAGS "--cuda-gpu-arch=sm_80")
101     #endif()
102     # Enable this when clang (12.0 ?) introduces sm_86 support
103     #if(NOT CUDA_VERSION VERSION_LESS 11.1)
104     #    list(APPEND _CUDA_CLANG_GENCODE_FLAGS "--cuda-gpu-arch=sm_86")
105     #endif()
106 endif()
107 if (GMX_CUDA_TARGET_SM)
108     set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY HELPSTRING "List of CUDA GPU architecture codes to compile for (without the sm_ prefix)")
109     set_property(CACHE GMX_CUDA_TARGET_SM PROPERTY TYPE STRING)
110 endif()
111
112 # default flags
113 list(APPEND _CUDA_CLANG_FLAGS "-x cuda" "-ffast-math" "-fcuda-flush-denormals-to-zero")
114 # Workaround for clang>=9 (Bug 45533). No CUDA file uses OpenMP.
115 list(APPEND _CUDA_CLANG_FLAGS "-fno-openmp")
116 # CUDA toolkit
117 list(APPEND _CUDA_CLANG_FLAGS "--cuda-path=${CUDA_TOOLKIT_ROOT_DIR}")
118 # codegen flags
119 list(APPEND _CUDA_CLANG_FLAGS "${_CUDA_CLANG_GENCODE_FLAGS}")
120 foreach(_flag ${_CUDA_CLANG_FLAGS})
121     set(GMX_CUDA_CLANG_FLAGS "${GMX_CUDA_CLANG_FLAGS} ${_flag}")
122 endforeach()
123
124 if (CUDA_USE_STATIC_CUDA_RUNTIME)
125     set(GMX_CUDA_CLANG_LINK_LIBS "cudart_static")
126 else()
127     set(GMX_CUDA_CLANG_LINK_LIBS "cudart")
128 endif()
129 set(GMX_CUDA_CLANG_LINK_LIBS "${GMX_CUDA_CLANG_LINK_LIBS}" "dl" "rt")
130 if (CUDA_64_BIT_DEVICE_CODE)
131     set(GMX_CUDA_CLANG_LINK_DIRS "${CUDA_TOOLKIT_ROOT_DIR}/lib64")
132 else()
133     set(GMX_CUDA_CLANG_LINK_DIRS "${CUDA_TOOLKIT_ROOT_DIR}/lib")
134 endif()
135
136 gmx_test_clang_cuda_support()