22// RUN: %{build} -o %t.out
33// RUN: %{run} %t.out
44
5+ // The name mangling for free function kernels currently does not work with PTX.
6+ // UNSUPPORTED: cuda
7+
58#include < cassert>
69#include < sycl/detail/core.hpp>
710#include < sycl/ext/oneapi/experimental/work_group_memory.hpp>
@@ -17,7 +20,6 @@ context ctx = q.get_context();
1720void sum_helper (sycl::ext::oneapi::experimental::work_group_memory<int []> mem,
1821 sycl::ext::oneapi::experimental::work_group_memory<int > ret,
1922 size_t WGSIZE) {
20- ret = 0 ;
2123 for (int i = 0 ; i < WGSIZE; ++i) {
2224 ret += mem[i];
2325 }
@@ -26,22 +28,23 @@ void sum_helper(sycl::ext::oneapi::experimental::work_group_memory<int[]> mem,
2628SYCL_EXT_ONEAPI_FUNCTION_PROPERTY (
2729 (ext::oneapi::experimental::nd_range_kernel<1 >))
2830void sum(sycl::ext::oneapi::experimental::work_group_memory<int []> mem,
29- int *buf, int *Result, size_t WGSIZE, bool UseHelper) {
31+ int *buf,
32+ sycl::ext::oneapi::experimental::work_group_memory<int > result,
33+ int expected, size_t WGSIZE, bool UseHelper) {
3034 const auto it = sycl::ext::oneapi::this_work_item::get_nd_item<1 >();
3135 size_t local_id = it.get_local_id ();
3236 mem[local_id] = buf[local_id];
3337 group_barrier (it.get_group ());
3438 if (it.get_group ().leader ()) {
35- *Result = 0 ;
39+ result = 0 ;
3640 if (!UseHelper) {
3741 for (int i = 0 ; i < WGSIZE; ++i) {
38- *Result += mem[i];
42+ result += mem[i];
3943 }
4044 } else {
41- sycl::ext::oneapi::experimental::work_group_memory<int > ret;
42- sum_helper (mem, ret, WGSIZE);
43- *Result = ret;
45+ sum_helper (mem, result, WGSIZE);
4446 }
47+ assert (result == expected);
4548 }
4649}
4750
@@ -53,23 +56,26 @@ void test(size_t SIZE, size_t WGSIZE, bool UseHelper) {
5356 buf[i] = i;
5457 expected += buf[i];
5558 }
56- int *result = malloc_shared<int >(1 , q);
57- assert (result && " Shared USM allocation failed!" );
59+
60+ // The following ifndef is required due to a number of limitations of free
61+ // function kernels
62+ // TODO: Remove it once these limitations are no longer there.
5863#ifndef __SYCL_DEVICE_ONLY__
64+
5965 // Get the kernel object for the "mykernel" kernel.
6066 auto Bundle = get_kernel_bundle<sycl::bundle_state::executable>(ctx);
6167 kernel_id sum_id = ext::oneapi::experimental::get_kernel_id<sum>();
6268 kernel k_sum = Bundle.get_kernel (sum_id);
6369 q.submit ([&](sycl::handler &cgh) {
6470 ext::oneapi::experimental::work_group_memory<int []> mem{WGSIZE, cgh};
65- cgh.set_args (mem, buf, result, WGSIZE, UseHelper);
71+ ext::oneapi::experimental ::work_group_memory<int > result{cgh};
72+ cgh.set_args (mem, buf, result, expected, WGSIZE, UseHelper);
6673 nd_range ndr{{SIZE}, {WGSIZE}};
6774 cgh.parallel_for (ndr, k_sum);
6875 }).wait ();
69- # endif
70- assert (expected == *result);
76+
77+ # endif // __SYCL_DEVICE_ONLY
7178 free (buf, q);
72- free (result, q);
7379}
7480
7581int main () {
0 commit comments