Skip to content

Commit 5b537af

Browse files
Static method of _Memory to create from external allocation
Method takes USMRef, number of bytes and queue reference. If USMRef is bound to context in the queue and USM type comes back not 'unknown', appropriate MemoryUSM* object is created which assumes ownership of this memory. Optional `memory_owner` keyword argument may be specified to set the recorded reference object to something other than None (signaling that _Memory is responsible for memory deallocation). This can be used to create MemoryUSM* Python object from natively allocated memory, populate it, but leave deallocation to the caller. It could also be set to a PyCapsule object that carries a custom deleter.
1 parent 5a60bd0 commit 5b537af

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

dpctl/memory/_memory.pxd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ cdef public class _Memory [object Py_MemoryObject, type Py_MemoryType]:
5555
DPCTLSyclUSMRef p, SyclContext ctx)
5656
@staticmethod
5757
cdef public bytes get_pointer_type(DPCTLSyclUSMRef p, SyclContext ctx)
58+
@staticmethod
59+
cdef public object create_from_usm_pointer_size_qref(
60+
DPCTLSyclUSMRef USMRef,
61+
Py_ssize_t nbytes,
62+
DPCTLSyclQueueRef QRef,
63+
object memory_owner=*)
5864

5965

6066
cdef public class MemoryUSMShared(_Memory) [object PyMemoryUSMSharedObject,

dpctl/memory/_memory.pyx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ from dpctl._backend cimport ( # noqa: E211
3939
DPCTLQueue_Copy,
4040
DPCTLQueue_Create,
4141
DPCTLQueue_Delete,
42+
DPCTLQueue_GetContext,
4243
DPCTLQueue_Memcpy,
4344
DPCTLSyclContextRef,
4445
DPCTLSyclDeviceRef,
46+
DPCTLSyclUSMRef,
4547
DPCTLUSM_GetPointerDevice,
4648
DPCTLUSM_GetPointerType,
4749
)
@@ -431,6 +433,43 @@ cdef class _Memory:
431433

432434
return <bytes>usm_type
433435

436+
@staticmethod
437+
cdef object create_from_usm_pointer_size_qref(
438+
DPCTLSyclUSMRef USMRef, Py_ssize_t nbytes, DPCTLSyclQueueRef QRef, object memory_owner=None):
439+
"""
440+
"""
441+
cdef const char *usm_type
442+
cdef DPCTLSyclContextRef CRef = NULL
443+
cdef DPCTLSyclQueueRef QRef_copy = NULL
444+
cdef _Memory _mem
445+
cdef object mem_ty
446+
if nbytes <= 0:
447+
raise ValueError("Number of bytes must must be positive")
448+
if (QRef is NULL):
449+
raise TypeError("Argument DPCTLSyclQueueRef is NULL")
450+
CRef = DPCTLQueue_GetContext(QRef)
451+
if (CRef is NULL):
452+
raise ValueError("Could not retrieve context from QRef")
453+
usm_type = DPCTLUSM_GetPointerType(USMRef, CRef)
454+
if usm_type == b"shared":
455+
mem_ty = MemoryUSMShared
456+
elif usm_type == b"device":
457+
mem_ty = MemoryUSMDevice
458+
elif usm_type == b"host":
459+
mem_ty = MemoryUSMHost
460+
else:
461+
raise ValueError("Argument pointer is not bound to context in the given queue")
462+
res = _Memory.__new__(_Memory)
463+
_mem = <_Memory> res
464+
_mem._cinit_empty()
465+
_mem.memory_ptr = USMRef
466+
_mem.nbytes = nbytes
467+
QRef_copy = DPCTLQueue_Copy(QRef)
468+
_mem.queue = SyclQueue._create(QRef_copy)
469+
_mem.refobj = memory_owner
470+
return mem_ty(res)
471+
472+
434473

435474
cdef class MemoryUSMShared(_Memory):
436475
"""

0 commit comments

Comments
 (0)