how use "mapped page-locked memory" in nvidia orin
- User
how use "mapped page-locked memory" in nvidia orin
- Assistant
how use "mapped page-locked memory" in nvidia orin
Using mapped page-locked memory (also called pinned mapped memory) in NVIDIA Orin—or any NVIDIA Jetson platform—can be very useful for efficiently sharing memory between the CPU and GPU without explicit data copies. This technique leverages zero-copy access, allowing the GPU to directly access the host memory (RAM) mapped into the GPU address space, reducing latency and overhead.
Here is a guide on how to use mapped page-locked memory in NVIDIA Orin (e.g., Jetson Orin Nano, Orin NX), leveraging CUDA APIs:
cudaMemcpy calls.#include <cuda_runtime.h>
#include <stdio.h>
cudaHostAlloc() with the cudaHostAllocMapped flagvoid* hostPtr;
size_t size = 1024 * sizeof(float); // Example: buffer for 1024 floats
cudaError_t err = cudaHostAlloc(&hostPtr, size, cudaHostAllocMapped);
if (err != cudaSuccess) {
printf("cudaHostAlloc failed: %s\n", cudaGetErrorString(err));
return -1;
}
cudaHostAllocMapped flag allocates pinned memory that is mapped into the CUDA address space (GPU can access it).hostPtr points to the CPU address space (CPU can read/write it).This device pointer lets the GPU access the same allocation directly:
void* devicePtr;
err = cudaHostGetDevicePointer(&devicePtr, hostPtr, 0);
if (err != cudaSuccess) {
printf("cudaHostGetDevicePointer failed: %s\n", cudaGetErrorString(err));
cudaFreeHost(hostPtr);
return -1;
}
Now devicePtr is the GPU accessible pointer to the same memory.
You can pass devicePtr to kernels and operate on it like normal device memory.
Example kernel:
__global__ void addOne(float* data, int N) {
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx < N)
data[idx] += 1.0f;
}
Launching:
int N = 1024;
addOne<<<(N+255)/256, 256>>>((float*)devicePtr, N);
cudaDeviceSynchronize();
Because the memory is mapped, CPU accesses can read results directly:
float* fptr = (float*)hostPtr;
printf("First element after kernel: %f\n", fptr[0]);
cudaFreeHost(hostPtr);
cudaDeviceSynchronize()).#include <cuda_runtime.h>
#include <stdio.h>
__global__ void addOne(float* data, int N) {
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx < N)
data[idx] += 1.0f;
}
int main() {
int N = 1024;
float* hostPtr;
size_t size = N * sizeof(float);
// Allocate pinned mapped memory
if (cudaHostAlloc(&hostPtr, size, cudaHostAllocMapped) != cudaSuccess) {
printf("cudaHostAlloc failed\n");
return -1;
}
// Initialize on host
for (int i = 0; i < N; i++) hostPtr[i] = float(i);
// Get device pointer for the same memory
float* devicePtr;
if (cudaHostGetDevicePointer(&devicePtr, hostPtr, 0) != cudaSuccess) {
printf("cudaHostGetDevicePointer failed\n");
cudaFreeHost(hostPtr);
return -1;
}
// Launch kernel
addOne<<<(N + 255)/256, 256>>>(devicePtr, N);
cudaDeviceSynchronize();
// Print result on host
printf("hostPtr[0] = %f\n", hostPtr[0]); // Should be 1.0
printf("hostPtr[100] = %f\n", hostPtr[100]); // Should be 101.0
// Free memory
cudaFreeHost(hostPtr);
return 0;
}
Compile with:
nvcc mapped_memory_example.cu -o mapped_memory_example
Run on NVIDIA Orin device natively.
If you want to use CUDA unified memory (cudaMallocManaged), that is another option, but mapped page-locked memory is often used when you want explicit zero-copy pointers with manual control.
cudaHostAlloc() with cudaHostAllocMapped to allocate page-locked host memory mapped into CUDA address space.cudaHostGetDevicePointer() to get GPU pointer to the mapped memory.cudaFreeHost().If you want, I can help you with more details on performance tuning or using mapped memory with specific APIs (e.g., TensorRT, cuDNN) on Orin. Just ask!