|
| 1 | +// RUN: %clangxx -fsycl %s -o %t.out |
| 2 | +// RUN: env SYCL_DEVICE_TYPE=HOST %t.out |
| 3 | +// RUN: %CPU_RUN_PLACEHOLDER %t.out |
| 4 | +// RUNx: %GPU_RUN_PLACEHOLDER %t.out |
| 5 | +// RUN: %ACC_RUN_PLACEHOLDER %t.out |
| 6 | +//==------- attributes.cpp - SYCL sub_group attributes test ----*- C++ -*---==// |
| 7 | +// |
| 8 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 9 | +// See https://llvm.org/LICENSE.txt for license information. |
| 10 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "helper.hpp" |
| 15 | + |
| 16 | +#include <CL/sycl.hpp> |
| 17 | + |
| 18 | +#define KERNEL_FUNCTOR_WITH_SIZE(SIZE) \ |
| 19 | + class KernelFunctor##SIZE { \ |
| 20 | + public: \ |
| 21 | + [[cl::intel_reqd_sub_group_size(SIZE)]] void \ |
| 22 | + operator()(cl::sycl::nd_item<1> Item) { \ |
| 23 | + const auto GID = Item.get_global_id(); \ |
| 24 | + } \ |
| 25 | + }; |
| 26 | + |
| 27 | +KERNEL_FUNCTOR_WITH_SIZE(1); |
| 28 | +KERNEL_FUNCTOR_WITH_SIZE(2); |
| 29 | +KERNEL_FUNCTOR_WITH_SIZE(4); |
| 30 | +KERNEL_FUNCTOR_WITH_SIZE(8); |
| 31 | +KERNEL_FUNCTOR_WITH_SIZE(16); |
| 32 | + |
| 33 | +#undef KERNEL_FUNCTOR_WITH_SIZE |
| 34 | + |
| 35 | +inline uint32_t flp2(uint32_t X) { |
| 36 | + X = X | (X >> 1); |
| 37 | + X = X | (X >> 2); |
| 38 | + X = X | (X >> 4); |
| 39 | + X = X | (X >> 8); |
| 40 | + X = X | (X >> 16); |
| 41 | + return X - (X >> 1); |
| 42 | +} |
| 43 | + |
| 44 | +template <typename Fn> inline void submit(cl::sycl::queue &Q) { |
| 45 | + Q.submit([](cl::sycl::handler &cgh) { |
| 46 | + Fn F; |
| 47 | + cgh.parallel_for(cl::sycl::nd_range<1>{64, 16}, F); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +int main() { |
| 52 | + queue Queue; |
| 53 | + device Device = Queue.get_device(); |
| 54 | + |
| 55 | + // According to specification, this kernel query requires `cl_khr_subgroups` |
| 56 | + // or `cl_intel_subgroups`, and also `cl_intel_required_subgroup_size` |
| 57 | + if ((!Device.has_extension("cl_intel_subgroups") && |
| 58 | + !Device.has_extension("cl_khr_subgroups")) || |
| 59 | + !Device.has_extension("cl_intel_required_subgroup_size")) { |
| 60 | + std::cout << "Skipping test\n"; |
| 61 | + return 0; |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + const auto SGSizes = Device.get_info<info::device::sub_group_sizes>(); |
| 66 | + |
| 67 | + for (const auto SGSize : SGSizes) { |
| 68 | + // Get the previous power of 2 |
| 69 | + auto ReqdSize = flp2(SGSize); |
| 70 | + |
| 71 | + cl::sycl::program Prog(Queue.get_context()); |
| 72 | + |
| 73 | + // Store the `cl::sycl::kernel` into a vector because `cl::sycl::kernel` |
| 74 | + // doesn't have default constructor |
| 75 | + cl::sycl::vector_class<cl::sycl::kernel> TheKernel; |
| 76 | + |
| 77 | + switch (ReqdSize) { |
| 78 | + case 16: |
| 79 | + Prog.build_with_kernel_type<KernelFunctor16>(); |
| 80 | + TheKernel.push_back(Prog.get_kernel<KernelFunctor16>()); |
| 81 | + submit<KernelFunctor16>(Queue); |
| 82 | + break; |
| 83 | + case 8: |
| 84 | + Prog.build_with_kernel_type<KernelFunctor8>(); |
| 85 | + TheKernel.push_back(Prog.get_kernel<KernelFunctor8>()); |
| 86 | + submit<KernelFunctor8>(Queue); |
| 87 | + break; |
| 88 | + case 4: |
| 89 | + Prog.build_with_kernel_type<KernelFunctor4>(); |
| 90 | + TheKernel.push_back(Prog.get_kernel<KernelFunctor4>()); |
| 91 | + submit<KernelFunctor4>(Queue); |
| 92 | + break; |
| 93 | + case 2: |
| 94 | + Prog.build_with_kernel_type<KernelFunctor2>(); |
| 95 | + TheKernel.push_back(Prog.get_kernel<KernelFunctor2>()); |
| 96 | + submit<KernelFunctor2>(Queue); |
| 97 | + break; |
| 98 | + case 1: |
| 99 | + Prog.build_with_kernel_type<KernelFunctor1>(); |
| 100 | + TheKernel.push_back(Prog.get_kernel<KernelFunctor1>()); |
| 101 | + submit<KernelFunctor1>(Queue); |
| 102 | + break; |
| 103 | + default: |
| 104 | + throw feature_not_supported("sub-group size is not supported"); |
| 105 | + } |
| 106 | + |
| 107 | + auto Kernel = TheKernel[0]; |
| 108 | + |
| 109 | + auto Res = Kernel.get_sub_group_info< |
| 110 | + cl::sycl::info::kernel_sub_group::compile_sub_group_size>(Device); |
| 111 | + |
| 112 | + exit_if_not_equal<size_t>(Res, ReqdSize, "compile_sub_group_size"); |
| 113 | + } |
| 114 | + } catch (exception e) { |
| 115 | + std::cout << "SYCL exception caught: " << e.what(); |
| 116 | + return 1; |
| 117 | + } |
| 118 | + |
| 119 | + std::cout << "Test passed.\n"; |
| 120 | + return 0; |
| 121 | +} |
0 commit comments