Repository navigation
Expand file tree
/
Copy pathtest_sycl_kernel_submit.py
More file actions
370 lines (334 loc) · 12 KB
/
Copy pathtest_sycl_kernel_submit.py
File metadata and controls
370 lines (334 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# Data Parallel Control (dpctl)
#
# Copyright 2020 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 kernel submission to a sycl::queue."""
import ctypes
import os
import numpy as np
import pytest
import dpctl
import dpctl.compiler as dpc
import dpctl.memory as dpm
from dpctl._sycl_queue import kernel_arg_type
@pytest.mark.parametrize(
"ctype_str,dtype,ctypes_ctor",
[
("short", np.dtype("i2"), ctypes.c_short),
("int", np.dtype("i4"), ctypes.c_int),
("unsigned int", np.dtype("u4"), ctypes.c_uint),
("long", np.dtype(np.longlong), ctypes.c_longlong),
("unsigned long", np.dtype(np.ulonglong), ctypes.c_ulonglong),
("float", np.dtype("f4"), ctypes.c_float),
("double", np.dtype("f8"), ctypes.c_double),
],
)
def test_create_kernel_bundle_from_source(ctype_str, dtype, ctypes_ctor):
try:
q = dpctl.SyclQueue("opencl", property="enable_profiling")
except dpctl.SyclQueueCreationError:
pytest.skip("OpenCL queue could not be created")
if dtype == np.dtype("f8") and q.sycl_device.has_aspect_fp64 is False:
pytest.skip(
"Device does not support double precision floating point type"
)
# OpenCL conventions for indexing global_id is opposite to
# that of SYCL (and DPCTL)
oclSrc = (
"kernel void axpy("
" global " + ctype_str + " *a, global " + ctype_str + " *b,"
" global " + ctype_str + " *c, " + ctype_str + " d) {"
" size_t index = get_global_id(0);"
" c[index] = d * a[index] + b[index];"
"}"
)
kb = dpc.create_kernel_bundle_from_source(q, oclSrc)
axpyKernel = kb.get_sycl_kernel("axpy")
n_elems = 1024 * 512
lws = 128
if dtype.kind in "ui":
n_elems = min(n_elems, np.iinfo(dtype).max)
n_elems = (n_elems // lws) * lws
a = np.arange(n_elems, dtype=dtype)
b = np.arange(n_elems, stop=0, step=-1, dtype=dtype)
c = np.zeros(n_elems, dtype=dtype)
a_usm = dpm.MemoryUSMDevice(a.nbytes, queue=q)
b_usm = dpm.MemoryUSMDevice(b.nbytes, queue=q)
c_usm = dpm.MemoryUSMDevice(c.nbytes, queue=q)
ev1 = q.memcpy_async(dest=a_usm, src=a, count=a.nbytes)
ev2 = q.memcpy_async(dest=b_usm, src=b, count=b.nbytes)
dpctl.SyclEvent.wait_for([ev1, ev2])
d = 2
args = [a_usm, b_usm, c_usm, ctypes_ctor(d)]
assert n_elems % lws == 0
for r in (
[
n_elems,
],
[2, n_elems],
[2, 2, n_elems],
):
c_usm.memset()
timer = dpctl.SyclTimer()
with timer(q):
q.submit(axpyKernel, args, r).wait()
ref_c = a * np.array(d, dtype=dtype) + b
host_dt, device_dt = timer.dt
assert type(host_dt) is float and type(device_dt) is float
q.memcpy(c, c_usm, c.nbytes)
assert np.allclose(c, ref_c), f"Failed for {r}"
for gr, lr in (
(
[
n_elems,
],
[lws],
),
([2, n_elems], [2, lws // 2]),
([2, 2, n_elems], [2, 2, lws // 4]),
):
c_usm.memset()
timer = dpctl.SyclTimer()
with timer(q):
q.submit(axpyKernel, args, gr, lr, [dpctl.SyclEvent()]).wait()
ref_c = a * np.array(d, dtype=dtype) + b
host_dt, device_dt = timer.dt
assert type(host_dt) is float and type(device_dt) is float
q.memcpy(c, c_usm, c.nbytes)
assert np.allclose(c, ref_c), f"Failed for {gr}, {lr}"
def test_submit_async():
try:
q = dpctl.SyclQueue("opencl")
except dpctl.SyclQueueCreationError:
pytest.skip("OpenCL queue could not be created")
oclSrc = (
"kernel void kern1("
" global unsigned int *res_base, ulong res_off, unsigned int mod) {"
" size_t unused_sum = 0;"
" size_t i = 0; "
" for (i = 0; i < 4000; i++) { "
" unused_sum += i;"
" } "
" global unsigned int *res = res_base + (size_t)res_off;"
" size_t index = get_global_id(0);"
" int ri = (index % mod);"
" res[index] = (ri * ri) % mod;"
"}"
" "
"kernel void kern2("
" global unsigned int *res_base, ulong res_off, unsigned int mod) {"
" size_t unused_sum = 0;"
" size_t i = 0; "
" for (i = 0; i < 4000; i++) { "
" unused_sum += i;"
" } "
" global unsigned int *res = res_base + (size_t)res_off;"
" size_t index = get_global_id(0);"
" int ri = (index % mod);"
" int ri2 = (ri * ri) % mod;"
" res[index] = (ri2 * ri) % mod;"
"}"
" "
"kernel void kern3("
" global unsigned int *res_base, ulong res_off,"
" global unsigned int *arg1_base, ulong arg1_off,"
" global unsigned int *arg2_base, ulong arg2_off)"
"{"
" global unsigned int *res = res_base + (size_t)res_off;"
" global unsigned int *arg1 = arg1_base + (size_t)arg1_off;"
" global unsigned int *arg2 = arg2_base + (size_t)arg2_off;"
" size_t index = get_global_id(0);"
" size_t i = 0; "
" size_t unused_sum = 0;"
" for (i = 0; i < 4000; i++) { "
" unused_sum += i;"
" } "
" res[index] = "
" (arg1[index] < arg2[index]) ? arg1[index] : arg2[index];"
"}"
)
kb = dpc.create_kernel_bundle_from_source(q, oclSrc)
kern1Kernel = kb.get_sycl_kernel("kern1")
kern2Kernel = kb.get_sycl_kernel("kern2")
kern3Kernel = kb.get_sycl_kernel("kern3")
assert isinstance(kern1Kernel, dpc.SyclKernel)
assert isinstance(kern2Kernel, dpc.SyclKernel)
assert isinstance(kern2Kernel, dpc.SyclKernel)
status_complete = dpctl.event_status_type.complete
# choose input size based on capability of the device
f = q.sycl_device.max_work_group_size
n = f * 1024
n_alloc = 4 * n
x = np.empty((3, n_alloc), dtype="u4")
x_usm = dpm.MemoryUSMDevice(x.nbytes, queue=q)
e1 = q.memcpy_async(dest=x_usm, src=x, count=x.nbytes)
p1, p2 = 17, 27
async_detected = False
for attempt in range(5):
e1 = q.submit_async(
kern1Kernel,
[
x_usm,
ctypes.c_ulonglong(0),
ctypes.c_uint(p1),
],
[
n,
],
None,
[e1],
)
e2 = q.submit_async(
kern2Kernel,
[
x_usm,
ctypes.c_ulonglong(n_alloc),
ctypes.c_uint(p2),
],
[
n,
],
None,
[e1],
)
e3 = q.submit_async(
kern3Kernel,
[
x_usm,
ctypes.c_ulonglong(2 * n_alloc),
x_usm,
ctypes.c_ulonglong(0),
x_usm,
ctypes.c_ulonglong(n_alloc),
],
[
n,
],
None,
[e1, e2],
)
e3_st = e3.execution_status
e2_st = e2.execution_status
e1_st = e1.execution_status
ht_e = q._submit_keep_args_alive([x_usm], [e1, e2, e3])
are_complete = [
e == status_complete
for e in (
e1_st,
e2_st,
e3_st,
)
]
e3.wait()
ht_e.wait()
if not all(are_complete):
async_detected = True
break
else:
n = n * (1 if attempt % 2 == 0 else 2)
if n > n_alloc:
break
assert async_detected, "No evidence of async submission detected, unlucky?"
q.memcpy(dest=x, src=x_usm, count=x.nbytes)
x_ref = np.empty((3, n), dtype="u4")
for i in range(n):
x_ref[0, i] = (i * i) % p1
x_ref[1, i] = (i * i * i) % p2
x_ref[2, i] = min(x_ref[0, i], x_ref[1, i])
assert np.array_equal(x[:, :n], x_ref[:, :n])
def _check_kernel_arg_type_instance(kati):
assert isinstance(kati.name, str)
assert isinstance(kati.value, int)
assert isinstance(repr(kati), str)
assert isinstance(str(kati), str)
def test_kernel_arg_type():
"""
Check that enum values for kernel_arg_type start at 0,
as numba_dpex expects. The next enumerated type must
have next value.
"""
assert isinstance(kernel_arg_type.__name__, str)
assert isinstance(repr(kernel_arg_type), str)
assert isinstance(str(kernel_arg_type), str)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_int8)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_uint8)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_int16)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_uint16)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_int32)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_uint32)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_int64)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_uint64)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_float32)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_float64)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_void_ptr)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_local_accessor)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_work_group_memory)
_check_kernel_arg_type_instance(kernel_arg_type.dpctl_raw_kernel_arg)
def get_spirv_abspath(fn):
curr_dir = os.path.dirname(os.path.abspath(__file__))
spirv_file = os.path.join(curr_dir, "input_files", fn)
return spirv_file
# the process for generating the .spv files in this test is documented in
# libsyclinterface/tests/test_sycl_queue_submit_local_accessor_arg.cpp
# in a comment starting on line 123
def test_submit_local_accessor_arg():
try:
q = dpctl.SyclQueue("level_zero")
except dpctl.SyclQueueCreationError:
pytest.skip("OpenCL queue could not be created")
fn = get_spirv_abspath("local_accessor_kernel_inttys_fp32.spv")
with open(fn, "br") as f:
spirv_bytes = f.read()
kb = dpc.create_kernel_bundle_from_spirv(q, spirv_bytes)
krn = kb.get_sycl_kernel("_ZTS14SyclKernel_SLMIlE")
lws = 32
gws = lws * 10
x = np.ones(gws, dtype="i8")
res = np.empty_like(x)
x_usm = dpm.MemoryUSMDevice(x.nbytes, queue=q)
q.memcpy(dest=x_usm, src=x, count=x.nbytes)
try:
e = q.submit(
krn,
[x_usm, dpctl.LocalAccessor("i8", (lws,))],
[gws],
[lws],
)
e.wait()
except dpctl._sycl_queue.SyclKernelSubmitError:
pytest.skip(f"Kernel submission failed for device {q.sycl_device}")
q.memcpy(dest=res, src=x_usm, count=x.nbytes)
expected = np.arange(1, x.size + 1, dtype=x.dtype) * (2 * lws)
assert np.all(res == expected)
def test_submit_wrong_number_of_args():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Could not create default queue")
fn = get_spirv_abspath("multi_kernel.spv")
with open(fn, "br") as f:
spirv_bytes = f.read()
kb = dpc.create_kernel_bundle_from_spirv(q, spirv_bytes)
krn = kb.get_sycl_kernel("axpy")
assert krn.num_args == 4
n = 16
x_usm = dpm.MemoryUSMDevice(n * 4, queue=q)
with pytest.raises(ValueError):
q.submit(krn, [x_usm, x_usm, x_usm], [n])
with pytest.raises(ValueError):
q.submit_async(
krn,
[x_usm, x_usm, x_usm, ctypes.c_int(2), ctypes.c_int(3)],
[n],
)