|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +# ################################################################################ |
| 6 | +# |
| 7 | +# This demo illustrates: |
| 8 | +# |
| 9 | +# 1. How to use different memory resources to allocate and manage memory |
| 10 | +# 2. How to copy data between different memory types |
| 11 | +# 3. How to use DLPack to interoperate with other libraries |
| 12 | +# |
| 13 | +# ################################################################################ |
| 14 | + |
| 15 | +import sys |
| 16 | + |
| 17 | +import cupy as cp |
| 18 | +import numpy as np |
| 19 | + |
| 20 | +from cuda.core.experimental import ( |
| 21 | + Device, |
| 22 | + LaunchConfig, |
| 23 | + LegacyPinnedMemoryResource, |
| 24 | + Program, |
| 25 | + ProgramOptions, |
| 26 | + launch, |
| 27 | +) |
| 28 | + |
| 29 | +if np.__version__ < "2.1.0": |
| 30 | + print("This example requires NumPy 2.1.0 or later", file=sys.stderr) |
| 31 | + sys.exit(0) |
| 32 | + |
| 33 | +# Kernel for memory operations |
| 34 | +code = """ |
| 35 | +extern "C" |
| 36 | +__global__ void memory_ops(float* device_data, |
| 37 | + float* pinned_data, |
| 38 | + size_t N) { |
| 39 | + const unsigned int tid = threadIdx.x + blockIdx.x * blockDim.x; |
| 40 | + if (tid < N) { |
| 41 | + // Access device memory |
| 42 | + device_data[tid] = device_data[tid] + 1.0f; |
| 43 | +
|
| 44 | + // Access pinned memory (zero-copy from GPU) |
| 45 | + pinned_data[tid] = pinned_data[tid] * 3.0f; |
| 46 | + } |
| 47 | +} |
| 48 | +""" |
| 49 | + |
| 50 | +dev = Device() |
| 51 | +dev.set_current() |
| 52 | +stream = dev.create_stream() |
| 53 | +# tell CuPy to use our stream as the current stream: |
| 54 | +cp.cuda.ExternalStream(int(stream.handle)).use() |
| 55 | + |
| 56 | +# Compile kernel |
| 57 | +arch = "".join(f"{i}" for i in dev.compute_capability) |
| 58 | +program_options = ProgramOptions(std="c++17", arch=f"sm_{arch}") |
| 59 | +prog = Program(code, code_type="c++", options=program_options) |
| 60 | +mod = prog.compile("cubin") |
| 61 | +kernel = mod.get_kernel("memory_ops") |
| 62 | + |
| 63 | +# Create different memory resources |
| 64 | +device_mr = dev.memory_resource |
| 65 | +pinned_mr = LegacyPinnedMemoryResource() |
| 66 | + |
| 67 | +# Allocate different types of memory |
| 68 | +size = 1024 |
| 69 | +dtype = cp.float32 |
| 70 | +element_size = dtype().itemsize |
| 71 | +total_size = size * element_size |
| 72 | + |
| 73 | +# 1. Device Memory (GPU-only) |
| 74 | +device_buffer = device_mr.allocate(total_size, stream=stream) |
| 75 | +device_array = cp.from_dlpack(device_buffer).view(dtype=dtype) |
| 76 | + |
| 77 | +# 2. Pinned Memory (CPU memory, GPU accessible) |
| 78 | +pinned_buffer = pinned_mr.allocate(total_size, stream=stream) |
| 79 | +pinned_array = np.from_dlpack(pinned_buffer).view(dtype=dtype) |
| 80 | + |
| 81 | +# Initialize data |
| 82 | +rng = cp.random.default_rng() |
| 83 | +device_array[:] = rng.random(size, dtype=dtype) |
| 84 | +pinned_array[:] = rng.random(size, dtype=dtype).get() |
| 85 | + |
| 86 | +# Store original values for verification |
| 87 | +device_original = device_array.copy() |
| 88 | +pinned_original = pinned_array.copy() |
| 89 | + |
| 90 | +# Sync before kernel launch |
| 91 | +stream.sync() |
| 92 | + |
| 93 | +# Launch kernel |
| 94 | +block = 256 |
| 95 | +grid = (size + block - 1) // block |
| 96 | +config = LaunchConfig(grid=grid, block=block) |
| 97 | + |
| 98 | +launch(stream, config, kernel, device_buffer, pinned_buffer, cp.uint64(size)) |
| 99 | +stream.sync() |
| 100 | + |
| 101 | +# Verify kernel operations |
| 102 | +assert cp.allclose(device_array, device_original + 1.0), "Device memory operation failed" |
| 103 | +assert cp.allclose(pinned_array, pinned_original * 3.0), "Pinned memory operation failed" |
| 104 | + |
| 105 | +# Copy data between different memory types |
| 106 | +print("\nCopying data between memory types...") |
| 107 | + |
| 108 | +# Copy from device to pinned memory |
| 109 | +device_buffer.copy_to(pinned_buffer, stream=stream) |
| 110 | +stream.sync() |
| 111 | + |
| 112 | +# Verify the copy operation |
| 113 | +assert cp.allclose(pinned_array, device_array), "Device to pinned copy failed" |
| 114 | + |
| 115 | +# Create a new device buffer and copy from pinned |
| 116 | +new_device_buffer = device_mr.allocate(total_size, stream=stream) |
| 117 | +new_device_array = cp.from_dlpack(new_device_buffer).view(dtype=dtype) |
| 118 | + |
| 119 | +pinned_buffer.copy_to(new_device_buffer, stream=stream) |
| 120 | +stream.sync() |
| 121 | + |
| 122 | +# Verify the copy operation |
| 123 | +assert cp.allclose(new_device_array, pinned_array), "Pinned to device copy failed" |
| 124 | + |
| 125 | +# Clean up |
| 126 | +device_buffer.close(stream) |
| 127 | +pinned_buffer.close(stream) |
| 128 | +new_device_buffer.close(stream) |
| 129 | +stream.close() |
| 130 | +cp.cuda.Stream.null.use() # reset CuPy's current stream to the null stream |
| 131 | + |
| 132 | +# Verify buffers are properly closed |
| 133 | +assert device_buffer.handle == 0, "Device buffer should be closed" |
| 134 | +assert pinned_buffer.handle == 0, "Pinned buffer should be closed" |
| 135 | +assert new_device_buffer.handle == 0, "New device buffer should be closed" |
| 136 | + |
| 137 | +print("Memory management example completed!") |
0 commit comments