|
| 1 | +#include <sycl/sycl.hpp> |
| 2 | + |
| 3 | +#include <helpers/PiImage.hpp> |
| 4 | +#include <helpers/PiMock.hpp> |
| 5 | + |
| 6 | +#include <gtest/gtest.h> |
| 7 | + |
| 8 | +class TestKernelCPU; |
| 9 | +class TestKernelGPU; |
| 10 | +class TestKernelACC; |
| 11 | + |
| 12 | +namespace sycl { |
| 13 | +__SYCL_INLINE_VER_NAMESPACE(_V1) { |
| 14 | +namespace detail { |
| 15 | +template <> struct KernelInfo<TestKernelCPU> { |
| 16 | + static constexpr unsigned getNumParams() { return 0; } |
| 17 | + static const kernel_param_desc_t &getParamDesc(int) { |
| 18 | + static kernel_param_desc_t Dummy; |
| 19 | + return Dummy; |
| 20 | + } |
| 21 | + static constexpr const char *getName() { return "TestKernelCPU"; } |
| 22 | + static constexpr bool isESIMD() { return false; } |
| 23 | + static constexpr bool callsThisItem() { return false; } |
| 24 | + static constexpr bool callsAnyThisFreeFunction() { return false; } |
| 25 | + static constexpr int64_t getKernelSize() { return 1; } |
| 26 | +}; |
| 27 | + |
| 28 | +} // namespace detail |
| 29 | +} // __SYCL_INLINE_VER_NAMESPACE(_V1) |
| 30 | +} // namespace sycl |
| 31 | + |
| 32 | +namespace sycl { |
| 33 | +__SYCL_INLINE_VER_NAMESPACE(_V1) { |
| 34 | +namespace detail { |
| 35 | +template <> struct KernelInfo<TestKernelGPU> { |
| 36 | + static constexpr unsigned getNumParams() { return 0; } |
| 37 | + static const kernel_param_desc_t &getParamDesc(int) { |
| 38 | + static kernel_param_desc_t Dummy; |
| 39 | + return Dummy; |
| 40 | + } |
| 41 | + static constexpr const char *getName() { return "TestKernelGPU"; } |
| 42 | + static constexpr bool isESIMD() { return false; } |
| 43 | + static constexpr bool callsThisItem() { return false; } |
| 44 | + static constexpr bool callsAnyThisFreeFunction() { return false; } |
| 45 | + static constexpr int64_t getKernelSize() { return 1; } |
| 46 | +}; |
| 47 | + |
| 48 | +} // namespace detail |
| 49 | +} // __SYCL_INLINE_VER_NAMESPACE(_V1) |
| 50 | +} // namespace sycl |
| 51 | + |
| 52 | +namespace sycl { |
| 53 | +__SYCL_INLINE_VER_NAMESPACE(_V1) { |
| 54 | +namespace detail { |
| 55 | +template <> struct KernelInfo<TestKernelACC> { |
| 56 | + static constexpr unsigned getNumParams() { return 0; } |
| 57 | + static const kernel_param_desc_t &getParamDesc(int) { |
| 58 | + static kernel_param_desc_t Dummy; |
| 59 | + return Dummy; |
| 60 | + } |
| 61 | + static constexpr const char *getName() { return "TestKernelACC"; } |
| 62 | + static constexpr bool isESIMD() { return false; } |
| 63 | + static constexpr bool callsThisItem() { return false; } |
| 64 | + static constexpr bool callsAnyThisFreeFunction() { return false; } |
| 65 | + static constexpr int64_t getKernelSize() { return 1; } |
| 66 | +}; |
| 67 | + |
| 68 | +} // namespace detail |
| 69 | +} // __SYCL_INLINE_VER_NAMESPACE(_V1) |
| 70 | +} // namespace sycl |
| 71 | + |
| 72 | +static sycl::unittest::PiImage |
| 73 | +generateDefaultImage(std::initializer_list<std::string> KernelNames, |
| 74 | + const std::vector<sycl::aspect> &Aspects) { |
| 75 | + using namespace sycl::unittest; |
| 76 | + |
| 77 | + PiPropertySet PropSet; |
| 78 | + addAspects(PropSet, Aspects); |
| 79 | + |
| 80 | + std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data |
| 81 | + |
| 82 | + PiArray<PiOffloadEntry> Entries = makeEmptyKernels(KernelNames); |
| 83 | + |
| 84 | + PiImage Img{PI_DEVICE_BINARY_TYPE_SPIRV, // Format |
| 85 | + __SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec |
| 86 | + "", // Compile options |
| 87 | + "", // Link options |
| 88 | + std::move(Bin), |
| 89 | + std::move(Entries), |
| 90 | + std::move(PropSet)}; |
| 91 | + |
| 92 | + return Img; |
| 93 | +} |
| 94 | + |
| 95 | +static sycl::unittest::PiImage Imgs[3] = { |
| 96 | + generateDefaultImage({"TestKernelCPU"}, {sycl::aspect::cpu}), |
| 97 | + generateDefaultImage({"TestKernelGPU"}, {sycl::aspect::gpu}), |
| 98 | + generateDefaultImage({"TestKernelACC"}, {sycl::aspect::accelerator})}; |
| 99 | + |
| 100 | +static sycl::unittest::PiImageArray<3> ImgArray{Imgs}; |
| 101 | + |
| 102 | +static pi_result redefinedDeviceGetInfoCPU(pi_device device, |
| 103 | + pi_device_info param_name, |
| 104 | + size_t param_value_size, |
| 105 | + void *param_value, |
| 106 | + size_t *param_value_size_ret) { |
| 107 | + if (param_name == PI_DEVICE_INFO_TYPE) { |
| 108 | + auto *Result = reinterpret_cast<_pi_device_type *>(param_value); |
| 109 | + *Result = PI_DEVICE_TYPE_CPU; |
| 110 | + } |
| 111 | + return PI_SUCCESS; |
| 112 | +} |
| 113 | + |
| 114 | +// Mock device is "GPU" by default, but we need to redefine it just in case |
| 115 | +// if there are some changes in the future |
| 116 | +static pi_result redefinedDeviceGetInfoGPU(pi_device device, |
| 117 | + pi_device_info param_name, |
| 118 | + size_t param_value_size, |
| 119 | + void *param_value, |
| 120 | + size_t *param_value_size_ret) { |
| 121 | + if (param_name == PI_DEVICE_INFO_TYPE) { |
| 122 | + auto *Result = reinterpret_cast<_pi_device_type *>(param_value); |
| 123 | + *Result = PI_DEVICE_TYPE_GPU; |
| 124 | + } |
| 125 | + return PI_SUCCESS; |
| 126 | +} |
| 127 | + |
| 128 | +static pi_result redefinedDeviceGetInfoACC(pi_device device, |
| 129 | + pi_device_info param_name, |
| 130 | + size_t param_value_size, |
| 131 | + void *param_value, |
| 132 | + size_t *param_value_size_ret) { |
| 133 | + if (param_name == PI_DEVICE_INFO_TYPE) { |
| 134 | + auto *Result = reinterpret_cast<_pi_device_type *>(param_value); |
| 135 | + *Result = PI_DEVICE_TYPE_ACC; |
| 136 | + } |
| 137 | + return PI_SUCCESS; |
| 138 | +} |
| 139 | + |
| 140 | +TEST(IsCompatible, CPU) { |
| 141 | + sycl::unittest::PiMock Mock; |
| 142 | + Mock.redefineAfter<sycl::detail::PiApiKind::piDeviceGetInfo>( |
| 143 | + redefinedDeviceGetInfoCPU); |
| 144 | + sycl::platform Plt = Mock.getPlatform(); |
| 145 | + const sycl::device Dev = Plt.get_devices()[0]; |
| 146 | + |
| 147 | + EXPECT_TRUE(Dev.is_cpu()); |
| 148 | + EXPECT_TRUE(sycl::is_compatible<TestKernelCPU>(Dev)); |
| 149 | + EXPECT_FALSE(sycl::is_compatible<TestKernelGPU>(Dev)); |
| 150 | + EXPECT_FALSE(sycl::is_compatible<TestKernelACC>(Dev)); |
| 151 | +} |
| 152 | + |
| 153 | +TEST(IsCompatible, GPU) { |
| 154 | + sycl::unittest::PiMock Mock; |
| 155 | + Mock.redefineAfter<sycl::detail::PiApiKind::piDeviceGetInfo>( |
| 156 | + redefinedDeviceGetInfoGPU); |
| 157 | + sycl::platform Plt = Mock.getPlatform(); |
| 158 | + const sycl::device Dev = Plt.get_devices()[0]; |
| 159 | + |
| 160 | + EXPECT_TRUE(Dev.is_gpu()); |
| 161 | + EXPECT_FALSE(sycl::is_compatible<TestKernelCPU>(Dev)); |
| 162 | + EXPECT_TRUE(sycl::is_compatible<TestKernelGPU>(Dev)); |
| 163 | + EXPECT_FALSE(sycl::is_compatible<TestKernelACC>(Dev)); |
| 164 | +} |
| 165 | + |
| 166 | +TEST(IsCompatible, ACC) { |
| 167 | + sycl::unittest::PiMock Mock; |
| 168 | + Mock.redefineAfter<sycl::detail::PiApiKind::piDeviceGetInfo>( |
| 169 | + redefinedDeviceGetInfoACC); |
| 170 | + sycl::platform Plt = Mock.getPlatform(); |
| 171 | + const sycl::device Dev = Plt.get_devices()[0]; |
| 172 | + |
| 173 | + EXPECT_TRUE(Dev.is_accelerator()); |
| 174 | + EXPECT_FALSE(sycl::is_compatible<TestKernelCPU>(Dev)); |
| 175 | + EXPECT_FALSE(sycl::is_compatible<TestKernelGPU>(Dev)); |
| 176 | + EXPECT_TRUE(sycl::is_compatible<TestKernelACC>(Dev)); |
| 177 | +} |
0 commit comments