From 1c52017ceb111d3678d534e8bcc4bd0552b0b87c Mon Sep 17 00:00:00 2001 From: Dominik Dabek Date: Tue, 4 Apr 2023 10:02:03 +0000 Subject: [PATCH] fix: use correct allocation type in program init Globals surface allocation via USM manager will have correct allocation type set (instead of just BUFFER) and will use cpu copy when possible. Related-To: NEO-7796 Signed-off-by: Dominik Dabek --- .../core/test/unit_tests/sources/module/test_module.cpp | 4 ++-- shared/source/memory_manager/unified_memory_manager.cpp | 8 +++++--- shared/source/memory_manager/unified_memory_manager.h | 1 + shared/source/program/program_initialization.cpp | 8 ++++---- .../unit_test/program/program_initialization_tests.cpp | 4 ++++ 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/level_zero/core/test/unit_tests/sources/module/test_module.cpp b/level_zero/core/test/unit_tests/sources/module/test_module.cpp index fc992d1408..4c637a2fc0 100644 --- a/level_zero/core/test/unit_tests/sources/module/test_module.cpp +++ b/level_zero/core/test/unit_tests/sources/module/test_module.cpp @@ -2485,8 +2485,8 @@ kernels: zebin.data(), zebin.size()); auto retVal = moduleTu.processUnpackedBinary(); EXPECT_EQ(retVal, ZE_RESULT_SUCCESS); - EXPECT_EQ(AllocationType::BUFFER, moduleTu.globalConstBuffer->getAllocationType()); - EXPECT_EQ(AllocationType::BUFFER, moduleTu.globalVarBuffer->getAllocationType()); + EXPECT_EQ(AllocationType::CONSTANT_SURFACE, moduleTu.globalConstBuffer->getAllocationType()); + EXPECT_EQ(AllocationType::GLOBAL_SURFACE, moduleTu.globalVarBuffer->getAllocationType()); auto svmAllocsManager = device->getDriverHandle()->getSvmAllocsManager(); auto globalConstBufferAllocType = svmAllocsManager->getSVMAlloc(reinterpret_cast(moduleTu.globalConstBuffer->getGpuAddress()))->memoryType; diff --git a/shared/source/memory_manager/unified_memory_manager.cpp b/shared/source/memory_manager/unified_memory_manager.cpp index 55bd3ca349..ad327727e5 100644 --- a/shared/source/memory_manager/unified_memory_manager.cpp +++ b/shared/source/memory_manager/unified_memory_manager.cpp @@ -430,7 +430,6 @@ void SVMAllocsManager::removeSVMAlloc(const SvmAllocationData &svmAllocData) { } bool SVMAllocsManager::freeSVMAlloc(void *ptr, bool blocking) { - if (SVMDeferFreeAllocs.allocations.size() > 0) { this->freeSVMAllocDeferImpl(); } @@ -516,7 +515,6 @@ void SVMAllocsManager::freeSVMAllocImpl(void *ptr, FreePolicyType policy, SvmAll } void SVMAllocsManager::freeSVMAllocDeferImpl() { - std::vector freedPtr; for (auto iter = SVMDeferFreeAllocs.allocations.begin(); iter != SVMDeferFreeAllocs.allocations.end(); ++iter) { void *ptr = reinterpret_cast(iter->second.gpuAllocations.getDefaultGraphicsAllocation()->getGpuAddress()); @@ -764,7 +762,11 @@ AllocationType SVMAllocsManager::getGraphicsAllocationTypeAndCompressionPreferen if (CompressionSelector::allowStatelessCompression()) { compressionEnabled = true; } - allocationType = AllocationType::BUFFER; + if (unifiedMemoryProperties.requestedAllocationType != AllocationType::UNKNOWN) { + allocationType = unifiedMemoryProperties.requestedAllocationType; + } else { + allocationType = AllocationType::BUFFER; + } } } return allocationType; diff --git a/shared/source/memory_manager/unified_memory_manager.h b/shared/source/memory_manager/unified_memory_manager.h index bf3884d919..18d8a3284a 100644 --- a/shared/source/memory_manager/unified_memory_manager.h +++ b/shared/source/memory_manager/unified_memory_manager.h @@ -123,6 +123,7 @@ class SVMAllocsManager { Device *device = nullptr; const RootDeviceIndicesContainer &rootDeviceIndices; const std::map &subdeviceBitfields; + AllocationType requestedAllocationType = AllocationType::UNKNOWN; }; struct SvmCacheAllocationInfo { diff --git a/shared/source/program/program_initialization.cpp b/shared/source/program/program_initialization.cpp index 207b5c6fee..62be9e42dd 100644 --- a/shared/source/program/program_initialization.cpp +++ b/shared/source/program/program_initialization.cpp @@ -23,13 +23,13 @@ GraphicsAllocation *allocateGlobalsSurface(NEO::SVMAllocsManager *const svmAlloc LinkerInput *const linkerInput, const void *initData) { bool globalsAreExported = false; GraphicsAllocation *gpuAllocation = nullptr; - auto rootDeviceIndex = device.getRootDeviceIndex(); - auto deviceBitfield = device.getDeviceBitfield(); + const auto rootDeviceIndex = device.getRootDeviceIndex(); + const auto deviceBitfield = device.getDeviceBitfield(); if (linkerInput != nullptr) { globalsAreExported = constant ? linkerInput->getTraits().exportsGlobalConstants : linkerInput->getTraits().exportsGlobalVariables; } - + const auto allocationType = constant ? AllocationType::CONSTANT_SURFACE : AllocationType::GLOBAL_SURFACE; if (globalsAreExported && (svmAllocManager != nullptr)) { RootDeviceIndicesContainer rootDeviceIndices; rootDeviceIndices.push_back(rootDeviceIndex); @@ -37,6 +37,7 @@ GraphicsAllocation *allocateGlobalsSurface(NEO::SVMAllocsManager *const svmAlloc subDeviceBitfields.insert({rootDeviceIndex, deviceBitfield}); NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::DEVICE_UNIFIED_MEMORY, rootDeviceIndices, subDeviceBitfields); unifiedMemoryProperties.device = &device; + unifiedMemoryProperties.requestedAllocationType = allocationType; auto ptr = svmAllocManager->createUnifiedMemoryAllocation(totalSize, unifiedMemoryProperties); DEBUG_BREAK_IF(ptr == nullptr); if (ptr == nullptr) { @@ -46,7 +47,6 @@ GraphicsAllocation *allocateGlobalsSurface(NEO::SVMAllocsManager *const svmAlloc UNRECOVERABLE_IF(usmAlloc == nullptr); gpuAllocation = usmAlloc->gpuAllocations.getGraphicsAllocation(rootDeviceIndex); } else { - auto allocationType = constant ? AllocationType::CONSTANT_SURFACE : AllocationType::GLOBAL_SURFACE; gpuAllocation = device.getMemoryManager()->allocateGraphicsMemoryWithProperties({rootDeviceIndex, true, // allocateMemory totalSize, allocationType, diff --git a/shared/test/unit_test/program/program_initialization_tests.cpp b/shared/test/unit_test/program/program_initialization_tests.cpp index 13add0c45d..cb1634c6ab 100644 --- a/shared/test/unit_test/program/program_initialization_tests.cpp +++ b/shared/test/unit_test/program/program_initialization_tests.cpp @@ -85,6 +85,7 @@ TEST(AllocateGlobalSurfaceTest, GivenSvmAllocsManagerWhenGlobalsAreExportedThenM ASSERT_NE(nullptr, svmAllocsManager.getSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress())))); EXPECT_TRUE(alloc->isMemObjectsAllocationWithWritableFlags()); EXPECT_EQ(DEVICE_UNIFIED_MEMORY, svmAllocsManager.getSVMAlloc(reinterpret_cast(alloc->getGpuAddress()))->memoryType); + EXPECT_EQ(AllocationType::CONSTANT_SURFACE, alloc->getAllocationType()); svmAllocsManager.freeSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress()))); alloc = allocateGlobalsSurface(&svmAllocsManager, device, initData.size(), 0u, true /* constant */, &linkerInputExportGlobalVariables, initData.data()); @@ -92,6 +93,7 @@ TEST(AllocateGlobalSurfaceTest, GivenSvmAllocsManagerWhenGlobalsAreExportedThenM ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); EXPECT_EQ(nullptr, svmAllocsManager.getSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress())))); + EXPECT_EQ(AllocationType::CONSTANT_SURFACE, alloc->getAllocationType()); device.getMemoryManager()->freeGraphicsMemory(alloc); alloc = allocateGlobalsSurface(&svmAllocsManager, device, initData.size(), 0u, false /* constant */, &linkerInputExportGlobalConstants, initData.data()); @@ -99,6 +101,7 @@ TEST(AllocateGlobalSurfaceTest, GivenSvmAllocsManagerWhenGlobalsAreExportedThenM ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); EXPECT_EQ(nullptr, svmAllocsManager.getSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress())))); + EXPECT_EQ(AllocationType::GLOBAL_SURFACE, alloc->getAllocationType()); device.getMemoryManager()->freeGraphicsMemory(alloc); alloc = allocateGlobalsSurface(&svmAllocsManager, device, initData.size(), 0u, false /* constant */, &linkerInputExportGlobalVariables, initData.data()); @@ -108,6 +111,7 @@ TEST(AllocateGlobalSurfaceTest, GivenSvmAllocsManagerWhenGlobalsAreExportedThenM EXPECT_NE(nullptr, svmAllocsManager.getSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress())))); EXPECT_TRUE(alloc->isMemObjectsAllocationWithWritableFlags()); EXPECT_EQ(DEVICE_UNIFIED_MEMORY, svmAllocsManager.getSVMAlloc(reinterpret_cast(alloc->getGpuAddress()))->memoryType); + EXPECT_EQ(AllocationType::GLOBAL_SURFACE, alloc->getAllocationType()); svmAllocsManager.freeSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress()))); }