Skip to content

Commit 09257b7

Browse files
[SYCL] tests of kernel bundle after adding kernel free function (#19518)
This PR adds tests of new kernel bundle API according to [test-plan](https://github.com/intel/llvm/blob/sycl/sycl/test-e2e/FreeFunctionKernels/test-plan.md#perform-tests-on-new-kernel_bundle-member-functions-for-free-function-kernels-by-declaring-nd_range_kernel-and-single_task_kernel-and-verifying-that). Functionality of num_args is added and tests in the [separate PR](#19517)
1 parent dc3027a commit 09257b7

File tree

3 files changed

+149
-2
lines changed

3 files changed

+149
-2
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// RUN: %{build} -o %t.out
2+
// RUN: %{run} %t.out
3+
4+
#include <iostream>
5+
#include <sycl/detail/core.hpp>
6+
#include <sycl/ext/oneapi/get_kernel_info.hpp>
7+
#include <sycl/kernel_bundle.hpp>
8+
9+
namespace syclext = sycl::ext::oneapi;
10+
namespace syclexp = sycl::ext::oneapi::experimental;
11+
12+
static constexpr size_t NUM = 1024;
13+
static constexpr size_t WGSIZE = 16;
14+
static constexpr auto FFTestMark = "Free function Kernel Test:";
15+
16+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<2>))
17+
void func_range(float start, float *ptr) {}
18+
19+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::single_task_kernel))
20+
void func_single(float start, float *ptr) {}
21+
22+
template <auto *Func> int test_free_function_kernel_id(sycl::context &ctxt) {
23+
sycl::kernel_id id = syclexp::get_kernel_id<Func>();
24+
auto exe_bndl =
25+
syclexp::get_kernel_bundle<Func, sycl::bundle_state::executable>(ctxt);
26+
const auto kernel_ids = exe_bndl.get_kernel_ids();
27+
const bool res =
28+
std::find(kernel_ids.begin(), kernel_ids.end(), id) == kernel_ids.end();
29+
if (res)
30+
std::cout << FFTestMark << "test_kernel_id failed: kernel id is not found"
31+
<< std::endl;
32+
return res;
33+
}
34+
35+
template <auto *Func>
36+
int test_kernel_bundle_ctxt(sycl::context &ctxt, std::string_view fname) {
37+
sycl::kernel_id id = syclexp::get_kernel_id<Func>();
38+
auto exe_bndl =
39+
syclexp::get_kernel_bundle<Func, sycl::bundle_state::executable>(ctxt);
40+
const bool res =
41+
exe_bndl.has_kernel(id) &&
42+
exe_bndl.get_kernel(id)
43+
.template get_info<sycl::info::kernel::function_name>() == fname;
44+
if (!res)
45+
std::cout
46+
<< FFTestMark
47+
<< "test_kernel_bundle_ctxt failed: bundle does not contain kernel id "
48+
"or function name "
49+
<< fname << std::endl;
50+
return res;
51+
}
52+
53+
template <auto *Func>
54+
int test_kernel_bundle_ctxt_dev(sycl::context &ctxt, sycl::device &dev,
55+
std::string_view fname) {
56+
sycl::kernel_id id = syclexp::get_kernel_id<Func>();
57+
auto exe_bndl =
58+
syclexp::get_kernel_bundle<Func, sycl::bundle_state::executable>(ctxt,
59+
{dev});
60+
const bool res =
61+
exe_bndl.has_kernel(id) &&
62+
exe_bndl.get_kernel(id)
63+
.template get_info<sycl::info::kernel::function_name>() == fname;
64+
if (!res)
65+
std::cout << FFTestMark
66+
<< "test_kernel_bundle_ctxt_dev failed: bundle does not contain "
67+
"kernel id "
68+
"or function name "
69+
<< fname << std::endl;
70+
return res;
71+
}
72+
73+
int main() {
74+
sycl::queue q;
75+
sycl::context ctxt = q.get_context();
76+
sycl::device dev = q.get_device();
77+
78+
int ret = test_free_function_kernel_id<func_range>(ctxt);
79+
ret |= test_free_function_kernel_id<func_single>(ctxt);
80+
ret |= test_kernel_bundle_ctxt<func_range>(ctxt, "func_range");
81+
ret |= test_kernel_bundle_ctxt<func_single>(ctxt, "func_single");
82+
ret |= test_kernel_bundle_ctxt_dev<func_range>(ctxt, dev, "func_range");
83+
ret |= test_kernel_bundle_ctxt_dev<func_single>(ctxt, dev, "func_single");
84+
return ret;
85+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %{build} -fsycl-device-code-split=per_kernel -o %t.out
2+
// RUN: %{run} %t.out
3+
4+
#include <iostream>
5+
#include <sycl/detail/core.hpp>
6+
#include <sycl/ext/oneapi/get_kernel_info.hpp>
7+
#include <sycl/kernel_bundle.hpp>
8+
9+
namespace syclexp = sycl::ext::oneapi::experimental;
10+
11+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<2>))
12+
void func_range(float start, float *ptr) {}
13+
14+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::single_task_kernel))
15+
void func_single(float start, float *ptr) {}
16+
17+
int main() {
18+
sycl::queue q;
19+
auto bundle_range =
20+
syclexp::get_kernel_bundle<func_range, sycl::bundle_state::executable>(
21+
q.get_context());
22+
try {
23+
sycl::kernel k = bundle_range.ext_oneapi_get_kernel<func_single>();
24+
} catch (const sycl::exception &e) {
25+
assert(e.code() == sycl::errc::invalid);
26+
return 0;
27+
}
28+
auto bundle_single =
29+
syclexp::get_kernel_bundle<func_single, sycl::bundle_state::executable>(
30+
q.get_context());
31+
try {
32+
sycl::kernel k = bundle_single.ext_oneapi_get_kernel<func_range>();
33+
} catch (const sycl::exception &e) {
34+
assert(e.code() == sycl::errc::invalid);
35+
return 0;
36+
}
37+
return 1;
38+
}

