Skip to content

Commit b64210d

Browse files
pwilmaCompute-Runtime-Automation
authored andcommitted
Add local memory usage selector in memory manager
Related-To: NEO-2906 Change-Id: I3172e9551b8d06437c273b122dc6e9d529155b5c Signed-off-by: Pawel Wilma <[email protected]>
1 parent a90270c commit b64210d

17 files changed

+205
-76
lines changed

runtime/memory_manager/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(RUNTIME_SRCS_MEMORY_MANAGER
3333
${CMAKE_CURRENT_SOURCE_DIR}/memory_constants.h
3434
${CMAKE_CURRENT_SOURCE_DIR}/memory_manager.cpp
3535
${CMAKE_CURRENT_SOURCE_DIR}/memory_manager.h
36+
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/memory_manager_banks_count.cpp
3637
${CMAKE_CURRENT_SOURCE_DIR}/memory_pool.h
3738
${CMAKE_CURRENT_SOURCE_DIR}/os_agnostic_memory_manager.cpp
3839
${CMAKE_CURRENT_SOURCE_DIR}/os_agnostic_memory_manager.h

runtime/memory_manager/definitions/storage_info.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
namespace NEO {
1111
struct StorageInfo {
1212
uint32_t getNumHandles() const;
13+
uint32_t getMemoryBanks() const { return 0u; }
1314
};
1415
} // namespace NEO

runtime/memory_manager/local_memory_usage.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "runtime/memory_manager/local_memory_usage.h"
99

1010
#include <algorithm>
11+
#include <bitset>
1112
#include <iterator>
1213

1314
namespace NEO {
@@ -35,4 +36,17 @@ void LocalMemoryUsageBankSelector::reserveOnBank(uint32_t bankIndex, uint64_t al
3536
memorySizes[bankIndex] += allocationSize;
3637
}
3738

39+
void LocalMemoryUsageBankSelector::updateUsageInfo(uint32_t memoryBanks, uint64_t allocationSize, bool reserve) {
40+
auto banks = std::bitset<32>(memoryBanks);
41+
for (uint32_t bankIndex = 0; bankIndex < banks.size() && bankIndex < banksCount; bankIndex++) {
42+
if (banks.test(bankIndex)) {
43+
if (reserve) {
44+
reserveOnBank(bankIndex, allocationSize);
45+
} else {
46+
freeOnBank(bankIndex, allocationSize);
47+
}
48+
}
49+
}
50+
}
51+
3852
} // namespace NEO

runtime/memory_manager/local_memory_usage.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,23 @@ class LocalMemoryUsageBankSelector : public NonCopyableOrMovableClass {
1919
LocalMemoryUsageBankSelector() = delete;
2020
LocalMemoryUsageBankSelector(uint32_t banksCount);
2121
uint32_t getLeastOccupiedBank();
22-
void freeOnBank(uint32_t bankIndex, uint64_t allocationSize);
23-
void reserveOnBank(uint32_t bankIndex, uint64_t allocationSize);
22+
void reserveOnBanks(uint32_t memoryBanks, uint64_t allocationSize) {
23+
updateUsageInfo(memoryBanks, allocationSize, true);
24+
}
25+
void freeOnBanks(uint32_t memoryBanks, uint64_t allocationSize) {
26+
updateUsageInfo(memoryBanks, allocationSize, false);
27+
}
2428

25-
MOCKABLE_VIRTUAL uint64_t getOccupiedMemorySizeForBank(uint32_t bankIndex) {
29+
uint64_t getOccupiedMemorySizeForBank(uint32_t bankIndex) {
2630
UNRECOVERABLE_IF(bankIndex >= banksCount);
2731
return memorySizes[bankIndex].load();
2832
}
2933

3034
protected:
3135
uint32_t banksCount = 0;
3236
std::unique_ptr<std::atomic<uint64_t>[]> memorySizes = nullptr;
37+
void updateUsageInfo(uint32_t memoryBanks, uint64_t allocationSize, bool reserve);
38+
void freeOnBank(uint32_t bankIndex, uint64_t allocationSize);
39+
void reserveOnBank(uint32_t bankIndex, uint64_t allocationSize);
3340
};
3441
} // namespace NEO

runtime/memory_manager/memory_manager.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "runtime/memory_manager/deferred_deleter.h"
2626
#include "runtime/memory_manager/host_ptr_manager.h"
2727
#include "runtime/memory_manager/internal_allocation_storage.h"
28+
#include "runtime/memory_manager/local_memory_usage.h"
2829
#include "runtime/os_interface/os_context.h"
2930
#include "runtime/os_interface/os_interface.h"
3031
#include "runtime/utilities/stackvec.h"
@@ -40,6 +41,7 @@ MemoryManager::MemoryManager(ExecutionEnvironment &executionEnvironment) : execu
4041
if (DebugManager.flags.Enable64kbpages.get() > -1) {
4142
this->enable64kbpages = DebugManager.flags.Enable64kbpages.get() != 0;
4243
}
44+
localMemoryUsageBankSelector.reset(new LocalMemoryUsageBankSelector(getBanksCount()));
4345
}
4446

