|
| 1 | +// REQUIRES: aspect-usm_shared_allocations |
| 2 | +// RUN: %{build} -o %t.out |
| 3 | +// RUN: %{run} %t.out |
| 4 | + |
| 5 | +// The name mangling for free function kernels currently does not work with PTX. |
| 6 | +// UNSUPPORTED: cuda, hip |
| 7 | +// UNSUPPORTED-INTENDED: Not implemented yet for Nvidia/AMD backends. |
| 8 | + |
| 9 | +#include <iostream> |
| 10 | +#include <sycl/detail/core.hpp> |
| 11 | +#include <sycl/ext/oneapi/free_function_queries.hpp> |
| 12 | +#include <sycl/kernel_bundle.hpp> |
| 13 | +#include <sycl/usm.hpp> |
| 14 | + |
| 15 | +namespace syclext = sycl::ext::oneapi; |
| 16 | +namespace syclexp = sycl::ext::oneapi::experimental; |
| 17 | + |
| 18 | +static constexpr size_t NUM = 1024; |
| 19 | +static constexpr size_t WGSIZE = 16; |
| 20 | + |
| 21 | +template <typename T> int check_result(T *ptr, T value) { |
| 22 | + for (size_t i = 0; i < NUM; ++i) { |
| 23 | + const T expected = value + static_cast<T>(i); |
| 24 | + if (ptr[i] != expected) { |
| 25 | + std::cout << "Kernel execution did not produce the expected result\n"; |
| 26 | + return 1; |
| 27 | + } |
| 28 | + } |
| 29 | + return 0; |
| 30 | +} |
| 31 | + |
| 32 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 33 | +void free_func(int *ptr, int start); |
| 34 | + |
| 35 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 36 | +void free_func(int *ptr, int start); |
| 37 | + |
| 38 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 39 | +void free_func1(int *ptr, int start); |
| 40 | + |
| 41 | +void free_func1(int *ptr, int start); |
| 42 | + |
| 43 | +template <typename T> |
| 44 | +static int call_kernel_code(sycl::queue &q, sycl::kernel &kernel, T value) { |
| 45 | + T *ptr = sycl::malloc_shared<T>(NUM, q); |
| 46 | + q.submit([&](sycl::handler &cgh) { |
| 47 | + if (value == 0) |
| 48 | + cgh.set_args(ptr); |
| 49 | + else |
| 50 | + cgh.set_args(ptr, value); |
| 51 | + sycl::nd_range ndr{{NUM}, {WGSIZE}}; |
| 52 | + cgh.parallel_for(ndr, kernel); |
| 53 | + }).wait(); |
| 54 | + const int ret = check_result(ptr, value); |
| 55 | + sycl::free(ptr, q); |
| 56 | + return ret; |
| 57 | +} |
| 58 | + |
| 59 | +#define KERNEL_CODE(start, ptr, type) \ |
| 60 | + size_t id = \ |
| 61 | + syclext::this_work_item::get_nd_item<1>().get_global_linear_id(); \ |
| 62 | + ptr[id] = static_cast<type>(start) + static_cast<type>(id); |
| 63 | + |
| 64 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 65 | +void free_func(int *ptr, int start) { KERNEL_CODE(start, ptr, int); } |
| 66 | + |
| 67 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 68 | +void free_func1(int *ptr, int start) { KERNEL_CODE(start, ptr, int); } |
| 69 | + |
| 70 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 71 | +void free_func2(int *ptr, int start) { KERNEL_CODE(start, ptr, int); } |
| 72 | + |
| 73 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 74 | +void free_func2(float *ptr, float start) { KERNEL_CODE(start, ptr, float); } |
| 75 | + |
| 76 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 77 | +void free_func2(int *ptr) { KERNEL_CODE(0, ptr, int); } |
| 78 | + |
| 79 | +template <auto Func, typename T> |
| 80 | +int test_declarations(sycl::queue &q, sycl::context &ctxt, T value) { |
| 81 | + auto exe_bndl = |
| 82 | + syclexp::get_kernel_bundle<Func, sycl::bundle_state::executable>(ctxt); |
| 83 | + sycl::kernel k_func = exe_bndl.template ext_oneapi_get_kernel<Func>(); |
| 84 | + return call_kernel_code<T>(q, k_func, value); |
| 85 | +} |
| 86 | + |
| 87 | +int main() { |
| 88 | + sycl::queue q; |
| 89 | + sycl::context ctxt = q.get_context(); |
| 90 | + |
| 91 | + int result{0}; |
| 92 | + result |= test_declarations<free_func, int>(q, ctxt, 3); |
| 93 | + result |= test_declarations<free_func1, int>(q, ctxt, 3); |
| 94 | + result |= |
| 95 | + test_declarations<static_cast<void (*)(int *, int)>(free_func2), int>( |
| 96 | + q, ctxt, 3); |
| 97 | + result |= test_declarations<static_cast<void (*)(float *, float)>(free_func2), |
| 98 | + float>(q, ctxt, 3.14f); |
| 99 | + result |= test_declarations<static_cast<void (*)(int *)>(free_func2), int>( |
| 100 | + q, ctxt, 0); |
| 101 | + return result; |
| 102 | +} |
0 commit comments