Fix style errors in modular simulator propagator
[alexxy/gromacs.git] / cmake / gmxManageGPU.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 # If the user did not set GMX_GPU we'll consider this option to be
37 # in "auto" mode meaning that we will:
38 # - search for CUDA and set GMX_GPU=ON we find it
39 # - check whether GPUs are present
40 # - if CUDA is not found but GPUs were detected issue a warning
41 if (NOT DEFINED GMX_GPU)
42     set(GMX_GPU_AUTO TRUE CACHE INTERNAL "GPU acceleration will be selected automatically")
43 else()
44     set(GMX_GPU_AUTO FALSE CACHE INTERNAL "GPU acceleration will be selected automatically")
45 endif()
46 option(GMX_GPU "Enable GPU acceleration" OFF)
47
48 option(GMX_CLANG_CUDA "Use clang for CUDA" OFF)
49
50 if(GMX_GPU AND GMX_DOUBLE)
51     message(FATAL_ERROR "GPU acceleration is not available in double precision!")
52 endif()
53 if(GMX_GPU_AUTO AND GMX_DOUBLE)
54     message(WARNING "GPU acceleration is not available in double precision, disabled!")
55     set_property(CACHE GMX_GPU PROPERTY VALUE OFF)
56     set_property(CACHE GMX_GPU_AUTO PROPERTY VALUE OFF)
57 endif()
58
59 # detect GPUs in the build host machine
60 if ((GMX_GPU OR GMX_GPU_AUTO) AND NOT GMX_GPU_DETECTION_DONE)
61     include(gmxDetectGpu)
62     gmx_detect_gpu()
63 endif()
64
65 # We need to call find_package even when we've already done the detection/setup
66 if(GMX_GPU OR GMX_GPU_AUTO)
67     if(NOT GMX_GPU AND NOT GMX_DETECT_GPU_AVAILABLE)
68         # Stay quiet when detection has occured and found no GPU.
69         # Noise is acceptable when there is a GPU or the user required one.
70         set(FIND_CUDA_QUIETLY QUIET)
71     endif()
72
73     # Cmake tries to use the static cuda runtime by default,
74     # but this leads to unusable GPU builds on OS X.
75     if(APPLE)
76         set(CUDA_USE_STATIC_CUDA_RUNTIME OFF CACHE STRING "Use the static version of the CUDA runtime library if available")
77     endif()
78
79     find_package(CUDA ${REQUIRED_CUDA_VERSION} ${FIND_CUDA_QUIETLY})
80 endif()
81
82 # Depending on the current vale of GMX_GPU and GMX_GPU_AUTO:
83 # - OFF, FALSE: Will skip this detection/setup.
84 # - OFF, TRUE : Will keep GMX_GPU=OFF if no CUDA is detected, but will assemble
85 #               a warning message which will be issued at the end of the
86 #               configuration if GPU(s) were found in the build system.
87 # - ON , FALSE: The user requested GPU build and this requires CUDA, so we will
88 #               fail if it is not available.
89 # - ON , TRUE : Can't happen (GMX_GPU=ON can only be user-set at this point)
90 if((GMX_GPU OR GMX_GPU_AUTO) AND NOT GMX_GPU_DETECTION_DONE)
91     # assemble warning/error message
92     if (GMX_DETECT_GPU_AVAILABLE)
93         set(_msg "${GMX_DETECT_GPU_COUNT} NVIDIA GPU(s) found in the system")
94
95         # append GPU names
96         if (NOT GMX_DETECT_GPU_INFO STREQUAL "")
97             set(_msg "${_msg}:")
98             foreach(gpu ${GMX_DETECT_GPU_INFO})
99                 set(_msg "${_msg}
100 ${gpu}")
101             endforeach()
102         endif()
103
104         # TODO remove the second part of the message when we'll have compute
105         # capability information from the detection.
106         set(_msg "${_msg}
107 Compute capability information not available, consult the NVIDIA website:
108 https://developer.nvidia.com/cuda-gpus")
109     endif()
110
111         set(CUDA_NOTFOUND_MESSAGE "mdrun supports native GPU acceleration on NVIDIA hardware with compute capability >= ${REQUIRED_CUDA_COMPUTE_CAPABILITY} (Kepler 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.
112
113 ${_msg}")
114         unset(_msg)
115
116     if (NOT CUDA_FOUND)
117         if (GMX_GPU_AUTO)
118             # Disable GPU acceleration in auto mode
119             message(STATUS "No compatible CUDA toolkit found (v5.0+), disabling native GPU acceleration")
120             set_property(CACHE GMX_GPU PROPERTY VALUE OFF)
121             set(CUDA_NOTFOUND_AUTO ON)
122         else()
123             # the user requested CUDA, but it wasn't found
124             message(FATAL_ERROR "${CUDA_NOTFOUND_MESSAGE}")
125         endif()
126     else()
127         if (GMX_GPU_AUTO)
128             message(STATUS "Enabling native GPU acceleration")
129             set_property(CACHE GMX_GPU PROPERTY VALUE ON)
130         endif()
131     endif()
132 endif()
133
134 # Annoyingly enough, FindCUDA leaves a few variables behind as non-advanced.
135 # We need to mark these advanced outside the conditional, otherwise, if the
136 # user turns GMX_GPU=OFF after a failed cmake pass, these variables will be
137 # left behind in the cache.
138 mark_as_advanced(CUDA_SDK_ROOT_DIR
139                  CUDA_USE_STATIC_CUDA_RUNTIME
140                  CUDA_dl_LIBRARY CUDA_rt_LIBRARY
141                  )
142 if(NOT GMX_GPU)
143     mark_as_advanced(CUDA_TOOLKIT_ROOT_DIR)
144     mark_as_advanced(CUDA_HOST_COMPILER)
145 endif()
146
147 # Try to execute ${CUDA_NVCC_EXECUTABLE} --version and set the output
148 # (or an error string) in the argument variable.
149 # Note that semicolon is used as separator for nvcc.
150 #
151 # Parameters:
152 #   COMPILER_INFO         - [output variable] string with compiler path, ID and
153 #                           some compiler-provided information
154 #   DEVICE_COMPILER_FLAGS - [output variable] device flags for the compiler
155 #   HOST_COMPILER_FLAGS   - [output variable] host flags for the compiler, if propagated
156 #
157 macro(get_cuda_compiler_info COMPILER_INFO DEVICE_COMPILER_FLAGS HOST_COMPILER_FLAGS)
158     if(NOT GMX_CLANG_CUDA)
159         if(CUDA_NVCC_EXECUTABLE)
160
161             # Get the nvcc version string. This is multi-line, but since it is only 4 lines
162             # and might change in the future it is better to store than trying to parse out
163             # the version from the current format.
164             execute_process(COMMAND ${CUDA_NVCC_EXECUTABLE} --version
165                 RESULT_VARIABLE _nvcc_version_res
166                 OUTPUT_VARIABLE _nvcc_version_out
167                 ERROR_VARIABLE  _nvcc_version_err
168                 OUTPUT_STRIP_TRAILING_WHITESPACE)
169             if (${_nvcc_version_res} EQUAL 0)
170                 # Fix multi-line mess: Replace newline with ";" so we can use it in a define
171                 string(REPLACE "\n" ";" _nvcc_info_singleline ${_nvcc_version_out})
172                 SET(${COMPILER_INFO} "${CUDA_NVCC_EXECUTABLE} ${_nvcc_info_singleline}")
173                 string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
174                 SET(_compiler_flags "${CUDA_NVCC_FLAGS_${_build_type}}")
175                 if(CUDA_PROPAGATE_HOST_FLAGS)
176                     set(${HOST_COMPILER_FLAGS} BUILD_CXXFLAGS)
177                 else()
178                     set(${HOST_COMPILER_FLAGS} "")
179                 endif()
180                 SET(${DEVICE_COMPILER_FLAGS} "${CUDA_NVCC_FLAGS}${CUDA_NVCC_FLAGS_${_build_type}}")
181             else()
182                 SET(${COMPILER_INFO} "N/A")
183                 SET(${COMPILER_FLAGS} "N/A")
184             endif()
185         endif()
186     else()
187         # CXX compiler is the CUDA compiler
188         set(${COMPILER_INFO} "${CMAKE_CXX_COMPILER}  ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
189         # there are some extra flags
190         set(${COMPILER_FLAGS} "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${_build_type}} ${GMX_CUDA_CLANG_FLAGS}")
191     endif()
192 endmacro ()
193
194 macro(enable_multiple_cuda_compilation_units)
195     message(STATUS "Enabling multiple compilation units for the CUDA non-bonded module.")
196     set_property(CACHE GMX_CUDA_NB_SINGLE_COMPILATION_UNIT PROPERTY VALUE OFF)
197 endmacro()
198
199 include(CMakeDependentOption)
200 include(gmxOptionUtilities)
201 macro(gmx_gpu_setup)
202     if(GMX_GPU)
203         if(NOT GMX_CLANG_CUDA)
204             if(NOT CUDA_NVCC_EXECUTABLE)
205                 message(FATAL_ERROR "nvcc is required for a CUDA build, please set CUDA_TOOLKIT_ROOT_DIR appropriately")
206             endif()
207             # set up nvcc options
208             include(gmxManageNvccConfig)
209         else()
210             include(gmxManageClangCudaConfig)
211         endif()
212
213         # no OpenMP is no good!
214         if(NOT GMX_OPENMP)
215             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.")
216         endif()
217     endif() # GMX_GPU
218
219     option(GMX_CUDA_NB_SINGLE_COMPILATION_UNIT "Whether to compile the CUDA non-bonded module using a single compilation unit." OFF)
220     mark_as_advanced(GMX_CUDA_NB_SINGLE_COMPILATION_UNIT)
221
222 endmacro()