@@ -21,6 +21,7 @@ import multiprocessing.reduction
2121import os
2222import platform
2323import sys
24+ import uuid as uuid_module
2425import weakref
2526from cuda.core.experimental._dlpack import DLDeviceType, make_py_capsule
2627from 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:
0 commit comments