Skip to content

Commit 40dcdf2

Browse files
committed
document passing a split local accessor
A LocalAccessor works for a kernel compiled from SYCL to SPIR-V when the accessor's ranges are passed after it; say so in the docstring and the error, and test it.
1 parent 1dfebbb commit 40dcdf2

2 files changed

Lines changed: 30 additions & 15 deletions

File tree

‎dpctl/_sycl_queue.pyx‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,15 @@ cdef class LocalAccessor:
175175
Python class for specifying the dimensionality and type of a
176176
``sycl::local_accessor``, to be used as a kernel argument type.
177177
178-
A ``LocalAccessor`` is passed as a single kernel argument. A
179-
``sycl::local_accessor`` parameter of a kernel compiled from SYCL to
180-
SPIR-V can take several arguments, so such a kernel can not be given a
181-
``LocalAccessor``; create it with
182-
:func:`dpctl.compiler.create_kernel_bundle_from_sycl_source` instead.
178+
A ``LocalAccessor`` is passed as a single kernel argument, the pointer to
179+
the local memory. A kernel compiled from SYCL to SPIR-V may expect a
180+
``sycl::local_accessor`` parameter as several arguments instead: the
181+
pointer, followed by the accessor's access range, memory range and
182+
offset. Pass the ``LocalAccessor`` as the first of them and the others
183+
explicitly, e.g. for a one-dimensional accessor of ``n`` elements::
184+
185+
[dpctl.LocalAccessor("i4", (n,)), ctypes.c_size_t(n),
186+
ctypes.c_size_t(n), ctypes.c_size_t(0), ...]
183187
184188
Args:
185189
dtype (str):
@@ -1588,9 +1592,10 @@ cdef class SyclQueue(_SyclQueue):
15881592
[isinstance(arg, LocalAccessor) for arg in args]
15891593
):
15901594
msg += (
1591-
" A sycl::local_accessor parameter of a kernel "
1592-
"compiled from SYCL to SPIR-V can take several "
1593-
"arguments, see the dpctl.LocalAccessor documentation."
1595+
" A kernel compiled from SYCL to SPIR-V may expect a "
1596+
"sycl::local_accessor parameter as several arguments, "
1597+
"of which a dpctl.LocalAccessor is only the first; "
1598+
"see the dpctl.LocalAccessor documentation."
15941599
)
15951600
raise ValueError(msg)
15961601

‎dpctl/tests/test_sycl_kernel_submit.py‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -477,18 +477,28 @@ def test_submit_local_accessor_arg_spirv_from_sycl():
477477
spirv_bytes = f.read()
478478
kb = dpc.create_kernel_bundle_from_spirv(q, spirv_bytes)
479479
krn = kb.get_sycl_kernel("__sycl_kernel_local_accessor_kernel")
480-
# the local accessor takes several arguments in this SPIR-V
481-
assert krn.num_args > 2
480+
# the local accessor is the pointer, access range, memory range and
481+
# offset in this SPIR-V
482+
assert krn.num_args == 5
482483

483-
x_usm = dpm.MemoryUSMDevice(64, queue=q)
484+
lws = 16
485+
gws = lws * 4
486+
x = np.arange(gws, dtype="i4")
487+
x_usm = dpm.MemoryUSMDevice(x.nbytes, queue=q)
488+
q.memcpy(dest=x_usm, src=x, count=x.nbytes)
489+
la = dpctl.LocalAccessor("i4", (lws,))
484490
with pytest.raises(ValueError, match="dpctl.LocalAccessor documentation"):
485-
q.submit_async(
486-
krn, [dpctl.LocalAccessor("i4", (16,)), x_usm], [16], [16]
487-
)
491+
q.submit_async(krn, [la, x_usm], [gws], [lws])
488492
with pytest.raises(ValueError) as e:
489-
q.submit_async(krn, [x_usm], [16], [16])
493+
q.submit_async(krn, [x_usm], [gws], [lws])
490494
assert "LocalAccessor" not in str(e.value)
491495

496+
sz = ctypes.c_size_t
497+
q.submit(krn, [la, sz(lws), sz(lws), sz(0), x_usm], [gws], [lws]).wait()
498+
res = np.empty_like(x)
499+
q.memcpy(dest=res, src=x_usm, count=x.nbytes)
500+
assert np.array_equal(res, x.reshape(-1, lws)[:, ::-1].ravel())
501+
492502

493503
def test_submit_wrong_number_of_args_opencl_source():
494504
try:

0 commit comments

Comments
 (0)