Repository navigation
Expand file tree
/
Copy pathtest_raw_kernel_arg.py
More file actions
119 lines (96 loc) · 3.37 KB
/
Copy pathtest_raw_kernel_arg.py
File metadata and controls
119 lines (96 loc) · 3.37 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
# Data Parallel Control (dpctl)
#
# Copyright 2025 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 work_group_memory in a SYCL kernel"""
import ctypes
import os
import numpy as np
import pytest
import dpctl
import dpctl.memory as dpm
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 kernel in the SPIR-V file used in this test was generated from the
# following SYCL source code:
# #include <sycl/sycl.hpp>
#
# using namespace sycl;
#
# namespace syclexp = sycl::ext::oneapi::experimental;
# namespace syclext = sycl::ext::oneapi;
#
# using data_t = int32_t;
#
# struct Params { data_t mul; data_t add; };
#
# extern "C" SYCL_EXTERNAL
# SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>))
# void raw_arg_kernel(data_t* in, data_t* out, Params p){
# auto item = syclext::this_work_item::get_nd_item<1>();
# size_t global_id = item.get_global_linear_id();
# out[global_id] = (in[global_id] * p.mul) + p.add;
# }
class Params(ctypes.Structure):
_fields_ = [("mul", ctypes.c_int32), ("add", ctypes.c_int32)]
def launch_raw_arg_kernel(raw):
if not dpctl.RawKernelArg.is_available():
pytest.skip("Raw kernel arg extension not supported")
try:
q = dpctl.SyclQueue("level_zero")
except dpctl.SyclQueueCreationError:
pytest.skip("LevelZero queue could not be created")
spirv_file = get_spirv_abspath("raw-arg-kernel.spv")
with open(spirv_file, "br") as spv:
spv_bytes = spv.read()
kb = dpctl.compiler.create_kernel_bundle_from_spirv(q, spv_bytes)
kernel = kb.get_sycl_kernel("__sycl_kernel_raw_arg_kernel")
local_size = 16
global_size = local_size * 8
x = np.ones(global_size, dtype="i4")
y = np.zeros_like(x)
x_usm = dpm.MemoryUSMDevice(x.nbytes, queue=q)
y_usm = dpm.MemoryUSMDevice(y.nbytes, queue=q)
ev1 = q.memcpy_async(dest=x_usm, src=x, count=x.nbytes)
try:
ev2 = q.submit(
kernel,
[
x_usm,
y_usm,
raw,
],
[global_size],
[local_size],
dEvents=[ev1],
)
except dpctl._sycl_queue.SyclKernelSubmitError:
pytest.skip(f"Kernel submission to {q.sycl_device} failed")
ev3 = q.memcpy_async(dest=y, src=y_usm, count=y.nbytes, dEvents=[ev2])
ev3.wait()
assert np.all(y == 9)
def test_submit_raw_kernel_arg_pointer():
paramStruct = Params(4, 5)
raw = dpctl.RawKernelArg(
ctypes.sizeof(paramStruct), ctypes.addressof(paramStruct)
)
launch_raw_arg_kernel(raw)
def test_submit_raw_kernel_arg_buffer():
paramStruct = Params(4, 5)
byteArr = bytearray(paramStruct)
raw = dpctl.RawKernelArg(byteArr)
del byteArr
launch_raw_arg_kernel(raw)