Skip to content

Add dpctl.SyclQueue.memset() method - #2361

Open
vlad-perevezentsev wants to merge 19 commits into
masterfrom
add_sycl_queue_memset
Open

Add dpctl.SyclQueue.memset() method#2361
vlad-perevezentsev wants to merge 19 commits into
masterfrom
add_sycl_queue_memset

Conversation

@vlad-perevezentsev

@vlad-perevezentsev vlad-perevezentsev commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

This PR proposes adding dpctl.SyclQueue.memset and dpctl.SyclQueue.memset_async methods as a Python wrapper over sycl::queue::memset backed by the existing DPCTLQueue_Memset and a new DPCTLQueue_MemsetWithEvents C-API function and adding a new test_sycl_queue_memset.py

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?
  • If this PR is a work in progress, are you opening the PR as a draft?

@github-actions

Copy link
Copy Markdown

@vlad-perevezentsev

Copy link
Copy Markdown
Collaborator Author

@ndgrigorian I implemented cpdef memset without validating val, i.e. matching sycl::queue::memset semantics where the value is silently truncated toits least significant byte.
Does it make sense to explicitly validate that val is within [0, 255]?

@coveralls

coveralls commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

Coverage Status

Coverage is 74.51%add_sycl_queue_memset into master. No base build found for master.

Comment thread dpctl/_sycl_queue.pyx
Comment thread dpctl/_sycl_queue.pyx
Comment thread dpctl/_sycl_queue.pyx Outdated
Comment thread dpctl/_sycl_queue.pyx
Comment thread dpctl/tests/test_sycl_queue_memset.py
Comment thread dpctl/_sycl_queue.pxd
Comment thread dpctl/_backend.pxd Outdated
Comment thread dpctl/_sycl_queue.pyx
Comment thread libsyclinterface/include/syclinterface/dpctl_sycl_queue_interface.h Outdated
Comment thread libsyclinterface/include/syclinterface/dpctl_sycl_queue_interface.h Outdated
Comment thread dpctl/tests/test_sycl_queue_memset.py
host = bytearray(nbytes)
q.memcpy(host, mobj, nbytes)
return bytes(host)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The docstring promises val is interpreted as unsigned char (LSB only), but nothing verifies e.g. memset(mem, 0x1FF) fills 0xFF or -1 fills 0xFF. There is no error case to test here (values truncate, not reject).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This case is already covered by test_memset_value_truncated_to_byte (negatives and overflow)
https://github.com/IntelPython/dpctl/pull/2361/changes#diff-8cf72656d339e0f446c2227869696dee611bdce0bc37434f62419482dc4cd075R192

Comment thread libsyclinterface/source/dpctl_sycl_queue_interface.cpp Outdated
Comment thread libsyclinterface/source/dpctl_sycl_queue_interface.cpp Outdated
Comment thread libsyclinterface/source/dpctl_sycl_queue_interface.cpp Outdated
Comment thread dpctl/_sycl_queue.pyx Outdated
Comment thread dpctl/_sycl_queue.pyx
mobj = _create_memory(q, nbytes)

e1 = q.memset_async(mobj, 0x01)
e2 = q.memset_async(mobj, 0x02, nbytes, [e1])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

e2 overwrites the entire buffer with 0x02, the final state is 0x02 no matter what e1 did or whether e2 actually waited for it.
To actually prove the dependency you'd need the two ops to interact where order changes the value — e.g. e1 fills the whole buffer with 0x01, e2 (depending on e1) fills only the first half with 0x02, then assert first half 0x02 + second half 0x01.

except dpctl.SyclQueueCreationError:
pytest.skip("Default constructor for SyclQueue failed")
nbytes = 256
mobj = mem_cls(nbytes, queue=q)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not every SYCL device supports every USM kind. Needs to check the aspect (or catch the allocation failure) and pytest.skip when unsupported.
Applicable to the memcpy tests also.

Comment thread dpctl/_sycl_queue.pyx
``mem``, the whole allocation is filled. Default: ``0``.

Raises:
TypeError:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Might raise OverflowError also:

import dpctl
import dpctl.memory as dpm

q = dpctl.SyclQueue()
m = dpm.MemoryUSMShared(16, queue=q)

# val too big for a C int (typically 32-bit here):
q.memset(m, 2**40)
# OverflowError: Python int too large to convert to C long

# negative count into an unsigned size_t:
q.memset(m, 0xAB, -1)
# OverflowError: can't convert negative value to size_t

return nullptr;
}

return wrap<event>(new event(ev));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return wrap<event>(new event(ev));
return wrap<event>(new event(std::move(ev)));

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.

4 participants