Skip to content

Commit 46ece5e

Browse files
Added dpctl.memory.create_MemoryUSM
The function takes an object with `__sycl_usm_array_interface__` and creates approproate MemoryUSM* object depending the the type of USM alocation the argument represents. Example: ``` In [1]: import dpctl, dpctl.memory as dpm, dpctl.memory._memory as dpm_m In [2]: import dpctl.tensor as dpt In [3]: X = dpt.usm_ndarray((4, 5)) In [4]: dpm_m.create_MemoryUSM(X) Out[4]: <SYCL(TM) USM-device allocated memory block of 160 bytes at 0xffffd556aa5f0000> In [5]: X.usm_data Out[5]: <SYCL(TM) USM-device allocated memory block of 160 bytes at 0xffffd556aa5f0000> In [6]: class Duck_USMAllocation: ...: def __init__(self, buf, syclobj): ...: self.buf_ = buf ...: self.syclobj_ = syclobj ...: In [7]: class Duck_USMAllocation: ...: def __init__(self, buf, syclobj): ...: self.buf_ = buf ...: self.syclobj_ = syclobj ...: @Property ...: def __sycl_usm_array_interface__(self): ...: iface = self.buf_.__sycl_usm_array_interface__ ...: iface['syclobj'] = self.syclobj_ ...: return iface ...: In [8]: d = Duck_USMAllocation(X, X.sycl_device.filter_string) In [9]: dpm_m.create_MemoryUSM(d) Out[9]: <SYCL(TM) USM-device allocated memory block of 160 bytes at 0xffffd556aa5f0000> ```
1 parent 3e2ea1c commit 46ece5e

File tree

2 files changed

+69
-15
lines changed

2 files changed

+69
-15
lines changed

dpctl/memory/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030
`memoryview`, or `array.array` classes.
3131
3232
"""
33-
from ._memory import MemoryUSMDevice, MemoryUSMHost, MemoryUSMShared
33+
from ._memory import (
34+
MemoryUSMDevice,
35+
MemoryUSMHost,
36+
MemoryUSMShared,
37+
create_MemoryUSM,
38+
)
3439

35-
__all__ = ["MemoryUSMDevice", "MemoryUSMHost", "MemoryUSMShared"]
40+
__all__ = [
41+
"MemoryUSMDevice",
42+
"MemoryUSMHost",
43+
"MemoryUSMShared",
44+
"create_MemoryUSM",
45+
]

dpctl/memory/_memory.pyx

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,14 @@ cdef class MemoryUSMShared(_Memory):
596596
allocates nbytes of USM shared memory.
597597
598598
Non-positive alignments are not used (malloc_shared is used instead).
599-
For the queue=None cast the `dpctl.SyclQueue()` is used to allocate memory.
600-
601-
MemoryUSMShared(usm_obj) constructor create instance from `usm_obj`
602-
expected to implement `__sycl_usm_array_interface__` protocol and exposing
603-
a contiguous block of USM memory of USM shared type. Using copy=True to
604-
perform a copy if USM type is other than 'shared'.
599+
For the queue=None case the ``dpctl.SyclQueue()`` is used to allocate
600+
memory.
601+
602+
MemoryUSMShared(usm_obj) constructor creates instance from `usm_obj`
603+
expected to implement `__sycl_usm_array_interface__` protocol and to expose
604+
a contiguous block of USM shared allocation. Use `copy=True` to
605+
perform a copy if USM type of the allocation represented by the argument
606+
is other than 'shared'.
605607
"""
606608
def __cinit__(self, other, *, Py_ssize_t alignment=0,
607609
SyclQueue queue=None, int copy=False):
@@ -635,12 +637,14 @@ cdef class MemoryUSMHost(_Memory):
635637
allocates nbytes of USM host memory.
636638
637639
Non-positive alignments are not used (malloc_host is used instead).
638-
For the queue=None case `dpctl.SyclQueue()` is used to allocate memory.
640+
For the queue=None case the ``dpctl.SyclQueue()`` is used to allocate
641+
memory.
639642
640643
MemoryUSMDevice(usm_obj) constructor create instance from `usm_obj`
641-
expected to implement `__sycl_usm_array_interface__` protocol and exposing
642-
a contiguous block of USM memory of USM host type. Using copy=True to
643-
perform a copy if USM type is other than 'host'.
644+
expected to implement `__sycl_usm_array_interface__` protocol and to expose
645+
a contiguous block of USM host allocation. Use `copy=True` to
646+
perform a copy if USM type of the allocation represented by the argument
647+
is other than 'host'.
644648
"""
645649
def __cinit__(self, other, *, Py_ssize_t alignment=0,
646650
SyclQueue queue=None, int copy=False):
@@ -675,12 +679,14 @@ cdef class MemoryUSMDevice(_Memory):
675679
allocates nbytes of USM device memory.
676680
677681
Non-positive alignments are not used (malloc_device is used instead).
678-
For the queue=None cast the `dpctl.SyclQueue()` is used to allocate memory.
682+
For the queue=None case the ``dpctl.SyclQueue()`` is used to allocate
683+
memory.
679684
680685
MemoryUSMDevice(usm_obj) constructor create instance from `usm_obj`
681686
expected to implement `__sycl_usm_array_interface__` protocol and exposing
682-
a contiguous block of USM memory of USM device type. Using copy=True to
683-
perform a copy if USM type is other than 'device'.
687+
a contiguous block of USM device allocation. Use `copy=True` to
688+
perform a copy if USM type of the allocation represented by the argument
689+
is other than 'device'.
684690
"""
685691
def __cinit__(self, other, *, Py_ssize_t alignment=0,
686692
SyclQueue queue=None, int copy=False):
@@ -704,3 +710,41 @@ cdef class MemoryUSMDevice(_Memory):
704710
other, self.get_usm_type()
705711
)
706712
)
713+
714+
715+
def create_MemoryUSM(obj):
716+
"""
717+
create_MemoryUSM(obj)
718+
719+
Converts Python object with `__sycl_usm_array_interface__` property
720+
to one of :class:`.MemoryUSMShared`, :class:`.MemoryUSMDevice`, or
721+
:class:`.MemoryUSMHost` instances depending on the type of USM allocation
722+
they represent.
723+
724+
Raises:
725+
ValueError
726+
When object does not expose the `__sycl_usm_array_interface__`,
727+
or it is malformed
728+
TypeError
729+
When unexpected types of entries in the interface are encountered
730+
SyclQueueCreationError
731+
When a :class:`dpctl.SyclQueue` could not be created from the
732+
information given by the interface
733+
"""
734+
cdef _Memory res = _Memory.__new__(_Memory)
735+
cdef str kind
736+
res._cinit_empty()
737+
res._cinit_other(obj)
738+
kind = res.get_usm_type()
739+
if kind == "shared":
740+
return MemoryUSMShared(res)
741+
elif kind == "device":
742+
return MemoryUSMDevice(res)
743+
elif kind == "host":
744+
return MemoryUSMHost(res)
745+
else:
746+
raise ValueError(
747+
"Could not determine the type "
748+
"USM allocation represented by argument {}".
749+
format(obj)
750+
)

0 commit comments

Comments
 (0)