Add local memory usage selector in memory manager

Related-To: NEO-2906

Change-Id: I3172e9551b8d06437c273b122dc6e9d529155b5c
Signed-off-by: Pawel Wilma <pawel.wilma@intel.com>
This commit is contained in:
Pawel Wilma
2019-05-10 11:30:07 +02:00
committed by sys_ocldev
parent a90270c1de
commit b64210d3db
17 changed files with 205 additions and 76 deletions

View File

@@ -33,6 +33,7 @@ set(RUNTIME_SRCS_MEMORY_MANAGER
${CMAKE_CURRENT_SOURCE_DIR}/memory_constants.h
${CMAKE_CURRENT_SOURCE_DIR}/memory_manager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/memory_manager.h
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/memory_manager_banks_count.cpp
${CMAKE_CURRENT_SOURCE_DIR}/memory_pool.h
${CMAKE_CURRENT_SOURCE_DIR}/os_agnostic_memory_manager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/os_agnostic_memory_manager.h

View File

@@ -10,5 +10,6 @@
namespace NEO {
struct StorageInfo {
uint32_t getNumHandles() const;
uint32_t getMemoryBanks() const { return 0u; }
};
} // namespace NEO

View File

@@ -8,6 +8,7 @@
#include "runtime/memory_manager/local_memory_usage.h"
#include <algorithm>
#include <bitset>
#include <iterator>
namespace NEO {
@@ -35,4 +36,17 @@ void LocalMemoryUsageBankSelector::reserveOnBank(uint32_t bankIndex, uint64_t al
memorySizes[bankIndex] += allocationSize;
}
void LocalMemoryUsageBankSelector::updateUsageInfo(uint32_t memoryBanks, uint64_t allocationSize, bool reserve) {
auto banks = std::bitset<32>(memoryBanks);
for (uint32_t bankIndex = 0; bankIndex < banks.size() && bankIndex < banksCount; bankIndex++) {
if (banks.test(bankIndex)) {
if (reserve) {
reserveOnBank(bankIndex, allocationSize);
} else {
freeOnBank(bankIndex, allocationSize);
}
}
}
}
} // namespace NEO

View File

@@ -19,10 +19,14 @@ class LocalMemoryUsageBankSelector : public NonCopyableOrMovableClass {
LocalMemoryUsageBankSelector() = delete;
LocalMemoryUsageBankSelector(uint32_t banksCount);
uint32_t getLeastOccupiedBank();
void freeOnBank(uint32_t bankIndex, uint64_t allocationSize);
void reserveOnBank(uint32_t bankIndex, uint64_t allocationSize);
void reserveOnBanks(uint32_t memoryBanks, uint64_t allocationSize) {
updateUsageInfo(memoryBanks, allocationSize, true);
}
void freeOnBanks(uint32_t memoryBanks, uint64_t allocationSize) {
updateUsageInfo(memoryBanks, allocationSize, false);
}
MOCKABLE_VIRTUAL uint64_t getOccupiedMemorySizeForBank(uint32_t bankIndex) {
uint64_t getOccupiedMemorySizeForBank(uint32_t bankIndex) {
UNRECOVERABLE_IF(bankIndex >= banksCount);
return memorySizes[bankIndex].load();
}
@@ -30,5 +34,8 @@ class LocalMemoryUsageBankSelector : public NonCopyableOrMovableClass {
protected:
uint32_t banksCount = 0;
std::unique_ptr<std::atomic<uint64_t>[]> memorySizes = nullptr;
void updateUsageInfo(uint32_t memoryBanks, uint64_t allocationSize, bool reserve);
void freeOnBank(uint32_t bankIndex, uint64_t allocationSize);
void reserveOnBank(uint32_t bankIndex, uint64_t allocationSize);
};
} // namespace NEO

View File

@@ -25,6 +25,7 @@
#include "runtime/memory_manager/deferred_deleter.h"
#include "runtime/memory_manager/host_ptr_manager.h"
#include "runtime/memory_manager/internal_allocation_storage.h"
#include "runtime/memory_manager/local_memory_usage.h"
#include "runtime/os_interface/os_context.h"
#include "runtime/os_interface/os_interface.h"
#include "runtime/utilities/stackvec.h"
@@ -40,6 +41,7 @@ MemoryManager::MemoryManager(ExecutionEnvironment &executionEnvironment) : execu
if (DebugManager.flags.Enable64kbpages.get() > -1) {
this->enable64kbpages = DebugManager.flags.Enable64kbpages.get() != 0;
}
localMemoryUsageBankSelector.reset(new LocalMemoryUsageBankSelector(getBanksCount()));
}
MemoryManager::~MemoryManager() {
@@ -147,6 +149,8 @@ void MemoryManager::freeGraphicsMemory(GraphicsAllocation *gfxAllocation) {
if (isLocked) {
freeAssociatedResourceImpl(*gfxAllocation);
}
localMemoryUsageBankSelector->freeOnBanks(gfxAllocation->storageInfo.getMemoryBanks(), gfxAllocation->getUnderlyingBufferSize());
freeGraphicsMemoryImpl(gfxAllocation);
}
//if not in use destroy in place
@@ -201,7 +205,7 @@ OsContext *MemoryManager::createAndRegisterOsContext(CommandStreamReceiver *comm
return osContext;
}
bool MemoryManager::getAllocationData(AllocationData &allocationData, const AllocationProperties &properties, const void *hostPtr) {
bool MemoryManager::getAllocationData(AllocationData &allocationData, const AllocationProperties &properties, const void *hostPtr, const StorageInfo &storageInfo) {
UNRECOVERABLE_IF(hostPtr == nullptr && !properties.flags.allocateMemory);
UNRECOVERABLE_IF(properties.allocationType == GraphicsAllocation::AllocationType::UNKNOWN);
@@ -298,7 +302,7 @@ bool MemoryManager::getAllocationData(AllocationData &allocationData, const Allo
allocationData.hostPtr = hostPtr;
allocationData.size = properties.size;
allocationData.type = properties.allocationType;
allocationData.storageInfo = MemoryManager::createStorageInfoFromProperties(properties);
allocationData.storageInfo = storageInfo;
allocationData.alignment = properties.alignment ? properties.alignment : MemoryConstants::preferredAlignment;
allocationData.imgInfo = properties.imgInfo;
@@ -310,10 +314,13 @@ bool MemoryManager::getAllocationData(AllocationData &allocationData, const Allo
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryInPreferredPool(const AllocationProperties &properties, const void *hostPtr) {
AllocationData allocationData;
getAllocationData(allocationData, properties, hostPtr);
getAllocationData(allocationData, properties, hostPtr, createStorageInfoFromProperties(properties));
AllocationStatus status = AllocationStatus::Error;
GraphicsAllocation *allocation = allocateGraphicsMemoryInDevicePool(allocationData, status);
if (allocation) {
localMemoryUsageBankSelector->reserveOnBanks(allocationData.storageInfo.getMemoryBanks(), allocation->getUnderlyingBufferSize());
}
if (!allocation && status == AllocationStatus::RetryInNonDevicePool) {
allocation = allocateGraphicsMemory(allocationData);
}

View File

@@ -15,6 +15,7 @@
#include "runtime/memory_manager/gfx_partition.h"
#include "runtime/memory_manager/graphics_allocation.h"
#include "runtime/memory_manager/host_ptr_defines.h"
#include "runtime/memory_manager/local_memory_usage.h"
#include "runtime/os_interface/32bit_memory.h"
#include "engine_node.h"
@@ -183,12 +184,12 @@ class MemoryManager {
ImageInfo *imgInfo = nullptr;
};
static bool getAllocationData(AllocationData &allocationData, const AllocationProperties &properties, const void *hostPtr);
static bool getAllocationData(AllocationData &allocationData, const AllocationProperties &properties, const void *hostPtr, const StorageInfo &storageInfo);
static bool useInternal32BitAllocator(GraphicsAllocation::AllocationType allocationType) {
return allocationType == GraphicsAllocation::AllocationType::KERNEL_ISA ||
allocationType == GraphicsAllocation::AllocationType::INTERNAL_HEAP;
}
static StorageInfo createStorageInfoFromProperties(const AllocationProperties &properties);
StorageInfo createStorageInfoFromProperties(const AllocationProperties &properties);
virtual GraphicsAllocation *createGraphicsAllocation(OsHandleStorage &handleStorage, const AllocationData &allocationData) = 0;
virtual GraphicsAllocation *allocateGraphicsMemoryForNonSvmHostPtr(const AllocationData &allocationData) = 0;
@@ -243,6 +244,7 @@ class MemoryManager {
virtual void *lockResourceImpl(GraphicsAllocation &graphicsAllocation) = 0;
virtual void unlockResourceImpl(GraphicsAllocation &graphicsAllocation) = 0;
virtual void freeAssociatedResourceImpl(GraphicsAllocation &graphicsAllocation) { return unlockResourceImpl(graphicsAllocation); };
uint32_t getBanksCount();
bool force32bitAllocations = false;
bool virtualPaddingAvailable = false;
@@ -260,6 +262,7 @@ class MemoryManager {
std::unique_ptr<DeferredDeleter> multiContextResourceDestructor;
std::unique_ptr<Allocator32bit> allocator32Bit;
GfxPartition gfxPartition;
std::unique_ptr<LocalMemoryUsageBankSelector> localMemoryUsageBankSelector;
};
std::unique_ptr<DeferredDeleter> createDeferredDeleter();

View File

@@ -0,0 +1,14 @@
/*
* Copyright (C) 2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "runtime/memory_manager/memory_manager.h"
namespace NEO {
uint32_t MemoryManager::getBanksCount() {
return 1u;
}
} // namespace NEO