What Is Pinned Host Memory? How to Use CUDA Transfer Buffers Safely
Pinned host memory is CPU-side system memory that CUDA keeps page-locked so the operating system cannot move or swap those pages while they are in use. It can improve host-to-device transfer behavior and is required for asynchronous transfers such as `cudaMemcpyAsync`. It is not GPU VRAM, and pinning too much memory or pinning it repeatedly can slow the application and the system.
What Is Pinned Host Memory? How to Use CUDA Transfer Buffers Safely
Pinned host memory is CPU-side system memory that CUDA keeps page-locked so the operating system cannot move or swap those pages while they are in use. It can improve host-to-device transfer behavior and is required for asynchronous transfers such as cudaMemcpyAsync. It is not GPU VRAM, and pinning too much memory or pinning it repeatedly can slow the application and the system.
This guide covers host-device copies in C++ programs that use the NVIDIA CUDA Runtime. Exact behavior depends on the operating system, GPU, driver, and runtime, so no transfer rate or overlap is guaranteed.
What changes when host pages are pinned?
Ordinary host allocations are usually pageable: the operating system may swap their pages to disk or move them in physical memory. CUDA keeps pinned pages in place until the allocation is freed or the registered range is unregistered. That stable residency is needed for asynchronous CPU-GPU copies. NVIDIA's guide notes that malloc, new, and mmap allocations are not page-locked by default and that asynchronous CPU-GPU copies require page-locked host memory.
| Memory type | Meaning | Main use and caution |
|---|---|---|
| Pageable host memory | Regular CPU memory whose pages may be moved or swapped by the operating system | Common for CPU work; not the right host buffer for an asynchronous CUDA copy. |
| Pinned host memory | CPU memory whose pages CUDA keeps locked while in use | A host-device transfer buffer; too much reduces memory available to the operating system. |
| Device memory | Memory attached to the GPU, as on a typical discrete GPU | Holds inputs, intermediate values, and results repeatedly used by kernels; it is a separate allocation from pinned host memory. |
“Pinned” does not mean the data has moved to VRAM or that CPU and GPU automatically share the same physical memory. In the common discrete-GPU path, the CPU prepares data in a pinned host buffer, CUDA copies it to a separate device buffer, and a kernel reads the device buffer. Mapping host memory for a kernel to access directly is a separate option; do not confuse it with an explicit copy. Direct mapped access, sometimes called zero-copy, is a different data-movement choice. CUDA Programming Guide — Page-Locked Host Memory
Which API allocates the buffer, and how is it released?
CUDA Runtime offers both new pinned allocations and registration of an existing host allocation. Choose based on who owns the buffer and how long it lives.
| Method | When to use it | Release or unregister |
|---|---|---|
cudaMallocHost(&ptr, bytes) | Create a pinned host buffer managed through CUDA | cudaFreeHost(ptr) |
cudaHostAlloc(&ptr, bytes, cudaHostAllocDefault) | Create a pinned buffer with the same default behavior, with optional flags when needed | cudaFreeHost(ptr) |
cudaHostRegister(ptr, bytes, flags) | Register an existing range allocated with malloc or by a library | After async work completes, call cudaHostUnregister(ptr), then let the original allocator free it. |
Registration tells CUDA about an existing range without copying it into a new buffer. Check device support and runtime conditions; registration does not behave identically on every platform. Use cudaFreeHost for a CUDA-created pinned allocation. For registered memory, first call cudaHostUnregister, then release the allocation through its original owner. Do not use cudaFreeHost on an ordinary malloc buffer or reuse/free a buffer while CUDA may still be using it. CUDA Runtime API — cudaHostAlloc and cudaHostRegister
The example below copies a pinned input buffer to device memory, runs a kernel, then copies the result back into the same host buffer. CUDA_CHECK is an illustrative macro that prints a CUDA error and exits. Production code should use lifetime management such as RAII so streams and buffers already created are cleaned up on error paths.
#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() {
constexpr int n = 1 << 20;
constexpr size_t bytes = n * sizeof(float);
float* host = nullptr;
float* device = nullptr;
cudaStream_t stream{};
CUDA_CHECK(cudaStreamCreate(&stream));
CUDA_CHECK(cudaMallocHost(&host, bytes));
CUDA_CHECK(cudaMalloc(&device, bytes));
for (int i = 0; i < n; ++i) host[i] = static_cast<float>(i);
CUDA_CHECK(cudaMemcpyAsync(device, host, bytes,
cudaMemcpyHostToDevice, stream));
constexpr int threads = 256;
doubleValues<<<(n + threads - 1) / threads, threads, 0, stream>>>(device, n);
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaMemcpyAsync(host, device, bytes,
cudaMemcpyDeviceToHost, stream));
CUDA_CHECK(cudaStreamSynchronize(stream));
std::printf("host[1] = %.1f\n", host[1]);
CUDA_CHECK(cudaFree(device));
CUDA_CHECK(cudaFreeHost(host));
CUDA_CHECK(cudaStreamDestroy(stream));
}This listing explains API order and buffer ownership; it is not claimed to have been compiled or measured on a particular GPU. If the CPU changes host before the first async copy finishes, the GPU may read changed input. Conversely, the CPU must not read the output until the device-to-host copy completes. cudaStreamSynchronize waits for the copies and kernel queued earlier in that stream. After it returns, the CPU can read, reuse, or release the buffer.
CPU prepares input in pinned host buffer
│ cudaMemcpyAsync (Host → Device, stream)
▼
GPU device buffer ── kernel in the same stream
│ cudaMemcpyAsync (Device → Host, stream)
▼
After cudaStreamSynchronize, CPU reads or reuses the result bufferDoes an asynchronous transfer guarantee overlap?
cudaMemcpyAsync lets the host thread continue without waiting for the copy to finish. The function name alone does not guarantee that a transfer and a kernel run simultaneously on the GPU. NVIDIA's Best Practices Guide says async host-device copies require pinned host memory. Device-side copy-compute overlap also depends on device capability, including a suitable copy engine, and on stream choices such as using distinct non-default streams. Check asyncEngineCount and the actual dependencies between streams.
The example puts the input copy, kernel, and output copy in one stream. This makes their dependencies clear, but those three operations for one chunk do not execute simultaneously. To pipeline multiple chunks, applications can use separate buffers and place independent work in different streams. Events or stream dependencies must still define when the CPU may overwrite a buffer and when the GPU may write it. Pinned memory is one requirement for an asynchronous pipeline; it does not replace stream design or device capability checks. CUDA C++ Best Practices Guide — Pinned Memory and Overlap
When should you pin memory, and how do you verify the result?
Pinned memory is worth considering when transfers are substantial, buffers are reused, or the program is designed to overlap asynchronous transfers. For a tiny or one-off transfer, allocation, registration, and synchronization costs may exceed any transfer savings. Also consider how often the CPU reads or writes the buffer. Allocate only the required amount and, where practical, reuse a small set of long-lived buffers.
| Observation | What to check first | Decision |
|---|---|---|
Transfer and kernel still do not overlap after cudaMemcpyAsync | Confirm the host pointer is pinned, the device supports concurrent copy and compute, and stream/default-stream dependencies allow overlap. | Distinguish host API return from GPU completion; check device capability and a profiler trace. |
| Pinning or registration is slow or fails | Check the byte count, how often pinning occurs, and whether the device supports registration. | Reuse a small number of long-lived buffers instead of registering on every operation. Do not ignore errors or pin a larger range blindly. |
| The whole program slows after pinning | Check system memory pressure, CPU access to the buffer, and the number of small copies. | Reduce pinned memory and batch small transfers; compare application time and system impact. |
| Data changes or results are unstable before a transfer completes | Check whether the CPU overwrites input or reads output too early. | Read, reuse, or free the buffer only after stream synchronization or a completion event. |
Use this sequence to separate pinning cost from transfer behavior:
**Record a baseline.** Measure a representative input size and repeat count, copy time, kernel time, and end-to-end time under the same conditions.
**Prepare the pinned buffer.** Allocate once with `cudaMallocHost` or default `cudaHostAlloc` and reuse it. For an existing library buffer, check device support and the original owner's lifetime.
**Set the operation order.** Enqueue copies and dependent kernels in one stream, or express dependencies with events/streams. The CPU must not change the buffer before completion.
**Check completion and correctness.** After stream synchronization or an event, compare the output with the baseline. Only then reuse or release the buffer.
**Compare the full path.** Include repeated allocation/registration, transfer, kernel, result return, and system memory pressure. If gains do not hold or system pressure rises, reduce pinning or return to a synchronous pageable-memory path.
Do not compare only kernel time. Measure input preparation, allocation or registration, host-device transfers, kernel execution, and result return in the real application path. Pinning and registration are heavyweight compared with ordinary allocation, so whether their cost is paid once or on every operation matters. Results depend on the data size and system; do not generalize one measurement into a guarantee for another GPU. NVIDIA's Best Practices Guide likewise advises using pinned memory sparingly and measuring on the target application and systems.
Start with a small reusable buffer, confirm that transfer is a bottleneck, then compare output correctness and end-to-end time after switching to pinned memory. If the gain is not repeatable or system memory pressure worsens, reduce the pinned pool or restore the pageable synchronous path. Directly mapped host memory accessed by a kernel is a separate zero-copy option, not the same solution as this explicit-copy workflow.
References
[NVIDIA CUDA Programming Guide — Page-Locked Host 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 — Data Transfer and Pinned Memory](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/index.html)