Merge branch release-2021
[alexxy/gromacs.git] / src / gromacs / gpu_utils / cudautils.cuh
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2014,2015,2016,2017 by the GROMACS development team.
5  * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 #ifndef GMX_GPU_UTILS_CUDAUTILS_CUH
37 #define GMX_GPU_UTILS_CUDAUTILS_CUH
38
39 #include <stdio.h>
40
41 #include <array>
42 #include <string>
43
44 #include "gromacs/gpu_utils/device_stream.h"
45 #include "gromacs/gpu_utils/gputraits.cuh"
46 #include "gromacs/math/vec.h"
47 #include "gromacs/math/vectypes.h"
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/fatalerror.h"
50 #include "gromacs/utility/gmxassert.h"
51 #include "gromacs/utility/stringutil.h"
52
53 namespace gmx
54 {
55 namespace
56 {
57
58 /*! \brief Add the API information on the specific error to the error message.
59  *
60  * \param[in]  deviceError  The error to assert cudaSuccess on.
61  *
62  * \returns A description of the API error. Returns '(CUDA error #0 (cudaSuccess): no error)' in case deviceError is cudaSuccess.
63  */
64 static inline std::string getDeviceErrorString(const cudaError_t deviceError)
65 {
66     return formatString("CUDA error #%d (%s): %s.",
67                         deviceError,
68                         cudaGetErrorName(deviceError),
69                         cudaGetErrorString(deviceError));
70 }
71
72 /*! \brief Check if API returned an error and throw an exception with information on it.
73  *
74  * \param[in]  deviceError  The error to assert cudaSuccess on.
75  * \param[in]  errorMessage  Undecorated error message.
76  *
77  *  \throws InternalError if deviceError is not a success.
78  */
79 static inline void checkDeviceError(const cudaError_t deviceError, const std::string& errorMessage)
80 {
81     if (deviceError != cudaSuccess)
82     {
83         GMX_THROW(gmx::InternalError(errorMessage + " " + getDeviceErrorString(deviceError)));
84     }
85 }
86
87 /*! \brief Helper function to ensure no pending error silently
88  * disrupts error handling.
89  *
90  * Asserts in a debug build if an unhandled error is present. Issues a
91  * warning at run time otherwise.
92  *
93  * \param[in]  errorMessage  Undecorated error message.
94  */
95 static inline void ensureNoPendingDeviceError(const std::string& errorMessage)
96 {
97     // Ensure there is no pending error that would otherwise affect
98     // the behaviour of future error handling.
99     cudaError_t deviceError = cudaGetLastError();
100     if (deviceError == cudaSuccess)
101     {
102         return;
103     }
104
105     // If we would find an error in a release build, we do not know
106     // what is appropriate to do about it, so assert only for debug
107     // builds.
108     const std::string fullErrorMessage =
109             errorMessage + " An unhandled error from a previous CUDA operation was detected. "
110             + gmx::getDeviceErrorString(deviceError);
111     GMX_ASSERT(deviceError == cudaSuccess, fullErrorMessage.c_str());
112     // TODO When we evolve a better logging framework, use that
113     // for release-build error reporting.
114     gmx_warning("%s", fullErrorMessage.c_str());
115 }
116
117 } // namespace
118 } // namespace gmx
119
120 enum class GpuApiCallBehavior;
121
122 /* TODO error checking needs to be rewritten. We have 2 types of error checks needed
123    based on where they occur in the code:
124    - non performance-critical: these errors are unsafe to be ignored and must be
125      _always_ checked for, e.g. initializations
126    - performance critical: handling errors might hurt performance so care need to be taken
127      when/if we should check for them at all, e.g. in cu_upload_X. However, we should be
128      able to turn the check for these errors on!
129
130    Probably we'll need two sets of the macros below...
131
132  */
133 #define CHECK_CUDA_ERRORS
134
135 #ifdef CHECK_CUDA_ERRORS
136
137 /*! Check for CUDA error on the return status of a CUDA RT API call. */
138 #    define CU_RET_ERR(deviceError, msg)                                                          \
139         do                                                                                        \
140         {                                                                                         \
141             if (deviceError != cudaSuccess)                                                       \
142             {                                                                                     \
143                 gmx_fatal(FARGS, "%s\n", (msg + gmx::getDeviceErrorString(deviceError)).c_str()); \
144             }                                                                                     \
145         } while (0)
146
147 #else /* CHECK_CUDA_ERRORS */
148
149 #    define CU_RET_ERR(status, msg) \
150         do                          \
151         {                           \
152         } while (0)
153
154 #endif /* CHECK_CUDA_ERRORS */
155
156 // TODO: the 2 functions below are pretty much a constructor/destructor of a simple
157 // GPU table object. There is also almost self-contained fetchFromParamLookupTable()
158 // in cuda_kernel_utils.cuh. They could all live in a separate class/struct file.
159
160 /*! \brief Add a triplets stored in a float3 to an rvec variable.
161  *
162  * \param[out]  a Rvec to increment
163  * \param[in]   b Float triplet to increment with.
164  */
165 static inline void rvec_inc(rvec a, const float3 b)
166 {
167     rvec tmp = { b.x, b.y, b.z };
168     rvec_inc(a, tmp);
169 }
170
171 /*! \brief  Returns true if all tasks in \p s have completed.
172  *
173  *  \param[in] deviceStream CUDA stream to check.
174  *
175  *  \returns True if all tasks enqueued in the stream \p deviceStream (at the time of this call) have completed.
176  */
177 static inline bool haveStreamTasksCompleted(const DeviceStream& deviceStream)
178 {
179     cudaError_t stat = cudaStreamQuery(deviceStream.stream());
180
181     if (stat == cudaErrorNotReady)
182     {
183         // work is still in progress in the stream
184         return false;
185     }
186
187     GMX_ASSERT(stat != cudaErrorInvalidResourceHandle,
188                ("Stream identifier not valid. " + gmx::getDeviceErrorString(stat)).c_str());
189
190     // cudaSuccess and cudaErrorNotReady are the expected return values
191     CU_RET_ERR(stat, "Unexpected cudaStreamQuery failure. ");
192
193     GMX_ASSERT(stat == cudaSuccess,
194                ("Values other than cudaSuccess should have been explicitly handled. "
195                 + gmx::getDeviceErrorString(stat))
196                        .c_str());
197
198     return true;
199 }
200
201 /* Kernel launch helpers */
202
203 /*! \brief
204  * A function for setting up a single CUDA kernel argument.
205  * This is the tail of the compile-time recursive function below.
206  * It has to be seen by the compiler first.
207  *
208  * \tparam        totalArgsCount  Number of the kernel arguments
209  * \tparam        KernelPtr       Kernel function handle type
210  * \param[in]     argIndex        Index of the current argument
211  */
212 template<size_t totalArgsCount, typename KernelPtr>
213 void prepareGpuKernelArgument(KernelPtr /*kernel*/,
214                               std::array<void*, totalArgsCount>* /* kernelArgsPtr */,
215                               size_t gmx_used_in_debug argIndex)
216 {
217     GMX_ASSERT(argIndex == totalArgsCount, "Tail expansion");
218 }
219
220 /*! \brief
221  * Compile-time recursive function for setting up a single CUDA kernel argument.
222  * This function copies a kernel argument pointer \p argPtr into \p kernelArgsPtr,
223  * and calls itself on the next argument, eventually calling the tail function above.
224  *
225  * \tparam        CurrentArg      Type of the current argument
226  * \tparam        RemainingArgs   Types of remaining arguments after the current one
227  * \tparam        totalArgsCount  Number of the kernel arguments
228  * \tparam        KernelPtr       Kernel function handle type
229  * \param[in]     kernel          Kernel function handle
230  * \param[in,out] kernelArgsPtr   Pointer to the argument array to be filled in
231  * \param[in]     argIndex        Index of the current argument
232  * \param[in]     argPtr          Pointer to the current argument
233  * \param[in]     otherArgsPtrs   Pack of pointers to arguments remaining to process after the current one
234  */
235 template<typename CurrentArg, typename... RemainingArgs, size_t totalArgsCount, typename KernelPtr>
236 void prepareGpuKernelArgument(KernelPtr                          kernel,
237                               std::array<void*, totalArgsCount>* kernelArgsPtr,
238                               size_t                             argIndex,
239                               const CurrentArg*                  argPtr,
240                               const RemainingArgs*... otherArgsPtrs)
241 {
242     (*kernelArgsPtr)[argIndex] = (void*)argPtr;
243     prepareGpuKernelArgument(kernel, kernelArgsPtr, argIndex + 1, otherArgsPtrs...);
244 }
245
246 /*! \brief
247  * A wrapper function for setting up all the CUDA kernel arguments.
248  * Calls the recursive functions above.
249  *
250  * \tparam    KernelPtr       Kernel function handle type
251  * \tparam    Args            Types of all the kernel arguments
252  * \param[in] kernel          Kernel function handle
253  * \param[in] argsPtrs        Pointers to all the kernel arguments
254  * \returns A prepared parameter pack to be used with launchGpuKernel() as the last argument.
255  */
256 template<typename KernelPtr, typename... Args>
257 std::array<void*, sizeof...(Args)> prepareGpuKernelArguments(KernelPtr kernel,
258                                                              const KernelLaunchConfig& /*config */,
259                                                              const Args*... argsPtrs)
260 {
261     std::array<void*, sizeof...(Args)> kernelArgs;
262     prepareGpuKernelArgument(kernel, &kernelArgs, 0, argsPtrs...);
263     return kernelArgs;
264 }
265
266 /*! \brief Launches the CUDA kernel and handles the errors.
267  *
268  * \tparam    Args            Types of all the kernel arguments
269  * \param[in] kernel          Kernel function handle
270  * \param[in] config          Kernel configuration for launching
271  * \param[in] deviceStream    GPU stream to launch kernel in
272  * \param[in] kernelName      Human readable kernel description, for error handling only
273  * \param[in] kernelArgs      Array of the pointers to the kernel arguments, prepared by
274  * prepareGpuKernelArguments() \throws gmx::InternalError on kernel launch failure
275  */
276 template<typename... Args>
277 void launchGpuKernel(void (*kernel)(Args...),
278                      const KernelLaunchConfig& config,
279                      const DeviceStream&       deviceStream,
280                      CommandEvent* /*timingEvent */,
281                      const char*                               kernelName,
282                      const std::array<void*, sizeof...(Args)>& kernelArgs)
283 {
284     dim3 blockSize(config.blockSize[0], config.blockSize[1], config.blockSize[2]);
285     dim3 gridSize(config.gridSize[0], config.gridSize[1], config.gridSize[2]);
286     cudaLaunchKernel((void*)kernel,
287                      gridSize,
288                      blockSize,
289                      const_cast<void**>(kernelArgs.data()),
290                      config.sharedMemorySize,
291                      deviceStream.stream());
292
293     gmx::ensureNoPendingDeviceError("GPU kernel (" + std::string(kernelName)
294                                     + ") failed to launch.");
295 }
296
297 #endif