Home Knowledge Base CUDA Unified Memory

CUDA Unified Memory is the programming model that provides a single, coherent address space accessible from both CPU and GPU — automatically migrating pages between CPU and GPU memory on demand, eliminating the need for explicit cudaMemcpy calls, simplifying GPU programming at the cost of potential performance overhead from page faults and migration latency.

Traditional vs. Unified Memory

AspectTraditional (Explicit)Unified Memory
AllocationcudaMalloc (GPU) + malloc (CPU)cudaMallocManaged (single pointer)
Data transfercudaMemcpy(dst, src, size, direction)Automatic page migration
Pointer sharingSeparate CPU/GPU pointersSame pointer on both
Programmer effortHigh (manage all transfers)Low (system handles migration)
PerformanceOptimal (programmer controls transfers)Good (may have page fault overhead)
OversubscriptionError if GPU memory exceededData spills to CPU memory

How Unified Memory Works (Pascal+)

1. cudaMallocManaged(&ptr, size) — allocates in unified virtual address space. 2. Pages initially reside on CPU. 3. GPU kernel accesses ptrpage fault → GPU driver migrates page from CPU to GPU. 4. CPU accesses ptrpage fault → driver migrates page from GPU to CPU. 5. Pages migrated on demand at granularity of 4 KB (CPU page) or 64 KB (GPU preferred).

Performance Considerations

Oversubscription

Performance Optimization Pattern

// Allocate managed memory
cudaMallocManaged(&data, N * sizeof(float));

// Initialize on CPU
initialize_data(data, N);

// Prefetch to GPU before kernel (avoids page faults during kernel)
cudaMemPrefetchAsync(data, N * sizeof(float), gpuDevice);

// Run kernel — data already on GPU, no faults
kernel<<<blocks, threads>>>(data, N);

// Prefetch back to CPU before CPU access
cudaMemPrefetchAsync(data, N * sizeof(float), cudaCpuDeviceId);
use_results(data, N);

When to Use Unified Memory

Use CaseRecommendation
Complex data structures (linked lists, trees)Unified (explicit copy impractical)
Prototyping / rapid developmentUnified (simplicity)
Production HPC / MLExplicit (maximum control and performance)
GPU memory oversubscriptionUnified (only option)
Multi-GPU with peer accessUnified (simplifies multi-GPU)

CUDA Unified Memory is an essential productivity tool that democratizes GPU programming — by removing the most error-prone aspect of GPU development (manual memory management), it enables faster development and handles complex data structures that would be impractical with explicit copies, while prefetching and memory advise hints allow recovering most of the performance.

cuda unified memorymanaged memorycuda uvmpage migration gpumemory oversubscription gpu

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.