Minor change to GPU compatibility reporting handling
[gromacs.git] / src / gromacs / gpu_utils / gpu_utils.h
blob86001cbdf1dfca93ae851bcf9d352b493dd19bc2
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 /*! \libinternal \file
38 * \brief Declare functions for detection and initialization for GPU devices.
40 * \author Szilard Pall <pall.szilard@gmail.com>
41 * \author Mark Abraham <mark.j.abraham@gmail.com>
43 * \inlibraryapi
45 #ifndef GMX_GPU_UTILS_GPU_UTILS_H
46 #define GMX_GPU_UTILS_GPU_UTILS_H
48 #include <cstdio>
50 #include "gromacs/gpu_utils/gpu_macros.h"
51 #include "gromacs/utility/basedefinitions.h"
53 struct gmx_gpu_info_t;
54 struct gmx_gpu_opt_t;
56 namespace gmx
58 class MDLogger;
61 /*! \brief Detect all GPUs in the system.
63 * Will detect every GPU supported by the device driver in use. Also
64 * check for the compatibility of each and fill the gpu_info->gpu_dev array
65 * with the required information on each the device: ID, device properties,
66 * status.
68 * \param[in] gpu_info pointer to structure holding GPU information.
69 * \param[out] err_str The error message of any GPU API error that caused
70 * the detection to fail (if there was any). The memory
71 * the pointer points to should be managed externally.
72 * \returns non-zero if the detection encountered a failure, zero otherwise.
74 GPU_FUNC_QUALIFIER
75 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)
77 /*! \brief Return whether the GPU with given \c index is compatible, ie suitable for use.
79 * \param[in] gpu_info pointer to structure holding GPU information
80 * \param[in] index index of GPU to ask about
81 * \returns Whether the GPU is compatible.
83 GPU_FUNC_QUALIFIER
84 bool isGpuCompatible(const gmx_gpu_info_t *GPU_FUNC_ARGUMENT(gpu_info),
85 int GPU_FUNC_ARGUMENT(index)) GPU_FUNC_TERM_WITH_RETURN(false)
87 /*! \brief Return a string describing how compatible the GPU with given \c index is.
89 * \param[in] gpu_info pointer to structure holding GPU information
90 * \param[in] index index of GPU to ask about
91 * \returns A null-terminated C string describing the compatibility status, useful for error messages.
93 GPU_FUNC_QUALIFIER
94 const char *getGpuCompatibilityDescription(const gmx_gpu_info_t *GPU_FUNC_ARGUMENT(gpu_info),
95 int GPU_FUNC_ARGUMENT(index)) GPU_FUNC_TERM_WITH_RETURN("")
97 /*! \brief Frees the gpu_dev and dev_use array fields of \p gpu_info.
99 * \param[in] gpu_info pointer to structure holding GPU information
101 GPU_FUNC_QUALIFIER
102 void free_gpu_info(const struct gmx_gpu_info_t *GPU_FUNC_ARGUMENT(gpu_info)) GPU_FUNC_TERM
104 /*! \brief Initializes the GPU with the given index.
106 * The varible \p mygpu is the index of the GPU to initialize in the
107 * gpu_info.gpu_dev array.
109 * \param mdlog log file to write to
110 * \param[in] mygpu index of the GPU to initialize
111 * \param[out] result_str the message related to the error that occurred
112 * during the initialization (if there was any).
113 * \param[in] gpu_info GPU info of all detected devices in the system.
114 * \param[in] gpu_opt options for using the GPUs in gpu_info
115 * \returns true if no error occurs during initialization.
117 GPU_FUNC_QUALIFIER
118 gmx_bool init_gpu(const gmx::MDLogger &GPU_FUNC_ARGUMENT(mdlog),
119 int GPU_FUNC_ARGUMENT(mygpu),
120 char *GPU_FUNC_ARGUMENT(result_str),
121 const struct gmx_gpu_info_t *GPU_FUNC_ARGUMENT(gpu_info),
122 const gmx_gpu_opt_t *GPU_FUNC_ARGUMENT(gpu_opt)) GPU_FUNC_TERM_WITH_RETURN(-1)
124 /*! \brief Frees up the CUDA GPU used by the active context at the time of calling.
126 * The context is explicitly destroyed and therefore all data uploaded to the GPU
127 * is lost. This should only be called when none of this data is required anymore.
129 * \param[in] mygpu index of the GPU clean up for
130 * \param[out] result_str the message related to the error that occurred
131 * during the initialization (if there was any).
132 * \param[in] gpu_info GPU info of all detected devices in the system.
133 * \param[in] gpu_opt options for using the GPUs in gpu_info
134 * \returns true if no error occurs during the freeing.
136 CUDA_FUNC_QUALIFIER
137 gmx_bool free_cuda_gpu(int CUDA_FUNC_ARGUMENT(mygpu),
138 char *CUDA_FUNC_ARGUMENT(result_str),
139 const gmx_gpu_info_t *CUDA_FUNC_ARGUMENT(gpu_info),
140 const gmx_gpu_opt_t *CUDA_FUNC_ARGUMENT(gpu_opt)) CUDA_FUNC_TERM_WITH_RETURN(TRUE)
142 /*! \brief Returns the device ID of the CUDA GPU currently in use.
144 * The GPU used is the one that is active at the time of the call in the active context.
146 * \returns device ID of the GPU in use at the time of the call
148 CUDA_FUNC_QUALIFIER
149 int get_current_cuda_gpu_device_id(void) CUDA_FUNC_TERM_WITH_RETURN(-1)
151 /*! \brief Returns an identifier for the GPU with a given index into the array of used GPUs.
153 * Getter function which, given an index into the array of GPUs in use
154 * (dev_use) -- typically an MPI rank --, returns an identifier of the
155 * respective GPU.
157 * \param[in] gpu_info Pointer to structure holding GPU information
158 * \param[in] gpu_opt Pointer to structure holding GPU options
159 * \param[in] idx Index into the array of used GPUs
160 * \returns device ID of the requested GPU
162 GPU_FUNC_QUALIFIER
163 int get_gpu_device_id(const struct gmx_gpu_info_t *GPU_FUNC_ARGUMENT(gpu_info),
164 const gmx_gpu_opt_t *GPU_FUNC_ARGUMENT(gpu_opt),
165 int GPU_FUNC_ARGUMENT(idx)) GPU_FUNC_TERM_WITH_RETURN(-1)
167 /*! \brief Returns the name for the OpenCL GPU with a given index into the array of used GPUs.
169 * Getter function which, given an index into the array of GPUs in use
170 * (dev_use) -- typically a tMPI/MPI rank --, returns the device name for the
171 * respective OpenCL GPU.
173 * \param[in] gpu_info Pointer to structure holding GPU information
174 * \param[in] gpu_opt Pointer to structure holding GPU options
175 * \param[in] idx Index into the array of used GPUs
176 * \returns A string with the name of the requested OpenCL GPU
178 OPENCL_FUNC_QUALIFIER
179 char* get_ocl_gpu_device_name(const struct gmx_gpu_info_t *OPENCL_FUNC_ARGUMENT(gpu_info),
180 const gmx_gpu_opt_t *OPENCL_FUNC_ARGUMENT(gpu_opt),
181 int OPENCL_FUNC_ARGUMENT(idx)) OPENCL_FUNC_TERM_WITH_RETURN(NULL)
183 /*! \brief Formats and returns a device information string for a given GPU.
185 * Given an index *directly* into the array of available GPUs (gpu_dev)
186 * returns a formatted info string for the respective GPU which includes
187 * ID, name, compute capability, and detection status.
189 * \param[out] s pointer to output string (has to be allocated externally)
190 * \param[in] gpu_info pointer to structure holding GPU information
191 * \param[in] index an index *directly* into the array of available GPUs
193 GPU_FUNC_QUALIFIER
194 void get_gpu_device_info_string(char *GPU_FUNC_ARGUMENT(s),
195 const struct gmx_gpu_info_t *GPU_FUNC_ARGUMENT(gpu_info),
196 int GPU_FUNC_ARGUMENT(index)) GPU_FUNC_TERM
198 /*! \brief Returns the size of the gpu_dev_info struct.
200 * The size of gpu_dev_info can be used for allocation and communication.
202 * \returns size in bytes of gpu_dev_info
204 GPU_FUNC_QUALIFIER
205 size_t sizeof_gpu_dev_info(void) GPU_FUNC_TERM_WITH_RETURN(0)
207 /*! \brief Returns a pointer *ptr to page-locked memory of size nbytes.
209 * The allocated memory is suitable to be used for data transfers between host
210 * and GPU.
211 * Error handling should be done within this function.
213 typedef void gmx_host_alloc_t (void **ptr, size_t nbytes);
215 /*! \brief Frees page-locked memory pointed to by *ptr.
217 * NULL should not be passed to this function.
219 typedef void gmx_host_free_t (void *ptr);
221 /*! \brief Set page-locked memory allocation functions used by the GPU host. */
222 void gpu_set_host_malloc_and_free(bool bUseGpuKernels,
223 gmx_host_alloc_t **nb_alloc,
224 gmx_host_free_t **nb_free);
228 /*! \brief Starts the GPU profiler if mdrun is being profiled.
230 * When a profiler run is in progress (based on the presence of the NVPROF_ID
231 * env. var.), the profiler is started to begin collecting data during the
232 * rest of the run (or until stopGpuProfiler is called).
234 * Note that this is implemented only for the CUDA API.
236 CUDA_FUNC_QUALIFIER
237 void startGpuProfiler(void) CUDA_FUNC_TERM
240 /*! \brief Resets the GPU profiler if mdrun is being profiled.
242 * When a profiler run is in progress (based on the presence of the NVPROF_ID
243 * env. var.), the profiler data is restet in order to eliminate the data collected
244 * from the preceding part fo the run.
246 * This function should typically be called at the mdrun counter reset time.
248 * Note that this is implemented only for the CUDA API.
250 CUDA_FUNC_QUALIFIER
251 void resetGpuProfiler(void) CUDA_FUNC_TERM
254 /*! \brief Stops the CUDA profiler if mdrun is being profiled.
256 * This function can be called at cleanup when skipping recording
257 * recording subsequent API calls from being traces/profiled is desired,
258 * e.g. before uninitialization.
260 * Note that this is implemented only for the CUDA API.
262 CUDA_FUNC_QUALIFIER
263 void stopGpuProfiler(void) CUDA_FUNC_TERM
266 #endif