Merge branch release-2016 into release-2018
[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, 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 <vector>
51
52 #include "gromacs/gpu_utils/gpu_macros.h"
53 #include "gromacs/utility/basedefinitions.h"
54
55 struct gmx_device_info_t;
56 struct gmx_gpu_info_t;
57
58 namespace gmx
59 {
60 class MDLogger;
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, and a valid device driver or ICD was detected by the GPU
81  * runtime.
82  *
83  * Does not throw. */
84 GPU_FUNC_QUALIFIER
85 bool canDetectGpus() GPU_FUNC_TERM_WITH_RETURN(false);
86
87 /*! \brief Detect all GPUs in the system.
88  *
89  *  Will detect every GPU supported by the device driver in use. If
90  *  the device driver is missing or unsuitable, returns the same error
91  *  as for "no valid devices detected," so generally calling code
92  *  should have checked the return value from canDetectGpus() first,
93  *  in order to understand the behaviour of this routine. This routine
94  *  also checks for the compatibility of each and fill the
95  *  gpu_info->gpu_dev array with the required information on each the
96  *  device: ID, device properties, status.
97  *
98  *  \param[in] gpu_info    pointer to structure holding GPU information.
99  *  \param[out] err_str    The error message of any GPU API error that caused
100  *                         the detection to fail (if there was any). The memory
101  *                         the pointer points to should be managed externally.
102  *  \returns               non-zero if the detection encountered a failure, zero otherwise.
103  */
104 GPU_FUNC_QUALIFIER
105 int detect_gpus(struct gmx_gpu_info_t *GPU_FUNC_ARGUMENT(gpu_info), char *GPU_FUNC_ARGUMENT(err_str)) GPU_FUNC_TERM_WITH_RETURN(-1)
106
107 /*! \brief Return a container of the detected GPUs that are compatible.
108  *
109  * This function filters the result of the detection for compatible
110  * GPUs, based on the previously run compatibility tests.
111  *
112  * \param[in]     gpu_info    Information detected about GPUs, including compatibility.
113  * \return                    vector of IDs of GPUs already recorded as compatible */
114 std::vector<int> getCompatibleGpus(const gmx_gpu_info_t &gpu_info);
115
116 /*! \brief Return a string describing how compatible the GPU with given \c index is.
117  *
118  * \param[in]   gpu_info    Information about detected GPUs
119  * \param[in]   index       index of GPU to ask about
120  * \returns                 A null-terminated C string describing the compatibility status, useful for error messages.
121  */
122 const char *getGpuCompatibilityDescription(const gmx_gpu_info_t &GPU_FUNC_ARGUMENT(gpu_info),
123                                            int GPU_FUNC_ARGUMENT(index));
124
125 /*! \brief Frees the gpu_dev and dev_use array fields of \p gpu_info.
126  *
127  * \param[in]    gpu_info    pointer to structure holding GPU information
128  */
129 GPU_FUNC_QUALIFIER
130 void free_gpu_info(const struct gmx_gpu_info_t *GPU_FUNC_ARGUMENT(gpu_info)) GPU_FUNC_TERM
131
132 /*! \brief Initializes the GPU described by \c deviceInfo.
133  *
134  * TODO Doxygen complains about these - probably a Doxygen bug, since
135  * the patterns here are the same as elsewhere in this header.
136  *
137  *  param[in]    mdlog        log file to write to
138  * \param[inout] deviceInfo   device info of the GPU to initialize
139  *
140  * Issues a fatal error for any critical errors that occur during
141  * initialization.
142  */
143 GPU_FUNC_QUALIFIER
144 void init_gpu(const gmx::MDLogger &GPU_FUNC_ARGUMENT(mdlog),
145               gmx_device_info_t *GPU_FUNC_ARGUMENT(deviceInfo)) GPU_FUNC_TERM
146
147 /*! \brief Frees up the CUDA GPU used by the active context at the time of calling.
148  *
149  * If \c deviceInfo is nullptr, then it is understood that no device
150  * was selected so no context is active to be freed. Otherwise, the
151  * context is explicitly destroyed and therefore all data uploaded to
152  * the GPU is lost. This must only be called when none of this data is
153  * required anymore, because subsequent attempts to free memory
154  * associated with the context will otherwise fail.
155  *
156  * Calls gmx_warning upon errors.
157  *
158  * \param[in]  deviceInfo   device info of the GPU to clean up for
159  *
160  * \returns                 true if no error occurs during the freeing.
161  */
162 CUDA_FUNC_QUALIFIER
163 void free_gpu(const gmx_device_info_t *CUDA_FUNC_ARGUMENT(deviceInfo)) CUDA_FUNC_TERM
164
165 /*! \brief Return a pointer to the device info for \c deviceId
166  *
167  * \param[in] gpu_info      GPU info of all detected devices in the system.
168  * \param[in] deviceId      ID for the GPU device requested.
169  *
170  * \returns                 Pointer to the device info for \c deviceId.
171  */
172 GPU_FUNC_QUALIFIER
173 gmx_device_info_t *getDeviceInfo(const gmx_gpu_info_t &GPU_FUNC_ARGUMENT(gpu_info),
174                                  int GPU_FUNC_ARGUMENT(deviceId)) GPU_FUNC_TERM_WITH_RETURN(NULL)
175
176 /*! \brief Returns the device ID of the CUDA GPU currently in use.
177  *
178  * The GPU used is the one that is active at the time of the call in the active context.
179  *
180  * \returns                 device ID of the GPU in use at the time of the call
181  */
182 CUDA_FUNC_QUALIFIER
183 int get_current_cuda_gpu_device_id(void) CUDA_FUNC_TERM_WITH_RETURN(-1)
184
185 /*! \brief Formats and returns a device information string for a given GPU.
186  *
187  * Given an index *directly* into the array of available GPUs (gpu_dev)
188  * returns a formatted info string for the respective GPU which includes
189  * ID, name, compute capability, and detection status.
190  *
191  * \param[out]  s           pointer to output string (has to be allocated externally)
192  * \param[in]   gpu_info    Information about detected GPUs
193  * \param[in]   index       an index *directly* into the array of available GPUs
194  */
195 GPU_FUNC_QUALIFIER
196 void get_gpu_device_info_string(char *GPU_FUNC_ARGUMENT(s),
197                                 const struct gmx_gpu_info_t &GPU_FUNC_ARGUMENT(gpu_info),
198                                 int GPU_FUNC_ARGUMENT(index)) GPU_FUNC_TERM
199
200 /*! \brief Returns the size of the gpu_dev_info struct.
201  *
202  * The size of gpu_dev_info can be used for allocation and communication.
203  *
204  * \returns                 size in bytes of gpu_dev_info
205  */
206 GPU_FUNC_QUALIFIER
207 size_t sizeof_gpu_dev_info(void) GPU_FUNC_TERM_WITH_RETURN(0)
208
209 /*! \brief Returns a pointer *ptr to page-locked memory of size nbytes.
210  *
211  * The allocated memory is suitable to be used for data transfers between host
212  * and GPU.
213  * Error handling should be done within this function.
214  */
215 typedef void gmx_host_alloc_t (void **ptr, size_t nbytes);
216
217 /*! \brief Frees page-locked memory pointed to by *ptr.
218  *
219  * NULL should not be passed to this function.
220  */
221 typedef void gmx_host_free_t (void *ptr);
222
223 /*! \brief Set page-locked memory allocation functions used by the GPU host. */
224 void gpu_set_host_malloc_and_free(bool               bUseGpuKernels,
225                                   gmx_host_alloc_t **nb_alloc,
226                                   gmx_host_free_t  **nb_free);
227
228
229
230 /*! \brief Starts the GPU profiler if mdrun is being profiled.
231  *
232  *  When a profiler run is in progress (based on the presence of the NVPROF_ID
233  *  env. var.), the profiler is started to begin collecting data during the
234  *  rest of the run (or until stopGpuProfiler is called).
235  *
236  *  Note that this is implemented only for the CUDA API.
237  */
238 CUDA_FUNC_QUALIFIER
239 void startGpuProfiler(void) CUDA_FUNC_TERM
240
241
242 /*! \brief Resets the GPU profiler if mdrun is being profiled.
243  *
244  * When a profiler run is in progress (based on the presence of the NVPROF_ID
245  * env. var.), the profiler data is restet in order to eliminate the data collected
246  * from the preceding part fo the run.
247  *
248  * This function should typically be called at the mdrun counter reset time.
249  *
250  * Note that this is implemented only for the CUDA API.
251  */
252 CUDA_FUNC_QUALIFIER
253 void resetGpuProfiler(void) CUDA_FUNC_TERM
254
255
256 /*! \brief Stops the CUDA profiler if mdrun is being profiled.
257  *
258  *  This function can be called at cleanup when skipping recording
259  *  recording subsequent API calls from being traces/profiled is desired,
260  *  e.g. before uninitialization.
261  *
262  *  Note that this is implemented only for the CUDA API.
263  */
264 CUDA_FUNC_QUALIFIER
265 void stopGpuProfiler(void) CUDA_FUNC_TERM
266
267 //! Tells whether the host buffer was pinned for non-blocking transfers. Only implemented for CUDA.
268 CUDA_FUNC_QUALIFIER
269 bool isHostMemoryPinned(void *CUDA_FUNC_ARGUMENT(h_ptr)) CUDA_FUNC_TERM_WITH_RETURN(false)
270
271 #endif