Skip to content

Commit b31d849

Browse files
committed
Changes DeviceMemoryResource remote_id to uuid.
1 parent 708e2b5 commit b31d849

File tree

4 files changed

+36
-35
lines changed

4 files changed

+36
-35
lines changed

cuda_core/cuda/core/experimental/_memory.pyx

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import multiprocessing.reduction
2121
import os
2222
import platform
2323
import sys
24+
import uuid as uuid_module
2425
import weakref
2526
from cuda.core.experimental._dlpack import DLDeviceType, make_py_capsule
2627
from cuda.core.experimental._stream import Stream, default_stream
@@ -537,7 +538,7 @@ class DeviceMemoryResourceAttributes:
537538

538539
# Holds DeviceMemoryResource objects imported by this process.
539540
# This enables buffer serialization, as buffers can reduce to a pair
540-
# of comprising the memory resource `remote_id` (the key into this registry)
541+
# of comprising the memory resource UUID (the key into this registry)
541542
# and the serialized buffer descriptor.
542543
_ipc_registry = {}
543544

@@ -565,7 +566,7 @@ class DeviceMemoryResource(MemoryResource):
565566
`False`), and closing the resource has no effect.
566567
"""
567568
__slots__ = ("_dev_id", "_mempool_handle", "_attributes", "_ipc_handle_type",
568-
"_mempool_owned", "_is_imported", "_remote_id", "_alloc_handle")
569+
"_mempool_owned", "_is_imported", "_uuid", "_alloc_handle")
569570

570571
def __init__(self, device_id: int | Device, options=None):
571572
device_id = getattr(device_id, 'device_id', device_id)
@@ -581,7 +582,7 @@ class DeviceMemoryResource(MemoryResource):
581582
self._ipc_handle_type = _NOIPC_HANDLE_TYPE
582583
self._mempool_owned = False
583584
self._is_imported = False
584-
self._remote_id = None
585+
self._uuid = None
585586
self._alloc_handle = None
586587

587588
err, self._mempool_handle = driver.cuDeviceGetMemPool(self.device_id)
@@ -624,14 +625,14 @@ class DeviceMemoryResource(MemoryResource):
624625
self._ipc_handle_type = properties.handleTypes
625626
self._mempool_owned = True
626627
self._is_imported = False
627-
self._remote_id = None
628+
self._uuid = None
628629
self._alloc_handle = None
629630

630631
err, self._mempool_handle = driver.cuMemPoolCreate(properties)
631632
raise_if_driver_error(err)
632633

633634
if opts.ipc_enabled:
634-
self.get_allocation_handle() # enables Buffer.export
635+
self.get_allocation_handle() # enables Buffer.export, sets uuid
635636

636637
def __del__(self):
637638
self.close()
@@ -650,46 +651,44 @@ class DeviceMemoryResource(MemoryResource):
650651
self._ipc_handle_type = _NOIPC_HANDLE_TYPE
651652
self._mempool_owned = False
652653
self._is_imported = False
653-
self._remote_id = None
654+
self._uuid = None
654655
self._alloc_handle = None
655656

656657

657658
def __reduce__(self):
658659
# If spawning a new process, serialize the resources; otherwise, just
659-
# send the remote_id, using the registry on the receiving end.
660+
# send the UUID, using the registry on the receiving end.
660661
is_spawning = multiprocessing.context.get_spawning_popen() is not None
661662
if is_spawning:
662663
from ._device import Device
663664
device = Device(self.device_id)
664665
alloc_handle = self.get_allocation_handle()
665-
return DeviceMemoryResource._reconstruct, (device, alloc_handle, self.remote_id)
666+
return DeviceMemoryResource._reconstruct, (device, alloc_handle, self.uuid)
666667
else:
667-
return DeviceMemoryResource.from_registry, (self.remote_id,)
668+
return DeviceMemoryResource.from_registry, (self.uuid,)
668669

669670
@staticmethod
670-
def _reconstruct(device, alloc_handle, remote_id):
671+
def _reconstruct(device, alloc_handle, uuid):
671672
self = DeviceMemoryResource.from_allocation_handle(device, alloc_handle)
672-
self.register(remote_id)
673+
self.register(uuid)
673674
return self
674675

675676
@staticmethod
676-
def from_registry(remote_id):
677+
def from_registry(uuid: uuid_module.UUID):
677678
try:
678-
return _ipc_registry[remote_id]
679+
return _ipc_registry[uuid]
679680
except KeyError:
680-
raise RuntimeError(f"Memory resource with {remote_id=} was not found")
681+
raise RuntimeError(f"Memory resource with {uuid=} was not found")
681682

682-
def register(self, remote_id: int):
683-
if remote_id not in _ipc_registry:
684-
assert self._remote_id is None or self._remote_id == remote_id
685-
_ipc_registry[remote_id] = self
686-
self._remote_id = remote_id
683+
def register(self, uuid: uuid_module.UUID):
684+
if uuid not in _ipc_registry:
685+
assert self._uuid is None or self._uuid == uuid
686+
_ipc_registry[uuid] = self
687+
self._uuid = uuid
687688

688689
@property
689-
def remote_id(self):
690-
if self._remote_id is None and not self._is_imported:
691-
self._remote_id = int(self._mempool_handle)
692-
return self._remote_id
690+
def uuid(self):
691+
return self._uuid
693692

694693
@classmethod
695694
def from_allocation_handle(cls, device_id: int | Device, alloc_handle: int | IPCAllocationHandle) -> DeviceMemoryResource:
@@ -721,7 +720,7 @@ class DeviceMemoryResource(MemoryResource):
721720
self._ipc_handle_type = _IPC_HANDLE_TYPE
722721
self._mempool_owned = True
723722
self._is_imported = True
724-
self._remote_id = None
723+
self._uuid = None
725724
self._alloc_handle = None # only used for non-imported
726725

727726
err, self._mempool_handle = driver.cuMemPoolImportFromShareableHandle(int(alloc_handle), _IPC_HANDLE_TYPE, 0)
@@ -746,6 +745,8 @@ class DeviceMemoryResource(MemoryResource):
746745
err, alloc_handle = driver.cuMemPoolExportToShareableHandle(self._mempool_handle, _IPC_HANDLE_TYPE, 0)
747746
raise_if_driver_error(err)
748747
self._alloc_handle = IPCAllocationHandle._init(alloc_handle)
748+
assert self._uuid is None
749+
self._uuid = uuid_module.uuid4()
749750
return self._alloc_handle
750751

751752
def allocate(self, size_t size, stream: Stream = None) -> Buffer:

cuda_core/tests/memory_ipc/test_memory_ipc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ def test_main(self, device, ipc_memory_resource):
149149

150150
# Start children.
151151
q1, q2 = (mp.Queue() for _ in range(2))
152-
p1 = mp.Process(target=self.child_main, args=(alloc_handle, mr.remote_id, 1, q1))
153-
p2 = mp.Process(target=self.child_main, args=(alloc_handle, mr.remote_id, 2, q2))
152+
p1 = mp.Process(target=self.child_main, args=(alloc_handle, mr.uuid, 1, q1))
153+
p2 = mp.Process(target=self.child_main, args=(alloc_handle, mr.uuid, 2, q2))
154154
p1.start()
155155
p2.start()
156156

@@ -171,12 +171,12 @@ def test_main(self, device, ipc_memory_resource):
171171
IPCBufferTestHelper(device, buf2).verify_buffer(starting_from=2)
172172

173173

174-
def child_main(self, alloc_handle, remote_id, idx, queue):
174+
def child_main(self, alloc_handle, uuid, idx, queue):
175175
"""Fills a shared memory buffer."""
176176
device = Device()
177177
device.set_current()
178178
mr = DeviceMemoryResource.from_allocation_handle(device, alloc_handle)
179-
mr.register(remote_id)
179+
mr.register(uuid)
180180
buffer = queue.get(timeout=CHILD_TIMEOUT_SEC)
181181
IPCBufferTestHelper(device, buffer).fill_buffer(starting_from=idx)
182182

cuda_core/tests/memory_ipc/test_serialize.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_main(self, device, ipc_memory_resource):
3333
# Send a memory resource by allocation handle.
3434
alloc_handle = mr.get_allocation_handle()
3535
mp.reduction.send_handle(parent_conn, alloc_handle.handle, process.pid)
36-
parent_conn.send(mr.remote_id)
36+
parent_conn.send(mr.uuid)
3737

3838
# Send a buffer.
3939
buffer1 = mr.allocate(NBYTES)
@@ -57,9 +57,9 @@ def child_main(self, conn):
5757

5858
# Receive the memory resource.
5959
handle = mp.reduction.recv_handle(conn)
60-
remote_id = conn.recv()
60+
uuid = conn.recv()
6161
mr = DeviceMemoryResource.from_allocation_handle(device, handle)
62-
mr.register(remote_id)
62+
mr.register(uuid)
6363
os.close(handle)
6464

6565
# Receive the buffers.
@@ -90,8 +90,8 @@ def test_main(self, device, ipc_memory_resource):
9090
# Send a memory resource directly. This relies on the mr already
9191
# being passed when spawning the child.
9292
pipe[0].put(mr)
93-
remote_id = pipe[1].get(timeout=CHILD_TIMEOUT_SEC)
94-
assert remote_id == mr.remote_id
93+
uuid = pipe[1].get(timeout=CHILD_TIMEOUT_SEC)
94+
assert uuid == mr.uuid
9595

9696
# Send a buffer.
9797
buffer = mr.allocate(NBYTES)
@@ -111,7 +111,7 @@ def child_main(self, pipe, _):
111111

112112
# Memory resource.
113113
mr = pipe[0].get(timeout=CHILD_TIMEOUT_SEC)
114-
pipe[1].put(mr.remote_id)
114+
pipe[1].put(mr.uuid)
115115

116116
# Buffer.
117117
buffer = pipe[0].get(timeout=CHILD_TIMEOUT_SEC)

cuda_core/tests/memory_ipc/test_workerpool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class TestIpcWorkerPool:
6767
Test buffer sharing without using export handles.
6868
6969
The memory resources need to be passed to subprocesses at startup. Buffers
70-
are serialized with the `remote_id` of the corresponding mr, and the
70+
are serialized with the `uuid` of the corresponding mr, and the
7171
import/export is handled automatically.
7272
"""
7373

0 commit comments

Comments
 (0)