CUDA: Error Checking

Error checks in CUDA code can help catch CUDA errors at their source. There are 2 sources of errors in CUDA source code:

  1. Errors from CUDA API calls. For example, a call to cudaMalloc() might fail.
  2. Errors from CUDA kernel calls. For example, there might be invalid memory access inside a kernel.

All CUDA API calls return a cudaError value, so these calls are easy to check:

if ( cudaSuccess != cudaMalloc( &fooPtr, fooSize ) )
    printf( "Error!\n" );

CUDA kernel invocations do not return any value. Error from a CUDA kernel call can be checked after its execution by calling cudaGetLastError():

fooKernel<<< x, y >>>(); // Kernel call
if ( cudaSuccess != cudaGetLastError() )
    printf( "Error!\n" );

The cutil.h header file has utility functions that make these types of error checking convenient. cutil.h can be found in the %NVSDKCOMPUTE_ROOT%/C/common/inc directory.

Many CUDA programmers do not prefer to use cutil.h since it ships with the GPU Computing SDK, and not the CUDA Toolkit. If you feel the same way about cutil.h, here are CudaSafeCall() and CudaCheckError() error checking calls adapted for use in your own source code:

Using these error checks is easy:

CudaSafeCall( cudaMalloc( &fooPtr, fooSize ) );

fooKernel<<< x, y >>>(); // Kernel call
CudaCheckError();

Notice that the calls are inline functions, so absolutely no code is produced when CUDA_CHECK_ERROR is not defined. These utility functions can prove their worth to catch errors as close as possible to the error source only if they are used everywhere. Use them to wrap all CUDA API calls and after all your kernel calls. :-)

Tried with: CUDA 4.1

8 thoughts on “CUDA: Error Checking

  1. Pingback: CUDA: Memory Checker « chooru_code

  2. Great! Made my work so much easier. Maybe you could explain to me what the do { … } while (0); part is for, I don’t seem to get that. And again, thanks!

  3. Pingback: CUDA: Initialization Error « chooru::code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s