|
| 1 | +// REQUIRES: aspect-usm_shared_allocations |
| 2 | +// RUN: %{build} -o %t.out |
| 3 | +// RUN: %{run} %t.out |
| 4 | + |
| 5 | +// XFAIL: cpu |
| 6 | +// XFAIL-TRACKER: CMPLRLLVM-68536 |
| 7 | +// UNSUPPORTED: cuda, hip |
| 8 | +// UNSUPPORTED-INTENDED: Not implemented yet for Nvidia/AMD backends. |
| 9 | + |
| 10 | +#include <iostream> |
| 11 | +#include <sycl/ext/oneapi/free_function_queries.hpp> |
| 12 | +#include <sycl/ext/oneapi/get_kernel_info.hpp> |
| 13 | +#include <sycl/kernel_bundle.hpp> |
| 14 | +#include <sycl/usm.hpp> |
| 15 | + |
| 16 | +namespace syclext = sycl::ext::oneapi; |
| 17 | +namespace syclexp = sycl::ext::oneapi::experimental; |
| 18 | + |
| 19 | +static constexpr size_t NUM = 1024; |
| 20 | +static constexpr size_t WGSIZE = 16; |
| 21 | + |
| 22 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::work_group_size<WGSIZE>)) |
| 23 | +SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclexp::nd_range_kernel<1>)) |
| 24 | +void func(float start, float *ptr) { |
| 25 | + size_t id = syclext::this_work_item::get_nd_item<1>().get_global_linear_id(); |
| 26 | + ptr[id] = start + static_cast<float>(id); |
| 27 | +} |
| 28 | + |
| 29 | +bool check_result(int *ptr) { |
| 30 | + for (size_t i = 0; i < NUM; ++i) { |
| 31 | + const int expected = 3 + static_cast<int>(i); |
| 32 | + if (ptr[i] != expected) |
| 33 | + return true; |
| 34 | + } |
| 35 | + return false; |
| 36 | +} |
| 37 | + |
| 38 | +static bool call_kernel_code(sycl::queue &q, sycl::kernel &kernel) { |
| 39 | + int *ptr = sycl::malloc_shared<int>(NUM, q); |
| 40 | + q.submit([&](sycl::handler &cgh) { |
| 41 | + cgh.set_args(3, ptr); |
| 42 | + sycl::nd_range ndr{{NUM}, {WGSIZE}}; |
| 43 | + cgh.parallel_for(ndr, kernel); |
| 44 | + }).wait(); |
| 45 | + const bool ret = check_result(ptr); |
| 46 | + sycl::free(ptr, q); |
| 47 | + return ret; |
| 48 | +} |
| 49 | + |
| 50 | +bool test_ctxt_dev(sycl::kernel &k, sycl::queue &q) { |
| 51 | + const auto wg_size_cmp = |
| 52 | + k.get_info<sycl::info::kernel_device_specific::work_group_size>( |
| 53 | + q.get_device()); |
| 54 | + const auto wg_size = syclexp::get_kernel_info< |
| 55 | + func, sycl::info::kernel_device_specific::work_group_size>( |
| 56 | + q.get_context(), q.get_device()); |
| 57 | + if (wg_size_cmp != wg_size) |
| 58 | + std::cerr << "Work group size from get_info: " << wg_size_cmp |
| 59 | + << " is not equal to work group size from get_kernel_info: " |
| 60 | + << wg_size << std::endl; |
| 61 | + return wg_size_cmp == wg_size; |
| 62 | +} |
| 63 | + |
| 64 | +bool test_ctxt(sycl::kernel &k, sycl::queue &q) { |
| 65 | + const auto attributes = |
| 66 | + syclexp::get_kernel_info<func, sycl::info::kernel::attributes>( |
| 67 | + q.get_context()); |
| 68 | + const std::string wg_size_str = "work_group_size("; |
| 69 | + if (attributes.empty() || attributes.find(wg_size_str) == std::string::npos) { |
| 70 | + std::cerr << "Work group size attribute is not found in kernel attributes, " |
| 71 | + "attributes:" |
| 72 | + << attributes << std::endl; |
| 73 | + return false; |
| 74 | + } |
| 75 | + auto wg_size_pos = attributes.find(wg_size_str); |
| 76 | + wg_size_pos += wg_size_str.size(); |
| 77 | + const auto comma_pos = attributes.find(',', wg_size_pos); |
| 78 | + if (comma_pos == std::string::npos) { |
| 79 | + std::cerr << "Comma not found in work group size attribute string" |
| 80 | + << std::endl; |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + const auto wg_size_str_value = |
| 85 | + attributes.substr(wg_size_pos, comma_pos - wg_size_pos); |
| 86 | + const size_t wg_size = std::stoul(wg_size_str_value); |
| 87 | + if (wg_size != WGSIZE) { |
| 88 | + std::cerr << "Work group size from attributes: " << wg_size |
| 89 | + << " is not equal to expected work group size: " << WGSIZE |
| 90 | + << std::endl; |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + if (const auto wg_size_cmp = |
| 95 | + k.get_info<sycl::info::kernel_device_specific::work_group_size>( |
| 96 | + q.get_device()); |
| 97 | + wg_size_cmp < wg_size) { |
| 98 | + std::cerr << "Work group size from get_info: " << wg_size_cmp |
| 99 | + << " is less work group size from attributes: " << wg_size |
| 100 | + << std::endl; |
| 101 | + return false; |
| 102 | + } |
| 103 | + return true; |
| 104 | +} |
| 105 | + |
| 106 | +bool test_queue(sycl::kernel &k, sycl::queue &q) { |
| 107 | + const auto wg_size_cmp = |
| 108 | + k.get_info<sycl::info::kernel_device_specific::work_group_size>( |
| 109 | + q.get_device()); |
| 110 | + const auto wg_size = syclexp::get_kernel_info< |
| 111 | + func, sycl::info::kernel_device_specific::work_group_size>(q); |
| 112 | + if (wg_size_cmp != wg_size) |
| 113 | + std::cerr << "Work group size from get_info: " << wg_size_cmp |
| 114 | + << " is not equal to work group size from get_kernel_info: " |
| 115 | + << wg_size << std::endl; |
| 116 | + return wg_size_cmp == wg_size; |
| 117 | +} |
| 118 | + |
| 119 | +int main() { |
| 120 | + sycl::queue q; |
| 121 | + sycl::context ctxt = q.get_context(); |
| 122 | + |
| 123 | + auto exe_bndl = |
| 124 | + syclexp::get_kernel_bundle<func, sycl::bundle_state::executable>(ctxt); |
| 125 | + sycl::kernel k_func = exe_bndl.template ext_oneapi_get_kernel<func>(); |
| 126 | + call_kernel_code(q, k_func); |
| 127 | + |
| 128 | + bool ret = test_ctxt_dev(k_func, q); |
| 129 | + ret &= test_ctxt(k_func, q); |
| 130 | + ret &= test_queue(k_func, q); |
| 131 | + return ret ? 0 : 1; |
| 132 | +} |
0 commit comments