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,82 @@
|
||||
# vectorAdd - Vector Addition
|
||||
# Sample: Vector Addition (Unified Memory)
|
||||
|
||||
## Description
|
||||
|
||||
This CUDA Runtime API sample is a very basic sample that implements element by element vector addition. It is the same as the sample illustrating Chapter 3 of the programming guide with some additions like error checking.
|
||||
A first-look CUDA sample: add two float vectors element-wise on the GPU. This version uses **Unified Memory** (`cudaMallocManaged`) so the same pointer is accessible from both host and device, eliminating the need for explicit `cudaMemcpy` calls. The GPU result is verified against a CPU reference computed on the same buffers.
|
||||
|
||||
The sample mirrors the corresponding Unified Memory example in the CUDA Programming Guide and uses the `//unified-memory-begin` / `//unified-memory-end` markers so the guide can extract the snippet directly.
|
||||
|
||||
## What You'll Learn
|
||||
|
||||
- Allocating memory accessible to both host and device with `cudaMallocManaged`
|
||||
- Launching a 1D CUDA kernel that operates on a Unified Memory buffer with no explicit transfers
|
||||
- Computing grid dimensions with `cuda::ceil_div` (from `<cuda/cmath>`)
|
||||
- Synchronizing host execution with device work via `cudaDeviceSynchronize`
|
||||
- Validating GPU output against a serial CPU reference
|
||||
|
||||
## Key Concepts
|
||||
|
||||
CUDA Runtime API, Vector Addition
|
||||
- **Unified Memory** — one pointer for both CPU and GPU; the CUDA runtime migrates pages on demand
|
||||
- **1D thread indexing** — `threadIdx.x + blockIdx.x * blockDim.x`
|
||||
- **Bounds checking** in the kernel so the vector length need not be a multiple of the block size
|
||||
|
||||
## 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
|
||||
- `cudaMallocManaged` — allocate Unified Memory accessible to both host and device
|
||||
- `cudaDeviceSynchronize` — block the host until all submitted device work completes
|
||||
- `cudaFree` — release a Unified Memory allocation
|
||||
|
||||
## Supported OSes
|
||||
### libcudacxx
|
||||
- `cuda::ceil_div` — `ceil(a / b)` used to compute the number of blocks; from `<cuda/cmath>`
|
||||
|
||||
Linux, Windows
|
||||
## Requirements
|
||||
|
||||
## Supported CPU Architecture
|
||||
### Hardware
|
||||
- NVIDIA GPU with Compute Capability 7.5 or higher
|
||||
|
||||
x86_64, armv7l
|
||||
### Software
|
||||
- CMake 3.20 or newer
|
||||
- A C++17-capable host compiler
|
||||
|
||||
## CUDA APIs involved
|
||||
## How to Build
|
||||
|
||||
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||
cudaMemcpy, cudaGetErrorString, cudaFree, cudaGetLastError, cudaMalloc
|
||||
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.
|
||||
|
||||
## Prerequisites
|
||||
## How to Run
|
||||
|
||||
Download and install the [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||
### Basic usage (default vector length = 1024)
|
||||
```bash
|
||||
./vectorAdd
|
||||
```
|
||||
|
||||
### Custom vector length
|
||||
```bash
|
||||
./vectorAdd 1000000
|
||||
```
|
||||
|
||||
## Expected Output
|
||||
|
||||
On success:
|
||||
|
||||
```text
|
||||
Unified Memory: CPU and GPU answers match
|
||||
```
|
||||
|
||||
On failure, the first mismatching index and the two diverging values are printed, followed by an error line:
|
||||
|
||||
```text
|
||||
Index 0 mismatch: 0.123456 != 0.123400
|
||||
Unified Memory: Error - CPU and GPU answers do not match
|
||||
```
|
||||
|
||||
## Files
|
||||
|
||||
- `vectorAdd.cu` — Unified Memory vector addition implementation (kernel + host driver)
|
||||
- `README.md` — this file
|
||||
- `CMakeLists.txt` — build configuration
|
||||
|
||||
## See Also
|
||||
|
||||
- [CUDA C++ Programming Guide — vectorAdd example with Unified Memory](https://docs.nvidia.com/cuda/cuda-programming-guide/02-basics/intro-to-cuda-cpp.html)
|
||||
|
||||
## References (for more details)
|
||||
|
||||
Reference in New Issue
Block a user