4547
MemoryManager::~MemoryManager() {
@@ -147,6 +149,8 @@ void MemoryManager::freeGraphicsMemory(GraphicsAllocation *gfxAllocation) {
147149
if (isLocked) {
148150
freeAssociatedResourceImpl(*gfxAllocation);
149151
}
152+
153+
localMemoryUsageBankSelector->freeOnBanks(gfxAllocation->storageInfo.getMemoryBanks(), gfxAllocation->getUnderlyingBufferSize());
150154
freeGraphicsMemoryImpl(gfxAllocation);
151155
}
152156
//if not in use destroy in place
@@ -201,7 +205,7 @@ OsContext *MemoryManager::createAndRegisterOsContext(CommandStreamReceiver *comm
201205
return osContext;
202206
}
203207

204-
bool MemoryManager::getAllocationData(AllocationData &allocationData, const AllocationProperties &properties, const void *hostPtr) {
208+
bool MemoryManager::getAllocationData(AllocationData &allocationData, const AllocationProperties &properties, const void *hostPtr, const StorageInfo &storageInfo) {
205209
UNRECOVERABLE_IF(hostPtr == nullptr && !properties.flags.allocateMemory);
206210
UNRECOVERABLE_IF(properties.allocationType == GraphicsAllocation::AllocationType::UNKNOWN);
207211

@@ -298,7 +302,7 @@ bool MemoryManager::getAllocationData(AllocationData &allocationData, const Allo
298302
allocationData.hostPtr = hostPtr;
299303
allocationData.size = properties.size;
300304
allocationData.type = properties.allocationType;
301-
allocationData.storageInfo = MemoryManager::createStorageInfoFromProperties(properties);
305+
allocationData.storageInfo = storageInfo;
302306
allocationData.alignment = properties.alignment ? properties.alignment : MemoryConstants::preferredAlignment;
303307
allocationData.imgInfo = properties.imgInfo;
304308

@@ -310,10 +314,13 @@ bool MemoryManager::getAllocationData(AllocationData &allocationData, const Allo
310314

311315
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryInPreferredPool(const AllocationProperties &properties, const void *hostPtr) {
312316
AllocationData allocationData;
313-
getAllocationData(allocationData, properties, hostPtr);
317+
getAllocationData(allocationData, properties, hostPtr, createStorageInfoFromProperties(properties));
314318

315319
AllocationStatus status = AllocationStatus::Error;
316320
GraphicsAllocation *allocation = allocateGraphicsMemoryInDevicePool(allocationData, status);
321+
if (allocation) {
322+
localMemoryUsageBankSelector->reserveOnBanks(allocationData.storageInfo.getMemoryBanks(), allocation->getUnderlyingBufferSize());
323+
}
317324
if (!allocation && status == AllocationStatus::RetryInNonDevicePool) {
318325
allocation = allocateGraphicsMemory(allocationData);
319326
}

runtime/memory_manager/memory_manager.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "runtime/memory_manager/gfx_partition.h"
1616
#include "runtime/memory_manager/graphics_allocation.h"
1717
#include "runtime/memory_manager/host_ptr_defines.h"
18+
#include "runtime/memory_manager/local_memory_usage.h"
1819
#include "runtime/os_interface/32bit_memory.h"
1920

2021
#include "engine_node.h"
@@ -183,12 +184,12 @@ class MemoryManager {
183184
ImageInfo *imgInfo = nullptr;
184185
};
185186

186-
static bool getAllocationData(AllocationData &allocationData, const AllocationProperties &properties, const void *hostPtr);
187+
static bool getAllocationData(AllocationData &allocationData, const AllocationProperties &properties, const void *hostPtr, const StorageInfo &storageInfo);
187188
static bool useInternal32BitAllocator(GraphicsAllocation::AllocationType allocationType) {
188189
return allocationType == GraphicsAllocation::AllocationType::KERNEL_ISA ||
189190
allocationType == GraphicsAllocation::AllocationType::INTERNAL_HEAP;
190191
}
191-
static StorageInfo createStorageInfoFromProperties(const AllocationProperties &properties);
192+
StorageInfo createStorageInfoFromProperties(const AllocationProperties &properties);
192193

193194
virtual GraphicsAllocation *createGraphicsAllocation(OsHandleStorage &handleStorage, const AllocationData &allocationData) = 0;
194195
virtual GraphicsAllocation *allocateGraphicsMemoryForNonSvmHostPtr(const AllocationData &allocationData) = 0;
@@ -243,6 +244,7 @@ class MemoryManager {
243244
virtual void *lockResourceImpl(GraphicsAllocation &graphicsAllocation) = 0;
244245
virtual void unlockResourceImpl(GraphicsAllocation &graphicsAllocation) = 0;
245246
virtual void freeAssociatedResourceImpl(GraphicsAllocation &graphicsAllocation) { return unlockResourceImpl(graphicsAllocation); };
247+
uint32_t getBanksCount();
246248

247249
bool force32bitAllocations = false;
248250
bool virtualPaddingAvailable = false;
@@ -260,6 +262,7 @@ class MemoryManager {
260262
std::unique_ptr<DeferredDeleter> multiContextResourceDestructor;
261263
std::unique_ptr<Allocator32bit> allocator32Bit;
262264
GfxPartition gfxPartition;
265+
std::unique_ptr<LocalMemoryUsageBankSelector> localMemoryUsageBankSelector;
263266
};
264267

265268
std::unique_ptr<DeferredDeleter> createDeferredDeleter();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "runtime/memory_manager/memory_manager.h"
9+
10+
namespace NEO {
11+
uint32_t MemoryManager::getBanksCount() {
12+
return 1u;
13+
}
14+
} // namespace NEO

unit_tests/helpers/hw_helper_tests.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@
2626

2727
using namespace NEO;
2828

29-
void HwHelperFixture::SetUp() {
30-
hardwareInfo = *platformDevices[0];
31-
DeviceFixture::SetUp();
32-
}
33-
void HwHelperFixture::TearDown() {
34-
DeviceFixture::TearDown();
35-
}
36-
3729
TEST(HwHelperSimpleTest, givenDebugVariableWhenAskingForRenderCompressionThenReturnCorrectValue) {
3830
DebugManagerStateRestore restore;
3931
HardwareInfo localHwInfo = **platformDevices;

unit_tests/helpers/hw_helper_tests.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414

1515
using namespace NEO;
1616

17-
class HwHelperFixture : public DeviceFixture {
18-
protected:
19-
void SetUp();
20-
void TearDown();
21-
HardwareInfo hardwareInfo;
22-
};
23-
24-
using HwHelperTest = Test<HwHelperFixture>;
17+
using HwHelperTest = Test<DeviceFixture>;
2518

2619
void testDefaultImplementationOfSetupHardwareCapabilities(HwHelper &hwHelper, const HardwareInfo &hwInfo);

unit_tests/memory_manager/local_memory_usage_tests.cpp

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ namespace NEO {
1414

1515
struct MockLocalMemoryUsageBankSelector : public LocalMemoryUsageBankSelector {
1616
using LocalMemoryUsageBankSelector::banksCount;
17+
using LocalMemoryUsageBankSelector::freeOnBank;
1718
using LocalMemoryUsageBankSelector::LocalMemoryUsageBankSelector;
19+
using LocalMemoryUsageBankSelector::reserveOnBank;
20+
using LocalMemoryUsageBankSelector::updateUsageInfo;
1821
std::atomic<uint64_t> *getMemorySizes() { return memorySizes.get(); }
1922
};
2023

@@ -27,7 +30,7 @@ TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenItsCreatedAllVal
2730
}
2831

2932
TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenMemoryIsReservedOnGivenBankThenValueStoredInTheArrayIsCorrect) {
30-
LocalMemoryUsageBankSelector selector(4u);
33+
MockLocalMemoryUsageBankSelector selector(4u);
3134

3235
uint64_t allocationSize = 1024u;
3336
auto bankIndex = selector.getLeastOccupiedBank();
@@ -37,7 +40,7 @@ TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenMemoryIsReserved
3740
}
3841

3942
TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenMemoryIsReleasedThenValueIsCorrectlyAllocated) {
40-
LocalMemoryUsageBankSelector selector(1u);
43+
MockLocalMemoryUsageBankSelector selector(1u);
4144

4245
uint64_t allocationSize = 1024u;
4346
auto bankIndex = selector.getLeastOccupiedBank();
@@ -75,7 +78,7 @@ TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenMemoryAllocatedS
7578
}
7679

7780
TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenIndexIsInvalidThenErrorIsReturned) {
78-
LocalMemoryUsageBankSelector selector(3u);
81+
MockLocalMemoryUsageBankSelector selector(3u);
7982
EXPECT_THROW(selector.reserveOnBank(8u, 1024u), std::exception);
8083
EXPECT_THROW(selector.freeOnBank(8u, 1024u), std::exception);
8184
EXPECT_THROW(selector.getOccupiedMemorySizeForBank(8u), std::exception);
@@ -84,4 +87,52 @@ TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenIndexIsInvalidTh
8487
TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenItsCreatedWithZeroBanksThenErrorIsReturned) {
8588
EXPECT_THROW(LocalMemoryUsageBankSelector(0u), std::exception);
8689
}
90+
91+
TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenMultipleBanksAreUsedThenMemoryIsReservedOnEachOfThem) {
92+
MockLocalMemoryUsageBankSelector selector(6u);
93+
uint32_t banks = 5u;
94+
uint64_t allocationSize = 1024u;
95+
96+
selector.reserveOnBanks(banks, allocationSize);
97+
EXPECT_EQ(allocationSize, selector.getOccupiedMemorySizeForBank(0u));
98+
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(1u));
99+
EXPECT_EQ(allocationSize, selector.getOccupiedMemorySizeForBank(2u));
100+
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(3u));
101+
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(4u));
102+
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(5u));
103+
}
104+
105+
TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenMultipleBanksAreUsedThenMemoryIsReleasedOnEachOfThem) {
106+
MockLocalMemoryUsageBankSelector selector(6u);
107+
uint32_t banks = 5u;
108+
uint64_t allocationSize = 1024u;
109+
110+
selector.reserveOnBanks(banks, allocationSize);
111+
selector.reserveOnBanks(banks, allocationSize);
112+
113+
EXPECT_EQ(2 * allocationSize, selector.getOccupiedMemorySizeForBank(0u));
114+
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(1u));
115+
EXPECT_EQ(2 * allocationSize, selector.getOccupiedMemorySizeForBank(2u));
116+
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(3));
117+
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(4));
118+
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(5));
119+
120+
selector.freeOnBanks(banks, allocationSize);
121+
EXPECT_EQ(allocationSize, selector.getOccupiedMemorySizeForBank(0u));
122+
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(1u));
123+
EXPECT_EQ(allocationSize, selector.getOccupiedMemorySizeForBank(2u));
124+
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(3u));
125+
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(4u));
126+
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(5u));
127+
}
128+
129+
TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenThereAreMoreThan32BanksThenOnly32AreUpdated) {
130+
MockLocalMemoryUsageBankSelector selector(33u);
131+
uint32_t banks = ~0u;
132+
uint64_t allocationSize = 1024u;
133+
134+
selector.reserveOnBanks(banks, allocationSize);
135+
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(32));
136+
}
137+
87138
} // namespace NEO

0 commit comments

Comments
 (0)