Skip to content

Commit 30834c0

Browse files
Add SyclQueue.memset method
1 parent d81c830 commit 30834c0

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

dpctl/_sycl_queue.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ cdef public api class SyclQueue (_SyclQueue) [
107107
cpdef SyclEvent copy_async(
108108
self, dest, src, size_t count, list dEvents=*, str dtype=*
109109
)
110+
cpdef memset(self, mem, int val, size_t count=*)
110111
cpdef prefetch(self, ptr, size_t count=*)
111112
cpdef mem_advise(self, ptr, size_t count, int mem)
112113
cpdef SyclEvent submit_barrier(self, dependent_events=*)

dpctl/_sycl_queue.pyx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ from ._backend cimport ( # noqa: E211
4949
DPCTLQueue_MemAdvise,
5050
DPCTLQueue_Memcpy,
5151
DPCTLQueue_MemcpyWithEvents,
52+
DPCTLQueue_Memset,
5253
DPCTLQueue_Prefetch,
5354
DPCTLQueue_SubmitBarrierForEvents,
5455
DPCTLQueue_SubmitNDRange,
@@ -1594,6 +1595,48 @@ cdef class SyclQueue(_SyclQueue):
15941595

15951596
return SyclEvent._create(ERef)
15961597

1598+
cpdef memset(self, mem, int val, size_t count=0):
1599+
"""Fill USM allocation ``mem`` with the byte value ``val`` and wait.
1600+
1601+
Internally, this dispatches ``sycl::queue::memset``. The operation is
1602+
byte-wise: ``count`` bytes are set, each to the same value ``val``.
1603+
1604+
Args:
1605+
mem:
1606+
Destination USM allocation, an instance of
1607+
:class:`dpctl.memory._Memory`.
1608+
val (int):
1609+
Value to fill ``mem`` with. Following ``sycl::queue::memset``,
1610+
it is interpreted as an ``unsigned char``, i.e. only the least
1611+
significant byte is used.
1612+
count (int, optional):
1613+
Number of bytes to fill. If ``0`` or greater than the size of
1614+
``mem``, the whole allocation is filled. Default: ``0``.
1615+
1616+
Raises:
1617+
TypeError:
1618+
If ``mem`` is not an instance of :class:`dpctl.memory._Memory`.
1619+
"""
1620+
cdef void *ptr
1621+
cdef DPCTLSyclEventRef ERef = NULL
1622+
1623+
if isinstance(mem, _Memory):
1624+
ptr = <void*>(<_Memory>mem).get_data_ptr()
1625+
else:
1626+
raise TypeError("Parameter `mem` should have type _Memory")
1627+
1628+
if (count <= 0 or count > mem.nbytes):
1629+
count = mem.nbytes
1630+
1631+
ERef = DPCTLQueue_Memset(self._queue_ref, ptr, val, count)
1632+
if (ERef is NULL):
1633+
raise RuntimeError(
1634+
"SyclQueue.memset operation encountered an error"
1635+
)
1636+
with nogil:
1637+
DPCTLEvent_Wait(ERef)
1638+
DPCTLEvent_Delete(ERef)
1639+
15971640
cpdef prefetch(self, mem, size_t count=0):
15981641
cdef void *ptr
15991642
cdef DPCTLSyclEventRef ERef = NULL

0 commit comments

Comments
 (0)