Skip to content

Allow for mutation of usm_ndarray.flags.writable flag #1141

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

Merged
merged 2 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions dpctl/tensor/_flags.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ cdef class Flags:
"""
return _check_bit(self.flags_, USM_ARRAY_WRITABLE)

@writable.setter
def writable(self, new_val):
if not isinstance(new_val, bool):
raise TypeError("Expecting a boolean value")
self.arr_._set_writable_flag(new_val)

@property
def fc(self):
"""
Expand Down Expand Up @@ -129,6 +135,14 @@ cdef class Flags:
elif name == "CONTIGUOUS":
return self.forc

def __setitem__(self, name, val):
if name in ["WRITABLE", "W"]:
self.writable = val
else:
raise ValueError(
"Only writable ('W' or 'WRITABLE') flag can be set"
)

def __repr__(self):
out = []
for name in "C_CONTIGUOUS", "F_CONTIGUOUS", "WRITABLE":
Expand Down
2 changes: 2 additions & 0 deletions dpctl/tensor/_usmarray.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ cdef api class usm_ndarray [object PyUSMArrayObject, type PyUSMArrayType]:
cdef dpctl.DPCTLSyclQueueRef get_queue_ref(self) except *
cdef dpctl.SyclQueue get_sycl_queue(self)

cdef _set_writable_flag(self, int)

cdef __cythonbufferdefaults__ = {"mode": "strided"}
12 changes: 8 additions & 4 deletions dpctl/tensor/_usmarray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,12 @@ cdef class usm_ndarray:
"""
return _flags.Flags(self, self.flags_)

cdef _set_writable_flag(self, int flag):
cdef int arr_fl = self.flags_
arr_fl ^= (arr_fl & USM_ARRAY_WRITABLE) # unset WRITABLE flag
arr_fl |= (USM_ARRAY_WRITABLE if flag else 0)
self.flags_ = arr_fl

@property
def usm_type(self):
"""
Expand Down Expand Up @@ -1390,12 +1396,10 @@ cdef api Py_ssize_t UsmNDArray_GetOffset(usm_ndarray arr):
allocation"""
return arr.get_offset()


cdef api void UsmNDArray_SetWritableFlag(usm_ndarray arr, int flag):
"""Set/unset USM_ARRAY_WRITABLE in the given array `arr`."""
cdef int arr_fl = arr.flags_
arr_fl ^= (arr_fl & USM_ARRAY_WRITABLE) # unset WRITABLE flag
arr_fl |= (USM_ARRAY_WRITABLE if flag else 0)
arr.flags_ = arr_fl
arr._set_writable_flag(flag)

cdef api object UsmNDArray_MakeSimpleFromMemory(
int nd, const Py_ssize_t *shape, int typenum,
Expand Down
12 changes: 12 additions & 0 deletions dpctl/tests/test_usm_ndarray_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_allocate_usm_ndarray(shape, usm_type):


def test_usm_ndarray_flags():
get_queue_or_skip()
assert dpt.usm_ndarray((5,), dtype="i4").flags.fc
assert dpt.usm_ndarray((5, 2), dtype="i4").flags.c_contiguous
assert dpt.usm_ndarray((5, 2), dtype="i4", order="F").flags.f_contiguous
Expand All @@ -68,6 +69,17 @@ def test_usm_ndarray_flags():
(5, 1, 2), dtype="i4", strides=(1, 0, 5)
).flags.f_contiguous
assert dpt.usm_ndarray((5, 1, 1), dtype="i4", strides=(1, 0, 1)).flags.fc
x = dpt.empty(5, dtype="u2")
assert x.flags.writable is True
x.flags.writable = False
assert x.flags.writable is False
with pytest.raises(ValueError):
x[:] = 0
x.flags["W"] = True
assert x.flags.writable is True
x.flags["WRITABLE"] = True
assert x.flags.writable is True
x[:] = 0


@pytest.mark.parametrize(
Expand Down