Skip to content

Commit 2170f5c

Browse files
refactor: usm reuse to unique ptr
Change usm allocation cache in usm manager to unique ptr Related-To: NEO-6893 Signed-off-by: Dominik Dabek <dominik.dabek@intel.com>
1 parent 110359c commit 2170f5c

File tree

6 files changed

+263
-250
lines changed

6 files changed

+263
-250
lines changed

opencl/test/unit_test/context/context_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,7 @@ TEST_F(GTPinContextDestroyTest, whenCallingConxtextDestructorThenGTPinIsNotified
826826
if (mockContext->svmAllocsManager) {
827827
mockContext->getDeviceMemAllocPool().cleanup();
828828
mockContext->getHostMemAllocPool().cleanup();
829-
mockContext->svmAllocsManager->trimUSMDeviceAllocCache();
830-
mockContext->svmAllocsManager->trimUSMHostAllocCache();
829+
mockContext->svmAllocsManager->cleanupUSMAllocCaches();
831830
delete mockContext->svmAllocsManager;
832831
}
833832
mockContext->svmAllocsManager = new MockSVMAllocManager();

opencl/test/unit_test/kernel/kernel_arg_buffer_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018-2024 Intel Corporation
2+
* Copyright (C) 2018-2025 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -355,7 +355,7 @@ TEST_F(KernelArgBufferTest, givenKernelExecInfoWithIndirectStatelessAccessWhenHa
355355
return;
356356
}
357357
pContext->getHostMemAllocPool().cleanup();
358-
svmAllocationsManager->trimUSMHostAllocCache();
358+
svmAllocationsManager->cleanupUSMAllocCaches();
359359

360360
mockKernel.unifiedMemoryControls.indirectHostAllocationsAllowed = true;
361361
EXPECT_FALSE(mockKernel.hasIndirectStatelessAccessToHostMemory());

shared/source/memory_manager/unified_memory_manager.cpp

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ void *SVMAllocsManager::createHostUnifiedMemoryAllocation(size_t size,
415415
unifiedMemoryProperties.flags.isUSMDeviceAllocation = false;
416416
unifiedMemoryProperties.cacheRegion = MemoryPropertiesHelper::getCacheRegion(memoryProperties.allocationFlags);
417417

418-
if (this->usmHostAllocationsCacheEnabled) {
419-
void *allocationFromCache = this->usmHostAllocationsCache.get(size, memoryProperties);
418+
if (this->usmHostAllocationsCache) {
419+
void *allocationFromCache = this->usmHostAllocationsCache->get(size, memoryProperties);
420420
if (allocationFromCache) {
421421
return allocationFromCache;
422422
}
@@ -428,7 +428,7 @@ void *SVMAllocsManager::createHostUnifiedMemoryAllocation(size_t size,
428428

429429
void *usmPtr = memoryManager->createMultiGraphicsAllocationInSystemMemoryPool(rootDeviceIndicesVector, unifiedMemoryProperties, allocData.gpuAllocations, externalHostPointer);
430430
if (!usmPtr) {
431-
if (this->usmHostAllocationsCacheEnabled) {
431+
if (this->usmHostAllocationsCache) {
432432
this->trimUSMHostAllocCache();
433433
usmPtr = memoryManager->createMultiGraphicsAllocationInSystemMemoryPool(rootDeviceIndicesVector, unifiedMemoryProperties, allocData.gpuAllocations, externalHostPointer);
434434
}
@@ -492,9 +492,9 @@ void *SVMAllocsManager::createUnifiedMemoryAllocation(size_t size,
492492

493493
if (memoryProperties.memoryType == InternalMemoryType::deviceUnifiedMemory) {
494494
unifiedMemoryProperties.flags.isUSMDeviceAllocation = true;
495-
if (this->usmDeviceAllocationsCacheEnabled &&
495+
if (this->usmDeviceAllocationsCache &&
496496
false == memoryProperties.isInternalAllocation) {
497-
void *allocationFromCache = this->usmDeviceAllocationsCache.get(size, memoryProperties);
497+
void *allocationFromCache = this->usmDeviceAllocationsCache->get(size, memoryProperties);
498498
if (allocationFromCache) {
499499
return allocationFromCache;
500500
}
@@ -508,7 +508,7 @@ void *SVMAllocsManager::createUnifiedMemoryAllocation(size_t size,
508508
GraphicsAllocation *unifiedMemoryAllocation = memoryManager->allocateGraphicsMemoryWithProperties(unifiedMemoryProperties, externalPtr);
509509
if (!unifiedMemoryAllocation) {
510510
if (memoryProperties.memoryType == InternalMemoryType::deviceUnifiedMemory &&
511-
this->usmDeviceAllocationsCacheEnabled) {
511+
this->usmDeviceAllocationsCache) {
512512
this->trimUSMDeviceAllocCache();
513513
unifiedMemoryAllocation = memoryManager->allocateGraphicsMemoryWithProperties(unifiedMemoryProperties, externalPtr);
514514
}
@@ -642,14 +642,14 @@ bool SVMAllocsManager::freeSVMAlloc(void *ptr, bool blocking) {
642642
if (svmData) {
643643
if (InternalMemoryType::deviceUnifiedMemory == svmData->memoryType &&
644644
false == svmData->isInternalAllocation &&
645-
this->usmDeviceAllocationsCacheEnabled) {
646-
if (this->usmDeviceAllocationsCache.insert(svmData->gpuAllocations.getDefaultGraphicsAllocation()->getUnderlyingBufferSize(), ptr, svmData)) {
645+
this->usmDeviceAllocationsCache) {
646+
if (this->usmDeviceAllocationsCache->insert(svmData->gpuAllocations.getDefaultGraphicsAllocation()->getUnderlyingBufferSize(), ptr, svmData)) {
647647
return true;
648648
}
649649
}
650650
if (InternalMemoryType::hostUnifiedMemory == svmData->memoryType &&
651-
this->usmHostAllocationsCacheEnabled) {
652-
if (this->usmHostAllocationsCache.insert(svmData->size, ptr, svmData)) {
651+
this->usmHostAllocationsCache) {
652+
if (this->usmHostAllocationsCache->insert(svmData->size, ptr, svmData)) {
653653
return true;
654654
}
655655
}
@@ -672,14 +672,14 @@ bool SVMAllocsManager::freeSVMAllocDefer(void *ptr) {
672672
SvmAllocationData *svmData = getSVMAlloc(ptr);
673673
if (svmData) {
674674
if (InternalMemoryType::deviceUnifiedMemory == svmData->memoryType &&
675-
this->usmDeviceAllocationsCacheEnabled) {
676-
if (this->usmDeviceAllocationsCache.insert(svmData->size, ptr, svmData)) {
675+
this->usmDeviceAllocationsCache) {
676+
if (this->usmDeviceAllocationsCache->insert(svmData->size, ptr, svmData)) {
677677
return true;
678678
}
679679
}
680680
if (InternalMemoryType::hostUnifiedMemory == svmData->memoryType &&
681-
this->usmHostAllocationsCacheEnabled) {
682-
if (this->usmHostAllocationsCache.insert(svmData->size, ptr, svmData)) {
681+
this->usmHostAllocationsCache) {
682+
if (this->usmHostAllocationsCache->insert(svmData->size, ptr, svmData)) {
683683
return true;
684684
}
685685
}
@@ -752,16 +752,20 @@ void SVMAllocsManager::freeSVMAllocDeferImpl() {
752752
}
753753

754754
void SVMAllocsManager::cleanupUSMAllocCaches() {
755-
this->usmDeviceAllocationsCache.cleanup();
756-
this->usmHostAllocationsCache.cleanup();
755+
if (this->usmDeviceAllocationsCache) {
756+
this->usmDeviceAllocationsCache->cleanup();
757+
}
758+
if (this->usmHostAllocationsCache) {
759+
this->usmHostAllocationsCache->cleanup();
760+
}
757761
}
758762

759763
void SVMAllocsManager::trimUSMDeviceAllocCache() {
760-
this->usmDeviceAllocationsCache.trim();
764+
this->usmDeviceAllocationsCache->trim();
761765
}
762766

763767
void SVMAllocsManager::trimUSMHostAllocCache() {
764-
this->usmHostAllocationsCache.trim();
768+
this->usmHostAllocationsCache->trim();
765769
}
766770

767771
void *SVMAllocsManager::createZeroCopySvmAllocation(size_t size, const SvmAllocationProperties &svmProperties,
@@ -896,12 +900,13 @@ void SVMAllocsManager::freeZeroCopySvmAllocation(SvmAllocationData *svmData) {
896900
}
897901

898902
void SVMAllocsManager::initUsmDeviceAllocationsCache(Device &device) {
903+
this->usmDeviceAllocationsCache.reset(new SvmAllocationCache);
899904
if (device.getMaxAllocationsSavedForReuseSize() > 0u) {
900-
this->usmDeviceAllocationsCache.allocations.reserve(128u);
901-
this->usmDeviceAllocationsCache.svmAllocsManager = this;
902-
this->usmDeviceAllocationsCache.memoryManager = memoryManager;
905+
this->usmDeviceAllocationsCache->allocations.reserve(128u);
906+
this->usmDeviceAllocationsCache->svmAllocsManager = this;
907+
this->usmDeviceAllocationsCache->memoryManager = memoryManager;
903908
if (auto usmReuseCleaner = device.getExecutionEnvironment()->unifiedMemoryReuseCleaner.get()) {
904-
usmReuseCleaner->registerSvmAllocationCache(&this->usmDeviceAllocationsCache);
909+
usmReuseCleaner->registerSvmAllocationCache(this->usmDeviceAllocationsCache.get());
905910
}
906911
}
907912
}
@@ -912,32 +917,33 @@ void SVMAllocsManager::initUsmHostAllocationsCache() {
912917
if (debugManager.flags.ExperimentalEnableHostAllocationCache.get() != -1) {
913918
fractionOfTotalMemoryForRecycling = 0.01 * std::min(100, debugManager.flags.ExperimentalEnableHostAllocationCache.get());
914919
}
915-
this->usmHostAllocationsCache.maxSize = static_cast<size_t>(fractionOfTotalMemoryForRecycling * totalSystemMemory);
916-
if (this->usmHostAllocationsCache.maxSize > 0u) {
917-
this->usmHostAllocationsCache.allocations.reserve(128u);
918-
this->usmHostAllocationsCache.svmAllocsManager = this;
919-
this->usmHostAllocationsCache.memoryManager = memoryManager;
920+
this->usmHostAllocationsCache.reset(new SvmAllocationCache);
921+
this->usmHostAllocationsCache->maxSize = static_cast<size_t>(fractionOfTotalMemoryForRecycling * totalSystemMemory);
922+
if (this->usmHostAllocationsCache->maxSize > 0u) {
923+
this->usmHostAllocationsCache->allocations.reserve(128u);
924+
this->usmHostAllocationsCache->svmAllocsManager = this;
925+
this->usmHostAllocationsCache->memoryManager = memoryManager;
920926
if (auto usmReuseCleaner = this->memoryManager->peekExecutionEnvironment().unifiedMemoryReuseCleaner.get()) {
921-
usmReuseCleaner->registerSvmAllocationCache(&this->usmHostAllocationsCache);
927+
usmReuseCleaner->registerSvmAllocationCache(this->usmHostAllocationsCache.get());
922928
}
923929
}
924930
}
925931

926932
void SVMAllocsManager::initUsmAllocationsCaches(Device &device) {
927-
this->usmDeviceAllocationsCacheEnabled = NEO::ApiSpecificConfig::isDeviceAllocationCacheEnabled() && device.getProductHelper().isDeviceUsmAllocationReuseSupported();
933+
bool usmDeviceAllocationsCacheEnabled = NEO::ApiSpecificConfig::isDeviceAllocationCacheEnabled() && device.getProductHelper().isDeviceUsmAllocationReuseSupported();
928934
if (debugManager.flags.ExperimentalEnableDeviceAllocationCache.get() != -1) {
929-
this->usmDeviceAllocationsCacheEnabled = !!debugManager.flags.ExperimentalEnableDeviceAllocationCache.get();
935+
usmDeviceAllocationsCacheEnabled = !!debugManager.flags.ExperimentalEnableDeviceAllocationCache.get();
930936
}
931-
if (this->usmDeviceAllocationsCacheEnabled) {
937+
if (usmDeviceAllocationsCacheEnabled) {
932938
device.getExecutionEnvironment()->initializeUnifiedMemoryReuseCleaner(!device.isAnyDirectSubmissionEnabled(true));
933939
this->initUsmDeviceAllocationsCache(device);
934940
}
935941

936-
this->usmHostAllocationsCacheEnabled = NEO::ApiSpecificConfig::isHostAllocationCacheEnabled() && device.getProductHelper().isHostUsmAllocationReuseSupported();
942+
bool usmHostAllocationsCacheEnabled = NEO::ApiSpecificConfig::isHostAllocationCacheEnabled() && device.getProductHelper().isHostUsmAllocationReuseSupported();
937943
if (debugManager.flags.ExperimentalEnableHostAllocationCache.get() != -1) {
938-
this->usmHostAllocationsCacheEnabled = !!debugManager.flags.ExperimentalEnableHostAllocationCache.get();
944+
usmHostAllocationsCacheEnabled = !!debugManager.flags.ExperimentalEnableHostAllocationCache.get();
939945
}
940-
if (this->usmHostAllocationsCacheEnabled) {
946+
if (usmHostAllocationsCacheEnabled) {
941947
device.getExecutionEnvironment()->initializeUnifiedMemoryReuseCleaner(!device.isAnyDirectSubmissionEnabled(true));
942948
this->initUsmHostAllocationsCache();
943949
}

shared/source/memory_manager/unified_memory_manager.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,8 @@ class SVMAllocsManager {
300300
std::shared_mutex mtx;
301301
std::mutex mtxForIndirectAccess;
302302
bool multiOsContextSupport;
303-
SvmAllocationCache usmDeviceAllocationsCache;
304-
SvmAllocationCache usmHostAllocationsCache;
305-
bool usmDeviceAllocationsCacheEnabled = false;
306-
bool usmHostAllocationsCacheEnabled = false;
303+
std::unique_ptr<SvmAllocationCache> usmDeviceAllocationsCache;
304+
std::unique_ptr<SvmAllocationCache> usmHostAllocationsCache;
307305
std::multimap<uint32_t, GraphicsAllocation *> internalAllocationsMap;
308306
};
309307
} // namespace NEO

shared/test/common/mocks/mock_svm_manager.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018-2024 Intel Corporation
2+
* Copyright (C) 2018-2025 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -23,9 +23,7 @@ struct MockSVMAllocsManager : public SVMAllocsManager {
2323
using SVMAllocsManager::svmDeferFreeAllocs;
2424
using SVMAllocsManager::svmMapOperations;
2525
using SVMAllocsManager::usmDeviceAllocationsCache;
26-
using SVMAllocsManager::usmDeviceAllocationsCacheEnabled;
2726
using SVMAllocsManager::usmHostAllocationsCache;
28-
using SVMAllocsManager::usmHostAllocationsCacheEnabled;
2927

3028
void prefetchMemory(Device &device, CommandStreamReceiver &commandStreamReceiver, SvmAllocationData &svmData) override {
3129
SVMAllocsManager::prefetchMemory(device, commandStreamReceiver, svmData);

0 commit comments

Comments
 (0)