ad2297eae05f8cd33c4d86433aa22321e749e5cd
[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 /*! \brief Detect all GPUs in the system.
71  *
72  *  Will detect every GPU supported by the device driver in use. Also
73  *  check for the compatibility of each and fill the gpu_info->gpu_dev array
74  *  with the required information on each the device: ID, device properties,
75  *  status.
76  *
77  *  \param[in] gpu_info    pointer to structure holding GPU information.
78  *  \param[out] err_str    The error message of any GPU API error that caused
79  *                         the detection to fail (if there was any). The memory
80  *                         the pointer points to should be managed externally.
81  *  \returns               non-zero if the detection encountered a failure, zero otherwise.
82  */
83 GPU_FUNC_QUALIFIER
84 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)
85
86 /*! \brief Return a container of the detected GPUs that are compatible.
87  *
88  * This function filters the result of the detection for compatible
89  * GPUs, based on the previously run compatibility tests.
90  *
91  * \param[in]     gpu_info    Information detected about GPUs, including compatibility.
92  * \return                    vector of IDs of GPUs already recorded as compatible */
93 std::vector<int> getCompatibleGpus(const gmx_gpu_info_t &gpu_info);
94
95 /*! \brief Return a string describing how compatible the GPU with given \c index is.
96  *
97  * \param[in]   gpu_info    Information about detected GPUs
98  * \param[in]   index       index of GPU to ask about
99  * \returns                 A null-terminated C string describing the compatibility status, useful for error messages.
100  */
101 const char *getGpuCompatibilityDescription(const gmx_gpu_info_t &GPU_FUNC_ARGUMENT(gpu_info),
102                                            int GPU_FUNC_ARGUMENT(index));
103
104 /*! \brief Frees the gpu_dev and dev_use array fields of \p gpu_info.
105  *
106  * \param[in]    gpu_info    pointer to structure holding GPU information
107  */
108 GPU_FUNC_QUALIFIER
109 void free_gpu_info(const struct gmx_gpu_info_t *GPU_FUNC_ARGUMENT(gpu_info)) GPU_FUNC_TERM
110
111 /*! \brief Initializes the GPU described by \c deviceInfo.
112  *
113  * TODO Doxygen complains about these - probably a Doxygen bug, since
114  * the patterns here are the same as elsewhere in this header.
115  *
116  *  param[in]    mdlog        log file to write to
117  *  param[in]    rank         MPI rank of this process (for error output)
118  * \param[inout] deviceInfo   device info of the GPU to initialize
119  *
120  * Issues a fatal error for any critical errors that occur during
121  * initialization.
122  */
123 GPU_FUNC_QUALIFIER
124 void init_gpu(const gmx::MDLogger &GPU_FUNC_ARGUMENT(mdlog),
125               int GPU_FUNC_ARGUMENT(rank),
126               gmx_device_info_t *GPU_FUNC_ARGUMENT(deviceInfo)) GPU_FUNC_TERM
127
128 /*! \brief Frees up the CUDA GPU used by the active context at the time of calling.
129  *
130  * The context is explicitly destroyed and therefore all data uploaded to the GPU
131  * is lost. This should only be called when none of this data is required anymore.
132  *
133  * \param[in]  deviceInfo   device info of the GPU to clean up for
134  * \param[out] result_str   the message related to the error that occurred
135  *                          during the initialization (if there was any).
136  *
137  * \returns                 true if no error occurs during the freeing.
138  */
139 CUDA_FUNC_QUALIFIER
140 gmx_bool free_cuda_gpu(const gmx_device_info_t *CUDA_FUNC_ARGUMENT(deviceInfo),
141                        char *CUDA_FUNC_ARGUMENT(result_str)) CUDA_FUNC_TERM_WITH_RETURN(TRUE)
142
143 /*! \brief Return a pointer to the device info for \c deviceId
144  *
145  * \param[in] gpu_info      GPU info of all detected devices in the system.
146  * \param[in] deviceId      ID for the GPU device requested.
147  *
148  * \returns                 Pointer to the device info for \c deviceId.
149  */
150 GPU_FUNC_QUALIFIER
151 gmx_device_info_t *getDeviceInfo(const gmx_gpu_info_t &GPU_FUNC_ARGUMENT(gpu_info),
152                                  int GPU_FUNC_ARGUMENT(deviceId)) GPU_FUNC_TERM_WITH_RETURN(NULL)
153
154 /*! \brief Returns the device ID of the CUDA GPU currently in use.
155  *
156  * The GPU used is the one that is active at the time of the call in the active context.
157  *
158  * \returns                 device ID of the GPU in use at the time of the call
159  */
160 CUDA_FUNC_QUALIFIER
161 int get_current_cuda_gpu_device_id(void) CUDA_FUNC_TERM_WITH_RETURN(-1)
162
163 /*! \brief Formats and returns a device information string for a given GPU.
164  *
165  * Given an index *directly* into the array of available GPUs (gpu_dev)
166  * returns a formatted info string for the respective GPU which includes
167  * ID, name, compute capability, and detection status.
168  *
169  * \param[out]  s           pointer to output string (has to be allocated externally)
170  * \param[in]   gpu_info    Information about detected GPUs
171  * \param[in]   index       an index *directly* into the array of available GPUs
172  */
173 GPU_FUNC_QUALIFIER
174 void get_gpu_device_info_string(char *GPU_FUNC_ARGUMENT(s),
175                                 const struct gmx_gpu_info_t &GPU_FUNC_ARGUMENT(gpu_info),
176                                 int GPU_FUNC_ARGUMENT(index)) GPU_FUNC_TERM
177
178 /*! \brief Returns the size of the gpu_dev_info struct.
179  *
180  * The size of gpu_dev_info can be used for allocation and communication.
181  *
182  * \returns                 size in bytes of gpu_dev_info
183  */
184 GPU_FUNC_QUALIFIER
185 size_t sizeof_gpu_dev_info(void) GPU_FUNC_TERM_WITH_RETURN(0)
186
187 /*! \brief Returns a pointer *ptr to page-locked memory of size nbytes.
188  *
189  * The allocated memory is suitable to be used for data transfers between host
190  * and GPU.
191  * Error handling should be done within this function.
192  */
193 typedef void gmx_host_alloc_t (void **ptr, size_t nbytes);
194
195 /*! \brief Frees page-locked memory pointed to by *ptr.
196  *
197  * NULL should not be passed to this function.
198  */
199 typedef void gmx_host_free_t (void *ptr);
200
201 /*! \brief Set page-locked memory allocation functions used by the GPU host. */
202 void gpu_set_host_malloc_and_free(bool               bUseGpuKernels,
203                                   gmx_host_alloc_t **nb_alloc,
204                                   gmx_host_free_t  **nb_free);
205
206
207
208 /*! \brief Starts the GPU profiler if mdrun is being profiled.
209  *
210  *  When a profiler run is in progress (based on the presence of the NVPROF_ID
211  *  env. var.), the profiler is started to begin collecting data during the
212  *  rest of the run (or until stopGpuProfiler is called).
213  *
214  *  Note that this is implemented only for the CUDA API.
215  */
216 CUDA_FUNC_QUALIFIER
217 void startGpuProfiler(void) CUDA_FUNC_TERM
218
219
220 /*! \brief Resets the GPU profiler if mdrun is being profiled.
221  *
222  * When a profiler run is in progress (based on the presence of the NVPROF_ID
223  * env. var.), the profiler data is restet in order to eliminate the data collected
224  * from the preceding part fo the run.
225  *
226  * This function should typically be called at the mdrun counter reset time.
227  *
228  * Note that this is implemented only for the CUDA API.
229  */
230 CUDA_FUNC_QUALIFIER
231 void resetGpuProfiler(void) CUDA_FUNC_TERM
232
233
234 /*! \brief Stops the CUDA profiler if mdrun is being profiled.
235  *
236  *  This function can be called at cleanup when skipping recording
237  *  recording subsequent API calls from being traces/profiled is desired,
238  *  e.g. before uninitialization.
239  *
240  *  Note that this is implemented only for the CUDA API.
241  */
242 CUDA_FUNC_QUALIFIER
243 void stopGpuProfiler(void) CUDA_FUNC_TERM
244
245
246 #endif