|
| 1 | +// RUN: %{build} -o %t.out |
| 2 | +// RUN: %{run} %t.out |
| 3 | + |
| 4 | +//===- work_item_queries.cpp - KHR work item queries test -----------------===// |
| 5 | +// |
| 6 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 7 | +// See https://llvm.org/LICENSE.txt for license information. |
| 8 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#define __DPCPP_ENABLE_UNFINISHED_KHR_EXTENSIONS |
| 13 | + |
| 14 | +#include <cassert> |
| 15 | +#include <iostream> |
| 16 | +#include <sycl/detail/core.hpp> |
| 17 | +#include <sycl/khr/work_item_queries.hpp> |
| 18 | + |
| 19 | +template <size_t... Dims> static int check_this_nd_item_api() { |
| 20 | + // Define the kernel ranges. |
| 21 | + constexpr int Dimensions = sizeof...(Dims); |
| 22 | + const sycl::range<Dimensions> local_range{Dims...}; |
| 23 | + const sycl::range<Dimensions> global_range = local_range; |
| 24 | + const sycl::nd_range<Dimensions> nd_range{global_range, local_range}; |
| 25 | + // Launch an ND-range kernel. |
| 26 | + sycl::queue q; |
| 27 | + sycl::buffer<bool, Dimensions> results{global_range}; |
| 28 | + q.submit([&](sycl::handler &cgh) { |
| 29 | + sycl::accessor acc{results, cgh, sycl::write_only}; |
| 30 | + cgh.parallel_for(nd_range, [=](sycl::nd_item<Dimensions> it) { |
| 31 | + // Compare it to this_nd_item<Dimensions>(). |
| 32 | + acc[it.get_global_id()] = (it == sycl::khr::this_nd_item<Dimensions>()); |
| 33 | + }); |
| 34 | + }); |
| 35 | + // Check the test results. |
| 36 | + sycl::host_accessor acc{results}; |
| 37 | + for (const auto &result : acc) { |
| 38 | + if (!result) { |
| 39 | + std::cerr << "check_this_nd_item_api failed for dimensionality " |
| 40 | + << Dimensions << ".\n"; |
| 41 | + return 1; |
| 42 | + } |
| 43 | + } |
| 44 | + return 0; |
| 45 | +} |
| 46 | + |
| 47 | +template <size_t... Dims> static int check_this_group_api() { |
| 48 | + // Define the kernel ranges. |
| 49 | + constexpr int Dimensions = sizeof...(Dims); |
| 50 | + const sycl::range<Dimensions> local_range{Dims...}; |
| 51 | + const sycl::range<Dimensions> global_range = local_range; |
| 52 | + const sycl::nd_range<Dimensions> nd_range{global_range, local_range}; |
| 53 | + // Launch an ND-range kernel. |
| 54 | + sycl::queue q; |
| 55 | + sycl::buffer<bool, Dimensions> results{global_range}; |
| 56 | + q.submit([&](sycl::handler &cgh) { |
| 57 | + sycl::accessor acc{results, cgh, sycl::write_only}; |
| 58 | + cgh.parallel_for(nd_range, [=](sycl::nd_item<Dimensions> it) { |
| 59 | + // Compare it.get_group() to this_group<Dimensions>(). |
| 60 | + acc[it.get_global_id()] = |
| 61 | + (it.get_group() == sycl::khr::this_group<Dimensions>()); |
| 62 | + }); |
| 63 | + }); |
| 64 | + // Check the test results. |
| 65 | + sycl::host_accessor acc{results}; |
| 66 | + for (const auto &result : acc) { |
| 67 | + if (!result) { |
| 68 | + std::cerr << "check_this_group_api failed for dimensionality " |
| 69 | + << Dimensions << ".\n"; |
| 70 | + return 1; |
| 71 | + } |
| 72 | + } |
| 73 | + return 0; |
| 74 | +} |
| 75 | + |
| 76 | +template <size_t... Dims> static int check_this_sub_group_api() { |
| 77 | + // Define the kernel ranges. |
| 78 | + constexpr int Dimensions = sizeof...(Dims); |
| 79 | + const sycl::range<Dimensions> local_range{Dims...}; |
| 80 | + const sycl::range<Dimensions> global_range = local_range; |
| 81 | + const sycl::nd_range<Dimensions> nd_range{global_range, local_range}; |
| 82 | + // Launch an ND-range kernel. |
| 83 | + sycl::queue q; |
| 84 | + sycl::buffer<bool, Dimensions> results{global_range}; |
| 85 | + q.submit([&](sycl::handler &cgh) { |
| 86 | + sycl::accessor acc{results, cgh, sycl::write_only}; |
| 87 | + cgh.parallel_for(nd_range, [=](sycl::nd_item<Dimensions> it) { |
| 88 | + // Compare it.get_sub_group() to this_sub_group(). |
| 89 | + acc[it.get_global_id()] = |
| 90 | + (it.get_sub_group() == sycl::khr::this_sub_group()); |
| 91 | + }); |
| 92 | + }); |
| 93 | + // Check the test results. |
| 94 | + sycl::host_accessor acc{results}; |
| 95 | + for (const auto &result : acc) { |
| 96 | + if (!result) { |
| 97 | + std::cerr << "check_this_sub_group_api failed for dimensionality " |
| 98 | + << Dimensions << ".\n"; |
| 99 | + return 1; |
| 100 | + } |
| 101 | + } |
| 102 | + return 0; |
| 103 | +} |
| 104 | + |
| 105 | +int main() { |
| 106 | + int failed = 0; |
| 107 | + // nd_item |
| 108 | + failed += check_this_nd_item_api<2>(); |
| 109 | + failed += check_this_nd_item_api<2, 3>(); |
| 110 | + failed += check_this_nd_item_api<2, 3, 4>(); |
| 111 | + // group |
| 112 | + failed += check_this_group_api<2>(); |
| 113 | + failed += check_this_group_api<2, 3>(); |
| 114 | + failed += check_this_group_api<2, 3, 4>(); |
| 115 | + // sub_group |
| 116 | + failed += check_this_sub_group_api<2>(); |
| 117 | + failed += check_this_sub_group_api<2, 3>(); |
| 118 | + failed += check_this_sub_group_api<2, 3, 4>(); |
| 119 | + return failed; |
| 120 | +} |
0 commit comments