Split espresso I/O routines from confio.cpp
[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, 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 # 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 endif()
43 option(GMX_GPU "Enable GPU acceleration" OFF)
44
45 if(GMX_GPU AND GMX_DOUBLE)
46     message(FATAL_ERROR "GPU acceleration is not available in double precision!")
47 endif()
48 if(GMX_GPU_AUTO AND GMX_DOUBLE)
49     message(WARNING "GPU acceleration is not available in double precision, disabled!")
50     set_property(CACHE GMX_GPU PROPERTY VALUE OFF)
51     set_property(CACHE GMX_GPU_AUTO PROPERTY VALUE OFF)
52 endif()
53
54 # detect GPUs in the build host machine
55 if ((GMX_GPU OR GMX_GPU_AUTO) AND NOT GMX_GPU_DETECTION_DONE)
56     include(gmxDetectGpu)
57     gmx_detect_gpu()
58 endif()
59
60 # CMake 3.0-3.1 has a bug in the following case, which breaks
61 # configuration on at least BlueGene/Q. Fixed in 3.1.1
62 if ((NOT CMAKE_VERSION VERSION_LESS "3.0.0") AND
63     (CMAKE_VERSION VERSION_LESS "3.1.1") AND
64         (CMAKE_CROSSCOMPILING AND NOT CMAKE_SYSTEM_PROCESSOR))
65     message(STATUS "Cannot search for CUDA because the CMake find package has a bug. Set a valid CMAKE_SYSTEM_PROCESSOR if you need to detect CUDA")
66 else()
67     set(CAN_RUN_CUDA_FIND_PACKAGE 1)
68 endif()
69
70 # We need to call find_package even when we've already done the detection/setup
71 if(GMX_GPU OR GMX_GPU_AUTO AND CAN_RUN_CUDA_FIND_PACKAGE)
72     if(NOT GMX_GPU AND NOT GMX_DETECT_GPU_AVAILABLE)
73         # Stay quiet when detection has occured and found no GPU.
74         # Noise is acceptable when there is a GPU or the user required one.
75         set(FIND_CUDA_QUIETLY QUIET)
76     endif()
77     find_package(CUDA ${REQUIRED_CUDA_VERSION} ${FIND_CUDA_QUIETLY})
78
79     # The IBM xlc compiler chokes if we use both altivec and Cuda. Solve
80     # this by not propagating the flags in this case, but add -O3
81     # to make sure we don't turn off optimization.
82     if(CMAKE_CXX_COMPILER_ID MATCHES "XL")
83         set(CUDA_PROPAGATE_HOST_FLAGS OFF)
84         list(APPEND CUDA_NVCC_FLAGS "-O3")
85     endif()
86
87     # Cmake 2.8.12 (and CMake 3.0) introduced a new bug where the cuda
88     # library dir is added twice as an rpath on APPLE, which in turn causes
89     # the install_name_tool to wreck the binaries when it tries to remove this
90     # path. Since this is set inside the cuda module, we remove the extra rpath
91     # added in the library string - an rpath is not a library anyway, and at
92     # least for Gromacs this works on all CMake versions. This should be
93     # reasonably future-proof, since newer versions of CMake appear to handle
94     # the rpath automatically based on the provided library path, meaning
95     # the explicit rpath specification is no longer needed.
96     if(APPLE AND (CMAKE_VERSION VERSION_GREATER 2.8.11))
97         foreach(elem ${CUDA_LIBRARIES})
98             if(elem MATCHES "-Wl,.*")
99                 list(REMOVE_ITEM CUDA_LIBRARIES ${elem})
100             endif()
101         endforeach(elem)
102     endif()
103 endif()
104
105 # Depending on the current vale of GMX_GPU and GMX_GPU_AUTO:
106 # - OFF, FALSE: Will skip this detection/setup.
107 # - OFF, TRUE : Will keep GMX_GPU=OFF if no CUDA is detected, but will assemble
108 #               a warning message which will be issued at the end of the
109 #               configuration if GPU(s) were found in the build system.
110 # - ON , FALSE: The user requested GPU build and this requires CUDA, so we will
111 #               fail if it is not available.
112 # - ON , TRUE : Can't happen (GMX_GPU=ON can only be user-set at this point)
113 if((GMX_GPU OR GMX_GPU_AUTO) AND NOT GMX_GPU_DETECTION_DONE)
114     if (EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
115         set(CUDA_FOUND TRUE CACHE INTERNAL "Whether the CUDA toolkit was found" FORCE)
116     else()
117         set(CUDA_FOUND FALSE CACHE INTERNAL "Whether the CUDA toolkit was found" FORCE)
118     endif()
119
120     # assemble warning/error message
121     if (GMX_DETECT_GPU_AVAILABLE)
122         set(_msg "${GMX_DETECT_GPU_COUNT} NVIDIA GPU(s) found in the system")
123
124         # append GPU names
125         if (NOT GMX_DETECT_GPU_INFO STREQUAL "")
126             set(_msg "${_msg}:")
127             foreach(gpu ${GMX_DETECT_GPU_INFO})
128                 set(_msg "${_msg}
129 ${gpu}")
130             endforeach()
131         endif()
132
133         # TODO remove the second part of the message when we'll have compute
134         # capability information from the detection.
135         set(_msg "${_msg}
136 Compute capability information not available, consult the NVIDIA website:
137 https://developer.nvidia.com/cuda-gpus")
138     endif()
139
140         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.
141
142 ${_msg}")
143         unset(_msg)
144
145     if (NOT CUDA_FOUND)
146         if (GMX_GPU_AUTO)
147             # Disable GPU acceleration in auto mode
148             message(STATUS "No compatible CUDA toolkit found (v4.0+), disabling native GPU acceleration")
149             set_property(CACHE GMX_GPU PROPERTY VALUE OFF)
150             set(CUDA_NOTFOUND_AUTO ON)
151         else()
152             # the user requested CUDA, but it wasn't found
153             message(FATAL_ERROR "${CUDA_NOTFOUND_MESSAGE}")
154         endif()
155     else()
156         if (GMX_GPU_AUTO)
157             message(STATUS "Enabling native GPU acceleration")
158             set_property(CACHE GMX_GPU PROPERTY VALUE ON)
159         endif()
160     endif() # NOT CUDA_FOUND
161 endif()
162
163 # Try to find NVML if a GPU accelerated binary should be build.
164 if (GMX_GPU)
165     find_package(NVML)
166     if(NVML_FOUND)
167         include_directories(${NVML_INCLUDE_DIR})
168         set(HAVE_NVML 1)
169         list(APPEND GMX_EXTRA_LIBRARIES ${NVML_LIBRARY})
170     endif(NVML_FOUND)
171 endif()
172
173 # Annoyingly enough, FindCUDA leaves a few variables behind as non-advanced.
174 # We need to mark these advanced outside the conditional, otherwise, if the
175 # user turns GMX_GPU=OFF after a failed cmake pass, these variables will be
176 # left behind in the cache.
177 mark_as_advanced(CUDA_BUILD_CUBIN CUDA_BUILD_EMULATION CUDA_SDK_ROOT_DIR CUDA_VERBOSE_BUILD)
178 if(NOT GMX_GPU)
179     mark_as_advanced(CUDA_TOOLKIT_ROOT_DIR)
180 endif()
181
182 # Try to execute ${CUDA_NVCC_EXECUTABLE} --version and set the output
183 # (or an error string) in the argument variable.
184 # Note that semicolon is used as separator for nvcc.
185 #
186 # Parameters:
187 #   COMPILER_INFO   - [output variable] string with compiler path, ID and
188 #                     some compiler-provided information
189 #   COMPILER_FLAGS  - [output variable] flags for the compiler
190 #
191 macro(get_cuda_compiler_info COMPILER_INFO COMPILER_FLAGS)
192     if(CUDA_NVCC_EXECUTABLE)
193
194         # Get the nvcc version string. This is multi-line, but since it is only 4 lines
195         # and might change in the future it is better to store than trying to parse out
196         # the version from the current format.
197         execute_process(COMMAND ${CUDA_NVCC_EXECUTABLE} --version
198             RESULT_VARIABLE _nvcc_version_res
199             OUTPUT_VARIABLE _nvcc_version_out
200             ERROR_VARIABLE  _nvcc_version_err
201             OUTPUT_STRIP_TRAILING_WHITESPACE)
202         if (${_nvcc_version_res} EQUAL 0)
203             # Fix multi-line mess: Replace newline with ";" so we can use it in a define
204             string(REPLACE "\n" ";" _nvcc_info_singleline ${_nvcc_version_out})
205             SET(${COMPILER_INFO} "${CUDA_NVCC_EXECUTABLE} ${_nvcc_info_singleline}")
206             string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
207             SET(_compiler_flags "${CUDA_NVCC_FLAGS_${_build_type}}")
208             if(CUDA_PROPAGATE_HOST_FLAGS)
209                 string(REGEX REPLACE "[ ]+" ";" _cxx_flags_nospace "${BUILD_CXXFLAGS}")
210             endif()
211             SET(${COMPILER_FLAGS} "${CUDA_NVCC_FLAGS}${CUDA_NVCC_FLAGS_${_build_type}}; ${_cxx_flags_nospace}")
212         else()
213             SET(${COMPILER_INFO} "N/A")
214             SET(${COMPILER_FLAGS} "N/A")
215         endif()
216     endif()
217 endmacro ()
218
219 macro(gmx_gpu_setup)
220     # set up nvcc options
221     include(gmxManageNvccConfig)
222
223     gmx_check_if_changed(_cuda_version_changed CUDA_VERSION)
224
225     # Generate CUDA RT API version string which will end up in config.h
226     # We do this because nvcc is silly enough to not define its own version
227     # (which should match the CUDA runtime API version AFAICT) and we want to
228     # avoid creating the fragile dependency on cuda_runtime_api.h.
229     #
230     # NOTE: CUDA v7.5 is expected to have nvcc define it own version, so in the
231     # future we should switch to using that version string instead of our own.
232     if (NOT GMX_CUDA_VERSION OR _cuda_version_changed)
233         MATH(EXPR GMX_CUDA_VERSION "${CUDA_VERSION_MAJOR}*1000 + ${CUDA_VERSION_MINOR}*10")
234     endif()
235
236     if (_cuda_version_changed)
237         # check the generated CUDA API version against the one present in cuda_runtime_api.h
238         try_compile(_get_cuda_version_compile_res
239             ${CMAKE_BINARY_DIR}
240             ${CMAKE_SOURCE_DIR}/cmake/TestCUDAVersion.c
241             COMPILE_DEFINITIONS "-DGMX_CUDA_VERSION=${GMX_CUDA_VERSION}"
242             CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${CUDA_TOOLKIT_INCLUDE}"
243             OUTPUT_VARIABLE _get_cuda_version_compile_out)
244
245         if (NOT _get_cuda_version_compile_res)
246             if (_get_cuda_version_compile_out MATCHES "CUDA version mismatch")
247                 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.")
248             else()
249                 message(FATAL_ERROR "Could not detect CUDA runtime API version")
250             endif()
251         endif()
252     endif()
253
254     # texture objects are supported in CUDA 5.0 and later
255     if (CUDA_VERSION VERSION_GREATER 4.999)
256         set(HAVE_CUDA_TEXOBJ_SUPPORT 1)
257     endif()
258
259     # Atomic operations used for polling wait for GPU
260     # (to avoid the cudaStreamSynchronize + ECC bug).
261     # ThreadMPI is now always included. Thus, we don't check for Atomics anymore here.
262
263     # no OpenMP is no good!
264     if(NOT GMX_OPENMP)
265         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.")
266     endif()
267 endmacro()