CUDA Error Handling
As in any application, error handling in accelerated CUDA code is essential. Many, if not most CUDA functions (see, for example, the memory management functions) return a value of type cudaError_t
, which can be used to check whether or not an error occurred while calling the function. Here is an example where error handling is performed for a call to cudaMallocManaged
:
Launching kernels, which are defined to return void
, do not return a value of type cudaError_t
. To check for errors occurring at the time of a kernel launch, for example if the launch configuration is erroneous, CUDA provides the cudaGetLastError
function, which does return a value of type cudaError_t
.
Finally, in order to catch errors that occur asynchronously, for example during the execution of an asynchronous kernel, it is essential to check the status returned by a subsequent synchronizing CUDA runtime API call, such as cudaDeviceSynchronize
, which will return an error if one of the kernels launched previously should fail.
CUDA Error Handling Function
It can be helpful to create a macro that wraps CUDA function calls for checking errors.