|
| 1 | +// REQUIRES: aspect-usm_shared_allocations |
| 2 | +// UNSUPPORTED: target-amd |
| 3 | +// UNSUPPORTED-TRACKER: https://github.com/intel/llvm/issues/16072 |
| 4 | + |
| 5 | +// RUN: %{build} -o %t.out |
| 6 | +// RUN: %{run} %t.out |
| 7 | + |
| 8 | +// This test verifies that we can compile, run and get correct results when |
| 9 | +// using a free function kernel that allocates shared local memory in a kernel |
| 10 | +// either by way of the work group scratch memory extension or the work group |
| 11 | +// static memory extension. |
| 12 | + |
| 13 | +#include <sycl/ext/oneapi/work_group_static.hpp> |
| 14 | + |
| 15 | +#include "helpers.hpp" |
| 16 | +#include <cassert> |
| 17 | +#include <sycl/ext/oneapi/experimental/enqueue_functions.hpp> |
| 18 | +#include <sycl/ext/oneapi/free_function_queries.hpp> |
| 19 | +#include <sycl/group_barrier.hpp> |
| 20 | +#include <sycl/usm.hpp> |
| 21 | + |
| 22 | +namespace syclext = sycl::ext::oneapi; |
| 23 | +namespace syclexp = sycl::ext::oneapi::experimental; |
| 24 | + |
| 25 | +constexpr int SIZE = 16; |
| 26 | + |
| 27 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 28 | +void scratch_kernel(float *src, float *dst) { |
| 29 | + size_t lid = syclext::this_work_item::get_nd_item<1>().get_local_linear_id(); |
| 30 | + float *local_mem = (float *)syclexp::get_work_group_scratch_memory(); |
| 31 | + local_mem[lid] = 2 * src[lid]; |
| 32 | + dst[lid] = local_mem[lid]; |
| 33 | +} |
| 34 | + |
| 35 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 36 | +void static_kernel(float *src, float *dst) { |
| 37 | + sycl::nd_item<1> item = syclext::this_work_item::get_nd_item<1>(); |
| 38 | + size_t lid = item.get_local_linear_id(); |
| 39 | + syclexp::work_group_static<float[SIZE]> local_mem; |
| 40 | + local_mem[lid] = src[lid] * src[lid]; |
| 41 | + sycl::group_barrier(item.get_group()); |
| 42 | + if (item.get_group().leader()) { // Check that memory is indeed shared between |
| 43 | + // the work group |
| 44 | + for (int i = 0; i < SIZE; ++i) |
| 45 | + assert(local_mem[i] == src[i] * src[i]); |
| 46 | + } |
| 47 | + dst[lid] = local_mem[lid]; |
| 48 | +} |
| 49 | + |
| 50 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 51 | +void scratch_static_kernel(float *src, float *dst) { |
| 52 | + size_t lid = syclext::this_work_item::get_nd_item<1>().get_local_linear_id(); |
| 53 | + float *scratch_mem = (float *)syclexp::get_work_group_scratch_memory(); |
| 54 | + syclexp::work_group_static<float[SIZE]> static_mem; |
| 55 | + scratch_mem[lid] = src[lid]; |
| 56 | + static_mem[lid] = src[lid]; |
| 57 | + dst[lid] = scratch_mem[lid] + static_mem[lid]; |
| 58 | +} |
| 59 | + |
| 60 | +int main() { |
| 61 | + sycl::queue q; |
| 62 | + float *src = sycl::malloc_shared<float>(SIZE, q); |
| 63 | + float *dst = sycl::malloc_shared<float>(SIZE, q); |
| 64 | + |
| 65 | + for (int i = 0; i < SIZE; i++) { |
| 66 | + src[i] = i; |
| 67 | + } |
| 68 | + |
| 69 | + auto scratchbndl = syclexp::get_kernel_bundle<scratch_kernel, |
| 70 | + sycl::bundle_state::executable>( |
| 71 | + q.get_context()); |
| 72 | + auto staticbndl = |
| 73 | + syclexp::get_kernel_bundle<static_kernel, sycl::bundle_state::executable>( |
| 74 | + q.get_context()); |
| 75 | + auto scratchstaticbndl = syclexp::get_kernel_bundle< |
| 76 | + scratch_static_kernel, sycl::bundle_state::executable>(q.get_context()); |
| 77 | + |
| 78 | + sycl::kernel ScratchKernel = |
| 79 | + scratchbndl.template ext_oneapi_get_kernel<scratch_kernel>(); |
| 80 | + sycl::kernel StaticKernel = |
| 81 | + staticbndl.template ext_oneapi_get_kernel<static_kernel>(); |
| 82 | + sycl::kernel ScratchStaticKernel = |
| 83 | + scratchstaticbndl.template ext_oneapi_get_kernel<scratch_static_kernel>(); |
| 84 | + syclexp::launch_config ScratchKernelcfg{ |
| 85 | + ::sycl::nd_range<1>(::sycl::range<1>(SIZE), ::sycl::range<1>(SIZE)), |
| 86 | + syclexp::properties{ |
| 87 | + syclexp::work_group_scratch_size(SIZE * sizeof(float))}}; |
| 88 | + syclexp::launch_config StaticKernelcfg{ |
| 89 | + ::sycl::nd_range<1>(::sycl::range<1>(SIZE), ::sycl::range<1>(SIZE))}; |
| 90 | + |
| 91 | + syclexp::nd_launch(q, ScratchKernelcfg, ScratchKernel, src, dst); |
| 92 | + q.wait(); |
| 93 | + for (int i = 0; i < SIZE; i++) { |
| 94 | + assert(dst[i] == 2 * src[i]); |
| 95 | + } |
| 96 | + |
| 97 | + syclexp::nd_launch(q, StaticKernelcfg, StaticKernel, src, dst); |
| 98 | + q.wait(); |
| 99 | + for (int i = 0; i < SIZE; i++) { |
| 100 | + assert(dst[i] == src[i] * src[i]); |
| 101 | + } |
| 102 | + |
| 103 | + syclexp::nd_launch(q, ScratchKernelcfg, ScratchStaticKernel, src, dst); |
| 104 | + q.wait(); |
| 105 | + for (int i = 0; i < SIZE; i++) { |
| 106 | + assert(dst[i] == 2 * src[i]); |
| 107 | + } |
| 108 | + |
| 109 | + sycl::free(src, q); |
| 110 | + sycl::free(dst, q); |
| 111 | + return 0; |
| 112 | +} |
0 commit comments