|
| 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 "helpers.hpp" |
| 14 | + |
| 15 | +#include <cassert> |
| 16 | +#include <sycl/ext/oneapi/experimental/enqueue_functions.hpp> |
| 17 | +#include <sycl/ext/oneapi/free_function_queries.hpp> |
| 18 | +#include <sycl/ext/oneapi/work_group_static.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 scratchKernel(float *Src, float *Dst) { |
| 29 | + size_t Lid = syclext::this_work_item::get_nd_item<1>().get_local_linear_id(); |
| 30 | + float *LocalMem = |
| 31 | + reinterpret_cast<float *>(syclexp::get_work_group_scratch_memory()); |
| 32 | + LocalMem[Lid] = 2 * Src[Lid]; |
| 33 | + Dst[Lid] = LocalMem[Lid]; |
| 34 | +} |
| 35 | + |
| 36 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 37 | +void staticKernel(float *Src, float *Dst) { |
| 38 | + sycl::nd_item<1> Item = syclext::this_work_item::get_nd_item<1>(); |
| 39 | + size_t Lid = Item.get_local_linear_id(); |
| 40 | + syclexp::work_group_static<float[SIZE]> LocalMem; |
| 41 | + LocalMem[Lid] = Src[Lid] * Src[Lid]; |
| 42 | + sycl::group_barrier(Item.get_group()); |
| 43 | + if (Item.get_group().leader()) { // Check that memory is indeed shared between |
| 44 | + // the work group. |
| 45 | + for (int I = 0; I < SIZE; ++I) |
| 46 | + assert(LocalMem[I] == Src[I] * Src[I]); |
| 47 | + } |
| 48 | + Dst[Lid] = LocalMem[Lid]; |
| 49 | +} |
| 50 | + |
| 51 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 52 | +void scratchStaticKernel(float *Src, float *Dst) { |
| 53 | + size_t Lid = syclext::this_work_item::get_nd_item<1>().get_local_linear_id(); |
| 54 | + float *ScratchMem = |
| 55 | + reinterpret_cast<float *>(syclexp::get_work_group_scratch_memory()); |
| 56 | + syclexp::work_group_static<float[SIZE]> StaticMem; |
| 57 | + ScratchMem[Lid] = Src[Lid]; |
| 58 | + StaticMem[Lid] = Src[Lid]; |
| 59 | + Dst[Lid] = ScratchMem[Lid] + StaticMem[Lid]; |
| 60 | +} |
| 61 | + |
| 62 | +int main() { |
| 63 | + sycl::queue Q; |
| 64 | + float *Src = sycl::malloc_shared<float>(SIZE, Q); |
| 65 | + float *Dst = sycl::malloc_shared<float>(SIZE, Q); |
| 66 | + |
| 67 | + for (int I = 0; I < SIZE; I++) { |
| 68 | + Src[I] = I; |
| 69 | + } |
| 70 | + |
| 71 | + auto ScratchBndl = |
| 72 | + syclexp::get_kernel_bundle<scratchKernel, sycl::bundle_state::executable>( |
| 73 | + Q.get_context()); |
| 74 | + auto StaticBndl = |
| 75 | + syclexp::get_kernel_bundle<staticKernel, sycl::bundle_state::executable>( |
| 76 | + Q.get_context()); |
| 77 | + auto ScratchStaticBndl = syclexp::get_kernel_bundle< |
| 78 | + scratchStaticKernel, sycl::bundle_state::executable>(Q.get_context()); |
| 79 | + |
| 80 | + sycl::kernel ScratchKrn = |
| 81 | + ScratchBndl.template ext_oneapi_get_kernel<scratchKernel>(); |
| 82 | + sycl::kernel StaticKrn = |
| 83 | + StaticBndl.template ext_oneapi_get_kernel<staticKernel>(); |
| 84 | + sycl::kernel ScratchStaticKrn = |
| 85 | + ScratchStaticBndl.template ext_oneapi_get_kernel<scratchStaticKernel>(); |
| 86 | + syclexp::launch_config ScratchKernelcfg{ |
| 87 | + ::sycl::nd_range<1>(::sycl::range<1>(SIZE), ::sycl::range<1>(SIZE)), |
| 88 | + syclexp::properties{ |
| 89 | + syclexp::work_group_scratch_size(SIZE * sizeof(float))}}; |
| 90 | + syclexp::launch_config StaticKernelcfg{ |
| 91 | + ::sycl::nd_range<1>(::sycl::range<1>(SIZE), ::sycl::range<1>(SIZE))}; |
| 92 | + |
| 93 | + syclexp::nd_launch(Q, ScratchKernelcfg, ScratchKrn, Src, Dst); |
| 94 | + Q.wait(); |
| 95 | + for (int I = 0; I < SIZE; I++) { |
| 96 | + assert(Dst[I] == 2 * Src[I]); |
| 97 | + } |
| 98 | + |
| 99 | + syclexp::nd_launch(Q, StaticKernelcfg, StaticKrn, Src, Dst); |
| 100 | + Q.wait(); |
| 101 | + for (int I = 0; I < SIZE; I++) { |
| 102 | + assert(Dst[I] == Src[I] * Src[I]); |
| 103 | + } |
| 104 | + |
| 105 | + syclexp::nd_launch(Q, ScratchKernelcfg, ScratchStaticKrn, Src, Dst); |
| 106 | + Q.wait(); |
| 107 | + for (int I = 0; I < SIZE; I++) { |
| 108 | + assert(Dst[I] == 2 * Src[I]); |
| 109 | + } |
| 110 | + |
| 111 | + sycl::free(Src, Q); |
| 112 | + sycl::free(Dst, Q); |
| 113 | + return 0; |
| 114 | +} |
0 commit comments