Skip to content

Commit 476014f

Browse files
authored
[SYCL] Fix bug in enqueue free functions implementation (#21461)
There is a bug in some of the enqueue free functions that causes them to work only with 1-dimensional kernels. This PR fixes this bug and adds a couple of test cases to test it.
1 parent 28cdd74 commit 476014f

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

sycl/include/sycl/ext/oneapi/experimental/enqueue_functions.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,16 @@ void nd_launch(queue Q, nd_range<Dimensions> Range,
416416
[[maybe_unused]] kernel_function_s<Func> KernelFunc,
417417
ArgsT &&...Args) {
418418
detail::submit_kernel_direct_parallel_for(
419-
std::move(Q), Range, [Args...](sycl::nd_item<>) { Func(Args...); });
419+
std::move(Q), Range,
420+
[Args...](sycl::nd_item<Dimensions>) { Func(Args...); });
420421
}
421422

422423
template <auto *Func, int Dimensions, typename... ArgsT>
423424
void nd_launch(handler &CGH, nd_range<Dimensions> Range,
424425
[[maybe_unused]] kernel_function_s<Func> KernelFunc,
425426
ArgsT &&...Args) {
426-
CGH.parallel_for(Range, [Args...](sycl::nd_item<>) { Func(Args...); });
427+
CGH.parallel_for(Range,
428+
[Args...](sycl::nd_item<Dimensions>) { Func(Args...); });
427429
}
428430

429431
template <auto *Func, int Dimensions, typename Properties, typename... ArgsT>
@@ -436,7 +438,7 @@ void nd_launch(queue Q, launch_config<nd_range<Dimensions>, Properties> Config,
436438
ConfigAccess(Config);
437439
detail::submit_kernel_direct_parallel_for(
438440
std::move(Q), ConfigAccess.getRange(),
439-
[Args...](sycl::nd_item<>) { Func(Args...); }, {},
441+
[Args...](sycl::nd_item<Dimensions>) { Func(Args...); }, {},
440442
ConfigAccess.getProperties());
441443
}
442444

@@ -449,7 +451,7 @@ void nd_launch(handler &CGH,
449451
Properties>
450452
ConfigAccess(Config);
451453
CGH.parallel_for(ConfigAccess.getRange(), ConfigAccess.getProperties(),
452-
[Args...](sycl::nd_item<>) { Func(Args...); });
454+
[Args...](sycl::nd_item<Dimensions>) { Func(Args...); });
453455
}
454456

455457
inline void memcpy(handler &CGH, void *Dest, const void *Src, size_t NumBytes) {

sycl/test-e2e/FreeFunctionKernels/free_function_kernels_enqueue.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ void square(int *src, int *dst) {
4545
dst[Lid] = src[Lid] * src[Lid];
4646
}
4747

48+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<2>))
49+
void square2D(int *src, int *dst) {
50+
size_t Gid = syclext::this_work_item::get_nd_item<2>().get_global_linear_id();
51+
dst[Gid] = src[Gid] * src[Gid];
52+
}
53+
54+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<3>))
55+
void square3D(int *src, int *dst) {
56+
size_t Gid = syclext::this_work_item::get_nd_item<3>().get_global_linear_id();
57+
dst[Gid] = src[Gid] * src[Gid];
58+
}
59+
4860
template <typename T>
4961
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>))
5062
void squareWithScratchMemoryTemplated(T *src, T *dst) {
@@ -60,7 +72,7 @@ void squareWithAccessor(accType src, accType dst) {
6072
dst[Lid] = src[Lid] * src[Lid];
6173
}
6274

63-
constexpr int SIZE = 16;
75+
constexpr int SIZE = 8;
6476

6577
int main() {
6678
sycl::queue Q;
@@ -175,6 +187,34 @@ int main() {
175187
assert(Dst[I] == Src[I] * Src[I]);
176188
}
177189

190+
int *Src2D = sycl::malloc_shared<int>(SIZE * SIZE, Q);
191+
int *Dst2D = sycl::malloc_shared<int>(SIZE * SIZE, Q);
192+
193+
Q.submit([&](sycl::handler &CGH) {
194+
syclexp::nd_launch(CGH,
195+
::sycl::nd_range<2>(::sycl::range<2>(SIZE, SIZE),
196+
::sycl::range<2>(SIZE, SIZE)),
197+
syclexp::kernel_function<square2D>, Src2D, Dst2D);
198+
}).wait();
199+
200+
for (int I = 0; I < SIZE * SIZE; I++) {
201+
assert(Dst2D[I] == Src2D[I] * Src2D[I]);
202+
}
203+
204+
int *Src3D = sycl::malloc_shared<int>(SIZE * SIZE * SIZE, Q);
205+
int *Dst3D = sycl::malloc_shared<int>(SIZE * SIZE * SIZE, Q);
206+
207+
Q.submit([&](sycl::handler &CGH) {
208+
syclexp::nd_launch(CGH,
209+
::sycl::nd_range<3>(::sycl::range<3>(SIZE, SIZE, SIZE),
210+
::sycl::range<3>(SIZE, SIZE, SIZE)),
211+
syclexp::kernel_function<square3D>, Src3D, Dst3D);
212+
}).wait();
213+
214+
for (int I = 0; I < SIZE * SIZE * SIZE; I++) {
215+
assert(Dst3D[I] == Src3D[I] * Src3D[I]);
216+
}
217+
178218
Q.submit([&](sycl::handler &CGH) {
179219
static_assert(std::is_same_v<decltype(syclexp::single_task(
180220
CGH, syclexp::kernel_function<successor>,

0 commit comments

Comments
 (0)