|
| 1 | +// REQUIRES: level_zero, level_zero_dev_kit, aspect-usm_shared_allocations |
| 2 | +// RUN: %{build} %level_zero_options -fno-sycl-dead-args-optimization -o %t.out |
| 3 | +// RUN: %{run} %t.out |
| 4 | +// |
| 5 | +#include <level_zero/ze_api.h> |
| 6 | +#include <sycl/ext/oneapi/backend/level_zero.hpp> |
| 7 | +#include <sycl/detail/core.hpp> |
| 8 | +#include <sycl/ext/oneapi/free_function_queries.hpp> |
| 9 | +#include <sycl/usm.hpp> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +namespace syclext = sycl::ext::oneapi; |
| 13 | +namespace syclexp = sycl::ext::oneapi::experimental; |
| 14 | + |
| 15 | +extern"C" SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 16 | +void iota(int *ptr) { |
| 17 | + size_t id = syclext::this_work_item::get_nd_item<1>().get_global_linear_id(); |
| 18 | + ptr[id] = 42; |
| 19 | +} |
| 20 | + |
| 21 | +int main() { |
| 22 | + sycl::device d([](const sycl::device &d) { |
| 23 | + return d.get_backend() == sycl::backend::ext_oneapi_level_zero; |
| 24 | + }); |
| 25 | + sycl::queue q{d}; |
| 26 | + sycl::context ctxt = q.get_context(); |
| 27 | + |
| 28 | +#ifndef __SYCL_DEVICE_ONLY__ |
| 29 | + // First, run the kernel using the SYCL API. |
| 30 | + auto Bundle = sycl::get_kernel_bundle<sycl::bundle_state::executable>(ctxt); |
| 31 | + sycl::kernel_id iota_id = syclexp::get_kernel_id<iota>(); |
| 32 | + sycl::kernel k_iota = Bundle.get_kernel(iota_id); |
| 33 | + int *ptr = sycl::malloc_shared<int>(1, q); |
| 34 | + q.submit([&](sycl::handler &cgh) { |
| 35 | + cgh.set_args(ptr); |
| 36 | + cgh.parallel_for(sycl::nd_range{{1}, {1}}, k_iota); |
| 37 | + }).wait(); |
| 38 | + |
| 39 | + // Now, run the kernel by first getting its image as an executable, |
| 40 | + // making an L0 kernel out of it and then making a SYCL kernel out of |
| 41 | + // the L0 kernel. Run this kernel on the SYCL API and verify |
| 42 | + // that it has the same result as the kernel that was run directly on SYCL API. |
| 43 | + // First, get a kernel bundle that contains the kernel "iota". |
| 44 | + auto exe_bndl = sycl::get_kernel_bundle<sycl::bundle_state::executable>( |
| 45 | + ctxt, {d}, |
| 46 | + [&](const sycl::device_image<sycl::bundle_state::executable> &img) { |
| 47 | + return img.has_kernel(iota_id, d); |
| 48 | + }); |
| 49 | + std::vector<std::byte> bytes; |
| 50 | + const sycl::device_image<sycl::bundle_state::executable> &img = |
| 51 | + *(exe_bndl.begin()); |
| 52 | + bytes = img.ext_oneapi_get_backend_content(); |
| 53 | + |
| 54 | + auto ZeContext = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(ctxt); |
| 55 | + auto ZeDevice = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(d); |
| 56 | + |
| 57 | + ze_result_t status; |
| 58 | + ze_module_desc_t moduleDesc = { |
| 59 | + ZE_STRUCTURE_TYPE_MODULE_DESC, |
| 60 | + nullptr, |
| 61 | + ZE_MODULE_FORMAT_IL_SPIRV, |
| 62 | + bytes.size(), |
| 63 | + reinterpret_cast<unsigned char *>(bytes.data()), |
| 64 | + nullptr, |
| 65 | + nullptr}; |
| 66 | + ze_module_handle_t ZeModule; |
| 67 | + status = zeModuleCreate(ZeContext, ZeDevice, &moduleDesc, &ZeModule, nullptr); |
| 68 | + assert(status == ZE_RESULT_SUCCESS); |
| 69 | + |
| 70 | + ze_kernel_desc_t kernelDesc = {ZE_STRUCTURE_TYPE_KERNEL_DESC, nullptr, 0, |
| 71 | + "__sycl_kernel_iota"}; |
| 72 | + ze_kernel_handle_t ZeKernel; |
| 73 | + status = zeKernelCreate(ZeModule, &kernelDesc, &ZeKernel); |
| 74 | + assert(status == ZE_RESULT_SUCCESS); |
| 75 | + sycl::kernel k_iota_twin = |
| 76 | + sycl::make_kernel<sycl::backend::ext_oneapi_level_zero>( |
| 77 | + {sycl::make_kernel_bundle<sycl::backend::ext_oneapi_level_zero, sycl::bundle_state::executable>({ZeModule}, ctxt), ZeKernel}, ctxt); |
| 78 | + int *ptr_twin = sycl::malloc_shared<int>(1, q); |
| 79 | + q.submit([&](sycl::handler &cgh) { |
| 80 | + cgh.set_args(ptr_twin); |
| 81 | + cgh.parallel_for(sycl::nd_range{{1}, {1}}, k_iota_twin); |
| 82 | + }).wait(); |
| 83 | + assert(*ptr_twin == *ptr); |
| 84 | + sycl::free(ptr, q); |
| 85 | + sycl::free(ptr_twin, q); |
| 86 | +#endif |
| 87 | +} |
0 commit comments