Copy image arrays between CPU and GPU memory using the modern cuda.core API with optimal performance through pinned memory and asynchronous transfers.
- How to use pinned memory for faster CPU↔GPU transfers
- Using the
cuda.coreAPI for memory management - Working with DLPack for zero-copy array views
- Performing asynchronous memory transfers with CUDA streams
- Interoperability between CUDA Core API and CuPy
- Proper CUDA resource management and cleanup
cuda.core- Modern CUDA Python APInumpy- Array operations and DLPack supportcupy- GPU array operations and CUDA interoperability
Device()- Initialize and access CUDA deviceDevice.set_current()- Set the current device for API callsDevice.create_stream()- Create CUDA stream for async operationsDevice.memory_resource- Access device memory allocatorPinnedMemoryResource()- Allocate pinned host memorybuffer.copy_to()- Copy data between memory spacesbuffer.close()- Release allocated memory
np.from_dlpack()- Create array view from DLPack capsulenp.copyto()- Copy data between arrays
cp.from_dlpack()- Create GPU array view from DLPack capsulecp.cuda.Stream.from_external()- Use external CUDA stream
verify_array_result()- Verify computation results
- NVIDIA GPU with CUDA support
- Sufficient GPU memory for image data (sample uses ~200KB for 256×256×3 image)
- CUDA Toolkit 13.0 or newer
- Python 3.10 or newer
- NumPy 2.3.2 or newer (required for DLPack support)
cuda-pythonpackage (>=13.0.0+)cuda-corepackage (>=1.0.0)cupy-cuda13xpackage (14.0.0+)
Install the required packages from requirements.txt:
cd /path/to/cuda-python/samples/cuda_core/copyImageArraytoGPU
pip install -r requirements.txtThe requirements.txt installs:
numpy(2.3.2+, required for DLPack)cuda-python(>=13.0.0+)cuda-core(>=1.0.0)cupy-cuda13x(14.0.0+)
cd samples/cuda_core/copyImageArraytoGPU
python copyImageArraytoGPU.py[Image Array Copy to GPU using CUDA Core API]
Device: NVIDIA GeForce RTX 4090
[Image array copy of 256x256x3 image]
Creating sample image...
Copying image to GPU...
Creating CuPy view of GPU data...
Mean pixel value (computed on GPU): 127.50
Copying image back from GPU...
Verifying result...
Test PASSED
Done
Note: Device name will vary based on your GPU.
copyImageArraytoGPU.py- Python implementation using cuda.core APIREADME.md- This filerequirements.txt- Sample dependencies../Utilities/cuda_samples_utils.py- Common utilities (imported by this sample)