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