CUDA kernel printf not showing (or the kernel does not run)

Common causes

The program exits before the kernel finishes

kernel<<<...>>>() returns immediately. Device printf output is buffered on the GPU and copied out when the host synchronizes, so if main returns first it is lost.

The launch failed and nobody checked

A launch with more than 1024 threads, zero blocks or too much shared memory never runs. The only trace is the error waiting in cudaGetLastError().

An earlier error already broke the context

After a sticky error such as an illegal memory access, later kernels do not run at all.

Too much output

Device printf writes to a fixed-size buffer (about 1.3 MB by default on our T4). Output beyond it is dropped. Raise it with cudaDeviceSetLimit(cudaLimitPrintfFifoSize, bytes) before the launch.

Reproduce it

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

Repro: the kernel output never appears
main.cu
1234567891011
#include <cstdio>

__global__ void hello() {
    printf("Hello from thread %d\n", threadIdx.x);
}

int main() {
    hello<<<1, 4>>>();
    printf("Host done\n");
    return 0;   // exits before the GPU flushes its printf buffer
}
Output on an NVIDIA T4
Host done

Fix

Fixed: check the launch, then wait for the GPU
main.cu
12345678910111213141516171819202122232425
#include <cstdio>
#include <cstdlib>
#include <cuda_runtime.h>

#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 hello() {
    printf("Hello from thread %d\n", threadIdx.x);
}

int main() {
    hello<<<1, 4>>>();
    CUDA_CHECK(cudaGetLastError());        // did the launch start?
    CUDA_CHECK(cudaDeviceSynchronize());   // wait for it, flush printf
    printf("Host done\n");
    return 0;
}
Output on an NVIDIA T4
Hello from thread 0
Hello from thread 1
Hello from thread 2
Hello from thread 3
Host done

Debugging checklist

  1. Add cudaDeviceSynchronize() after the launch. Output appears at that point.
  2. Add cudaGetLastError() right after the launch and print the result. "invalid configuration argument" means the kernel never started.
  3. Check the return value of cudaDeviceSynchronize() too: it reports errors that happened while the kernel ran.
  4. If only part of the output appears, the printf buffer is full. Print less, or raise cudaLimitPrintfFifoSize.

FAQ

Why does my code work without cudaDeviceSynchronize?

cudaMemcpy from device to host waits for earlier kernels on the default stream, so programs that copy results back are synchronized by the copy. Programs that only print from the kernel, or read managed memory, need an explicit cudaDeviceSynchronize().

In what order do printf lines from different threads appear?

In no guaranteed order. Threads in a warp often print in order, but blocks run in whatever order the scheduler picks. Include threadIdx and blockIdx in the message.