From 42c5f2235ba329bf445bc553760a9c775a5f4b98 Mon Sep 17 00:00:00 2001 From: Szymon Morek Date: Tue, 13 Jul 2021 16:01:30 +0000 Subject: [PATCH] Fix calculating maxMemAllocSize Signed-off-by: Szymon Morek --- .../unit_tests/sources/device/test_device.cpp | 8 ++++ .../unit_tests/sources/memory/test_memory.cpp | 6 +++ opencl/source/cl_device/cl_device_caps.cpp | 1 + .../api/cl_unified_shared_memory_tests.inl | 2 +- .../unit_test/device/device_caps_tests.cpp | 37 +++++++++++++------ shared/source/device/device.h | 1 + shared/source/device/device_caps.cpp | 15 ++++++-- 7 files changed, 55 insertions(+), 15 deletions(-) diff --git a/level_zero/core/test/unit_tests/sources/device/test_device.cpp b/level_zero/core/test/unit_tests/sources/device/test_device.cpp index dead457744..abf1b2611b 100644 --- a/level_zero/core/test/unit_tests/sources/device/test_device.cpp +++ b/level_zero/core/test/unit_tests/sources/device/test_device.cpp @@ -625,6 +625,14 @@ TEST_F(DeviceTest, givenCallToDevicePropertiesThenMaximumMemoryToBeAllocatedIsCo deviceProperties.maxMemAllocSize = 0; device->getProperties(&deviceProperties); EXPECT_EQ(deviceProperties.maxMemAllocSize, this->neoDevice->getDeviceInfo().maxMemAllocSize); + HardwareCapabilities hwCaps = {0}; + auto &hwHelper = HwHelper::get(defaultHwInfo->platform.eRenderCoreFamily); + hwHelper.setupHardwareCapabilities(&hwCaps, *defaultHwInfo); + auto expectedSize = this->neoDevice->getDeviceInfo().globalMemSize; + if (!this->neoDevice->getDeviceInfo().sharedSystemAllocationsSupport) { + expectedSize = std::min(expectedSize, hwCaps.maxMemAllocSize); + } + EXPECT_EQ(deviceProperties.maxMemAllocSize, expectedSize); } struct DeviceHwInfoTest : public ::testing::Test { diff --git a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp index fc51c65760..359f0168c0 100644 --- a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp +++ b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp @@ -392,6 +392,9 @@ TEST_F(MemoryRelaxedSizeTests, TEST_F(MemoryRelaxedSizeTests, givenCallToDeviceAllocWithLargerThanAllowedSizeAndRelaxedFlagThenAllocationIsMade) { + if (device->getDeviceInfo().sharedSystemAllocationsSupport) { + GTEST_SKIP(); + } size_t size = device->getNEODevice()->getDeviceInfo().maxMemAllocSize + 1; size_t alignment = 1u; void *ptr = nullptr; @@ -506,6 +509,9 @@ TEST_F(MemoryRelaxedSizeTests, TEST_F(MemoryRelaxedSizeTests, givenCallToSharedAllocWithLargerThanAllowedSizeAndRelaxedFlagThenAllocationIsMade) { + if (device->getDeviceInfo().sharedSystemAllocationsSupport) { + GTEST_SKIP(); + } size_t size = device->getNEODevice()->getDeviceInfo().maxMemAllocSize + 1; size_t alignment = 1u; void *ptr = nullptr; diff --git a/opencl/source/cl_device/cl_device_caps.cpp b/opencl/source/cl_device/cl_device_caps.cpp index e059c0e661..9a2e93fb3f 100644 --- a/opencl/source/cl_device/cl_device_caps.cpp +++ b/opencl/source/cl_device/cl_device_caps.cpp @@ -302,6 +302,7 @@ void ClDevice::initializeCaps() { deviceInfo.preferredInteropUserSync = 1u; + device.reduceMaxMemAllocSize(); // OpenCL 1.2 requires 128MB minimum deviceInfo.maxConstantBufferSize = sharedDeviceInfo.maxMemAllocSize; diff --git a/opencl/test/unit_test/api/cl_unified_shared_memory_tests.inl b/opencl/test/unit_test/api/cl_unified_shared_memory_tests.inl index d152e9b138..774e95cdf1 100644 --- a/opencl/test/unit_test/api/cl_unified_shared_memory_tests.inl +++ b/opencl/test/unit_test/api/cl_unified_shared_memory_tests.inl @@ -1045,7 +1045,7 @@ TEST(clUnifiedSharedMemoryTests, givenUnifiedMemoryAllocationSizeGreaterThanMaxM MockContext mockContext; cl_int retVal = CL_SUCCESS; cl_mem_properties_intel properties[] = {0}; - auto bigSize = MemoryConstants::gigaByte * 10; + auto bigSize = MemoryConstants::gigaByte * 20; auto allocationSize = static_cast(bigSize); auto memoryManager = static_cast(mockContext.getDevice(0u)->getMemoryManager()); memoryManager->turnOnFakingBigAllocations(); diff --git a/opencl/test/unit_test/device/device_caps_tests.cpp b/opencl/test/unit_test/device/device_caps_tests.cpp index ca7b450bf1..89e49a7a6f 100644 --- a/opencl/test/unit_test/device/device_caps_tests.cpp +++ b/opencl/test/unit_test/device/device_caps_tests.cpp @@ -503,8 +503,8 @@ TEST_F(DeviceGetCapsTest, givenDeviceCapsWhenLocalMemoryIsEnabledThenCalculateGl TEST_F(DeviceGetCapsTest, givenGlobalMemSizeAndSharedSystemAllocationsNotSupportedWhenCalculatingMaxAllocSizeThenAdjustToHWCap) { DebugManagerStateRestore dbgRestorer; DebugManager.flags.EnableSharedSystemUsmSupport.set(0); - auto device = std::unique_ptr(MockDevice::createWithNewExecutionEnvironment(defaultHwInfo.get())); - const auto &caps = device->getDeviceInfo(); + auto device = std::make_unique(MockDevice::createWithNewExecutionEnvironment(defaultHwInfo.get())); + const auto &caps = device->getSharedDeviceInfo(); HardwareCapabilities hwCaps = {0}; auto &hwHelper = HwHelper::get(defaultHwInfo->platform.eRenderCoreFamily); @@ -515,18 +515,13 @@ TEST_F(DeviceGetCapsTest, givenGlobalMemSizeAndSharedSystemAllocationsNotSupport EXPECT_EQ(caps.maxMemAllocSize, expectedSize); } -TEST_F(DeviceGetCapsTest, givenGlobalMemSizeAndSharedSystemAllocationsSupportedWhenCalculatingMaxAllocSizeThenAdjustToGlobalMemSize) { +TEST_F(DeviceGetCapsTest, givenGlobalMemSizeAndSharedSystemAllocationsSupportedWhenCalculatingMaxAllocSizeThenEqualsToGlobalMemSize) { DebugManagerStateRestore dbgRestorer; DebugManager.flags.EnableSharedSystemUsmSupport.set(1); - auto device = std::unique_ptr(MockDevice::createWithNewExecutionEnvironment(defaultHwInfo.get())); - const auto &caps = device->getDeviceInfo(); + auto device = std::make_unique(MockDevice::createWithNewExecutionEnvironment(defaultHwInfo.get())); + const auto &caps = device->getSharedDeviceInfo(); - HardwareCapabilities hwCaps = {0}; - auto &hwHelper = HwHelper::get(defaultHwInfo->platform.eRenderCoreFamily); - hwHelper.setupHardwareCapabilities(&hwCaps, *defaultHwInfo); - - uint64_t expectedSize = std::max((caps.globalMemSize / 2), static_cast(128ULL * MemoryConstants::megaByte)); - EXPECT_EQ(caps.maxMemAllocSize, expectedSize); + EXPECT_EQ(caps.maxMemAllocSize, caps.globalMemSize); } TEST_F(DeviceGetCapsTest, whenDriverModelHasLimitationForMaxMemoryAllocationSizeThenTakeItIntoAccount) { @@ -1732,3 +1727,23 @@ HWTEST_F(DeviceGetCapsTest, givenDSSCountEqualZeroWhenDeviceCreatedThenMaxEuPerD EXPECT_EQ(device->sharedDeviceInfo.maxNumEUsPerSubSlice, device->sharedDeviceInfo.maxNumEUsPerDualSubSlice); } + +TEST_F(DeviceGetCapsTest, givenGlobalMemSizeAndSharedSystemAllocationSupportWhenReduceMaxMemAllocSizeThenValidValueIsSet) { + HardwareCapabilities hwCaps = {0}; + auto &hwHelper = HwHelper::get(defaultHwInfo->platform.eRenderCoreFamily); + hwHelper.setupHardwareCapabilities(&hwCaps, *defaultHwInfo); + DebugManagerStateRestore dbgRestorer; + + DebugManager.flags.EnableSharedSystemUsmSupport.set(0); + auto device = std::make_unique(MockDevice::createWithNewExecutionEnvironment(defaultHwInfo.get())); + auto globalMemSize = device->getSharedDeviceInfo().globalMemSize; + device->getDevice().reduceMaxMemAllocSize(); + auto expectedSize = std::min(globalMemSize / 2, hwCaps.maxMemAllocSize); + expectedSize = std::max(expectedSize, static_cast(128llu * MB)); + EXPECT_EQ(device->getSharedDeviceInfo().maxMemAllocSize, expectedSize); + + DebugManager.flags.EnableSharedSystemUsmSupport.set(1); + device.reset(new MockClDevice(MockDevice::createWithNewExecutionEnvironment(defaultHwInfo.get()))); + device->getDevice().reduceMaxMemAllocSize(); + EXPECT_EQ(device->getSharedDeviceInfo().maxMemAllocSize, globalMemSize); +} diff --git a/shared/source/device/device.h b/shared/source/device/device.h index 2c5fbb55b1..e4a5345993 100644 --- a/shared/source/device/device.h +++ b/shared/source/device/device.h @@ -119,6 +119,7 @@ class Device : public ReferenceTrackedObject { std::unique_ptr syncBufferHandler; GraphicsAllocation *getRTMemoryBackedBuffer() { return rtMemoryBackedBuffer; } void initializeRayTracing(); + void reduceMaxMemAllocSize(); protected: Device() = delete; diff --git a/shared/source/device/device_caps.cpp b/shared/source/device/device_caps.cpp index 75fc175713..3acbcf4f4b 100644 --- a/shared/source/device/device_caps.cpp +++ b/shared/source/device/device_caps.cpp @@ -71,9 +71,6 @@ void Device::initializeCaps() { deviceInfo.globalMemSize = alignDown(deviceInfo.globalMemSize, MemoryConstants::pageSize); deviceInfo.maxMemAllocSize = std::min(deviceInfo.globalMemSize, deviceInfo.maxMemAllocSize); // if globalMemSize was reduced for 32b - // OpenCL 1.2 requires 128MB minimum - deviceInfo.maxMemAllocSize = std::max(deviceInfo.maxMemAllocSize / 2, static_cast(128llu * MB)); - if (!deviceInfo.sharedSystemAllocationsSupport) { deviceInfo.maxMemAllocSize = std::min(deviceInfo.maxMemAllocSize, this->hardwareCapabilities.maxMemAllocSize); } @@ -171,4 +168,16 @@ void Device::initializeCaps() { deviceInfo.name = deviceName.str(); } +void Device::reduceMaxMemAllocSize() { + deviceInfo.maxMemAllocSize = deviceInfo.globalMemSize; + + if (!deviceInfo.sharedSystemAllocationsSupport) { + deviceInfo.maxMemAllocSize /= 2; + deviceInfo.maxMemAllocSize = std::min(deviceInfo.maxMemAllocSize, this->hardwareCapabilities.maxMemAllocSize); + } + + // OpenCL 1.2 requires 128MB minimum + deviceInfo.maxMemAllocSize = std::max(deviceInfo.maxMemAllocSize, static_cast(128llu * MB)); +} + } // namespace NEO