A CUDA Error Checking Macro
- 1 min read
I just switched over to a new CUDA error checking macro and really like how it works.
This error checking macro will transform a CUDA Runtime API function
into its error-checked equivalent simply by parenthesizing everything
after the lower case cuda function prefix.
Examples:
cuda(GetDeviceProperties(&props,device));
cuda(SetDevice(device));
cuda(Malloc(&vin_d, bytes));
cuda(Memset(vin_d,0,bytes));
cuda(Free(vin_d));
cuda(EventCreate(&end));
cuda(EventRecord(end));
cuda(EventSynchronize(end));
cuda(EventElapsedTime(&elapsed,start,end));
cuda(EventDestroy(end));
cuda(DeviceReset());
The C99/C++ macro and assert look like this:
#define cuda(...) cuda_assert((cuda##__VA_ARGS__), __FILE__, __LINE__, true);
cudaError_t
cuda_assert(const cudaError_t code, const char* const file, const int line, const bool abort)
{
if (code != cudaSuccess)
{
const char* const err_str = cudaGetErrorString(code);
fprintf(stderr,"cuda_assert: %s %s %d\n",err_str,file,line);
if (abort)
{
cudaDeviceReset();
exit(code);
}
}
return code;
}
That’s all there is to it!