Added free Python function as_usm_memory(obj) - #443
Conversation
d24dc44 to
70390dd
Compare
Used the Cython static method from _Memmory.get_pointer_type in the added function, as well as in the method `get_usm_type`. Added docstrings. ``` Python 3.7.9 (default, Mar 10 2021, 05:18:00) Type 'copyright', 'credits' or 'license' for more information IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: import dpctl.memory as dpm, dpctl.memory._memory as dpm_m, dpctl In [2]: m = dpm.MemoryUSMDevice(2048, alignment=256) In [3]: dpm_m.get_usm_pointer_type(m._pointer, m.sycl_context) Out[3]: 'device' In [4]: dpm_m.get_usm_pointer_type(m._pointer + 1024, m.sycl_context) Out[4]: 'device' In [5]: m.get_usm_type() Out[5]: 'device' ```
70390dd to
4afd698
Compare
|
@oleksandr-pavlyk In the general case where you have sycl_usm_array_interface but don't know if it came from dpctl.memory, what should be the sycl_context that we pass? |
if hasattr(obj, '__sycl_usm_array_interface__'):
sua_iface = getattr(obj, '__sycl_usm_array_interface__')
ptr, writeable = sua_iface['data']
syclobj = sua_iface['syclobj']
usm_type = dpctl.memory._memory.get_usm_pointer_type(ptr, syclobj)This will work if Really, I may be solving a wrong problem here. Maybe I just need a stand-alone function to convert an object exposing In other words, maybe all we need is to expose Which of these would be more useful? |
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> ```
``` In [1]: import dpctl.memory as dpm In [2]: dpm.MemoryUSMShared(64) Out[2]: <SYCL(TM) USM-shared allocation of 64 bytes at 0x564eb7f30000> ```
d53312c to
ea0b571
Compare
|
@DrTodd13 I added |
|
Feel free to suggest a better name than |
|
Will rename I will also remove |
Used the Cython static method from _Memmory.get_pointer_type in the added function,
as well as in the method
get_usm_type.Added docstrings.