Skip to content

Commit 55bd3b1

Browse files
committed
Simplify pinned memory example
1 parent fb65ab8 commit 55bd3b1

1 file changed

Lines changed: 5 additions & 45 deletions

File tree

cuda_core/examples/memory_ops.py

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
import cupy as cp
6+
import numpy as np
67

78
from cuda.core.experimental import (
89
Device,
@@ -55,28 +56,16 @@
5556

5657
# 1. Device Memory (GPU-only)
5758
device_buffer = device_mr.allocate(total_size, stream=stream)
58-
device_array = cp.ndarray(
59-
size,
60-
dtype=dtype,
61-
memptr=cp.cuda.MemoryPointer(
62-
cp.cuda.UnownedMemory(int(device_buffer.handle), device_buffer.size, device_buffer), 0
63-
),
64-
)
59+
device_array = cp.from_dlpack(device_buffer).view(dtype=dtype)
6560

6661
# 2. Pinned Memory (CPU memory, GPU accessible)
6762
pinned_buffer = pinned_mr.allocate(total_size, stream=stream)
68-
pinned_array = cp.ndarray(
69-
size,
70-
dtype=dtype,
71-
memptr=cp.cuda.MemoryPointer(
72-
cp.cuda.UnownedMemory(int(pinned_buffer.handle), pinned_buffer.size, pinned_buffer), 0
73-
),
74-
)
63+
pinned_array = np.from_dlpack(pinned_buffer).view(dtype=dtype)
7564

7665
# Initialize data
7766
rng = cp.random.default_rng()
7867
device_array[:] = rng.random(size, dtype=dtype)
79-
pinned_array[:] = rng.random(size, dtype=dtype)
68+
pinned_array[:] = rng.random(size, dtype=dtype).get()
8069

8170
# Store original values for verification
8271
device_original = device_array.copy()
@@ -97,17 +86,6 @@
9786
assert cp.allclose(device_array, device_original + 1.0), "Device memory operation failed"
9887
assert cp.allclose(pinned_array, pinned_original * 3.0), "Pinned memory operation failed"
9988

100-
# Demonstrate buffer copying operations
101-
print("Memory buffer properties:")
102-
print(f"Device buffer - Device accessible: {device_buffer.is_device_accessible}")
103-
print(f"Pinned buffer - Device accessible: {pinned_buffer.is_device_accessible}")
104-
105-
# Assert memory properties
106-
assert device_buffer.is_device_accessible, "Device buffer should be device accessible"
107-
assert not device_buffer.is_host_accessible, "Device buffer should not be host accessible"
108-
assert pinned_buffer.is_device_accessible, "Pinned buffer should be device accessible"
109-
assert pinned_buffer.is_host_accessible, "Pinned buffer should be host accessible"
110-
11189
# Copy data between different memory types
11290
print("\nCopying data between memory types...")
11391

@@ -120,13 +98,7 @@
12098

12199
# Create a new device buffer and copy from pinned
122100
new_device_buffer = device_mr.allocate(total_size, stream=stream)
123-
new_device_array = cp.ndarray(
124-
size,
125-
dtype=dtype,
126-
memptr=cp.cuda.MemoryPointer(
127-
cp.cuda.UnownedMemory(int(new_device_buffer.handle), new_device_buffer.size, new_device_buffer), 0
128-
),
129-
)
101+
new_device_array = cp.from_dlpack(new_device_buffer).view(dtype=dtype)
130102

131103
pinned_buffer.copy_to(new_device_buffer, stream=stream)
132104
stream.sync()
@@ -146,18 +118,6 @@
146118
assert device_dlpack[0] == DLDeviceType.kDLCUDA, "Device buffer should have CUDA device type"
147119
assert pinned_dlpack[0] == DLDeviceType.kDLCUDAHost, "Pinned buffer should have CUDA host device type"
148120

149-
# Test buffer size properties
150-
assert device_buffer.size == total_size, f"Device buffer size mismatch: expected {total_size}, got {device_buffer.size}"
151-
assert pinned_buffer.size == total_size, f"Pinned buffer size mismatch: expected {total_size}, got {pinned_buffer.size}"
152-
assert new_device_buffer.size == total_size, (
153-
f"New device buffer size mismatch: expected {total_size}, got {new_device_buffer.size}"
154-
)
155-
156-
# Test memory resource properties
157-
assert device_buffer.memory_resource == device_mr, "Device buffer should use device memory resource"
158-
assert pinned_buffer.memory_resource == pinned_mr, "Pinned buffer should use pinned memory resource"
159-
assert new_device_buffer.memory_resource == device_mr, "New device buffer should use device memory resource"
160-
161121
# Clean up
162122
device_buffer.close(stream)
163123
pinned_buffer.close(stream)

0 commit comments

Comments
 (0)