cudaGetLastError

Check for errors, including kernel launch failures

Syntax

cpp
1
cudaError_t cudaGetLastError(void);

Returns

cudaSuccess
No error since the last reset.
the last error
The most recent error from any runtime call or launch on this thread. Reading it resets the stored value to cudaSuccess.

How it works

Check every launch

Write kernel<<<grid, block>>>(args); CUDA_CHECK(cudaGetLastError());. This catches configuration errors immediately. Errors that happen while the kernel runs show up at the next synchronizing call.

Get vs peek

cudaGetLastError reads and clears. cudaPeekAtLastError reads only, so a later check still sees the same error.

Name vs description

cudaGetErrorName(err) gives the enum name, such as cudaErrorInvalidConfiguration. cudaGetErrorString(err) gives the message, such as "invalid configuration argument". Printing both makes errors easy to search for.

One error state per host thread

Each CPU thread has its own last error, so check on the thread that made the call.

Example

Press Run to execute this on a real NVIDIA GPU in the playground.

Example: the CUDA_CHECK macro, and get vs peek
main.cu
1234567891011121314151617181920212223242526272829303132
#include <cstdio>
#include <cstdlib>
#include <cuda_runtime.h>

// Wrap every runtime call: prints file, line and the error, then exits.
#define CUDA_CHECK(call)                                                  \
    do {                                                                  \
        cudaError_t err_ = (call);                                        \
        if (err_ != cudaSuccess) {                                        \
            fprintf(stderr, "%s:%d: %s (%s)\n", __FILE__, __LINE__,       \
                    cudaGetErrorString(err_), cudaGetErrorName(err_));    \
            exit(1);                                                      \
        }                                                                 \
    } while (0)

__global__ void k() {}

int main() {
    k<<<1, 2048>>>();   // too many threads per block: the launch is rejected

    printf("peek: %s\n", cudaGetErrorName(cudaPeekAtLastError()));
    printf("peek: %s\n", cudaGetErrorName(cudaPeekAtLastError()));
    printf("get:  %s\n", cudaGetErrorName(cudaGetLastError()));
    printf("get:  %s\n", cudaGetErrorName(cudaGetLastError()));

    fflush(stdout);     // show the lines above before the error on stderr

    k<<<1, 2048>>>();
    CUDA_CHECK(cudaGetLastError());   // stops the program here
    printf("not reached\n");
    return 0;
}
Output on an NVIDIA T4
peek: cudaErrorInvalidConfiguration
peek: cudaErrorInvalidConfiguration
get:  cudaErrorInvalidConfiguration
get:  cudaSuccess
main.cu:29: invalid configuration argument (cudaErrorInvalidConfiguration)

Common mistakes

Only checking at the end

Errors from several calls collapse into the last one, and you lose where it happened. Check right after each call while debugging.

Expecting it to catch errors inside the kernel immediately

Right after a launch the kernel has not run yet. Faults during execution appear at cudaDeviceSynchronize() or the next copy.

FAQ

What is the standard way to check for CUDA errors?

Wrap every runtime call in a macro like CUDA_CHECK above, and after each kernel launch call CUDA_CHECK(cudaGetLastError()). In debug builds, also call CUDA_CHECK(cudaDeviceSynchronize()) after each launch to catch execution errors at the right kernel.

Can cudaGetLastError clear an illegal memory access?

It resets the stored value, but the context stays broken, so the next call fails with the same error. Sticky errors require restarting the process.