Skip to content

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

Merged
diptorupd merged 2 commits intomasterfrom
feature/implement-issue-396
Apr 23, 2021
Merged

Implemented support for constructing MemoryUSM* from object with __sycl_usm_array_interface__ when array-info is not contiguous#400
diptorupd merged 2 commits intomasterfrom
feature/implement-issue-396

Conversation

@oleksandr-pavlyk
Copy link
Copy Markdown
Contributor

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
Comment thread dpctl/memory/_sycl_usm_array_interface_utils.pxi
Comment thread dpctl/memory/_sycl_usm_array_interface_utils.pxi Outdated
Comment thread dpctl/memory/_sycl_usm_array_interface_utils.pxi
Comment thread dpctl/memory/_sycl_usm_array_interface_utils.pxi
Comment thread dpctl/memory/_sycl_usm_array_interface_utils.pxi
Comment thread dpctl/memory/_sycl_usm_array_interface_utils.pxi Outdated
@oleksandr-pavlyk oleksandr-pavlyk force-pushed the feature/implement-issue-396 branch from e28b8c8 to 5f66623 Compare April 22, 2021 00:57
Comment thread dpctl/memory/_sycl_usm_array_interface_utils.pxi
Copy link
Copy Markdown
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

Development

Successfully merging this pull request may close these issues.

MemoryUSM* constructors should be able to handle objects with __sycl_usm_array_interface__ and non-trivial strides.

2 participants