Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added free Python function as_usm_memory(obj) #443

Merged
merged 5 commits into from
May 17, 2021

Conversation

oleksandr-pavlyk
Copy link
Collaborator

@oleksandr-pavlyk oleksandr-pavlyk commented May 14, 2021

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.

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.as_usm_memory(X)
Out[4]: <SYCL(TM) USM-device allocation of 160 bytes at 0xffffd556aa5f0000>

In [5]: X.usm_data
Out[5]: <SYCL(TM) USM-device allocation 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.as_usm_memory(d)
Out[9]: <SYCL(TM) USM-device allocation of 160 bytes at 0xffffd556aa5f0000>

In [10]: Out[9].get_usm_type()
Out[10]: 'device'

@oleksandr-pavlyk oleksandr-pavlyk changed the title Added Python get_usm_pointer_type Added free Python function get_usm_pointer_type(ptr, syclobj) May 14, 2021
@oleksandr-pavlyk oleksandr-pavlyk force-pushed the free-function-to-determinate-usm-type branch from d24dc44 to 70390dd Compare May 14, 2021 00:10
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'
```
@oleksandr-pavlyk oleksandr-pavlyk force-pushed the free-function-to-determinate-usm-type branch from 70390dd to 4afd698 Compare May 14, 2021 00:20
@DrTodd13
Copy link
Contributor

@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?

@oleksandr-pavlyk
Copy link
Collaborator Author

@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 syclobj is either a dpctl.SyclContext, or dpctl.SyclQueue, but not for other possible values of syclobj entry in the interface. I will need to extend get_usm_pointer_type to process other possibilities too, in particular extracting queue/context from capsules without renaming them.

Really, I may be solving a wrong problem here. Maybe I just need a stand-alone function to convert an object exposing __sycl_usm_array_interface__ to one of the memory bytes containers: MemoryUSMShared, etc.

In other words, maybe all we need is to expose _Memory._cinit_other as a stand-alone function?

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>
```
@oleksandr-pavlyk oleksandr-pavlyk force-pushed the free-function-to-determinate-usm-type branch from d53312c to ea0b571 Compare May 14, 2021 15:29
@oleksandr-pavlyk
Copy link
Collaborator Author

oleksandr-pavlyk commented May 14, 2021

@DrTodd13 I added dpctl.memory.create_MemoryUSM(sua_obj) which will create the allocation of the correct type:

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 allocation of 160 bytes at 0xffffd556aa5f0000>

In [5]: X.usm_data
Out[5]: <SYCL(TM) USM-device allocation 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 allocation of 160 bytes at 0xffffd556aa5f0000>

@oleksandr-pavlyk
Copy link
Collaborator Author

Feel free to suggest a better name than create_MemoryUSM.

@oleksandr-pavlyk
Copy link
Collaborator Author

oleksandr-pavlyk commented May 14, 2021

Will rename dpctl.memory.create_MemoryUSM to dpctl.memory.as_usm_memory.

I will also remove dpctl.memory._memory.get_usm_pointer_type, since querying the type of USM allocation exposed by suai_obj can be queried as mem = dpctl.memory.as_usm_memory(obj); mem.get_usm_type().

@oleksandr-pavlyk oleksandr-pavlyk changed the title Added free Python function get_usm_pointer_type(ptr, syclobj) Added free Python function as_usm_memory(obj) May 15, 2021
@oleksandr-pavlyk oleksandr-pavlyk merged commit 5a5170b into master May 17, 2021
@oleksandr-pavlyk oleksandr-pavlyk deleted the free-function-to-determinate-usm-type branch May 17, 2021 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants