mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-11-17 17:38:18 +08:00
CUDA 13.4 samples update - v13.4-public
Release 13.4 of the CUDA Samples supported by CUDA Toolkit 13.4. See Changelog for more information.
This commit is contained in:
@@ -1,32 +1,83 @@
|
||||
# simpleAssert - simpleAssert
|
||||
# Sample: Device-Side assert (simpleAssert)
|
||||
|
||||
## Description
|
||||
|
||||
This CUDA Runtime API sample is a very basic sample that implements how to use the assert function in the device code. Requires Compute Capability 2.0 .
|
||||
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 `assert` directly 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`, and `threadIdx`
|
||||
- Turning an error code into a human-readable message with `cudaGetErrorString`
|
||||
|
||||
## Key Concepts
|
||||
|
||||
Assert
|
||||
- **Device-side `assert`** — `assert(condition)` in a kernel; a false condition halts the kernel, prints `file: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`
|
||||
|
||||
## Supported SM Architectures
|
||||
## Key APIs
|
||||
|
||||
[SM 5.0 ](https://developer.nvidia.com/cuda-gpus) [SM 5.2 ](https://developer.nvidia.com/cuda-gpus) [SM 5.3 ](https://developer.nvidia.com/cuda-gpus) [SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
|
||||
### CUDA Runtime
|
||||
- `cudaSetDevice` — select the active GPU
|
||||
- `cudaDeviceGetAttribute` — query compute capability (major, minor) and SM count
|
||||
- `cudaDeviceSynchronize` — block the host until the kernel finishes; flushes assert output and returns `cudaErrorAssert` if an assertion failed
|
||||
- `cudaGetErrorString` — convert a `cudaError_t` into a readable description
|
||||
|
||||
## Supported OSes
|
||||
## Requirements
|
||||
|
||||
Linux, Windows
|
||||
### Hardware
|
||||
- NVIDIA GPU with Compute Capability 7.5 or higher
|
||||
|
||||
## Supported CPU Architecture
|
||||
### Software
|
||||
- CMake 3.20 or newer
|
||||
- A C++17-capable host compiler
|
||||
|
||||
x86_64, armv7l
|
||||
## How to Build
|
||||
|
||||
## CUDA APIs involved
|
||||
See the [top-level README](../../../README.md#building-cuda-samples) for full build instructions, including how to build all samples or a single sample standalone.
|
||||
|
||||
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||
cudaDeviceSynchronize, cudaGetErrorString
|
||||
## How to Run
|
||||
|
||||
## Prerequisites
|
||||
```bash
|
||||
./simpleAssert
|
||||
```
|
||||
|
||||
Download and install the [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||
## Expected Output
|
||||
|
||||
## References (for more details)
|
||||
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`:
|
||||
|
||||
```text
|
||||
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-side `assert` kernel + host driver
|
||||
- `README.md` — this file
|
||||
- `CMakeLists.txt` — build configuration
|
||||
|
||||
## See Also
|
||||
|
||||
- [CUDA C++ Programming Guide - Assertion](https://docs.nvidia.com/cuda/cuda-programming-guide/05-appendices/cpp-language-extensions.html#assertion)
|
||||
|
||||
Reference in New Issue
Block a user