Skip to content

Commit 138f22f

Browse files
fix: correct calculation for chunking size
Resolves: NEO-9562 Signed-off-by: John Falkowski <john.falkowski@intel.com>
1 parent 3680700 commit 138f22f

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

shared/source/debug_settings/debug_variables_base.inl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ DECLARE_DEBUG_VARIABLE(bool, EnableBOChunkingPrefetch, false, "Enables prefetchi
541541
DECLARE_DEBUG_VARIABLE(bool, EnableBOChunkingDevMemPrefetch, false, "Enables prefetching of Device Memory chunks")
542542
DECLARE_DEBUG_VARIABLE(bool, EnableBOChunkingPreferredLocationHint, false, "Enables preferred location advise on chunks")
543543
DECLARE_DEBUG_VARIABLE(int32_t, EnableBOChunking, -1, "Enables use of chunking of BOs in the KMD, mask: -1 = default, 0 = no chunking, 1 = shared allocations only, 2 = multi-tile device allocations only, 3 = shared and multi-tile device allocations .")
544-
DECLARE_DEBUG_VARIABLE(int32_t, NumberOfBOChunks, 2, "Number of chunks to use. Must be a power of two")
544+
DECLARE_DEBUG_VARIABLE(int32_t, NumberOfBOChunks, 2, "Number of chunks to use")
545545
DECLARE_DEBUG_VARIABLE(int32_t, SetBOChunkingSize, -1, "Size of chunk in bytes: -1 = default, otherwise power of two chunk size in bytes")
546546
DECLARE_DEBUG_VARIABLE(int32_t, MinimalAllocationSizeForChunking, -1, "2097152: default, >0: size in B. Minimal size an allocation should have to use chunking.")
547547
DECLARE_DEBUG_VARIABLE(int32_t, ForceAutoGrfCompilationMode, -1, "Adds build option -*-intel-enable-auto-large-GRF-mode to force kernel compilation")

shared/source/os_interface/linux/drm_memory_manager.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,25 +1905,27 @@ size_t DrmMemoryManager::getSizeOfChunk(size_t allocSize) {
19051905
size_t chunkSize = MemoryConstants::chunkThreshold;
19061906
size_t chunkMask = (~(MemoryConstants::chunkThreshold - 1));
19071907
size_t numChunk = debugManager.flags.NumberOfBOChunks.get();
1908+
size_t alignSize = alignUp(allocSize, MemoryConstants::pageSize64k);
19081909
if (debugManager.flags.SetBOChunkingSize.get() != -1) {
19091910
chunkSize = debugManager.flags.SetBOChunkingSize.get() & chunkMask;
19101911
if (chunkSize == 0) {
19111912
chunkSize = MemoryConstants::chunkThreshold;
19121913
}
1913-
numChunk = allocSize / chunkSize;
1914+
numChunk = alignSize / chunkSize;
19141915
if (numChunk < 2) {
19151916
numChunk = 2;
19161917
}
19171918
}
19181919
if (numChunk > 1) {
1919-
chunkSize = (allocSize / numChunk) & chunkMask;
1920+
chunkSize = (alignSize / numChunk) & chunkMask;
19201921
if (chunkSize == 0) {
19211922
chunkSize = MemoryConstants::chunkThreshold;
19221923
}
1923-
numChunk = allocSize / chunkSize;
1924-
while (((!Math::isPow2(numChunk)) || (chunkSize & (MemoryConstants::chunkThreshold - 1))) && (numChunk > 2)) {
1925-
numChunk -= 1;
1926-
chunkSize = allocSize / numChunk;
1924+
while ((alignSize % chunkSize) && ((alignSize / chunkSize) > 1)) {
1925+
chunkSize += MemoryConstants::chunkThreshold;
1926+
}
1927+
while ((alignSize % chunkSize) && (chunkSize >= (2 * MemoryConstants::chunkThreshold))) {
1928+
chunkSize -= MemoryConstants::chunkThreshold;
19271929
}
19281930
}
19291931
return chunkSize;
@@ -1939,7 +1941,8 @@ bool DrmMemoryManager::createDrmChunkedAllocation(Drm *drm, DrmAllocation *alloc
19391941
auto memoryInfo = drm->getMemoryInfo();
19401942
uint32_t handle = 0;
19411943
auto memoryBanks = static_cast<uint32_t>(storageInfo.memoryBanks.to_ulong());
1942-
uint32_t numOfChunks = static_cast<uint32_t>(boSize / getSizeOfChunk(boSize));
1944+
auto alignSize = alignUp(boSize, MemoryConstants::pageSize64k);
1945+
uint32_t numOfChunks = static_cast<uint32_t>(alignSize / getSizeOfChunk(alignSize));
19431946

19441947
auto gmm = allocation->getGmm(0u);
19451948
auto patIndex = drm->getPatIndex(gmm, allocation->getAllocationType(), CacheRegion::defaultRegion, CachePolicy::writeBack, false, !allocation->isAllocatedInLocalMemoryPool());
@@ -2316,18 +2319,19 @@ GraphicsAllocation *DrmMemoryManager::createSharedUnifiedMemoryAllocation(const
23162319
BufferObjects bos{};
23172320
auto currentAddress = cpuPointer;
23182321
auto remainingSize = size;
2322+
auto alignSize = alignUp(remainingSize, MemoryConstants::pageSize64k);
23192323
auto remainingMemoryBanks = allocationData.storageInfo.memoryBanks;
23202324
auto numHandles = GraphicsAllocation::getNumHandlesForKmdSharedAllocation(allocationData.storageInfo.getNumBanks());
23212325

23222326
bool useChunking = false;
23232327
uint32_t numOfChunks = 0;
23242328

2325-
if (checkAllocationForChunking(size, drm.getMinimalSizeForChunking(),
2329+
if (checkAllocationForChunking(alignSize, drm.getMinimalSizeForChunking(),
23262330
true, (!executionEnvironment.isDebuggingEnabled()),
23272331
(drm.getChunkingMode() & chunkingModeShared), true)) {
23282332
numHandles = 1;
23292333
useChunking = true;
2330-
numOfChunks = static_cast<uint32_t>(size / getSizeOfChunk(size));
2334+
numOfChunks = static_cast<uint32_t>(alignSize / getSizeOfChunk(alignSize));
23312335
}
23322336

23332337
const auto memoryPool = MemoryPool::localMemory;

shared/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5318,7 +5318,7 @@ TEST_F(DrmMemoryManagerWithLocalMemoryAndExplicitExpectationsTest, givenDrmMemor
53185318
EXPECT_EQ(expectedSize, chunkSize);
53195319
}
53205320

5321-
TEST_F(DrmMemoryManagerWithLocalMemoryAndExplicitExpectationsTest, givenDrmMemoryManagerWhenGetSizeOfChunkFor7ChunksThenCorrectedValueReturned) {
5321+
TEST_F(DrmMemoryManagerWithLocalMemoryAndExplicitExpectationsTest, givenDrmMemoryManagerWhenGetSizeOfChunkFor6ChunksThenCorrectedValueReturned) {
53225322
DebugManagerStateRestore stateRestore;
53235323
debugManager.flags.NumberOfBOChunks.set(6);
53245324
size_t allocSize = 2097152;
@@ -5327,6 +5327,15 @@ TEST_F(DrmMemoryManagerWithLocalMemoryAndExplicitExpectationsTest, givenDrmMemor
53275327
EXPECT_EQ(expectedSize, chunkSize);
53285328
}
53295329

5330+
TEST_F(DrmMemoryManagerWithLocalMemoryAndExplicitExpectationsTest, givenDrmMemoryManagerWhenGetSizeOfChunkForUnevenChunksThenCorrectedValueReturned) {
5331+
DebugManagerStateRestore stateRestore;
5332+
debugManager.flags.NumberOfBOChunks.set(2);
5333+
size_t allocSize = 2162688;
5334+
size_t expectedSize = 720896;
5335+
size_t chunkSize = memoryManager->getSizeOfChunk(allocSize);
5336+
EXPECT_EQ(expectedSize, chunkSize);
5337+
}
5338+
53305339
TEST_F(DrmMemoryManagerWithLocalMemoryAndExplicitExpectationsTest, givenDrmMemoryManagerWhenGetSizeOfChunkFor1ChunkThenDefaultMinimumChunkSizeReturned) {
53315340
DebugManagerStateRestore stateRestore;
53325341
debugManager.flags.NumberOfBOChunks.set(1);

0 commit comments

Comments
 (0)