From 74ac02e1f34589590717f0a058bef1e4f9c94fd2 Mon Sep 17 00:00:00 2001 From: Jaime Arteaga Date: Mon, 13 Jul 2020 12:59:16 -0700 Subject: [PATCH] Improve logic in addInternalAllocationsToResidencyContainer Change-Id: I0be0082d2d70623867b691629cac09f5d653fc0d Signed-off: Jaime Arteaga --- .../command_queue/enqueue_svm_tests.cpp | 85 +++++++++++++++---- .../mocks/mock_graphics_allocation.h | 3 + .../memory_manager/unified_memory_manager.cpp | 8 +- 3 files changed, 74 insertions(+), 22 deletions(-) diff --git a/opencl/test/unit_test/command_queue/enqueue_svm_tests.cpp b/opencl/test/unit_test/command_queue/enqueue_svm_tests.cpp index 75f4a14786..99a1c14022 100644 --- a/opencl/test/unit_test/command_queue/enqueue_svm_tests.cpp +++ b/opencl/test/unit_test/command_queue/enqueue_svm_tests.cpp @@ -1447,6 +1447,8 @@ struct UpdateResidencyContainerMultipleDevicesTest : public ::testing::WithParam ClDevice *peerSubDevice0 = nullptr; ClDevice *peerSubDevice1 = nullptr; SVMAllocsManager *svmManager = nullptr; + const uint32_t numRootDevices = 2; + const uint32_t maxRootDeviceIndex = numRootDevices - 1; }; HWTEST_F(UpdateResidencyContainerMultipleDevicesTest, @@ -1461,12 +1463,13 @@ HWTEST_F(UpdateResidencyContainerMultipleDevicesTest, HWTEST_P(UpdateResidencyContainerMultipleDevicesTest, givenAllocationItIsAddedToContainerOnlyIfMaskMatches) { uint32_t pCmdBuffer[1024]; - MockGraphicsAllocation gfxAllocation(static_cast(pCmdBuffer), sizeof(pCmdBuffer)); + MockGraphicsAllocation gfxAllocation(device->getDevice().getRootDeviceIndex(), + static_cast(pCmdBuffer), sizeof(pCmdBuffer)); InternalMemoryType type = std::get<0>(GetParam()); uint32_t mask = std::get<1>(GetParam()); - SvmAllocationData allocData(gfxAllocation.getRootDeviceIndex()); + SvmAllocationData allocData(maxRootDeviceIndex); allocData.gpuAllocations.addAllocation(&gfxAllocation); allocData.memoryType = type; allocData.device = &device->getDevice(); @@ -1500,15 +1503,17 @@ HWTEST_F(UpdateResidencyContainerMultipleDevicesTest, whenInternalAllocationsAreAddedToResidencyContainerThenOnlyAllocationsFromSameDeviceAreAdded) { uint32_t pCmdBuffer[1024]; - MockGraphicsAllocation gfxAllocation(static_cast(pCmdBuffer), sizeof(pCmdBuffer)); - SvmAllocationData allocData(gfxAllocation.getRootDeviceIndex()); + MockGraphicsAllocation gfxAllocation(device->getDevice().getRootDeviceIndex(), + static_cast(pCmdBuffer), sizeof(pCmdBuffer)); + SvmAllocationData allocData(maxRootDeviceIndex); allocData.gpuAllocations.addAllocation(&gfxAllocation); allocData.memoryType = InternalMemoryType::DEVICE_UNIFIED_MEMORY; allocData.device = &device->getDevice(); uint32_t pCmdBufferPeer[1024]; - MockGraphicsAllocation gfxAllocationPeer((void *)pCmdBufferPeer, sizeof(pCmdBufferPeer)); - SvmAllocationData allocDataPeer(gfxAllocationPeer.getRootDeviceIndex()); + MockGraphicsAllocation gfxAllocationPeer(peerDevice->getDevice().getRootDeviceIndex(), + (void *)pCmdBufferPeer, sizeof(pCmdBufferPeer)); + SvmAllocationData allocDataPeer(maxRootDeviceIndex); allocDataPeer.gpuAllocations.addAllocation(&gfxAllocationPeer); allocDataPeer.memoryType = InternalMemoryType::DEVICE_UNIFIED_MEMORY; allocDataPeer.device = &peerDevice->getDevice(); @@ -1527,18 +1532,64 @@ HWTEST_F(UpdateResidencyContainerMultipleDevicesTest, } HWTEST_F(UpdateResidencyContainerMultipleDevicesTest, - givenAllocationsFromSubDevicesBelongingTheTargetDeviceThenTheyAreAddedToTheResidencyContainer) { + givenSharedAllocationWithNullDevicePointerThenAllocationIsAddedToResidencyContainer) { uint32_t pCmdBuffer[1024]; - MockGraphicsAllocation gfxAllocation(static_cast(pCmdBuffer), sizeof(pCmdBuffer)); - SvmAllocationData allocData0(gfxAllocation.getRootDeviceIndex()); + MockGraphicsAllocation gfxAllocation(device->getDevice().getRootDeviceIndex(), static_cast(pCmdBuffer), sizeof(pCmdBuffer)); + SvmAllocationData allocData(maxRootDeviceIndex); + allocData.gpuAllocations.addAllocation(&gfxAllocation); + allocData.memoryType = InternalMemoryType::SHARED_UNIFIED_MEMORY; + allocData.device = nullptr; + + svmManager->insertSVMAlloc(allocData); + EXPECT_EQ(1u, svmManager->getNumAllocs()); + + ResidencyContainer residencyContainer; + EXPECT_EQ(0u, residencyContainer.size()); + svmManager->addInternalAllocationsToResidencyContainer(device->getDevice().getRootDeviceIndex(), + residencyContainer, + InternalMemoryType::SHARED_UNIFIED_MEMORY); + EXPECT_EQ(1u, residencyContainer.size()); + EXPECT_EQ(residencyContainer[0]->getGpuAddress(), gfxAllocation.getGpuAddress()); +} + +HWTEST_F(UpdateResidencyContainerMultipleDevicesTest, + givenSharedAllocationWithNonNullDevicePointerAndDifferentDeviceToOnePassedToResidencyCallThenAllocationIsNotAddedToResidencyContainer) { + + uint32_t pCmdBuffer[1024]; + MockGraphicsAllocation gfxAllocation(peerDevice->getDevice().getRootDeviceIndex(), + static_cast(pCmdBuffer), sizeof(pCmdBuffer)); + SvmAllocationData allocData(maxRootDeviceIndex); + allocData.gpuAllocations.addAllocation(&gfxAllocation); + allocData.memoryType = InternalMemoryType::SHARED_UNIFIED_MEMORY; + allocData.device = &peerDevice->getDevice(); + + svmManager->insertSVMAlloc(allocData); + EXPECT_EQ(1u, svmManager->getNumAllocs()); + + ResidencyContainer residencyContainer; + EXPECT_EQ(0u, residencyContainer.size()); + svmManager->addInternalAllocationsToResidencyContainer(device->getDevice().getRootDeviceIndex(), + residencyContainer, + InternalMemoryType::SHARED_UNIFIED_MEMORY); + EXPECT_EQ(0u, residencyContainer.size()); +} + +HWTEST_F(UpdateResidencyContainerMultipleDevicesTest, + givenAllocationsFromSubDevicesBelongingToTheSameTargetDeviceThenTheyAreAddedToTheResidencyContainer) { + + uint32_t pCmdBuffer[1024]; + MockGraphicsAllocation gfxAllocation(device->getDevice().getRootDeviceIndex(), + static_cast(pCmdBuffer), sizeof(pCmdBuffer)); + SvmAllocationData allocData0(maxRootDeviceIndex); allocData0.gpuAllocations.addAllocation(&gfxAllocation); allocData0.memoryType = InternalMemoryType::DEVICE_UNIFIED_MEMORY; allocData0.device = &subDevice0->getDevice(); uint32_t pCmdBufferPeer[1024]; - MockGraphicsAllocation gfxAllocationPeer((void *)pCmdBufferPeer, sizeof(pCmdBufferPeer)); - SvmAllocationData allocData1(gfxAllocationPeer.getRootDeviceIndex()); + MockGraphicsAllocation gfxAllocationPeer(device->getDevice().getRootDeviceIndex(), + (void *)pCmdBufferPeer, sizeof(pCmdBufferPeer)); + SvmAllocationData allocData1(maxRootDeviceIndex); allocData1.gpuAllocations.addAllocation(&gfxAllocationPeer); allocData1.memoryType = InternalMemoryType::DEVICE_UNIFIED_MEMORY; allocData1.device = &subDevice1->getDevice(); @@ -1556,18 +1607,20 @@ HWTEST_F(UpdateResidencyContainerMultipleDevicesTest, } HWTEST_F(UpdateResidencyContainerMultipleDevicesTest, - givenAllocationsFromSubDevicesNotBelongingTheTargetDeviceThenTheyAreNotAddedToTheResidencyContainer) { + givenAllocationsFromSubDevicesNotBelongingToTheSameTargetDeviceThenTheyAreNotAddedToTheResidencyContainer) { uint32_t pCmdBuffer[1024]; - MockGraphicsAllocation gfxAllocation(static_cast(pCmdBuffer), sizeof(pCmdBuffer)); - SvmAllocationData allocData0(gfxAllocation.getRootDeviceIndex()); + MockGraphicsAllocation gfxAllocation(device->getDevice().getRootDeviceIndex(), + static_cast(pCmdBuffer), sizeof(pCmdBuffer)); + SvmAllocationData allocData0(maxRootDeviceIndex); allocData0.gpuAllocations.addAllocation(&gfxAllocation); allocData0.memoryType = InternalMemoryType::DEVICE_UNIFIED_MEMORY; allocData0.device = &subDevice0->getDevice(); uint32_t pCmdBufferPeer[1024]; - MockGraphicsAllocation gfxAllocationPeer((void *)pCmdBufferPeer, sizeof(pCmdBufferPeer)); - SvmAllocationData allocData1(gfxAllocationPeer.getRootDeviceIndex()); + MockGraphicsAllocation gfxAllocationPeer(device->getDevice().getRootDeviceIndex(), + (void *)pCmdBufferPeer, sizeof(pCmdBufferPeer)); + SvmAllocationData allocData1(maxRootDeviceIndex); allocData1.gpuAllocations.addAllocation(&gfxAllocationPeer); allocData1.memoryType = InternalMemoryType::DEVICE_UNIFIED_MEMORY; allocData1.device = &subDevice1->getDevice(); diff --git a/opencl/test/unit_test/mocks/mock_graphics_allocation.h b/opencl/test/unit_test/mocks/mock_graphics_allocation.h index 7c0b092ba4..02b7ace51d 100644 --- a/opencl/test/unit_test/mocks/mock_graphics_allocation.h +++ b/opencl/test/unit_test/mocks/mock_graphics_allocation.h @@ -33,6 +33,9 @@ class MockGraphicsAllocation : public MemoryAllocation { MockGraphicsAllocation(void *buffer, uint64_t gpuAddr, size_t sizeIn) : MemoryAllocation(0, AllocationType::UNKNOWN, buffer, gpuAddr, 0llu, sizeIn, MemoryPool::MemoryNull) {} + MockGraphicsAllocation(uint32_t rootDeviceIndex, void *buffer, size_t sizeIn) + : MemoryAllocation(rootDeviceIndex, AllocationType::UNKNOWN, buffer, castToUint64(buffer), 0llu, sizeIn, MemoryPool::MemoryNull) {} + void resetInspectionIds() { for (auto &usageInfo : usageInfos) { usageInfo.inspectionId = 0u; diff --git a/shared/source/memory_manager/unified_memory_manager.cpp b/shared/source/memory_manager/unified_memory_manager.cpp index b8792959ea..7111fc218f 100644 --- a/shared/source/memory_manager/unified_memory_manager.cpp +++ b/shared/source/memory_manager/unified_memory_manager.cpp @@ -75,12 +75,8 @@ void SVMAllocsManager::addInternalAllocationsToResidencyContainer(uint32_t rootD uint32_t requestedTypesMask) { std::unique_lock lock(mtx); for (auto &allocation : this->SVMAllocs.allocations) { - if (!(allocation.second.memoryType & requestedTypesMask)) { - continue; - } - if ((allocation.second.memoryType & InternalMemoryType::DEVICE_UNIFIED_MEMORY || - allocation.second.memoryType & InternalMemoryType::SHARED_UNIFIED_MEMORY) && - (static_cast(allocation.second.device)->getRootDeviceIndex() != rootDeviceIndex)) { + if (!(allocation.second.memoryType & requestedTypesMask) || + (nullptr == allocation.second.gpuAllocations.getGraphicsAllocation(rootDeviceIndex))) { continue; } residencyContainer.push_back(allocation.second.gpuAllocations.getGraphicsAllocation(rootDeviceIndex));