diff --git a/shared/source/memory_manager/memory_manager.cpp b/shared/source/memory_manager/memory_manager.cpp index aaa66d8e3e..2bddded09c 100644 --- a/shared/source/memory_manager/memory_manager.cpp +++ b/shared/source/memory_manager/memory_manager.cpp @@ -135,6 +135,36 @@ GraphicsAllocation *MemoryManager::createPaddedAllocation(GraphicsAllocation *in return allocateGraphicsMemoryWithProperties({inputGraphicsAllocation->getRootDeviceIndex(), sizeWithPadding, GraphicsAllocation::AllocationType::INTERNAL_HOST_MEMORY, systemMemoryBitfield}); } +void *MemoryManager::createMultiGraphicsAllocation(std::vector &rootDeviceIndices, AllocationProperties &properties, MultiGraphicsAllocation &multiGraphicsAllocation) { + void *ptr = nullptr; + + for (auto &rootDeviceIndex : rootDeviceIndices) { + properties.rootDeviceIndex = rootDeviceIndex; + + if (!ptr) { + auto graphicsAllocation = allocateGraphicsMemoryWithProperties(properties); + if (!graphicsAllocation) { + return nullptr; + } + multiGraphicsAllocation.addAllocation(graphicsAllocation); + ptr = reinterpret_cast(graphicsAllocation->getGpuAddress()); + } else { + properties.flags.allocateMemory = false; + + auto graphicsAllocation = allocateGraphicsMemoryWithProperties(properties, ptr); + if (!graphicsAllocation) { + for (auto gpuAllocation : multiGraphicsAllocation.getGraphicsAllocations()) { + freeGraphicsMemory(gpuAllocation); + } + return nullptr; + } + multiGraphicsAllocation.addAllocation(graphicsAllocation); + } + } + + return ptr; +} + void MemoryManager::freeSystemMemory(void *ptr) { ::alignedFree(ptr); } diff --git a/shared/source/memory_manager/memory_manager.h b/shared/source/memory_manager/memory_manager.h index 67713e3c68..ee890d88a3 100644 --- a/shared/source/memory_manager/memory_manager.h +++ b/shared/source/memory_manager/memory_manager.h @@ -16,6 +16,7 @@ #include "shared/source/memory_manager/graphics_allocation.h" #include "shared/source/memory_manager/host_ptr_defines.h" #include "shared/source/memory_manager/local_memory_usage.h" +#include "shared/source/memory_manager/multi_graphics_allocation.h" #include "shared/source/page_fault_manager/cpu_page_fault_manager.h" #include "engine_node.h" @@ -89,6 +90,8 @@ class MemoryManager { GraphicsAllocation *createGraphicsAllocationWithPadding(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding); virtual GraphicsAllocation *createPaddedAllocation(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding); + void *createMultiGraphicsAllocation(std::vector &rootDeviceIndices, AllocationProperties &properties, MultiGraphicsAllocation &multiGraphicsAllocation); + virtual AllocationStatus populateOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) = 0; virtual void cleanOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) = 0; diff --git a/shared/source/memory_manager/unified_memory_manager.cpp b/shared/source/memory_manager/unified_memory_manager.cpp index 7111fc218f..3dda87dc99 100644 --- a/shared/source/memory_manager/unified_memory_manager.cpp +++ b/shared/source/memory_manager/unified_memory_manager.cpp @@ -118,7 +118,14 @@ void *SVMAllocsManager::createHostUnifiedMemoryAllocation(uint32_t maxRootDevice GraphicsAllocation::AllocationType allocationType = GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY; - uint32_t rootDeviceIndex = 0u; + std::vector rootDeviceIndices; + rootDeviceIndices.reserve(maxRootDeviceIndex); + for (auto rootDeviceIndex = 0u; rootDeviceIndex <= maxRootDeviceIndex; rootDeviceIndex++) { + rootDeviceIndices.push_back(rootDeviceIndex); + } + + uint32_t rootDeviceIndex = rootDeviceIndices.at(0); + AllocationProperties unifiedMemoryProperties{rootDeviceIndex, true, alignedSize, @@ -130,37 +137,17 @@ void *SVMAllocsManager::createHostUnifiedMemoryAllocation(uint32_t maxRootDevice SvmAllocationData allocData(maxRootDeviceIndex); - GraphicsAllocation *unifiedMemoryAllocation = memoryManager->allocateGraphicsMemoryWithProperties(unifiedMemoryProperties); - if (!unifiedMemoryAllocation) { + void *usmPtr = memoryManager->createMultiGraphicsAllocation(rootDeviceIndices, unifiedMemoryProperties, allocData.gpuAllocations); + if (!usmPtr) { return nullptr; } - allocData.gpuAllocations.addAllocation(unifiedMemoryAllocation); allocData.cpuAllocation = nullptr; allocData.size = size; allocData.memoryType = memoryProperties.memoryType; allocData.allocationFlagsProperty = memoryProperties.allocationFlags; allocData.device = nullptr; - void *usmPtr = reinterpret_cast(unifiedMemoryAllocation->getGpuAddress()); - - // Create allocation for the rest of the indexes, using the previously-allocated memory - for (rootDeviceIndex = 1; rootDeviceIndex <= maxRootDeviceIndex; rootDeviceIndex++) { - unifiedMemoryProperties.rootDeviceIndex = rootDeviceIndex; - unifiedMemoryProperties.flags.allocateMemory = false; - - GraphicsAllocation *unifiedMemoryAllocation = memoryManager->allocateGraphicsMemoryWithProperties(unifiedMemoryProperties, - usmPtr); - if (!unifiedMemoryAllocation) { - for (auto gpuAllocation : allocData.gpuAllocations.getGraphicsAllocations()) { - memoryManager->freeGraphicsMemory(gpuAllocation); - } - return nullptr; - } - - allocData.gpuAllocations.addAllocation(unifiedMemoryAllocation); - } - std::unique_lock lock(mtx); this->SVMAllocs.insert(allocData);