Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
30834c0
Add SyclQueue.memset method
vlad-perevezentsev Aug 13, 2026
4f08b2f
Add tests for SyclQueue.memset
vlad-perevezentsev Aug 13, 2026
a591bcf
Add gh-2361 to changelog
vlad-perevezentsev Aug 13, 2026
af2da99
Add DPCTLQueue_MemsetWithEvents C-API function
vlad-perevezentsev Aug 13, 2026
4b197d5
Add tests for DPCTLQueue_MemsetWithEvents
vlad-perevezentsev Aug 13, 2026
9d63c23
Add DPCTLQueue_MemsetWithEvents declaration to _backend.pxd
vlad-perevezentsev Aug 13, 2026
fd541c0
Add SyclQueue.memset_async method
vlad-perevezentsev Aug 13, 2026
9a3fbe1
Fix typos and improve DPCTLQueue_Memset doc
vlad-perevezentsev Aug 13, 2026
c905349
Add tests for SyclQueue.memset_async
vlad-perevezentsev Aug 13, 2026
8e1cfde
Update gh-2361 changelog entry
vlad-perevezentsev Aug 13, 2026
24d58fa
Fix memset_async error message and improve memset docstrings
vlad-perevezentsev Aug 19, 2026
5661ab9
Fix memset value type to uint8_t
vlad-perevezentsev Aug 19, 2026
51c36df
Cover host/device memory and value truncation in memset tests
vlad-perevezentsev Aug 19, 2026
b6d9804
Merge remote-tracking branch 'origin/master' into add_sycl_queue_memset
vlad-perevezentsev Aug 19, 2026
0f08fe2
Fix stray semicolons and Count docstring in memset C-API
vlad-perevezentsev Aug 19, 2026
0689902
Add a note about keeping mem alive in memset_async docs
vlad-perevezentsev Aug 19, 2026
7240bad
Cast self to SyclQueue in memset_async
vlad-perevezentsev Aug 19, 2026
d584ca6
Add test_memset_fills_bytewise
vlad-perevezentsev Aug 19, 2026
5e0630b
Update an error message in DPCTLQueue_MemsetWithEvents
vlad-perevezentsev Aug 19, 2026
7b4bebc
Apply remarks for _sycl_queue.pyx
vlad-perevezentsev Aug 19, 2026
2359b0c
Update test_sycl_queue_memset.py
vlad-perevezentsev Aug 19, 2026
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added a number of `sycl::device` info queries to `dpctl.SyclDevice` [gh-2324](https://github.com/IntelPython/dpctl/pull/2324)
* Added `sycl::info::context` queries `sycl_platform`, `atomic_memory_order_capabilities`, `atomic_fence_order_capabilities`, `atomic_memory_scope_capabilities`, and `atomic_fence_scope_capabilities` to `dpctl.SyclContext` [gh-2354](https://github.com/IntelPython/dpctl/pull/2354)
* Added `create_kernel_bundle_from_sycl_source`, `is_sycl_source_compilation_available`, and `dpctl.SyclDevice.can_compile` for supporting the creation of `dpctl.SyclKernelBundle`s from SYCL source strings via DPC++ extension, as well as corresponding C-API functions to support it [gh-2206](https://github.com/IntelPython/dpctl/pull/2206)
* Added `dpctl.SyclQueue.memset` method [gh-2361](https://github.com/IntelPython/dpctl/pull/2361)

### Changed
* Bump minimum NumPy version to 1.26 [gh-2192](https://github.com/IntelPython/dpctl/pull/2192)
Expand Down
1 change: 1 addition & 0 deletions dpctl/_sycl_queue.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ cdef public api class SyclQueue (_SyclQueue) [
cpdef SyclEvent copy_async(
self, dest, src, size_t count, list dEvents=*, str dtype=*
)
cpdef memset(self, mem, int val, size_t count=*)
cpdef prefetch(self, ptr, size_t count=*)
cpdef mem_advise(self, ptr, size_t count, int mem)
cpdef SyclEvent submit_barrier(self, dependent_events=*)
Expand Down
43 changes: 43 additions & 0 deletions dpctl/_sycl_queue.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ from ._backend cimport ( # noqa: E211
DPCTLQueue_MemAdvise,
DPCTLQueue_Memcpy,
DPCTLQueue_MemcpyWithEvents,
DPCTLQueue_Memset,
DPCTLQueue_Prefetch,
DPCTLQueue_SubmitBarrierForEvents,
DPCTLQueue_SubmitNDRange,
Expand Down Expand Up @@ -1594,6 +1595,48 @@ cdef class SyclQueue(_SyclQueue):

return SyclEvent._create(ERef)

cpdef memset(self, mem, int val, size_t count=0):
Comment thread
vlad-perevezentsev marked this conversation as resolved.
"""Fill USM allocation ``mem`` with the byte value ``val`` and wait.

Internally, this dispatches ``sycl::queue::memset``. The operation is
byte-wise: ``count`` bytes are set, each to the same value ``val``.
Comment thread
vlad-perevezentsev marked this conversation as resolved.

Args:
mem:
Destination USM allocation, an instance of
:class:`dpctl.memory._Memory`.
val (int):
Value to fill ``mem`` with. Following ``sycl::queue::memset``,
it is interpreted as an ``unsigned char``, i.e. only the least
significant byte is used.
count (int, optional):
Number of bytes to fill. If ``0`` or greater than the size of
``mem``, the whole allocation is filled. Default: ``0``.

Raises:
TypeError:
Comment thread
vlad-perevezentsev marked this conversation as resolved.
If ``mem`` is not an instance of :class:`dpctl.memory._Memory`.
"""
cdef void *ptr
cdef DPCTLSyclEventRef ERef = NULL

if isinstance(mem, _Memory):
ptr = <void*>(<_Memory>mem).get_data_ptr()
else:
raise TypeError("Parameter `mem` should have type _Memory")

if (count <= 0 or count > mem.nbytes):
count = mem.nbytes

ERef = DPCTLQueue_Memset(self._queue_ref, ptr, val, count)
if (ERef is NULL):
raise RuntimeError(
"SyclQueue.memset operation encountered an error"
)
with nogil:
DPCTLEvent_Wait(ERef)
DPCTLEvent_Delete(ERef)

cpdef prefetch(self, mem, size_t count=0):
cdef void *ptr
cdef DPCTLSyclEventRef ERef = NULL
Expand Down
106 changes: 106 additions & 0 deletions dpctl/tests/test_sycl_queue_memset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Data Parallel Control (dpctl)
#
# Copyright 2026 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Defines unit test cases for the SyclQueue.memset."""

import pytest

import dpctl
import dpctl.memory


def _create_memory(q, nbytes=1024):
Comment thread
vlad-perevezentsev marked this conversation as resolved.
return dpctl.memory.MemoryUSMShared(nbytes, queue=q)


def test_memset_fills_whole_allocation():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Default constructor for SyclQueue failed")
nbytes = 256
mobj = _create_memory(q, nbytes)

q.memset(mobj, 0xAB)

assert bytes(memoryview(mobj)) == b"\xab" * nbytes


def test_memset_zero_count_fills_whole_allocation():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Default constructor for SyclQueue failed")
nbytes = 64
mobj = _create_memory(q, nbytes)

q.memset(mobj, 0x01, 0)

assert bytes(memoryview(mobj)) == b"\x01" * nbytes


def test_memset_partial_count():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Default constructor for SyclQueue failed")
nbytes = 16
mobj = _create_memory(q, nbytes)

# zero-out first, then fill only the leading 4 bytes
q.memset(mobj, 0x00)
q.memset(mobj, 0x7F, 4)

assert bytes(memoryview(mobj)) == b"\x7f" * 4 + b"\x00" * (nbytes - 4)


def test_memset_count_clamped_to_allocation():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Default constructor for SyclQueue failed")
nbytes = 8
mobj = _create_memory(q, nbytes)

# requesting more bytes than allocated must not overrun; it is clamped
q.memset(mobj, 0x02, 4 * nbytes)

assert bytes(memoryview(mobj)) == b"\x02" * nbytes


def test_memset_zero_value():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Default constructor for SyclQueue failed")
nbytes = 32
mobj = _create_memory(q, nbytes)

q.memset(mobj, 0xFF)
q.memset(mobj, 0)

assert bytes(memoryview(mobj)) == b"\x00" * nbytes


def test_memset_type_error():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Default constructor for SyclQueue failed")

with pytest.raises(TypeError) as cm:
q.memset(None, 1)
assert "_Memory" in str(cm.value)
Loading