sycl/test-e2e/KernelAndProgram/free_function_apis.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ void ff_2(int *ptr, int start) {
2222
ptr2D[GId.get(0)][GId.get(1)] = LId.get(0) + LId.get(1) + start;
2323
}
2424

25+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(
26+
(ext::oneapi::experimental::nd_range_kernel<2>))
27+
void ff_b(int *ptr, int start) {}
28+
2529
// Templated free function definition.
2630
template <typename T>
2731
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(
@@ -97,6 +101,21 @@ bool test_kernel_apis(queue Queue) {
97101
return Pass;
98102
}
99103

104+
template <auto *Func>
105+
bool check_kernel_id(const sycl::kernel k, const sycl::context &ctx) {
106+
namespace exp = ext::oneapi::experimental;
107+
const auto id = exp::get_kernel_id<Func>();
108+
auto exe_bndl =
109+
exp::get_kernel_bundle<Func, sycl::bundle_state::executable>(ctx);
110+
if (!exe_bndl.has_kernel(id))
111+
return false;
112+
const auto kb = sycl::get_kernel_bundle<sycl::bundle_state::executable>(ctx);
113+
const auto kernel_ids = kb.get_kernel_ids();
114+
bool ret =
115+
std::find(kernel_ids.begin(), kernel_ids.end(), id) != kernel_ids.end();
116+
return ret;
117+
}
118+
100119
bool test_bundle_apis(queue Queue) {
101120
bool Pass = true;
102121

@@ -134,6 +153,10 @@ bool test_bundle_apis(queue Queue) {
134153
std::cout << "PassE=" << PassE << std::endl;
135154
Pass &= PassE;
136155

156+
bool PassE2 = ext::oneapi::experimental::is_compatible<ff_2>(Device);
157+
std::cout << "PassE2=" << PassE2 << std::endl;
158+
Pass &= PassE2;
159+
137160
// Check that ff_2 is found in bundle.
138161
kernel_bundle Bundle2 = ext::oneapi::experimental::get_kernel_bundle<
139162
ff_2, bundle_state::executable>(Context);
@@ -145,7 +168,7 @@ bool test_bundle_apis(queue Queue) {
145168
std::cout << "PassG=" << PassG << std::endl;
146169
Pass &= PassG;
147170
kernel Kernel2 = Bundle2.ext_oneapi_get_kernel<ff_2>();
148-
bool PassH = true;
171+
bool PassH = check_kernel_id<ff_2>(Kernel2, Context);
149172
std::cout << "PassH=" << PassH << std::endl;
150173
Pass &= PassH;
151174

@@ -162,7 +185,8 @@ bool test_bundle_apis(queue Queue) {
162185
Pass &= PassJ;
163186
kernel Kernel3 =
164187
Bundle3.ext_oneapi_get_kernel<(void (*)(int *, int))ff_3<int>>();
165-
bool PassK = true;
188+
bool PassK =
189+
check_kernel_id<(void (*)(int *, int))ff_3<int>>(Kernel3, Context);
166190
std::cout << "PassK=" << PassK << std::endl;
167191
Pass &= PassK;
168192

0 commit comments

Comments
 (0)