|
| 1 | +//==------------------- FPGADeviceSelectors.cpp ----------------------------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include <sycl/ext/intel/fpga_device_selector.hpp> |
| 10 | +#include <sycl/sycl.hpp> |
| 11 | + |
| 12 | +#include <detail/config.hpp> |
| 13 | +#include <helpers/PiMock.hpp> |
| 14 | +#include <helpers/ScopedEnvVar.hpp> |
| 15 | + |
| 16 | +#include <gtest/gtest.h> |
| 17 | + |
| 18 | +static constexpr char EMULATION_PLATFORM_NAME[] = |
| 19 | + "Intel(R) FPGA Emulation Platform for OpenCL(TM)"; |
| 20 | +static constexpr char HARDWARE_PLATFORM_NAME[] = |
| 21 | + "Intel(R) FPGA SDK for OpenCL(TM)"; |
| 22 | + |
| 23 | +template <const char PlatformName[]> struct RedefTemplatedWrapper { |
| 24 | + static pi_result redefinedPlatformGetInfo(pi_platform platform, |
| 25 | + pi_platform_info param_name, |
| 26 | + size_t param_value_size, |
| 27 | + void *param_value, |
| 28 | + size_t *param_value_size_ret) { |
| 29 | + switch (param_name) { |
| 30 | + case PI_PLATFORM_INFO_NAME: { |
| 31 | + size_t PlatformNameLen = strlen(PlatformName) + 1; |
| 32 | + if (param_value) { |
| 33 | + assert(param_value_size == PlatformNameLen); |
| 34 | + std::memcpy(param_value, PlatformName, PlatformNameLen); |
| 35 | + } |
| 36 | + if (param_value_size_ret) |
| 37 | + *param_value_size_ret = PlatformNameLen; |
| 38 | + return PI_SUCCESS; |
| 39 | + } |
| 40 | + default: |
| 41 | + return PI_SUCCESS; |
| 42 | + } |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +static pi_result redefinedDeviceGetInfo(pi_device device, |
| 47 | + pi_device_info param_name, |
| 48 | + size_t param_value_size, |
| 49 | + void *param_value, |
| 50 | + size_t *param_value_size_ret) { |
| 51 | + constexpr char MockDeviceName[] = "Mock FPGA device"; |
| 52 | + switch (param_name) { |
| 53 | + case PI_DEVICE_INFO_TYPE: { |
| 54 | + if (param_value) |
| 55 | + *static_cast<_pi_device_type *>(param_value) = PI_DEVICE_TYPE_ACC; |
| 56 | + if (param_value_size_ret) |
| 57 | + *param_value_size_ret = sizeof(PI_DEVICE_TYPE_ACC); |
| 58 | + return PI_SUCCESS; |
| 59 | + } |
| 60 | + case PI_DEVICE_INFO_NAME: { |
| 61 | + if (param_value) { |
| 62 | + assert(param_value_size == sizeof(MockDeviceName)); |
| 63 | + std::memcpy(param_value, MockDeviceName, sizeof(MockDeviceName)); |
| 64 | + } |
| 65 | + if (param_value_size_ret) |
| 66 | + *param_value_size_ret = sizeof(MockDeviceName); |
| 67 | + return PI_SUCCESS; |
| 68 | + } |
| 69 | + default: |
| 70 | + return PI_SUCCESS; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +TEST(FPGADeviceSelectorsTest, FPGASelectorTest) { |
| 75 | + using namespace sycl::detail; |
| 76 | + using namespace sycl::unittest; |
| 77 | + |
| 78 | + sycl::unittest::PiMock Mock; |
| 79 | + Mock.redefine<detail::PiApiKind::piDeviceGetInfo>(redefinedDeviceGetInfo); |
| 80 | + Mock.redefine<detail::PiApiKind::piPlatformGetInfo>( |
| 81 | + RedefTemplatedWrapper<HARDWARE_PLATFORM_NAME>::redefinedPlatformGetInfo); |
| 82 | + sycl::platform Plt = Mock.getPlatform(); |
| 83 | + sycl::context Ctx{Plt.get_devices()}; |
| 84 | + |
| 85 | + sycl::queue FPGAQueue{Ctx, sycl::ext::intel::fpga_selector_v}; |
| 86 | + EXPECT_EQ(FPGAQueue.get_device(), Plt.get_devices()[0]) |
| 87 | + << "Queue did not contain the expected device"; |
| 88 | + |
| 89 | + try { |
| 90 | + sycl::queue EmuFPGAQueue{Ctx, sycl::ext::intel::fpga_emulator_selector_v}; |
| 91 | + FAIL() << "Unexpectedly selected emulator device."; |
| 92 | + } catch (sycl::exception &E) { |
| 93 | + EXPECT_EQ(E.code(), sycl::make_error_code(sycl::errc::runtime)) |
| 94 | + << "Unexpected exception errc."; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +TEST(FPGADeviceSelectorsTest, FPGAEmulatorSelectorTest) { |
| 99 | + using namespace sycl::detail; |
| 100 | + using namespace sycl::unittest; |
| 101 | + |
| 102 | + sycl::unittest::PiMock Mock; |
| 103 | + Mock.redefine<detail::PiApiKind::piDeviceGetInfo>(redefinedDeviceGetInfo); |
| 104 | + Mock.redefine<detail::PiApiKind::piPlatformGetInfo>( |
| 105 | + RedefTemplatedWrapper<EMULATION_PLATFORM_NAME>::redefinedPlatformGetInfo); |
| 106 | + sycl::platform Plt = Mock.getPlatform(); |
| 107 | + sycl::context Ctx{Plt.get_devices()}; |
| 108 | + |
| 109 | + sycl::queue EmuFPGAQueue{Ctx, sycl::ext::intel::fpga_emulator_selector_v}; |
| 110 | + EXPECT_EQ(EmuFPGAQueue.get_device(), Plt.get_devices()[0]) |
| 111 | + << "Queue did not contain the expected device"; |
| 112 | + |
| 113 | + try { |
| 114 | + sycl::queue FPGAQueue{Ctx, sycl::ext::intel::fpga_selector_v}; |
| 115 | + FAIL() << "Unexpectedly selected non-emulator device."; |
| 116 | + } catch (sycl::exception &E) { |
| 117 | + EXPECT_EQ(E.code(), sycl::make_error_code(sycl::errc::runtime)) |
| 118 | + << "Unexpected exception errc."; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +TEST(FPGADeviceSelectorsTest, FPGASimulatorSelectorTest) { |
| 123 | + using namespace sycl::detail; |
| 124 | + using namespace sycl::unittest; |
| 125 | + |
| 126 | + constexpr char INTELFPGA_ENV[] = "CL_CONTEXT_MPSIM_DEVICE_INTELFPGA"; |
| 127 | + ScopedEnvVar EnvVar(INTELFPGA_ENV, nullptr, []() {}); |
| 128 | + |
| 129 | + sycl::unittest::PiMock Mock; |
| 130 | + Mock.redefine<detail::PiApiKind::piDeviceGetInfo>(redefinedDeviceGetInfo); |
| 131 | + Mock.redefine<detail::PiApiKind::piPlatformGetInfo>( |
| 132 | + RedefTemplatedWrapper<HARDWARE_PLATFORM_NAME>::redefinedPlatformGetInfo); |
| 133 | + sycl::platform Plt = Mock.getPlatform(); |
| 134 | + sycl::context Ctx{Plt.get_devices()}; |
| 135 | + |
| 136 | + sycl::queue SimuFPGAQueue{Ctx, sycl::ext::intel::fpga_simulator_selector_v}; |
| 137 | + EXPECT_EQ(SimuFPGAQueue.get_device(), Plt.get_devices()[0]) |
| 138 | + << "Queue did not contain the expected device"; |
| 139 | + |
| 140 | + const char *ReadEnv = getenv(INTELFPGA_ENV); |
| 141 | + EXPECT_NE(ReadEnv, nullptr) << "Environment was unset after call."; |
| 142 | + EXPECT_EQ(std::string(ReadEnv), "1") << "Environment value was not 1"; |
| 143 | + |
| 144 | + try { |
| 145 | + sycl::queue EmuFPGAQueue{Ctx, sycl::ext::intel::fpga_emulator_selector_v}; |
| 146 | + FAIL() << "Unexpectedly selected emulator device."; |
| 147 | + } catch (sycl::exception &E) { |
| 148 | + EXPECT_EQ(E.code(), sycl::make_error_code(sycl::errc::runtime)) |
| 149 | + << "Unexpected exception errc."; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +TEST(FPGADeviceSelectorsTest, NegativeFPGASelectorTest) { |
| 154 | + using namespace sycl::detail; |
| 155 | + using namespace sycl::unittest; |
| 156 | + |
| 157 | + constexpr char INTELFPGA_ENV[] = "CL_CONTEXT_MPSIM_DEVICE_INTELFPGA"; |
| 158 | + ScopedEnvVar EnvVar(INTELFPGA_ENV, nullptr, []() {}); |
| 159 | + |
| 160 | + // Do not redefine any APIs. We want it to fail for all. |
| 161 | + sycl::unittest::PiMock Mock; |
| 162 | + sycl::platform Plt = Mock.getPlatform(); |
| 163 | + sycl::context Ctx{Plt.get_devices()}; |
| 164 | + |
| 165 | + try { |
| 166 | + sycl::queue FPGAQueue{Ctx, sycl::ext::intel::fpga_selector_v}; |
| 167 | + FAIL() << "Unexpectedly selected non-emulator device."; |
| 168 | + } catch (sycl::exception &E) { |
| 169 | + EXPECT_EQ(E.code(), sycl::make_error_code(sycl::errc::runtime)) |
| 170 | + << "Unexpected exception errc."; |
| 171 | + } |
| 172 | + |
| 173 | + try { |
| 174 | + sycl::queue EmuFPGAQueue{Ctx, sycl::ext::intel::fpga_emulator_selector_v}; |
| 175 | + FAIL() << "Unexpectedly selected emulator device."; |
| 176 | + } catch (sycl::exception &E) { |
| 177 | + EXPECT_EQ(E.code(), sycl::make_error_code(sycl::errc::runtime)) |
| 178 | + << "Unexpected exception errc."; |
| 179 | + } |
| 180 | + |
| 181 | + try { |
| 182 | + sycl::queue SimuFPGAQueue{Ctx, sycl::ext::intel::fpga_simulator_selector_v}; |
| 183 | + FAIL() << "Unexpectedly selected simulator device."; |
| 184 | + } catch (sycl::exception &E) { |
| 185 | + EXPECT_EQ(E.code(), sycl::make_error_code(sycl::errc::runtime)) |
| 186 | + << "Unexpected exception errc."; |
| 187 | + } |
| 188 | +} |
0 commit comments