What Is CUDA Zero-Copy Memory? Using Host Buffers Directly in GPU Kernels
CUDA zero-copy lets a GPU kernel read or write mapped, page-locked host memory without an explicit copy into a separate device buffer. “No copy call” does not mean that data-access costs disappear. On a discrete GPU, memory requests still travel over a CPU-GPU connection such as PCIe or NVLink C2C, so copying reusable data into GPU memory may be faster.
What Is CUDA Zero-Copy Memory? Using Host Buffers Directly in GPU Kernels
CUDA zero-copy lets a GPU kernel read or write mapped, page-locked host memory without an explicit copy into a separate device buffer. “No copy call” does not mean that data-access costs disappear. On a discrete GPU, memory requests still travel over a CPU-GPU connection such as PCIe or NVLink C2C, so copying reusable data into GPU memory may be faster.
This guide covers mapped host memory in NVIDIA CUDA C++. Mapping and performance vary by system, GPU, and CUDA version; no throughput or speedup is guaranteed.
What happens to the data and address in zero-copy?
Zero-copy maps page-locked host memory into the GPU address space so a kernel can access the CPU allocation directly. The CPU can prepare data and the GPU can read or write that same allocation. However, the physical memory may remain on the host. On a discrete GPU, each access can cross the CPU-GPU link instead of using local GPU-memory bandwidth. The CUDA Programming Guide says mapped-memory accesses have higher latency and lower available bandwidth than device memory.
CPU prepares data in a mapped, page-locked host buffer
│ cudaHostGetDevicePointer: obtain the GPU-visible address
▼
GPU kernel directly reads or writes the same host allocation
│ memory requests may cross the CPU-GPU connection
▼
After synchronizing kernel completion, CPU reads results or reuses the bufferZero-copy avoids an explicit cudaMemcpy for this access pattern. cudaHostAlloc with cudaHostAllocMapped can create a mapped pinned buffer; cudaHostRegister can register and map an existing host range. Pass the device pointer returned by cudaHostGetDevicePointer to the kernel. Do not assume it always equals the CPU pointer. Systems with HMM or ATS support may also let the GPU access ordinary host pointers, so exact address behavior depends on the system. CUDA Programming Guide — Mapped Memory
Which workloads are candidates for mapped access?
Mapped host memory can avoid an unnecessary separate copy on integrated GPUs where CPU and GPU use the same physical memory. On discrete GPUs, consider it for a one-pass read or write where warp accesses are coalesced. NVIDIA’s Best Practices Guide treats mapped pinned memory on discrete GPUs as useful only in some cases and recommends limiting repeated access.
| Approach | Where the kernel accesses data | When it may fit | Main trade-off |
|---|---|---|---|
| Zero-copy / mapped pinned host memory | The GPU directly accesses a CPU host allocation | Small one-time data, a configuration with shared physical memory, or a flow where a separate copy is unnecessary | On a discrete GPU, access crosses the CPU-GPU link; repeated use can repeat link traffic. |
| Pinned-host staging plus explicit copy | Copy from a host buffer into device memory; kernel then accesses GPU memory | Data reused by multiple kernels or workloads that need local device-memory bandwidth | Requires a separate buffer and copy/synchronization, but reuse after copying can reduce repeated link access. |
| CUDA Unified Memory | CPU and GPU use a managed allocation through a common pointer | Simplifying code while letting the runtime manage placement and migration | Page movement and access behavior depend on the platform; it is not the same mechanism as mapped host memory. |
Zero-copy is not always faster just because an array is small. Kernel execution, launch, link transactions, and synchronization all affect total time. Conversely, when data is reused, copying it once into device memory may reduce repeated trips over the discrete-GPU link. Choose based on reuse count, access contiguity, GPU topology, and the whole workload rather than array size alone. CUDA C++ Best Practices Guide — Zero Copy
How do you map a buffer and synchronize access?
The example checks whether the device supports host-memory mapping, creates a mapped pinned host buffer, and has a kernel double each value in place. Input and output are in the same host allocation, so there is no explicit copy to device memory. CUDA Runtime API contexts set the host-mapping flag by default, but check device support and obtain the device pointer separately.
| API | Purpose | End of lifetime |
|---|---|---|
cudaHostAlloc(&host, bytes, cudaHostAllocMapped) | Create a new page-locked host buffer and request GPU mapping | After kernel work completes, call cudaFreeHost(host). |
cudaHostRegister(ptr, bytes, cudaHostRegisterMapped) | Pin and map an existing host range allocated with malloc or another allocator | After GPU work completes, call cudaHostUnregister(ptr), then free it with the original allocator. |
cudaHostGetDevicePointer(&deviceView, host, 0) | Obtain the GPU address used by the kernel | The returned address may differ from the host pointer; pass the returned device view to the kernel. |
Check mapping support and context conditions for the device you will use. Runtime API contexts enable host mapping by default, but the device must support mapping and cudaHostGetDevicePointer can still fail. CUDA Runtime API — cudaHostAlloc, cudaHostRegister and cudaHostGetDevicePointer
#include <cuda_runtime.h>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#define CUDA_CHECK(call) do { \
cudaError_t err = (call); \
if (err != cudaSuccess) { \
std::fprintf(stderr, "%s\n", cudaGetErrorString(err)); \
std::exit(EXIT_FAILURE); \
} \
} while (0)
__global__ void doubleValues(float* data, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) data[i] *= 2.0f;
}
int main() {
int device = 0;
cudaDeviceProp prop{};
CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
if (!prop.canMapHostMemory) {
std::fprintf(stderr, "Mapped host memory is unavailable.\n");
return EXIT_FAILURE;
}
CUDA_CHECK(cudaSetDevice(device));
constexpr int n = 1 << 20;
constexpr std::size_t bytes = n * sizeof(float);
float* host = nullptr;
void* mappedAddress = nullptr;
float* deviceView = nullptr;
CUDA_CHECK(cudaHostAlloc(&host, bytes, cudaHostAllocMapped));
CUDA_CHECK(cudaHostGetDevicePointer(&mappedAddress, host, 0));
deviceView = static_cast<float*>(mappedAddress);
for (int i = 0; i < n; ++i) host[i] = static_cast<float>(i);
constexpr int threads = 256;
int blocks = (n + threads - 1) / threads;
doubleValues<<<blocks, threads>>>(deviceView, n);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaDeviceSynchronize());
std::printf("host[1] = %.1f\n", host[1]);
CUDA_CHECK(cudaFreeHost(host));
}This listing explains API order; it is not claimed to have been compiled or measured on a specific GPU. Production code should use lifetime management such as RAII so the host allocation is released on error paths. If mapping is unsupported or cudaHostGetDevicePointer fails, do not pass an invalid pointer to a kernel; handle the error or switch to a device allocation with an explicit copy.
It is safe for the CPU to fill the buffer before launch. But if the CPU accesses the same locations while the GPU kernel is reading or writing them, a race can occur. Synchronize GPU work before the CPU reads results or reuses or frees the buffer. Atomics on mapped host memory are not automatically atomic from the host or another GPU’s point of view; use a separate synchronization design when concurrent access is required. CUDA Runtime API — Host Memory Mapping
| Symptom | Possible cause | Safe next step |
|---|---|---|
| Cannot obtain the device pointer or the kernel fails | Mapping is unsupported or the context conditions are not met | Check canMapHostMemory and CUDA errors; if mapping fails, switch to device memory with an explicit copy. |
| Direct access is slower than expected on a discrete GPU | Kernel repeatedly crosses the CPU-GPU link or accesses scattered addresses | Check reuse count and address contiguity; compare with one copy to device memory followed by reuse. |
| Results change or vary between runs | CPU and GPU access the same buffer before synchronization | Prevent CPU access until the kernel completes, then read results or reuse the buffer. |
| Assuming CPU and GPU atomics are mutually safe | Device atomics on mapped host memory are not atomic from the host or another GPU’s viewpoint | Do not use mapped-memory atomics as a cross-processor lock; use supported synchronization or message passing. |
How do you verify performance on the real workload?
Decide whether to keep zero-copy by comparing both paths on the same input and computation. On a discrete GPU, distinguish one-pass coalesced access from repeated reuse. Measure the full path, not only the kernel: preparation, copies in the alternative path, synchronization, and result handling all contribute. If repeated use increases link traffic or kernel time, compare copying the input into device memory and reusing it there.
**Check device and buffer support.** Query `canMapHostMemory` and confirm that `cudaHostAllocMapped` or registration of the existing range succeeds. Pass only the returned device pointer to the kernel.
**Describe the access pattern.** Record whether neighboring threads access neighboring addresses, whether the data is read once, and how many kernels reuse it. Note whether the GPU is integrated or discrete.
**Prepare equivalent paths.** Compare a kernel that accesses mapped host memory with one that copies to a device buffer first. Keep data lifetime and synchronization correct in both.
**Check correctness first.** Compare outputs on representative inputs and ensure the CPU does not read or overwrite the mapped buffer before the kernel completes.
**Measure and decide on the whole path.** Repeat measurements under the same conditions and compare transfer, kernel, synchronization, and total time. If the gain is not repeatable or link access is the bottleneck, reduce zero-copy use and return to explicit copy plus device-memory reuse.
Zero-copy removes an explicit copy call; it does not remove memory hierarchy or interconnect cost. Results depend on where the pages reside, how often the kernel accesses them, and whether the accesses are coalesced. Mapped pinned memory is a candidate for integrated configurations or one-time access, while repeated work on a discrete GPU should be compared against an explicit copy into device memory.
References
[NVIDIA CUDA Programming Guide — Mapped Memory](https://docs.nvidia.com/cuda/cuda-programming-guide/02-basics/understanding-memory.html)
[NVIDIA CUDA Runtime API 13.5 — Memory Management](https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY.html)
[NVIDIA CUDA C++ Best Practices Guide 13.5 — Zero Copy](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/index.html)