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

Implemented support for constructing MemoryUSM* from object with __sycl_usm_array_interface__ when array-info is not contiguous #400

Merged
merged 2 commits into from
Apr 23, 2021

Conversation

oleksandr-pavlyk
Copy link
Collaborator

Closes #396

This PR permits constructor calls like dpctl.memory.MemoryUSMShared( obj_with_suai ) to work with Python object whose __sycl_usm_array_interface__ dictionary specifies non-contiguous strides, and non-byte-size type-string, used to address elements of a contiguous block of memory.

I.e. index tuple (i0, i1, ...) would address element offset + strides[0]*i0 + strides[1]*i1 + ... in C-array of dimension shape[0]*shape[1]*....

In this case type-less USM memory buffer created addresses memory from the minimal index to maxima index (inclusive) generated by enumeration of all displacements offset + strides[0]*i0 + strides[1]*i1 + ... when iterating over all possible tuple of indexes in determined by the shape.

Example:

import numpy as np
import dpctl
import dpctl.memory as dpmem

class View:
    def __init__(self, buffer, shape, strides, offset):
        self.buffer=buffer
        self.shape=shape
        self.strides=strides
        self.offset = offset

    @property
    def __sycl_usm_array_interface__(self):
        sua_iface = self.buffer.__sycl_usm_array_interface__
        sua_iface['offset'] = self.offset
        sua_iface['shape'] = self.shape
        sua_iface['strides'] = self.strides
        return sua_iface

# Allocate 32 bytes, and fill them with canary numbers
buf = dpmem.MemoryUSMShared(32)
buf.copy_from_host(np.full((32,), 77, dtype='|u1'))

# View of 10 bytes, starting with byte 8, and going every other byte
v = View(buf, (10,), strides=(2,), offset=8)

# Create USMShared buffer from the view 
# spanning from the lowest byte, offset=8 to highest byte, offset=8 +(10-1)*2
buf2 = dpmem.MemoryUSMShared(v)
# That is total of 19 bytes, (counting the last one too)
assert buf2.nbytes == 19
# populate this buffer with different values
buf2.copy_from_host(np.full((19,), 1, dtype="|u1"))
# copy content of the parent buffer to host
res = buf.copy_to_host()
del buf
del buf2
print(res)

Executing this prints

[77 77 77 77 77 77 77 77  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1 77 77 77 77 77]

@oleksandr-pavlyk oleksandr-pavlyk changed the title Feature/implement issue 396 Implemented support for constructign MemoryUSM* from object with __sycl_usm_array_interface__ when array-info is not contiguous Apr 20, 2021
@oleksandr-pavlyk oleksandr-pavlyk changed the title Implemented support for constructign MemoryUSM* from object with __sycl_usm_array_interface__ when array-info is not contiguous Implemented support for constructing MemoryUSM* from object with __sycl_usm_array_interface__ when array-info is not contiguous Apr 20, 2021
@oleksandr-pavlyk oleksandr-pavlyk force-pushed the feature/implement-issue-396 branch from e28b8c8 to 5f66623 Compare April 22, 2021 00:57
Copy link
Contributor

@diptorupd diptorupd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please squash and merge.

@diptorupd diptorupd merged commit ee1d590 into master Apr 23, 2021
@diptorupd diptorupd deleted the feature/implement-issue-396 branch April 23, 2021 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants