97fee7560ec0cc0c286618647239f20a0de76787
[alexxy/gromacs.git] / cmake / gmxManageGPU.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012,2013, by the GROMACS development team, led by
5 # David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 # others, as listed in the AUTHORS file in the top-level source
7 # 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 # 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)
66     endif()
67     # We support CUDA >=v3.2 on *nix, but <= v4.1 doesn't work with MSVC
68     if(MSVC)
69         find_package(CUDA 4.1 ${FIND_CUDA_QUIETLY})
70     else()
71         find_package(CUDA 3.2 ${FIND_CUDA_QUIETLY})
72     endif()
73 endif()
74
75 # Depending on the current vale of GMX_GPU and GMX_GPU_AUTO:
76 # - OFF, FALSE: Will skip this detection/setup.
77 # - OFF, TRUE : Will keep GMX_GPU=OFF if no CUDA is detected, but will assemble
78 #               a warning message which will be issued at the end of the
79 #               configuration if GPU(s) were found in the build system.
80 # - ON , FALSE: The user requested GPU build and this requires CUDA, so we will
81 #               fail if it is not available.
82 # - ON , TRUE : Can't happen (GMX_GPU=ON can only be user-set at this point)
83 if((GMX_GPU OR GMX_GPU_AUTO) AND NOT GMX_GPU_DETECTION_DONE)
84     if (EXISTS ${CUDA_TOOLKIT_ROOT_DIR})
85         set(CUDA_FOUND TRUE CACHE INTERNAL "Whether the CUDA toolkit was found" FORCE)
86     else()
87         set(CUDA_FOUND FALSE CACHE INTERNAL "Whether the CUDA toolkit was found" FORCE)
88     endif()
89
90     # assemble warning/error message
91     if (GMX_DETECT_GPU_AVAILABLE)
92         set(_msg "
93     ${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             ")
110     endif()
111
112         set(CUDA_NOTFOUND_MESSAGE "
113     mdrun supports native GPU acceleration on NVIDIA hardware with compute
114     capability >=2.0 (Fermi or later). This requires the NVIDIA CUDA toolkit,
115     which was not found. Its location can be hinted by setting the
116     CUDA_TOOLKIT_ROOT_DIR CMake option (does not work as an environment variable).
117     The typical location would be /usr/local/cuda[-version].
118     Note that CPU or GPU acceleration can be selected at runtime!
119
120     ${_msg}")
121         unset(_msg)
122
123     if (NOT CUDA_FOUND)
124         if (GMX_GPU_AUTO)
125             # Disable GPU acceleration in auto mode
126             message(STATUS "No compatible CUDA toolkit found (v3.2+), disabling native GPU acceleration")
127             set_property(CACHE GMX_GPU PROPERTY VALUE OFF)
128             set(CUDA_NOTFOUND_AUTO ON)
129         else ()
130             # the user requested CUDA, but it wasn't found
131             message(FATAL_ERROR "${CUDA_NOTFOUND_MESSAGE}")
132         endif()
133     else()
134         if (GMX_GPU_AUTO)
135             message(STATUS "Enabling native GPU acceleration")
136             set_property(CACHE GMX_GPU PROPERTY VALUE ON)
137         endif()
138     endif() # NOT CUDA_FOUND
139 endif()
140 # Annoyingly enough, FindCUDA leaves a few variables behind as non-advanced.
141 # We need to mark these advanced outside the conditional, otherwise, if the
142 # user turns GMX_GPU=OFF after a failed cmake pass, these variables will be
143 # left behind in the cache.
144 mark_as_advanced(CUDA_BUILD_CUBIN CUDA_BUILD_EMULATION CUDA_SDK_ROOT_DIR CUDA_VERBOSE_BUILD)
145 if(NOT GMX_GPU)
146     mark_as_advanced(CUDA_TOOLKIT_ROOT_DIR)
147 endif()
148
149 macro(gmx_gpu_setup)
150     # set up nvcc options
151     include(gmxManageNvccConfig)
152
153     # Version info (semicolon used as line separator) for nvcc.
154     get_nvcc_version_info()
155
156     # Atomic operations used for polling wait for GPU
157     # (to avoid the cudaStreamSynchronize + ECC bug).
158     # ThreadMPI is now always included. Thus, we don't check for Atomics anymore here.
159
160     # no OpenMP is no good!
161     if(NOT GMX_OPENMP)
162         message(WARNING "
163     To use GPU acceleration efficiently, mdrun requires OpenMP multi-threading.
164     Without OpenMP a single CPU core can be used with a GPU which is not optimal.
165     Note that with MPI multiple processes can be forced to use a single GPU, but this
166     typically inefficient. Note that you need to set both C and C++ compilers that
167     support OpenMP (CC and CXX environment variables, respectively) when using GPUs.")
168     endif()
169 endmacro()