eca657e257106a26914f23a0115034b4cc1ece41
[alexxy/gromacs.git] / src / gromacs / gpu_utils / gpu_utils.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2010, The GROMACS development team.
6  * Copyright (c) 2012,2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 /*! \libinternal \file
38  *  \brief Declare functions for detection and initialization for GPU devices.
39  *
40  *  \author Szilard Pall <pall.szilard@gmail.com>
41  *  \author Mark Abraham <mark.j.abraham@gmail.com>
42  *
43  *  \inlibraryapi
44  */
45 #ifndef GMX_GPU_UTILS_GPU_UTILS_H
46 #define GMX_GPU_UTILS_GPU_UTILS_H
47
48 #include <cstdio>
49
50 #include <string>
51 #include <vector>
52
53 #include "gromacs/gpu_utils/gpu_macros.h"
54 #include "gromacs/utility/basedefinitions.h"
55
56 struct gmx_device_info_t;
57 struct gmx_gpu_info_t;
58
59 namespace gmx
60 {
61 }
62
63 //! Enum which is only used to describe transfer calls at the moment
64 enum class GpuApiCallBehavior
65 {
66     Sync,
67     Async
68 };
69
70 //! Types of actions associated to waiting or checking the completion of GPU tasks
71 enum class GpuTaskCompletion
72 {
73     Wait, /*<< Issue a blocking wait for the task */
74     Check /*<< Only check whether the task has completed */
75 };
76
77 /*! \brief Return whether GPUs can be detected
78  *
79  * Returns true when this is a build of \Gromacs configured to support
80  * GPU usage, GPU detection is not disabled by an environment variable
81  * and a valid device driver, ICD, and/or runtime was detected.
82  * Does not throw. */
83 bool canPerformGpuDetection();
84
85 /*! \brief Return whether GPU detection is functioning correctly
86  *
87  * Returns true when this is a build of \Gromacs configured to support
88  * GPU usage, and a valid device driver, ICD, and/or runtime was detected.
89  *
90  * This function is not intended to be called from build
91  * configurations that do not support GPUs, and there will be no
92  * descriptive message in that case.
93  *
94  * \param[out] errorMessage  When returning false on a build configured with
95  *                           GPU support and non-nullptr was passed,
96  *                           the string contains a descriptive message about
97  *                           why GPUs cannot be detected.
98  *
99  * Does not throw. */
100 GPU_FUNC_QUALIFIER
101 bool isGpuDetectionFunctional(std::string *GPU_FUNC_ARGUMENT(errorMessage)) GPU_FUNC_TERM_WITH_RETURN(false);
102
103 /*! \brief Find all GPUs in the system.
104  *
105  *  Will detect every GPU supported by the device driver in use.
106  *  Must only be called if canPerformGpuDetection() has returned true.
107  *  This routine also checks for the compatibility of each and fill the
108  *  gpu_info->gpu_dev array with the required information on each the
109  *  device: ID, device properties, status.
110  *
111  *  Note that this function leaves the GPU runtime API error state clean;
112  *  this is implemented ATM in the CUDA flavor.
113  *  TODO: check if errors do propagate in OpenCL as they do in CUDA and
114  *  whether there is a mechanism to "clear" them.
115  *
116  *  \param[in] gpu_info    pointer to structure holding GPU information.
117  *
118  *  \throws                InternalError if a GPU API returns an unexpected failure (because
119  *                         the call to canDetectGpus() should always prevent this occuring)
120  */
121 GPU_FUNC_QUALIFIER
122 void findGpus(gmx_gpu_info_t *GPU_FUNC_ARGUMENT(gpu_info)) GPU_FUNC_TERM
123
124 /*! \brief Return a container of the detected GPUs that are compatible.
125  *
126  * This function filters the result of the detection for compatible
127  * GPUs, based on the previously run compatibility tests.
128  *
129  * \param[in]     gpu_info    Information detected about GPUs, including compatibility.
130  * \return                    vector of IDs of GPUs already recorded as compatible */
131 std::vector<int> getCompatibleGpus(const gmx_gpu_info_t &gpu_info);
132
133 /*! \brief Return a string describing how compatible the GPU with given \c index is.
134  *
135  * \param[in]   gpu_info    Information about detected GPUs
136  * \param[in]   index       index of GPU to ask about
137  * \returns                 A null-terminated C string describing the compatibility status, useful for error messages.
138  */
139 const char *getGpuCompatibilityDescription(const gmx_gpu_info_t &gpu_info,
140                                            int                   index);
141
142 /*! \brief Frees the gpu_dev and dev_use array fields of \p gpu_info.
143  *
144  * \param[in]    gpu_info    pointer to structure holding GPU information
145  */
146 void free_gpu_info(const gmx_gpu_info_t *gpu_info);
147
148 /*! \brief Initializes the GPU described by \c deviceInfo.
149  *
150  * TODO Doxygen complains about these - probably a Doxygen bug, since
151  * the patterns here are the same as elsewhere in this header.
152  *
153  * \param[in]    deviceInfo   device info of the GPU to initialize
154  *
155  * Issues a fatal error for any critical errors that occur during
156  * initialization.
157  */
158 GPU_FUNC_QUALIFIER
159 void init_gpu(const gmx_device_info_t *GPU_FUNC_ARGUMENT(deviceInfo)) GPU_FUNC_TERM
160
161 /*! \brief Frees up the CUDA GPU used by the active context at the time of calling.
162  *
163  * If \c deviceInfo is nullptr, then it is understood that no device
164  * was selected so no context is active to be freed. Otherwise, the
165  * context is explicitly destroyed and therefore all data uploaded to
166  * the GPU is lost. This must only be called when none of this data is
167  * required anymore, because subsequent attempts to free memory
168  * associated with the context will otherwise fail.
169  *
170  * Calls gmx_warning upon errors.
171  *
172  * \param[in]  deviceInfo   device info of the GPU to clean up for
173  *
174  * \returns                 true if no error occurs during the freeing.
175  */
176 CUDA_FUNC_QUALIFIER
177 void free_gpu(const gmx_device_info_t *CUDA_FUNC_ARGUMENT(deviceInfo)) CUDA_FUNC_TERM
178
179 /*! \brief Return a pointer to the device info for \c deviceId
180  *
181  * \param[in] gpu_info      GPU info of all detected devices in the system.
182  * \param[in] deviceId      ID for the GPU device requested.
183  *
184  * \returns                 Pointer to the device info for \c deviceId.
185  */
186 GPU_FUNC_QUALIFIER
187 gmx_device_info_t *getDeviceInfo(const gmx_gpu_info_t &GPU_FUNC_ARGUMENT(gpu_info),
188                                  int GPU_FUNC_ARGUMENT(deviceId)) GPU_FUNC_TERM_WITH_RETURN(nullptr)
189
190 /*! \brief Returns the device ID of the CUDA GPU currently in use.
191  *
192  * The GPU used is the one that is active at the time of the call in the active context.
193  *
194  * \returns                 device ID of the GPU in use at the time of the call
195  */
196 CUDA_FUNC_QUALIFIER
197 int get_current_cuda_gpu_device_id(void) CUDA_FUNC_TERM_WITH_RETURN(-1)
198
199 /*! \brief Formats and returns a device information string for a given GPU.
200  *
201  * Given an index *directly* into the array of available GPUs (gpu_dev)
202  * returns a formatted info string for the respective GPU which includes
203  * ID, name, compute capability, and detection status.
204  *
205  * \param[out]  s           pointer to output string (has to be allocated externally)
206  * \param[in]   gpu_info    Information about detected GPUs
207  * \param[in]   index       an index *directly* into the array of available GPUs
208  */
209 GPU_FUNC_QUALIFIER
210 void get_gpu_device_info_string(char *GPU_FUNC_ARGUMENT(s),
211                                 const gmx_gpu_info_t &GPU_FUNC_ARGUMENT(gpu_info),
212                                 int GPU_FUNC_ARGUMENT(index)) GPU_FUNC_TERM
213
214 /*! \brief Returns whether all compatible OpenCL devices are from AMD.
215  *
216  * This is currently the most useful and best tested platform for
217  * supported OpenCL devices, so some modules may need to check what
218  * degree of support they should offer.
219  *
220  * \todo An enumeration visible in the hardware module would make such
221  * checks more configurable, if we discover other needs in future.
222  *
223  * \returns whether all detected compatible devices have AMD for the vendor.
224  */
225 OPENCL_FUNC_QUALIFIER
226 bool areAllGpuDevicesFromAmd(const gmx_gpu_info_t &OPENCL_FUNC_ARGUMENT(gpuInfo))
227 OPENCL_FUNC_TERM_WITH_RETURN(false)
228
229 /*! \brief Returns the size of the gpu_dev_info struct.
230  *
231  * The size of gpu_dev_info can be used for allocation and communication.
232  *
233  * \returns                 size in bytes of gpu_dev_info
234  */
235 GPU_FUNC_QUALIFIER
236 size_t sizeof_gpu_dev_info() GPU_FUNC_TERM_WITH_RETURN(0)
237
238 //! Get status of device with specified index
239 int gpu_info_get_stat(const gmx_gpu_info_t &info, int index);
240
241 /*! \brief Check if GROMACS has been built with GPU support.
242  *
243  * \param[in] error Pointer to error string or nullptr.
244  * \todo Move this to NB module once it exists.
245  */
246 bool buildSupportsNonbondedOnGpu(std::string *error);
247
248 /*! \brief Starts the GPU profiler if mdrun is being profiled.
249  *
250  *  When a profiler run is in progress (based on the presence of the NVPROF_ID
251  *  env. var.), the profiler is started to begin collecting data during the
252  *  rest of the run (or until stopGpuProfiler is called).
253  *
254  *  Note that this is implemented only for the CUDA API.
255  */
256 CUDA_FUNC_QUALIFIER
257 void startGpuProfiler(void) CUDA_FUNC_TERM
258
259
260 /*! \brief Resets the GPU profiler if mdrun is being profiled.
261  *
262  * When a profiler run is in progress (based on the presence of the NVPROF_ID
263  * env. var.), the profiler data is restet in order to eliminate the data collected
264  * from the preceding part fo the run.
265  *
266  * This function should typically be called at the mdrun counter reset time.
267  *
268  * Note that this is implemented only for the CUDA API.
269  */
270 CUDA_FUNC_QUALIFIER
271 void resetGpuProfiler(void) CUDA_FUNC_TERM
272
273
274 /*! \brief Stops the CUDA profiler if mdrun is being profiled.
275  *
276  *  This function can be called at cleanup when skipping recording
277  *  recording subsequent API calls from being traces/profiled is desired,
278  *  e.g. before uninitialization.
279  *
280  *  Note that this is implemented only for the CUDA API.
281  */
282 CUDA_FUNC_QUALIFIER
283 void stopGpuProfiler(void) CUDA_FUNC_TERM
284
285 //! Tells whether the host buffer was pinned for non-blocking transfers. Only implemented for CUDA.
286 CUDA_FUNC_QUALIFIER
287 bool isHostMemoryPinned(const void *CUDA_FUNC_ARGUMENT(h_ptr)) CUDA_FUNC_TERM_WITH_RETURN(false)
288
289 #endif