From f95644fbd73aebf152407f3999b604b01500ac53 Mon Sep 17 00:00:00 2001 From: Aravind Gopalakrishnan Date: Fri, 4 Apr 2025 19:50:18 +0000 Subject: [PATCH] fix: Fix usage of root device when opening IPC handle - Use root device if device is implicit scaling capable Related-To: NEO-13433 Signed-off-by: Aravind Gopalakrishnan --- .../core/source/context/context_imp.cpp | 7 +- .../fixtures/memory_ipc_fixture.cpp | 3 +- .../memory/linux/test_memory_linux.cpp | 287 ++++++++++++++ .../unit_tests/sources/memory/test_memory.cpp | 360 +++++------------- 4 files changed, 384 insertions(+), 273 deletions(-) diff --git a/level_zero/core/source/context/context_imp.cpp b/level_zero/core/source/context/context_imp.cpp index 427d3a5b4a..85b169c07f 100644 --- a/level_zero/core/source/context/context_imp.cpp +++ b/level_zero/core/source/context/context_imp.cpp @@ -791,7 +791,12 @@ ze_result_t ContextImp::openIpcMemHandles(ze_device_handle_t hDevice, handles.push_back(static_cast(handle)); } - auto neoDevice = Device::fromHandle(hDevice)->getNEODevice(); + + auto device = Device::fromHandle(hDevice); + auto neoDevice = device->getNEODevice(); + if (device->isImplicitScalingCapable()) { + neoDevice = device->getNEODevice()->getRootDevice(); + } NEO::SvmAllocationData allocDataInternal(neoDevice->getRootDeviceIndex()); *pptr = this->driverHandle->importFdHandles(neoDevice, flags, handles, nullptr, nullptr, allocDataInternal); if (nullptr == *pptr) { diff --git a/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.cpp b/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.cpp index 8567e43fa7..bef68f5214 100644 --- a/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.cpp +++ b/level_zero/core/test/unit_tests/fixtures/memory_ipc_fixture.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2024 Intel Corporation + * Copyright (C) 2023-2025 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -507,6 +507,7 @@ NEO::GraphicsAllocation *MemoryManagerIpcImplicitScalingMock::createGraphicsAllo void MemoryExportImportImplicitScalingTest::SetUp() { DebugManagerStateRestore restorer; debugManager.flags.EnableImplicitScaling.set(1); + debugManager.flags.EnableWalkerPartition.set(1); neoDevice = NEO::MockDevice::createWithNewExecutionEnvironment(NEO::defaultHwInfo.get()); diff --git a/level_zero/core/test/unit_tests/sources/memory/linux/test_memory_linux.cpp b/level_zero/core/test/unit_tests/sources/memory/linux/test_memory_linux.cpp index 12fa5d6dd8..c128ca0f02 100644 --- a/level_zero/core/test/unit_tests/sources/memory/linux/test_memory_linux.cpp +++ b/level_zero/core/test/unit_tests/sources/memory/linux/test_memory_linux.cpp @@ -702,5 +702,292 @@ TEST_F(MemoryObtainFdTest, ASSERT_EQ(result, ZE_RESULT_SUCCESS); } +TEST_F(MemoryExportImportImplicitScalingTest, + whenCallingOpenIpcHandlesWithIpcHandleThenDeviceAllocationIsReturned) { + size_t size = 10; + size_t alignment = 1u; + void *ptr = nullptr; + + ze_device_mem_alloc_desc_t deviceDesc = {}; + ze_result_t result = context->allocDeviceMem(device->toHandle(), + &deviceDesc, + size, alignment, &ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, ptr); + + uint32_t numIpcHandles = 0; + result = context->getIpcMemHandles(ptr, &numIpcHandles, nullptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(numIpcHandles, 2u); + + std::vector ipcHandles(numIpcHandles); + result = context->getIpcMemHandles(ptr, &numIpcHandles, ipcHandles.data()); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); + + ze_ipc_memory_flags_t flags = {}; + void *ipcPtr; + result = context->openIpcMemHandles(device->toHandle(), numIpcHandles, ipcHandles.data(), flags, &ipcPtr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + result = context->closeIpcMemHandle(ipcPtr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + result = context->freeMem(ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); +} + +TEST_F(MemoryExportImportImplicitScalingTest, + whenCallingOpenIpcHandleWithIpcHandleThenAllocationCountIsIncremented) { + size_t size = 10; + size_t alignment = 1u; + void *ptr = nullptr; + + ze_device_mem_alloc_desc_t deviceDesc = {}; + ze_result_t result = context->allocDeviceMem(device->toHandle(), + &deviceDesc, + size, alignment, &ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, ptr); + + uint32_t numIpcHandles = 0; + result = context->getIpcMemHandles(ptr, &numIpcHandles, nullptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + auto usmManager = context->getDriverHandle()->getSvmAllocsManager(); + auto currentAllocationCount = usmManager->allocationsCounter.load(); + + std::vector ipcHandles(numIpcHandles); + result = context->getIpcMemHandles(ptr, &numIpcHandles, ipcHandles.data()); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); + + ze_ipc_memory_flags_t flags = {}; + void *ipcPtr; + + result = context->openIpcMemHandle(device->toHandle(), ipcHandles[0], flags, &ipcPtr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + auto newAllocationCount = usmManager->allocationsCounter.load(); + EXPECT_GT(newAllocationCount, currentAllocationCount); + EXPECT_EQ(usmManager->getSVMAlloc(ipcPtr)->getAllocId(), newAllocationCount); + + result = context->closeIpcMemHandle(ipcPtr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + result = context->freeMem(ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); +} + +TEST_F(MemoryExportImportImplicitScalingTest, + whenCallingOpenIpcHandleWithIpcHandleAndSharedMemoryTypeThenInvalidArgumentIsReturned) { + DebugManagerStateRestore restorer; + debugManager.flags.EnableImplicitScaling.set(0u); + + size_t size = 10; + size_t alignment = 1u; + void *ptr = nullptr; + + ze_device_mem_alloc_desc_t deviceDesc = {}; + ze_result_t result = context->allocDeviceMem(device->toHandle(), + &deviceDesc, + size, alignment, &ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, ptr); + + ze_ipc_mem_handle_t ipcHandle; + result = context->getIpcMemHandle(ptr, &ipcHandle); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); + + ze_ipc_memory_flags_t flags = {}; + void *ipcPtr; + + IpcMemoryData &ipcData = *reinterpret_cast(ipcHandle.data); + ipcData.type = static_cast(ZE_MEMORY_TYPE_SHARED); + + result = context->openIpcMemHandle(device->toHandle(), ipcHandle, flags, &ipcPtr); + EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, result); + + result = context->freeMem(ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); +} + +TEST_F(MemoryExportImportImplicitScalingTest, + whenCallingOpenIpcHandlesWithIpcHandleAndHostTypeAllocationTheninvalidArgumentIsReturned) { + size_t size = 10; + size_t alignment = 1u; + void *ptr = nullptr; + + ze_host_mem_alloc_desc_t hostDesc = {}; + ze_result_t result = context->allocHostMem(&hostDesc, + size, alignment, &ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, ptr); + + uint32_t numIpcHandles = 0; + result = context->getIpcMemHandles(ptr, &numIpcHandles, nullptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(numIpcHandles, 2u); + + std::vector ipcHandles(numIpcHandles); + result = context->getIpcMemHandles(ptr, &numIpcHandles, ipcHandles.data()); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); + + ze_ipc_memory_flags_t flags = {}; + void *ipcPtr; + result = context->openIpcMemHandles(device->toHandle(), numIpcHandles, ipcHandles.data(), flags, &ipcPtr); + EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, result); + + result = context->freeMem(ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); +} + +TEST_F(MemoryExportImportImplicitScalingTest, + whenCallingImportFdHandlesWithAllocationPointerThenAllocationIsReturned) { + size_t size = 10; + size_t alignment = 1u; + void *ptr = nullptr; + + ze_device_mem_alloc_desc_t deviceDesc = {}; + ze_result_t result = context->allocDeviceMem(device->toHandle(), + &deviceDesc, + size, alignment, &ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, ptr); + + uint32_t numIpcHandles = 0; + result = context->getIpcMemHandles(ptr, &numIpcHandles, nullptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(numIpcHandles, 2u); + + std::vector ipcHandles(numIpcHandles); + result = context->getIpcMemHandles(ptr, &numIpcHandles, ipcHandles.data()); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); + + std::vector handles; + for (uint32_t i = 0; i < numIpcHandles; i++) { + uint64_t handle = 0; + memcpy_s(&handle, + sizeof(handle), + reinterpret_cast(ipcHandles[i].data), + sizeof(handle)); + handles.push_back(static_cast(handle)); + } + + ze_ipc_memory_flags_t flags = {}; + void *ipcPtr; + NEO::GraphicsAllocation *ipcAlloc = nullptr; + DriverHandleImp *driverHandleImp = static_cast(context->getDriverHandle()); + NEO::SvmAllocationData allocDataInternal(device->getNEODevice()->getRootDeviceIndex()); + ipcPtr = driverHandleImp->importFdHandles(device->getNEODevice(), flags, handles, nullptr, &ipcAlloc, allocDataInternal); + EXPECT_NE(ipcPtr, nullptr); + EXPECT_NE(ipcAlloc, nullptr); + + result = context->closeIpcMemHandle(ipcPtr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + result = context->freeMem(ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); +} + +TEST_F(MemoryExportImportImplicitScalingTest, + whenCallingImportFdHandlesWithUncachedFlagAllocationPointerThenAllocationIsReturned) { + size_t size = 10; + size_t alignment = 1u; + void *ptr = nullptr; + + ze_device_mem_alloc_desc_t deviceDesc = {}; + ze_result_t result = context->allocDeviceMem(device->toHandle(), + &deviceDesc, + size, alignment, &ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, ptr); + + uint32_t numIpcHandles = 0; + result = context->getIpcMemHandles(ptr, &numIpcHandles, nullptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(numIpcHandles, 2u); + + std::vector ipcHandles(numIpcHandles); + result = context->getIpcMemHandles(ptr, &numIpcHandles, ipcHandles.data()); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); + + std::vector handles; + for (uint32_t i = 0; i < numIpcHandles; i++) { + uint64_t handle = 0; + memcpy_s(&handle, + sizeof(handle), + reinterpret_cast(ipcHandles[i].data), + sizeof(handle)); + handles.push_back(static_cast(handle)); + } + + ze_ipc_memory_flags_t flags = {ZE_IPC_MEMORY_FLAG_BIAS_UNCACHED}; + void *ipcPtr; + NEO::GraphicsAllocation *ipcAlloc = nullptr; + DriverHandleImp *driverHandleImp = static_cast(context->getDriverHandle()); + NEO::SvmAllocationData allocDataInternal(device->getNEODevice()->getRootDeviceIndex()); + ipcPtr = driverHandleImp->importFdHandles(device->getNEODevice(), flags, handles, nullptr, &ipcAlloc, allocDataInternal); + EXPECT_NE(ipcPtr, nullptr); + EXPECT_NE(ipcAlloc, nullptr); + + result = context->closeIpcMemHandle(ipcPtr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + result = context->freeMem(ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); +} + +TEST_F(MemoryExportImportImplicitScalingTest, + whenCallingGetIpcMemHandlesAndImportFailsThenInvalidArgumentFails) { + currMemoryManager->failOnCreateGraphicsAllocationFromSharedHandle = true; + + size_t size = 10; + size_t alignment = 1u; + void *ptr = nullptr; + + ze_device_mem_alloc_desc_t deviceDesc = {}; + ze_result_t result = context->allocDeviceMem(device->toHandle(), + &deviceDesc, + size, alignment, &ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, ptr); + + uint32_t numIpcHandles = 0; + result = context->getIpcMemHandles(ptr, &numIpcHandles, nullptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(numIpcHandles, 2u); + + std::vector ipcHandles(numIpcHandles); + result = context->getIpcMemHandles(ptr, &numIpcHandles, ipcHandles.data()); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); + neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); + + ze_ipc_memory_flags_t flags = {}; + void *ipcPtr; + result = context->openIpcMemHandles(device->toHandle(), numIpcHandles, ipcHandles.data(), flags, &ipcPtr); + EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, result); + + result = context->freeMem(ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); +} + } // namespace ult } // namespace L0 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 cf589c532f..64114d7749 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 @@ -116,74 +116,109 @@ TEST_F(MemoryExportImportImplicitScalingTest, EXPECT_EQ(ZE_RESULT_SUCCESS, result); } -TEST_F(MemoryExportImportImplicitScalingTest, - whenCallingOpenIpcHandlesWithIpcHandleAndHostTypeAllocationTheninvalidArgumentIsReturned) { - size_t size = 10; - size_t alignment = 1u; - void *ptr = nullptr; +struct IpcMemoryImplicitScalingTest : public ::testing::Test { + void SetUp() override { + DebugManagerStateRestore restorer; + debugManager.flags.EnableImplicitScaling.set(1); + debugManager.flags.EnableWalkerPartition.set(1); + debugManager.flags.CreateMultipleSubDevices.set(2u); - ze_host_mem_alloc_desc_t hostDesc = {}; - ze_result_t result = context->allocHostMem(&hostDesc, - size, alignment, &ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_NE(nullptr, ptr); + std::vector> devices; + NEO::ExecutionEnvironment *executionEnvironment = new NEO::ExecutionEnvironment(); + executionEnvironment->prepareRootDeviceEnvironments(1u); + for (auto i = 0u; i < executionEnvironment->rootDeviceEnvironments.size(); i++) { + executionEnvironment->rootDeviceEnvironments[i]->setHwInfoAndInitHelpers(NEO::defaultHwInfo.get()); + executionEnvironment->rootDeviceEnvironments[i]->initGmm(); + } - uint32_t numIpcHandles = 0; - result = context->getIpcMemHandles(ptr, &numIpcHandles, nullptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_EQ(numIpcHandles, 2u); + deviceFactory = std::make_unique(1u, 2u, *executionEnvironment); - std::vector ipcHandles(numIpcHandles); - result = context->getIpcMemHandles(ptr, &numIpcHandles, ipcHandles.data()); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); + for (auto i = 0u; i < executionEnvironment->rootDeviceEnvironments.size(); i++) { + devices.push_back(std::unique_ptr(deviceFactory->rootDevices[i])); + } + driverHandle = std::make_unique(); + driverHandle->initialize(std::move(devices)); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); + device = driverHandle->devices[0]; + context = std::make_unique(driverHandle.get()); + EXPECT_NE(context, nullptr); + context->getDevices().insert(std::make_pair(device->getRootDeviceIndex(), device->toHandle())); - ze_ipc_memory_flags_t flags = {}; - void *ipcPtr; - result = context->openIpcMemHandles(device->toHandle(), numIpcHandles, ipcHandles.data(), flags, &ipcPtr); - EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, result); + auto neoDevice = device->getNEODevice(); + prevMemoryManager = driverHandle->getMemoryManager(); + currMemoryManager = new MemoryManagerIpcImplicitScalingMock(*executionEnvironment); + driverHandle->setMemoryManager(currMemoryManager); - result = context->freeMem(ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); -} + prevSvmAllocsManager = driverHandle->svmAllocsManager; + currSvmAllocsManager = new NEO::SVMAllocsManager(currMemoryManager, false); + driverHandle->svmAllocsManager = currSvmAllocsManager; -TEST_F(MemoryExportImportImplicitScalingTest, - whenCallingOpenIpcHandlesWithIpcHandleThenDeviceAllocationIsReturned) { - size_t size = 10; - size_t alignment = 1u; - void *ptr = nullptr; + context->rootDeviceIndices.pushUnique(neoDevice->getRootDeviceIndex()); + context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + } - ze_device_mem_alloc_desc_t deviceDesc = {}; - ze_result_t result = context->allocDeviceMem(device->toHandle(), - &deviceDesc, - size, alignment, &ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_NE(nullptr, ptr); + void TearDown() override { + driverHandle->svmAllocsManager = prevSvmAllocsManager; + delete currSvmAllocsManager; + driverHandle->setMemoryManager(prevMemoryManager); + delete currMemoryManager; + } - uint32_t numIpcHandles = 0; - result = context->getIpcMemHandles(ptr, &numIpcHandles, nullptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_EQ(numIpcHandles, 2u); + NEO::SVMAllocsManager *prevSvmAllocsManager; + NEO::SVMAllocsManager *currSvmAllocsManager; - std::vector ipcHandles(numIpcHandles); - result = context->getIpcMemHandles(ptr, &numIpcHandles, ipcHandles.data()); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); + NEO::MemoryManager *prevMemoryManager = nullptr; + MemoryManagerIpcImplicitScalingMock *currMemoryManager = nullptr; + std::unique_ptr driverHandle; + NEO::MockDevice *neoDevice = nullptr; + L0::Device *device = nullptr; + std::unique_ptr context; + std::unique_ptr deviceFactory; +}; - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); +TEST_F(IpcMemoryImplicitScalingTest, + whenGettingAllocationProperityOfAnIpcBufferWithImplicitScalingThenTheSameSubDeviceIsNotReturned) { + uint32_t nSubDevices = 0; + L0::DeviceImp *deviceImp = static_cast(device); + EXPECT_EQ(ZE_RESULT_SUCCESS, deviceImp->getSubDevices(&nSubDevices, nullptr)); + EXPECT_EQ(2u, nSubDevices); + std::vector subDevices(nSubDevices); + EXPECT_EQ(ZE_RESULT_SUCCESS, deviceImp->getSubDevices(&nSubDevices, subDevices.data())); - ze_ipc_memory_flags_t flags = {}; - void *ipcPtr; - result = context->openIpcMemHandles(device->toHandle(), numIpcHandles, ipcHandles.data(), flags, &ipcPtr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); + for (auto subDevice : subDevices) { + constexpr size_t size = 1ul << 18; + ze_device_mem_alloc_desc_t deviceDesc{}; + void *ptr = nullptr; - result = context->closeIpcMemHandle(ipcPtr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(ZE_RESULT_SUCCESS, context->allocDeviceMem(subDevice, &deviceDesc, size, 1ul, &ptr)); + EXPECT_NE(nullptr, ptr); - result = context->freeMem(ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); + uint32_t numIpcHandles = 0; + EXPECT_EQ(ZE_RESULT_SUCCESS, context->getIpcMemHandles(ptr, &numIpcHandles, nullptr)); + EXPECT_EQ(numIpcHandles, 2u); + + std::vector ipcHandles(numIpcHandles); + EXPECT_EQ(ZE_RESULT_SUCCESS, context->getIpcMemHandles(ptr, &numIpcHandles, ipcHandles.data())); + + void *ipcPtr = nullptr; + EXPECT_EQ(ZE_RESULT_SUCCESS, context->openIpcMemHandles(subDevice, numIpcHandles, ipcHandles.data(), 0, &ipcPtr)); + EXPECT_NE(nullptr, ipcPtr); + + ze_device_handle_t registeredDevice = nullptr; + ze_memory_allocation_properties_t allocProp{}; + EXPECT_EQ(ZE_RESULT_SUCCESS, context->getMemAllocProperties(ipcPtr, &allocProp, ®isteredDevice)); + EXPECT_EQ(ZE_MEMORY_TYPE_DEVICE, allocProp.type); + EXPECT_NE(subDevice, registeredDevice); + EXPECT_EQ(device->toHandle(), registeredDevice); + + EXPECT_EQ(ZE_RESULT_SUCCESS, context->closeIpcMemHandle(ipcPtr)); + + for (auto &ipcHandle : ipcHandles) { + EXPECT_EQ(ZE_RESULT_SUCCESS, context->putIpcMemHandle(ipcHandle)); + } + + EXPECT_EQ(ZE_RESULT_SUCCESS, context->freeMem(ptr)); + } } TEST_F(MemoryExportImportImplicitScalingTest, @@ -225,85 +260,6 @@ TEST_F(MemoryExportImportImplicitScalingTest, EXPECT_EQ(ZE_RESULT_SUCCESS, result); } -TEST_F(MemoryExportImportImplicitScalingTest, - whenCallingOpenIpcHandleWithIpcHandleThenAllocationCountIsIncremented) { - size_t size = 10; - size_t alignment = 1u; - void *ptr = nullptr; - - ze_device_mem_alloc_desc_t deviceDesc = {}; - ze_result_t result = context->allocDeviceMem(device->toHandle(), - &deviceDesc, - size, alignment, &ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_NE(nullptr, ptr); - - uint32_t numIpcHandles = 0; - result = context->getIpcMemHandles(ptr, &numIpcHandles, nullptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - auto usmManager = context->getDriverHandle()->getSvmAllocsManager(); - auto currentAllocationCount = usmManager->allocationsCounter.load(); - - std::vector ipcHandles(numIpcHandles); - result = context->getIpcMemHandles(ptr, &numIpcHandles, ipcHandles.data()); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - ze_ipc_memory_flags_t flags = {}; - void *ipcPtr; - - result = context->openIpcMemHandle(device->toHandle(), ipcHandles[0], flags, &ipcPtr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - - auto newAllocationCount = usmManager->allocationsCounter.load(); - EXPECT_GT(newAllocationCount, currentAllocationCount); - EXPECT_EQ(usmManager->getSVMAlloc(ipcPtr)->getAllocId(), newAllocationCount); - - result = context->closeIpcMemHandle(ipcPtr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - - result = context->freeMem(ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); -} - -TEST_F(MemoryExportImportImplicitScalingTest, - whenCallingOpenIpcHandleWithIpcHandleAndSharedMemoryTypeThenInvalidArgumentIsReturned) { - DebugManagerStateRestore restorer; - debugManager.flags.EnableImplicitScaling.set(0u); - - size_t size = 10; - size_t alignment = 1u; - void *ptr = nullptr; - - ze_device_mem_alloc_desc_t deviceDesc = {}; - ze_result_t result = context->allocDeviceMem(device->toHandle(), - &deviceDesc, - size, alignment, &ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_NE(nullptr, ptr); - - ze_ipc_mem_handle_t ipcHandle; - result = context->getIpcMemHandle(ptr, &ipcHandle); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - ze_ipc_memory_flags_t flags = {}; - void *ipcPtr; - - IpcMemoryData &ipcData = *reinterpret_cast(ipcHandle.data); - ipcData.type = static_cast(ZE_MEMORY_TYPE_SHARED); - - result = context->openIpcMemHandle(device->toHandle(), ipcHandle, flags, &ipcPtr); - EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, result); - - result = context->freeMem(ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); -} - TEST_F(MemoryExportImportImplicitScalingTest, whenCallingOpenIpcHandleWithIpcHandleAndHostMemoryTypeThenInvalidArgumentIsReturned) { DebugManagerStateRestore restorer; @@ -338,144 +294,6 @@ TEST_F(MemoryExportImportImplicitScalingTest, EXPECT_EQ(ZE_RESULT_SUCCESS, result); } -TEST_F(MemoryExportImportImplicitScalingTest, - whenCallingImportFdHandlesWithAllocationPointerThenAllocationIsReturned) { - size_t size = 10; - size_t alignment = 1u; - void *ptr = nullptr; - - ze_device_mem_alloc_desc_t deviceDesc = {}; - ze_result_t result = context->allocDeviceMem(device->toHandle(), - &deviceDesc, - size, alignment, &ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_NE(nullptr, ptr); - - uint32_t numIpcHandles = 0; - result = context->getIpcMemHandles(ptr, &numIpcHandles, nullptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_EQ(numIpcHandles, 2u); - - std::vector ipcHandles(numIpcHandles); - result = context->getIpcMemHandles(ptr, &numIpcHandles, ipcHandles.data()); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - std::vector handles; - for (uint32_t i = 0; i < numIpcHandles; i++) { - uint64_t handle = 0; - memcpy_s(&handle, - sizeof(handle), - reinterpret_cast(ipcHandles[i].data), - sizeof(handle)); - handles.push_back(static_cast(handle)); - } - - ze_ipc_memory_flags_t flags = {}; - void *ipcPtr; - NEO::GraphicsAllocation *ipcAlloc = nullptr; - DriverHandleImp *driverHandleImp = static_cast(context->getDriverHandle()); - NEO::SvmAllocationData allocDataInternal(device->getNEODevice()->getRootDeviceIndex()); - ipcPtr = driverHandleImp->importFdHandles(device->getNEODevice(), flags, handles, nullptr, &ipcAlloc, allocDataInternal); - EXPECT_NE(ipcPtr, nullptr); - EXPECT_NE(ipcAlloc, nullptr); - - result = context->closeIpcMemHandle(ipcPtr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - - result = context->freeMem(ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); -} - -TEST_F(MemoryExportImportImplicitScalingTest, - whenCallingImportFdHandlesWithUncachedFlagAllocationPointerThenAllocationIsReturned) { - size_t size = 10; - size_t alignment = 1u; - void *ptr = nullptr; - - ze_device_mem_alloc_desc_t deviceDesc = {}; - ze_result_t result = context->allocDeviceMem(device->toHandle(), - &deviceDesc, - size, alignment, &ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_NE(nullptr, ptr); - - uint32_t numIpcHandles = 0; - result = context->getIpcMemHandles(ptr, &numIpcHandles, nullptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_EQ(numIpcHandles, 2u); - - std::vector ipcHandles(numIpcHandles); - result = context->getIpcMemHandles(ptr, &numIpcHandles, ipcHandles.data()); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - std::vector handles; - for (uint32_t i = 0; i < numIpcHandles; i++) { - uint64_t handle = 0; - memcpy_s(&handle, - sizeof(handle), - reinterpret_cast(ipcHandles[i].data), - sizeof(handle)); - handles.push_back(static_cast(handle)); - } - - ze_ipc_memory_flags_t flags = {ZE_IPC_MEMORY_FLAG_BIAS_UNCACHED}; - void *ipcPtr; - NEO::GraphicsAllocation *ipcAlloc = nullptr; - DriverHandleImp *driverHandleImp = static_cast(context->getDriverHandle()); - NEO::SvmAllocationData allocDataInternal(device->getNEODevice()->getRootDeviceIndex()); - ipcPtr = driverHandleImp->importFdHandles(device->getNEODevice(), flags, handles, nullptr, &ipcAlloc, allocDataInternal); - EXPECT_NE(ipcPtr, nullptr); - EXPECT_NE(ipcAlloc, nullptr); - - result = context->closeIpcMemHandle(ipcPtr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - - result = context->freeMem(ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); -} - -TEST_F(MemoryExportImportImplicitScalingTest, - whenCallingGetIpcMemHandlesAndImportFailsThenInvalidArgumentFails) { - currMemoryManager->failOnCreateGraphicsAllocationFromSharedHandle = true; - - size_t size = 10; - size_t alignment = 1u; - void *ptr = nullptr; - - ze_device_mem_alloc_desc_t deviceDesc = {}; - ze_result_t result = context->allocDeviceMem(device->toHandle(), - &deviceDesc, - size, alignment, &ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_NE(nullptr, ptr); - - uint32_t numIpcHandles = 0; - result = context->getIpcMemHandles(ptr, &numIpcHandles, nullptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_EQ(numIpcHandles, 2u); - - std::vector ipcHandles(numIpcHandles); - result = context->getIpcMemHandles(ptr, &numIpcHandles, ipcHandles.data()); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); - - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface()); - neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique()); - - ze_ipc_memory_flags_t flags = {}; - void *ipcPtr; - result = context->openIpcMemHandles(device->toHandle(), numIpcHandles, ipcHandles.data(), flags, &ipcPtr); - EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, result); - - result = context->freeMem(ptr); - EXPECT_EQ(ZE_RESULT_SUCCESS, result); -} - TEST_F(MemoryExportImportImplicitScalingTest, whenCallingGetImportFdHandleAndAllocationFailsThenNullptrIsReturned) { currMemoryManager->failOnCreateGraphicsAllocationFromSharedHandle = true;