2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2012,2013,2014, 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")
43 option(GMX_GPU "Enable GPU acceleration" OFF)
45 if(GMX_GPU AND GMX_DOUBLE)
46 message(FATAL_ERROR "GPU acceleration is not available in double precision!")
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)
54 # detect GPUs in the build host machine
55 if ((GMX_GPU OR GMX_GPU_AUTO) AND NOT GMX_GPU_DETECTION_DONE)
60 # We need to call find_package even when we've already done the detection/setup
61 if(GMX_GPU OR GMX_GPU_AUTO)
62 if(NOT GMX_GPU AND NOT GMX_DETECT_GPU_AVAILABLE)
63 # Stay quiet when detection has occured and found no GPU.
64 # Noise is acceptable when there is a GPU or the user required one.
65 set(FIND_CUDA_QUIETLY QUIET)
67 # We support CUDA >=v4.0 on *nix, but <= v4.1 doesn't work with MSVC
69 find_package(CUDA 4.1 ${FIND_CUDA_QUIETLY})
71 find_package(CUDA 4.0 ${FIND_CUDA_QUIETLY})
73 # Cmake 2.8.12 (and CMake 3.0) introduced a new bug where the cuda
74 # library dir is added twice as an rpath on APPLE, which in turn causes
75 # the install_name_tool to wreck the binaries when it tries to remove this
76 # path. Since this is set inside the cuda module, we remove the extra rpath
77 # added in the library string - an rpath is not a library anyway, and at
78 # least for Gromacs this works on all CMake versions. This should be
79 # reasonably future-proof, since newer versions of CMake appear to handle
80 # the rpath automatically based on the provided library path, meaning
81 # the explicit rpath specification is no longer needed.
82 if(APPLE AND (CMAKE_VERSION VERSION_GREATER 2.8.11))
83 foreach(elem ${CUDA_LIBRARIES})
84 if(elem MATCHES "-Wl,.*")
85 list(REMOVE_ITEM CUDA_LIBRARIES ${elem})
91 # Depending on the current vale of GMX_GPU and GMX_GPU_AUTO:
92 # - OFF, FALSE: Will skip this detection/setup.
93 # - OFF, TRUE : Will keep GMX_GPU=OFF if no CUDA is detected, but will assemble
94 # a warning message which will be issued at the end of the
95 # configuration if GPU(s) were found in the build system.
96 # - ON , FALSE: The user requested GPU build and this requires CUDA, so we will
97 # fail if it is not available.
98 # - ON , TRUE : Can't happen (GMX_GPU=ON can only be user-set at this point)
99 if((GMX_GPU OR GMX_GPU_AUTO) AND NOT GMX_GPU_DETECTION_DONE)
100 if (EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
101 set(CUDA_FOUND TRUE CACHE INTERNAL "Whether the CUDA toolkit was found" FORCE)
103 set(CUDA_FOUND FALSE CACHE INTERNAL "Whether the CUDA toolkit was found" FORCE)
106 # assemble warning/error message
107 if (GMX_DETECT_GPU_AVAILABLE)
108 set(_msg "${GMX_DETECT_GPU_COUNT} NVIDIA GPU(s) found in the system")
111 if (NOT GMX_DETECT_GPU_INFO STREQUAL "")
113 foreach(gpu ${GMX_DETECT_GPU_INFO})
119 # TODO remove the second part of the message when we'll have compute
120 # capability information from the detection.
122 Compute capability information not available, consult the NVIDIA website:
123 https://developer.nvidia.com/cuda-gpus")
126 set(CUDA_NOTFOUND_MESSAGE "mdrun supports native GPU acceleration on NVIDIA hardware with compute capability >=2.0 (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.
133 # Disable GPU acceleration in auto mode
134 message(STATUS "No compatible CUDA toolkit found (v4.0+), disabling native GPU acceleration")
135 set_property(CACHE GMX_GPU PROPERTY VALUE OFF)
136 set(CUDA_NOTFOUND_AUTO ON)
138 # the user requested CUDA, but it wasn't found
139 message(FATAL_ERROR "${CUDA_NOTFOUND_MESSAGE}")
143 message(STATUS "Enabling native GPU acceleration")
144 set_property(CACHE GMX_GPU PROPERTY VALUE ON)
146 endif() # NOT CUDA_FOUND
148 # Annoyingly enough, FindCUDA leaves a few variables behind as non-advanced.
149 # We need to mark these advanced outside the conditional, otherwise, if the
150 # user turns GMX_GPU=OFF after a failed cmake pass, these variables will be
151 # left behind in the cache.
152 mark_as_advanced(CUDA_BUILD_CUBIN CUDA_BUILD_EMULATION CUDA_SDK_ROOT_DIR CUDA_VERBOSE_BUILD)
154 mark_as_advanced(CUDA_TOOLKIT_ROOT_DIR)
157 # Try to execute ${CUDA_NVCC_EXECUTABLE} --version and set the output
158 # (or an error string) in the argument variable.
159 # Note that semicolon is used as separator for nvcc.
162 # COMPILER_INFO - [output variable] string with compiler path, ID and
163 # some compiler-provided information
164 # COMPILER_FLAGS - [output variable] flags for the compiler
166 macro(get_cuda_compiler_info COMPILER_INFO COMPILER_FLAGS)
167 if(CUDA_NVCC_EXECUTABLE)
169 # Get the nvcc version string. This is multi-line, but since it is only 4 lines
170 # and might change in the future it is better to store than trying to parse out
171 # the version from the current format.
172 execute_process(COMMAND ${CUDA_NVCC_EXECUTABLE} --version
173 RESULT_VARIABLE _nvcc_version_res
174 OUTPUT_VARIABLE _nvcc_version_out
175 ERROR_VARIABLE _nvcc_version_err
176 OUTPUT_STRIP_TRAILING_WHITESPACE)
177 if (${_nvcc_version_res} EQUAL 0)
178 # Fix multi-line mess: Replace newline with ";" so we can use it in a define
179 string(REPLACE "\n" ";" _nvcc_info_singleline ${_nvcc_version_out})
180 SET(${COMPILER_INFO} "${CUDA_NVCC_EXECUTABLE} ${_nvcc_info_singleline}")
181 string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
182 SET(_compiler_flags "${CUDA_NVCC_FLAGS_${_build_type}}")
183 if(CUDA_PROPAGATE_HOST_FLAGS)
184 string(REGEX REPLACE "[ ]+" ";" _cxx_flags_nospace "${BUILD_CXXFLAGS}")
186 SET(${COMPILER_FLAGS} "${CUDA_NVCC_FLAGS}${CUDA_NVCC_FLAGS_${_build_type}}; ${_cxx_flags_nospace}")
188 SET(${COMPILER_INFO} "N/A")
189 SET(${COMPILER_FLAGS} "N/A")
195 # set up nvcc options
196 include(gmxManageNvccConfig)
198 # Atomic operations used for polling wait for GPU
199 # (to avoid the cudaStreamSynchronize + ECC bug).
200 # ThreadMPI is now always included. Thus, we don't check for Atomics anymore here.
202 # no OpenMP is no good!
204 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.")