|
| 1 | +//===----------------------------------------------------------------------===// |
| 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 <CL/sycl/program.hpp> |
| 10 | +#include <detail/kernel_bundle_impl.hpp> |
| 11 | + |
| 12 | +#include <helpers/CommonRedefinitions.hpp> |
| 13 | +#include <helpers/PiImage.hpp> |
| 14 | +#include <helpers/PiMock.hpp> |
| 15 | + |
| 16 | +#include <gtest/gtest.h> |
| 17 | + |
| 18 | +#include <helpers/TestKernel.hpp> |
| 19 | + |
| 20 | +static pi_device rootDevice; |
| 21 | +static pi_device piSubDev1 = (pi_device)0x1; |
| 22 | +static pi_device piSubDev2 = (pi_device)0x2; |
| 23 | + |
| 24 | +namespace { |
| 25 | +pi_result redefinedDeviceGetInfo(pi_device device, pi_device_info param_name, |
| 26 | + size_t param_value_size, void *param_value, |
| 27 | + size_t *param_value_size_ret) { |
| 28 | + if (param_name == PI_DEVICE_INFO_PARTITION_PROPERTIES) { |
| 29 | + if (!param_value) { |
| 30 | + *param_value_size_ret = 2 * sizeof(pi_device_partition_property); |
| 31 | + } else { |
| 32 | + ((pi_device_partition_property *)param_value)[0] = |
| 33 | + PI_DEVICE_PARTITION_BY_AFFINITY_DOMAIN; |
| 34 | + ((pi_device_partition_property *)param_value)[1] = |
| 35 | + PI_DEVICE_PARTITION_BY_AFFINITY_DOMAIN; |
| 36 | + } |
| 37 | + } |
| 38 | + if (param_name == PI_DEVICE_INFO_PARTITION_AFFINITY_DOMAIN) { |
| 39 | + if (!param_value) { |
| 40 | + *param_value_size_ret = sizeof(pi_device_affinity_domain); |
| 41 | + } else { |
| 42 | + ((pi_device_affinity_domain *)param_value)[0] = |
| 43 | + PI_DEVICE_AFFINITY_DOMAIN_NUMA | |
| 44 | + PI_DEVICE_AFFINITY_DOMAIN_NEXT_PARTITIONABLE; |
| 45 | + } |
| 46 | + } |
| 47 | + if (param_name == PI_DEVICE_INFO_PARTITION_MAX_SUB_DEVICES) { |
| 48 | + ((pi_uint32 *)param_value)[0] = 2; |
| 49 | + } |
| 50 | + if (param_name == PI_DEVICE_INFO_PARENT_DEVICE) { |
| 51 | + if (device == piSubDev1 || device == piSubDev2) |
| 52 | + ((pi_device *)param_value)[0] = rootDevice; |
| 53 | + else |
| 54 | + ((pi_device *)param_value)[0] = nullptr; |
| 55 | + } |
| 56 | + return PI_SUCCESS; |
| 57 | +} |
| 58 | + |
| 59 | +pi_result redefinedDevicePartition( |
| 60 | + pi_device Device, const pi_device_partition_property *Properties, |
| 61 | + pi_uint32 NumDevices, pi_device *OutDevices, pi_uint32 *OutNumDevices) { |
| 62 | + if (OutNumDevices) |
| 63 | + *OutNumDevices = 2; |
| 64 | + if (OutDevices) { |
| 65 | + OutDevices[0] = {}; |
| 66 | + OutDevices[1] = {}; |
| 67 | + } |
| 68 | + return PI_SUCCESS; |
| 69 | +} |
| 70 | + |
| 71 | +pi_result redefinedDeviceRetain(pi_device c) { return PI_SUCCESS; } |
| 72 | + |
| 73 | +pi_result redefinedDeviceRelease(pi_device c) { return PI_SUCCESS; } |
| 74 | + |
| 75 | +pi_result redefinedProgramBuild( |
| 76 | + pi_program prog, pi_uint32, const pi_device *, const char *, |
| 77 | + void (*pfn_notify)(pi_program program, void *user_data), void *user_data) { |
| 78 | + static int m = 0; |
| 79 | + m++; |
| 80 | + // if called more than once return an error |
| 81 | + if (m > 1) |
| 82 | + return PI_ERROR_UNKNOWN; |
| 83 | + |
| 84 | + return PI_SUCCESS; |
| 85 | +} |
| 86 | +} // anonymous namespace |
| 87 | + |
| 88 | +// Check that program is built once for all sub-devices |
| 89 | +TEST(SubDevices, BuildProgramForSubdevices) { |
| 90 | + sycl::platform Plt{sycl::default_selector()}; |
| 91 | + // Host devices do not support sub-devices |
| 92 | + if (Plt.is_host()) { |
| 93 | + std::cerr << "Test is not supported on " |
| 94 | + << Plt.get_info<sycl::info::platform::name>() << ", skipping\n"; |
| 95 | + GTEST_SKIP(); // test is not supported on selected platform. |
| 96 | + } |
| 97 | + |
| 98 | + // Setup Mock APIs |
| 99 | + sycl::unittest::PiMock Mock{Plt}; |
| 100 | + setupDefaultMockAPIs(Mock); |
| 101 | + Mock.redefine<sycl::detail::PiApiKind::piDeviceGetInfo>( |
| 102 | + redefinedDeviceGetInfo); |
| 103 | + Mock.redefine<sycl::detail::PiApiKind::piDevicePartition>( |
| 104 | + redefinedDevicePartition); |
| 105 | + Mock.redefine<sycl::detail::PiApiKind::piDeviceRetain>(redefinedDeviceRetain); |
| 106 | + Mock.redefine<sycl::detail::PiApiKind::piDeviceRelease>( |
| 107 | + redefinedDeviceRelease); |
| 108 | + Mock.redefine<sycl::detail::PiApiKind::piProgramBuild>(redefinedProgramBuild); |
| 109 | + |
| 110 | + // Create 2 sub-devices and use first device as a root device |
| 111 | + sycl::context Ctx{Plt}; |
| 112 | + const sycl::device device = Plt.get_devices()[0]; |
| 113 | + // Initialize root device |
| 114 | + rootDevice = sycl::detail::getSyclObjImpl(device)->getHandleRef(); |
| 115 | + // Initialize sub-devices |
| 116 | + auto PltImpl = sycl::detail::getSyclObjImpl(Plt); |
| 117 | + auto subDev1 = std::make_shared<sycl::detail::device_impl>( |
| 118 | + piSubDev1, PltImpl->getPlugin()); |
| 119 | + auto subDev2 = std::make_shared<sycl::detail::device_impl>( |
| 120 | + piSubDev2, PltImpl->getPlugin()); |
| 121 | + |
| 122 | + // Create device binary description structures for getBuiltPIProgram API. |
| 123 | + auto devBin = Img.convertToNativeType(); |
| 124 | + pi_device_binaries_struct devBinStruct{PI_DEVICE_BINARIES_VERSION, 1, |
| 125 | + &devBin}; |
| 126 | + sycl::detail::ProgramManager::getInstance().addImages(&devBinStruct); |
| 127 | + |
| 128 | + // Build program via getBuiltPIProgram API |
| 129 | + sycl::detail::ProgramManager::getInstance().getBuiltPIProgram( |
| 130 | + sycl::detail::OSUtil::getOSModuleHandle(&devBin), |
| 131 | + sycl::detail::getSyclObjImpl(Ctx), subDev1, |
| 132 | + sycl::detail::KernelInfo<TestKernel>::getName()); |
| 133 | + // This call should re-use built binary from the cache. If piProgramBuild is |
| 134 | + // called again, the test will fail as second call of redefinedProgramBuild |
| 135 | + sycl::detail::ProgramManager::getInstance().getBuiltPIProgram( |
| 136 | + sycl::detail::OSUtil::getOSModuleHandle(&devBin), |
| 137 | + sycl::detail::getSyclObjImpl(Ctx), subDev2, |
| 138 | + sycl::detail::KernelInfo<TestKernel>::getName()); |
| 139 | +} |
0 commit comments