Remove cycle suppression
[gromacs.git] / cmake / gmxManageGPU.cmake
blob66156c227dadcf488476cefe7965528cc4456c50
2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2012,2013,2014,2015,2016,2017,2018, 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.
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.
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.
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.
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.
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.
35 # If the user did not set GMX_GPU we'll consider this option to be
36 # in "auto" mode meaning that we will:
37 # - search for CUDA and set GMX_GPU=ON we find it
38 # - check whether GPUs are present
39 # - if CUDA is not found but GPUs were detected issue a warning
40 if (NOT DEFINED GMX_GPU)
41     set(GMX_GPU_AUTO TRUE CACHE INTERNAL "GPU acceleration will be selected automatically")
42 else()
43     set(GMX_GPU_AUTO FALSE CACHE INTERNAL "GPU acceleration will be selected automatically")
44 endif()
45 option(GMX_GPU "Enable GPU acceleration" OFF)
47 option(GMX_CLANG_CUDA "Use clang for CUDA" OFF)
48 if (GMX_CLANG_CUDA)
49     # CUDA 7.0 or later required, override req. version
50     set(REQUIRED_CUDA_VERSION 7.0)
51 endif()
53 if(GMX_GPU AND GMX_DOUBLE)
54     message(FATAL_ERROR "GPU acceleration is not available in double precision!")
55 endif()
56 if(GMX_GPU_AUTO AND GMX_DOUBLE)
57     message(WARNING "GPU acceleration is not available in double precision, disabled!")
58     set_property(CACHE GMX_GPU PROPERTY VALUE OFF)
59     set_property(CACHE GMX_GPU_AUTO PROPERTY VALUE OFF)
60 endif()
62 # detect GPUs in the build host machine
63 if ((GMX_GPU OR GMX_GPU_AUTO) AND NOT GMX_GPU_DETECTION_DONE)
64     include(gmxDetectGpu)
65     gmx_detect_gpu()
66 endif()
68 # We need to call find_package even when we've already done the detection/setup
69 if(GMX_GPU OR GMX_GPU_AUTO)
70     if(NOT GMX_GPU AND NOT GMX_DETECT_GPU_AVAILABLE)
71         # Stay quiet when detection has occured and found no GPU.
72         # Noise is acceptable when there is a GPU or the user required one.
73         set(FIND_CUDA_QUIETLY QUIET)
74     endif()
76     # Cmake tries to use the static cuda runtime by default,
77     # but this leads to unusable GPU builds on OS X.
78     if(APPLE)
79         set(CUDA_USE_STATIC_CUDA_RUNTIME OFF CACHE STRING "Use the static version of the CUDA runtime library if available")
80     endif()
82     find_package(CUDA ${REQUIRED_CUDA_VERSION} ${FIND_CUDA_QUIETLY})
83 endif()
85 # Depending on the current vale of GMX_GPU and GMX_GPU_AUTO:
86 # - OFF, FALSE: Will skip this detection/setup.
87 # - OFF, TRUE : Will keep GMX_GPU=OFF if no CUDA is detected, but will assemble
88 #               a warning message which will be issued at the end of the
89 #               configuration if GPU(s) were found in the build system.
90 # - ON , FALSE: The user requested GPU build and this requires CUDA, so we will
91 #               fail if it is not available.
92 # - ON , TRUE : Can't happen (GMX_GPU=ON can only be user-set at this point)
93 if((GMX_GPU OR GMX_GPU_AUTO) AND NOT GMX_GPU_DETECTION_DONE)
94     if (EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
95         set(CUDA_FOUND TRUE CACHE INTERNAL "Whether the CUDA toolkit was found" FORCE)
96     else()
97         set(CUDA_FOUND FALSE CACHE INTERNAL "Whether the CUDA toolkit was found" FORCE)
98     endif()
100     # assemble warning/error message
101     if (GMX_DETECT_GPU_AVAILABLE)
102         set(_msg "${GMX_DETECT_GPU_COUNT} NVIDIA GPU(s) found in the system")
104         # append GPU names
105         if (NOT GMX_DETECT_GPU_INFO STREQUAL "")
106             set(_msg "${_msg}:")
107             foreach(gpu ${GMX_DETECT_GPU_INFO})
108                 set(_msg "${_msg}
109 ${gpu}")
110             endforeach()
111         endif()
113         # TODO remove the second part of the message when we'll have compute
114         # capability information from the detection.
115         set(_msg "${_msg}
116 Compute capability information not available, consult the NVIDIA website:
117 https://developer.nvidia.com/cuda-gpus")
118     endif()
120         set(CUDA_NOTFOUND_MESSAGE "mdrun supports native GPU acceleration on NVIDIA hardware with compute capability >= ${REQUIRED_CUDA_COMPUTE_CAPABILITY} (Fermi or later). This requires the NVIDIA CUDA toolkit, which was not found. Its location can be hinted by setting the CUDA_TOOLKIT_ROOT_DIR CMake option (does not work as an environment variable). The typical location would be /usr/local/cuda[-version]. Note that CPU or GPU acceleration can be selected at runtime.
122 ${_msg}")
123         unset(_msg)
125     if (NOT CUDA_FOUND)
126         if (GMX_GPU_AUTO)
127             # Disable GPU acceleration in auto mode
128             message(STATUS "No compatible CUDA toolkit found (v5.0+), disabling native GPU acceleration")
129             set_property(CACHE GMX_GPU PROPERTY VALUE OFF)
130             set(CUDA_NOTFOUND_AUTO ON)
131         else()
132             # the user requested CUDA, but it wasn't found
133             message(FATAL_ERROR "${CUDA_NOTFOUND_MESSAGE}")
134         endif()
135     else()
136         if (GMX_GPU_AUTO)
137             message(STATUS "Enabling native GPU acceleration")
138             set_property(CACHE GMX_GPU PROPERTY VALUE ON)
139         endif()
140     endif() # NOT CUDA_FOUND
141 endif()
143 # Try to find NVML if a GPU accelerated binary should be build.
144 if (GMX_GPU)
145     if (DEFINED NVML_LIBRARY)
146         set(NVML_FIND_QUIETLY TRUE)
147     endif()
148     find_package(NVML)
149     # TODO Default to off, since linking is not implemented reliably
150     option(GMX_USE_NVML "Use NVML support for better CUDA performance" OFF)
151     mark_as_advanced(GMX_USE_NVML)
152     if(GMX_USE_NVML)
153         if(NVML_FOUND)
154             include_directories(SYSTEM ${NVML_INCLUDE_DIR})
155             set(HAVE_NVML 1)
156             list(APPEND GMX_EXTRA_LIBRARIES ${NVML_LIBRARY})
157         else()
158             message(FATAL_ERROR "NVML support was required, but was not detected. Please consult the install guide.")
159         endif()
160     endif()
161 endif()
163 # Annoyingly enough, FindCUDA leaves a few variables behind as non-advanced.
164 # We need to mark these advanced outside the conditional, otherwise, if the
165 # user turns GMX_GPU=OFF after a failed cmake pass, these variables will be
166 # left behind in the cache.
167 mark_as_advanced(CUDA_SDK_ROOT_DIR
168                  CUDA_USE_STATIC_CUDA_RUNTIME
169                  CUDA_dl_LIBRARY CUDA_rt_LIBRARY
170                  )
171 if(NOT GMX_GPU)
172     mark_as_advanced(CUDA_TOOLKIT_ROOT_DIR)
173     mark_as_advanced(CUDA_HOST_COMPILER)
174 endif()
176 # Try to execute ${CUDA_NVCC_EXECUTABLE} --version and set the output
177 # (or an error string) in the argument variable.
178 # Note that semicolon is used as separator for nvcc.
180 # Parameters:
181 #   COMPILER_INFO   - [output variable] string with compiler path, ID and
182 #                     some compiler-provided information
183 #   COMPILER_FLAGS  - [output variable] flags for the compiler
185 macro(get_cuda_compiler_info COMPILER_INFO COMPILER_FLAGS)
186     if(NOT GMX_CLANG_CUDA)
187         if(CUDA_NVCC_EXECUTABLE)
189             # Get the nvcc version string. This is multi-line, but since it is only 4 lines
190             # and might change in the future it is better to store than trying to parse out
191             # the version from the current format.
192             execute_process(COMMAND ${CUDA_NVCC_EXECUTABLE} --version
193                 RESULT_VARIABLE _nvcc_version_res
194                 OUTPUT_VARIABLE _nvcc_version_out
195                 ERROR_VARIABLE  _nvcc_version_err
196                 OUTPUT_STRIP_TRAILING_WHITESPACE)
197             if (${_nvcc_version_res} EQUAL 0)
198                 # Fix multi-line mess: Replace newline with ";" so we can use it in a define
199                 string(REPLACE "\n" ";" _nvcc_info_singleline ${_nvcc_version_out})
200                 SET(${COMPILER_INFO} "${CUDA_NVCC_EXECUTABLE} ${_nvcc_info_singleline}")
201                 string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
202                 SET(_compiler_flags "${CUDA_NVCC_FLAGS_${_build_type}}")
203                 if(CUDA_PROPAGATE_HOST_FLAGS)
204                     string(REGEX REPLACE "[ ]+" ";" _cxx_flags_nospace "${BUILD_CXXFLAGS}")
205                 endif()
206                 SET(${COMPILER_FLAGS} "${CUDA_NVCC_FLAGS}${CUDA_NVCC_FLAGS_${_build_type}}; ${_cxx_flags_nospace}")
207             else()
208                 SET(${COMPILER_INFO} "N/A")
209                 SET(${COMPILER_FLAGS} "N/A")
210             endif()
211         endif()
212     else()
213         # CXX compiler is the CUDA compiler
214         set(${COMPILER_INFO} "${CMAKE_CXX_COMPILER}  ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
215         # there are some extra flags
216         set(${COMPILER_FLAGS} "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${_build_type}} ${GMX_CUDA_CLANG_FLAGS}")
217     endif()
218 endmacro ()
220 macro(enable_multiple_cuda_compilation_units)
221     message(STATUS "Enabling multiple compilation units for the CUDA non-bonded module.")
222     set_property(CACHE GMX_CUDA_NB_SINGLE_COMPILATION_UNIT PROPERTY VALUE OFF)
223 endmacro()
225 include(CMakeDependentOption)
226 include(gmxOptionUtilities)
227 macro(gmx_gpu_setup)
228     if(GMX_GPU)
229         if(NOT GMX_CLANG_CUDA)
230             if(NOT CUDA_NVCC_EXECUTABLE)
231                 message(FATAL_ERROR "nvcc is required for a CUDA build, please set CUDA_TOOLKIT_ROOT_DIR appropriately")
232             endif()
233             # set up nvcc options
234             include(gmxManageNvccConfig)
235         else()
236             include(gmxManageClangCudaConfig)
237         endif()
239         gmx_check_if_changed(_cuda_version_changed CUDA_VERSION)
241         # Generate CUDA RT API version string which will end up in config.h
242         # We do this because nvcc is silly enough to not define its own version
243         # (which should match the CUDA runtime API version AFAICT) and we want to
244         # avoid creating the fragile dependency on cuda_runtime_api.h.
245         #
246         # NOTE: CUDA v7.5 is expected to have nvcc define it own version, so in the
247         # future we should switch to using that version string instead of our own.
248         if (NOT GMX_CUDA_VERSION OR _cuda_version_changed)
249             MATH(EXPR GMX_CUDA_VERSION "${CUDA_VERSION_MAJOR}*1000 + ${CUDA_VERSION_MINOR}*10")
250         endif()
252         if (_cuda_version_changed)
253             # check the generated CUDA API version against the one present in cuda_runtime_api.h
254             try_compile(_get_cuda_version_compile_res
255                 ${CMAKE_BINARY_DIR}
256                 ${CMAKE_SOURCE_DIR}/cmake/TestCUDAVersion.c
257                 COMPILE_DEFINITIONS "-DGMX_CUDA_VERSION=${GMX_CUDA_VERSION}"
258                 CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${CUDA_TOOLKIT_INCLUDE}"
259                 OUTPUT_VARIABLE _get_cuda_version_compile_out)
261             if (NOT _get_cuda_version_compile_res)
262                 if (_get_cuda_version_compile_out MATCHES "CUDA version mismatch")
263                     message(FATAL_ERROR "The CUDA API version generated internally from the compiler version does not match the version reported by cuda.h. This means either that the CUDA detection picked up mismatching nvcc and the CUDA headers (likely not part of the same toolkit installation) or that there is an error in the internal version generation. If you are sure that it is not the former causing the error (check the relevant cache variables), define the GMX_CUDA_VERSION cache variable to work around the error.")
264                 else()
265                     message(FATAL_ERROR "Could not detect CUDA runtime API version")
266                 endif()
267             endif()
268         endif()
270         # no OpenMP is no good!
271         if(NOT GMX_OPENMP)
272             message(WARNING "To use GPU acceleration efficiently, mdrun requires OpenMP multi-threading. Without OpenMP a single CPU core can be used with a GPU which is not optimal. Note that with MPI multiple processes can be forced to use a single GPU, but this is typically inefficient. You need to set both C and C++ compilers that support OpenMP (CC and CXX environment variables, respectively) when using GPUs.")
273         endif()
275         if(NOT GMX_CLANG_CUDA)
276             gmx_check_if_changed(GMX_CHECK_NVCC CUDA_NVCC_EXECUTABLE CUDA_HOST_COMPILER CUDA_NVCC_FLAGS)
278             if(GMX_CHECK_NVCC OR NOT GMX_NVCC_WORKS)
279                 message(STATUS "Check for working NVCC/C compiler combination")
280                 execute_process(COMMAND ${CUDA_NVCC_EXECUTABLE} -ccbin ${CUDA_HOST_COMPILER} -c ${CUDA_NVCC_FLAGS} ${CMAKE_SOURCE_DIR}/cmake/TestCUDA.cu
281                                 RESULT_VARIABLE _cuda_test_res
282                                 OUTPUT_VARIABLE _cuda_test_out
283                                 ERROR_VARIABLE  _cuda_test_err
284                                 OUTPUT_STRIP_TRAILING_WHITESPACE)
286                 if(${_cuda_test_res})
287                     message(STATUS "Check for working NVCC/C compiler combination - broken")
288                     if(${_cuda_test_err} MATCHES "nsupported")
289                         message(FATAL_ERROR "NVCC/C compiler combination does not seem to be supported. CUDA frequently does not support the latest versions of the host compiler, so you might want to try an earlier C/C++ compiler version and make sure your CUDA compiler and driver are as recent as possible.")
290                     else()
291                         message(FATAL_ERROR "CUDA compiler does not seem to be functional.")
292                     endif()
293                 elseif(NOT GMX_CUDA_TEST_COMPILER_QUIETLY)
294                     message(STATUS "Check for working NVCC/C compiler combination - works")
295                     set(GMX_NVCC_WORKS TRUE CACHE INTERNAL "Nvcc can compile a trivial test program")
296                 endif()
297             endif() # GMX_CHECK_NVCC
298         endif() #GMX_CLANG_CUDA
299     endif() # GMX_GPU
301     option(GMX_CUDA_NB_SINGLE_COMPILATION_UNIT "Whether to compile the CUDA non-bonded module using a single compilation unit." OFF)
302     mark_as_advanced(GMX_CUDA_NB_SINGLE_COMPILATION_UNIT)
304 endmacro()