Release 13.4 of the CUDA Samples supported by CUDA Toolkit 13.4. See Changelog for more information.
Sample: Device-Side assert (simpleAssert)
Description
Use the standard C assert macro inside a CUDA kernel. The sample launches threads that each assert gtid < N; threads whose global index reaches N trip the assertion, print a diagnostic message to the host, and cause the kernel to fail. The host detects this through the cudaErrorAssert status returned by cudaDeviceSynchronize and reports it as the expected outcome.
This sample demonstrates device-side assert, a debugging aid for catching invalid conditions in kernel code, and shows how an assertion failure surfaces on the host as an asynchronous CUDA error.
What You'll Learn
- Calling
assertdirectly from device code (__global__kernel) - How a failed device assertion is reported to the host as
cudaErrorAssert - Detecting that error after the launch via
cudaDeviceSynchronize - Computing a global thread index from
blockIdx,blockDim, andthreadIdx - Turning an error code into a human-readable message with
cudaGetErrorString
Key Concepts
- Device-side
assert—assert(condition)in a kernel; a false condition halts the kernel, printsfile:line: function: block: ... Assertion ... failed, and flags the launch as failed - Asynchronous error reporting — the assertion failure is not seen at launch time; it is surfaced at the next synchronization point as
cudaErrorAssert - Global thread indexing —
blockIdx.x * blockDim.x + threadIdx.x
Key APIs
CUDA Runtime
cudaSetDevice— select the active GPUcudaDeviceGetAttribute— query compute capability (major, minor) and SM countcudaDeviceSynchronize— block the host until the kernel finishes; flushes assert output and returnscudaErrorAssertif an assertion failedcudaGetErrorString— convert acudaError_tinto a readable description
Requirements
Hardware
- NVIDIA GPU with Compute Capability 7.5 or higher
Software
- CMake 3.20 or newer
- A C++17-capable host compiler
How to Build
See the top-level README for full build instructions, including how to build all samples or a single sample standalone.
How to Run
./simpleAssert
Expected Output
The kernel launches 2 blocks × 32 threads = 64 threads and asserts gtid < 60, so the 4 threads with global indices 60–63 fail the assertion. The assertion failures are expected — the sample reports OK because it successfully detected cudaErrorAssert:
simpleAssert starting...
GPU Device 0: with compute capability X.Y and Number of SMs <smCount>
Launch kernel to generate assertion failures
-- Begin assert output
simpleAssert.cu:50: void simpleAssertKernel(int): block: [1,0,0], thread: [28,0,0] Assertion `gtid < N` failed.
...
-- End assert output
Device assert failed as expected, CUDA error message is: device-side assert triggered
simpleAssert completed, returned OK
The order and exact set of failing-thread lines may vary between runs.
Files
simpleAssert.cu— device-sideassertkernel + host driverREADME.md— this fileCMakeLists.txt— build configuration