diff --git a/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.h b/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.h index 3a739b6365..cbf2715fff 100644 --- a/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.h +++ b/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.h @@ -470,6 +470,10 @@ class MemoryManagerOpenIpcMock : public MemoryManagerIpcMock { MemoryManagerOpenIpcMock(NEO::ExecutionEnvironment &executionEnvironment) : MemoryManagerIpcMock(executionEnvironment) {} NEO::GraphicsAllocation *allocateGraphicsMemoryWithProperties(const AllocationProperties &properties) override { + return allocateGraphicsMemoryWithProperties(properties, nullptr); + } + + NEO::GraphicsAllocation *allocateGraphicsMemoryWithProperties(const AllocationProperties &properties, const void *externalPtr) override { auto ptr = reinterpret_cast(sharedHandleAddress++); auto gmmHelper = getGmmHelper(0); auto canonizedGpuAddress = gmmHelper->canonize(castToUint64(ptr)); diff --git a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp index 3e21c4bd11..f350489ac8 100644 --- a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp +++ b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp @@ -844,6 +844,32 @@ TEST_F(MemoryTest, whenAllocatingSharedMemoryWithUseHostPtrFlagThenExternalHostP ASSERT_EQ(result, ZE_RESULT_SUCCESS); } +TEST_F(MemoryTest, givenNoSupportForDualStorageSharedMemoryWhenAllocatingSharedMemoryWithUseHostPtrFlagThenExternalHostPtrIsSet) { + DebugManagerStateRestore restorer; + DebugManager.flags.AllocateSharedAllocationsWithCpuAndGpuStorage.set(0); + + size_t size = 10; + size_t alignment = 1u; + void *ptr = reinterpret_cast(0x1234); + + ze_device_mem_alloc_desc_t deviceDesc = {}; + ze_host_mem_alloc_desc_t hostDesc = {}; + hostDesc.flags = ZEX_HOST_MEM_ALLOC_FLAG_USE_HOST_PTR; + ze_result_t result = context->allocSharedMem(device->toHandle(), + &deviceDesc, + &hostDesc, + size, alignment, &ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, ptr); + + auto allocData = driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr); + EXPECT_NE(nullptr, allocData); + EXPECT_EQ(allocData->allocationFlagsProperty.hostptr, 0x1234u); + + result = context->freeMem(ptr); + ASSERT_EQ(result, ZE_RESULT_SUCCESS); +} + TEST_F(MemoryTest, whenAllocatingHostMemoryWithUseHostPtrFlagThenExternalHostPtrIsSet) { size_t size = 10; size_t alignment = 1u; @@ -1338,7 +1364,7 @@ TEST_F(FreeExtTests, } TEST_F(FreeExtTests, - whenallocMemFailsWithDeferredFreeAllocationThenMemoryFreed) { + whenAllocMemFailsWithDeferredFreeAllocationThenMemoryFreed) { size_t size = 1024; size_t alignment = 1u; void *ptr = nullptr; @@ -1359,7 +1385,9 @@ TEST_F(FreeExtTests, EXPECT_EQ(1u, memManager->deferFreeCallsMade); static_cast(driverHandle->getMemoryManager())->deferAllocInUse = false; static_cast(driverHandle->getMemoryManager())->isMockHostMemoryManager = true; + static_cast(driverHandle->getMemoryManager())->forceFailureInPrimaryAllocation = true; + static_cast(driverHandle->getMemoryManager())->forceFailureInAllocationWithHostPointer = true; void *ptr2 = nullptr; ze_device_mem_alloc_desc_t deviceDesc = {}; @@ -1370,6 +1398,8 @@ TEST_F(FreeExtTests, EXPECT_EQ(0u, memManager->numDeferFreeAllocs()); static_cast(driverHandle->getMemoryManager())->forceFailureInPrimaryAllocation = false; + static_cast(driverHandle->getMemoryManager())->forceFailureInAllocationWithHostPointer = false; + result = context->allocHostMem(&hostDesc, size, alignment, &ptr); EXPECT_EQ(ZE_RESULT_SUCCESS, result); @@ -1383,6 +1413,8 @@ TEST_F(FreeExtTests, static_cast(driverHandle->getMemoryManager())->deferAllocInUse = false; static_cast(driverHandle->getMemoryManager())->forceFailureInPrimaryAllocation = true; + static_cast(driverHandle->getMemoryManager())->forceFailureInAllocationWithHostPointer = true; + result = context->allocDeviceMem(device, &deviceDesc, size, alignment, &ptr2); @@ -1390,6 +1422,8 @@ TEST_F(FreeExtTests, EXPECT_EQ(0u, memManager->numDeferFreeAllocs()); static_cast(driverHandle->getMemoryManager())->forceFailureInPrimaryAllocation = false; + static_cast(driverHandle->getMemoryManager())->forceFailureInAllocationWithHostPointer = false; + result = context->allocHostMem(&hostDesc, size, alignment, &ptr); EXPECT_EQ(ZE_RESULT_SUCCESS, result); @@ -1403,6 +1437,8 @@ TEST_F(FreeExtTests, static_cast(driverHandle->getMemoryManager())->deferAllocInUse = false; static_cast(driverHandle->getMemoryManager())->forceFailureInPrimaryAllocation = true; + static_cast(driverHandle->getMemoryManager())->forceFailureInAllocationWithHostPointer = true; + result = context->allocSharedMem(device->toHandle(), &deviceDesc, &hostDesc, @@ -1412,7 +1448,7 @@ TEST_F(FreeExtTests, } TEST_F(FreeExtTests, - whenallocMemFailsWithDeferredFreeAllocationThenMemoryFreedAndRetrySucceeds) { + whenAllocMemFailsWithDeferredFreeAllocationThenMemoryFreedAndRetrySucceeds) { size_t size = 1024; size_t alignment = 1u; void *ptr = nullptr; @@ -1433,8 +1469,11 @@ TEST_F(FreeExtTests, EXPECT_EQ(1u, memManager->deferFreeCallsMade); static_cast(driverHandle->getMemoryManager())->deferAllocInUse = false; static_cast(driverHandle->getMemoryManager())->isMockHostMemoryManager = true; + static_cast(driverHandle->getMemoryManager())->forceFailureInPrimaryAllocation = true; static_cast(driverHandle->getMemoryManager())->singleFailureInPrimaryAllocation = true; + static_cast(driverHandle->getMemoryManager())->forceFailureInAllocationWithHostPointer = true; + static_cast(driverHandle->getMemoryManager())->singleFailureInAllocationWithHostPointer = true; void *ptr2 = nullptr; ze_device_mem_alloc_desc_t deviceDesc = {}; @@ -1454,6 +1493,8 @@ TEST_F(FreeExtTests, static_cast(driverHandle->getMemoryManager())->forceFailureInPrimaryAllocation = true; static_cast(driverHandle->getMemoryManager())->singleFailureInPrimaryAllocation = true; + static_cast(driverHandle->getMemoryManager())->forceFailureInAllocationWithHostPointer = true; + static_cast(driverHandle->getMemoryManager())->singleFailureInAllocationWithHostPointer = true; result = context->allocDeviceMem(device, &deviceDesc, @@ -1471,6 +1512,8 @@ TEST_F(FreeExtTests, static_cast(driverHandle->getMemoryManager())->forceFailureInPrimaryAllocation = true; static_cast(driverHandle->getMemoryManager())->singleFailureInPrimaryAllocation = true; + static_cast(driverHandle->getMemoryManager())->forceFailureInAllocationWithHostPointer = true; + static_cast(driverHandle->getMemoryManager())->singleFailureInAllocationWithHostPointer = true; result = context->allocSharedMem(device->toHandle(), &deviceDesc, @@ -3919,8 +3962,8 @@ TEST_F(MemoryBitfieldTest, givenDeviceWithValidBitfieldWhenAllocatingDeviceMemor EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_NE(nullptr, ptr); } -TEST(MemoryBitfieldTests, givenDeviceWithValidBitfieldWhenAllocatingSharedMemoryThenPassProperBitfield) { +TEST(MemoryBitfieldTests, givenDeviceWithValidBitfieldWhenAllocatingSharedMemoryThenPassProperBitfield) { DebugManagerStateRestore restorer; size_t size = 10; size_t alignment = 1u; diff --git a/opencl/test/unit_test/memory_manager/memory_manager_tests.cpp b/opencl/test/unit_test/memory_manager/memory_manager_tests.cpp index ca3eb4b0cc..28d18c14bd 100644 --- a/opencl/test/unit_test/memory_manager/memory_manager_tests.cpp +++ b/opencl/test/unit_test/memory_manager/memory_manager_tests.cpp @@ -2706,6 +2706,11 @@ class MemoryManagerWithFailure : public MockMemoryManager { recentlyPassedDeviceBitfield = properties.subDevicesBitfield; return nullptr; } + + GraphicsAllocation *allocateGraphicsMemoryWithProperties(const AllocationProperties &properties, const void *ptr) override { + recentlyPassedDeviceBitfield = properties.subDevicesBitfield; + return nullptr; + } }; TEST(MemoryManagerTest, whenMemoryManagerReturnsNullptrThenAllocateGlobalsSurfaceAlsoReturnsNullptr) { diff --git a/shared/source/memory_manager/unified_memory_manager.cpp b/shared/source/memory_manager/unified_memory_manager.cpp index fa61b2b23d..413e0772e8 100644 --- a/shared/source/memory_manager/unified_memory_manager.cpp +++ b/shared/source/memory_manager/unified_memory_manager.cpp @@ -244,6 +244,9 @@ void *SVMAllocsManager::createUnifiedMemoryAllocation(size_t size, size_t pageSizeForAlignment = MemoryConstants::pageSize64k; size_t alignedSize = alignUp(size, pageSizeForAlignment); + auto externalPtr = reinterpret_cast(memoryProperties.allocationFlags.hostptr); + bool useExternalHostPtrForCpu = externalPtr != nullptr; + bool compressionEnabled = false; AllocationType allocationType = getGraphicsAllocationTypeAndCompressionPreference(memoryProperties, compressionEnabled); @@ -259,7 +262,7 @@ void *SVMAllocsManager::createUnifiedMemoryAllocation(size_t size, } AllocationProperties unifiedMemoryProperties{rootDeviceIndex, - true, + !useExternalHostPtrForCpu, // allocateMemory alignedSize, allocationType, false, @@ -281,14 +284,16 @@ void *SVMAllocsManager::createUnifiedMemoryAllocation(size_t size, } } else if (memoryProperties.memoryType == InternalMemoryType::HOST_UNIFIED_MEMORY) { unifiedMemoryProperties.flags.isUSMHostAllocation = true; + } else { + unifiedMemoryProperties.flags.isUSMHostAllocation = useExternalHostPtrForCpu; } - GraphicsAllocation *unifiedMemoryAllocation = memoryManager->allocateGraphicsMemoryWithProperties(unifiedMemoryProperties); + GraphicsAllocation *unifiedMemoryAllocation = memoryManager->allocateGraphicsMemoryWithProperties(unifiedMemoryProperties, externalPtr); if (!unifiedMemoryAllocation) { if (memoryProperties.memoryType == InternalMemoryType::DEVICE_UNIFIED_MEMORY && this->usmDeviceAllocationsCacheEnabled) { this->trimUSMDeviceAllocCache(); - unifiedMemoryAllocation = memoryManager->allocateGraphicsMemoryWithProperties(unifiedMemoryProperties); + unifiedMemoryAllocation = memoryManager->allocateGraphicsMemoryWithProperties(unifiedMemoryProperties, externalPtr); } if (!unifiedMemoryAllocation) { return nullptr; @@ -308,7 +313,11 @@ void *SVMAllocsManager::createUnifiedMemoryAllocation(size_t size, std::unique_lock lock(mtx); this->SVMAllocs.insert(allocData); - return reinterpret_cast(unifiedMemoryAllocation->getGpuAddress()); + + auto retPtr = reinterpret_cast(unifiedMemoryAllocation->getGpuAddress()); + UNRECOVERABLE_IF(useExternalHostPtrForCpu && (externalPtr != retPtr)); + + return retPtr; } void *SVMAllocsManager::createSharedUnifiedMemoryAllocation(size_t size, diff --git a/shared/test/common/mocks/mock_memory_manager.cpp b/shared/test/common/mocks/mock_memory_manager.cpp index e9f82c9d5a..f421cf8f5f 100644 --- a/shared/test/common/mocks/mock_memory_manager.cpp +++ b/shared/test/common/mocks/mock_memory_manager.cpp @@ -93,10 +93,17 @@ GraphicsAllocation *MockMemoryManager::allocateGraphicsMemoryWithProperties(cons if (isMockHostMemoryManager) { allocateGraphicsMemoryWithPropertiesCount++; if (forceFailureInAllocationWithHostPointer) { + if (singleFailureInAllocationWithHostPointer) { + forceFailureInAllocationWithHostPointer = false; + } + return nullptr; } + return NEO::MemoryManager::allocateGraphicsMemoryWithProperties(properties, ptr); } + + recentlyPassedDeviceBitfield = properties.subDevicesBitfield; return OsAgnosticMemoryManager::allocateGraphicsMemoryWithProperties(properties, ptr); } diff --git a/shared/test/common/mocks/mock_memory_manager.h b/shared/test/common/mocks/mock_memory_manager.h index 7cc7463e8b..5320bf96b6 100644 --- a/shared/test/common/mocks/mock_memory_manager.h +++ b/shared/test/common/mocks/mock_memory_manager.h @@ -250,6 +250,7 @@ class MockMemoryManager : public MemoryManagerCreate { bool forceFailureInPrimaryAllocation = false; bool singleFailureInPrimaryAllocation = false; bool forceFailureInAllocationWithHostPointer = false; + bool singleFailureInAllocationWithHostPointer = false; bool isMockHostMemoryManager = false; bool deferAllocInUse = false; bool isMockEventPoolCreateMemoryManager = false;