refactor: Add IPC memory data

Refactor structure and add field to pass USM memory type.

To maintain backwards compatibility with current applications,
pass 0 as type for device allocations, and 1 for host
allocations.

Related-To: LOCI-3771

Signed-off-by: Jaime Arteaga <jaime.a.arteaga.molina@intel.com>
This commit is contained in:
Jaime Arteaga
2023-01-19 09:14:03 +00:00
committed by Compute-Runtime-Automation
parent 371ee24554
commit 51d767daea
6 changed files with 195 additions and 27 deletions

View File

@@ -500,10 +500,13 @@ ze_result_t ContextImp::getIpcMemHandle(const void *ptr,
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
memcpy_s(reinterpret_cast<void *>(pIpcHandle->data),
sizeof(ze_ipc_mem_handle_t),
&handle,
sizeof(handle));
IpcMemoryData &ipcData = *reinterpret_cast<IpcMemoryData *>(pIpcHandle->data);
ipcData = {};
ipcData.handle = handle;
auto type = allocData->memoryType;
if (type == HOST_UNIFIED_MEMORY) {
ipcData.type = static_cast<uint8_t>(InternalIpcMemoryType::IPC_HOST_UNIFIED_MEMORY);
}
return ZE_RESULT_SUCCESS;
}
@@ -527,16 +530,23 @@ ze_result_t ContextImp::getIpcMemHandles(const void *ptr,
return ZE_RESULT_SUCCESS;
}
auto type = allocData->memoryType;
auto ipcType = InternalIpcMemoryType::IPC_DEVICE_UNIFIED_MEMORY;
if (type == HOST_UNIFIED_MEMORY) {
ipcType = InternalIpcMemoryType::IPC_HOST_UNIFIED_MEMORY;
}
for (uint32_t i = 0; i < *numIpcHandles; i++) {
uint64_t handle = 0;
int ret = allocData->gpuAllocations.getDefaultGraphicsAllocation()->peekInternalHandle(this->driverHandle->getMemoryManager(), i, handle);
if (ret < 0) {
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
memcpy_s(reinterpret_cast<void *>(pIpcHandles[i].data),
sizeof(ze_ipc_mem_handle_t),
&handle,
sizeof(int));
IpcMemoryData &ipcData = *reinterpret_cast<IpcMemoryData *>(pIpcHandles[i].data);
ipcData = {};
ipcData.handle = handle;
ipcData.type = static_cast<uint8_t>(ipcType);
}
return ZE_RESULT_SUCCESS;
@@ -548,15 +558,23 @@ ze_result_t ContextImp::openIpcMemHandle(ze_device_handle_t hDevice,
const ze_ipc_mem_handle_t &pIpcHandle,
ze_ipc_memory_flags_t flags,
void **ptr) {
uint64_t handle = 0u;
memcpy_s(&handle,
sizeof(handle),
pIpcHandle.data,
sizeof(handle));
const IpcMemoryData &ipcData = *reinterpret_cast<const IpcMemoryData *>(pIpcHandle.data);
uint64_t handle = ipcData.handle;
uint8_t type = ipcData.type;
NEO::AllocationType allocationType = NEO::AllocationType::UNKNOWN;
if (type == static_cast<uint8_t>(InternalIpcMemoryType::IPC_DEVICE_UNIFIED_MEMORY)) {
allocationType = NEO::AllocationType::BUFFER;
} else if (type == static_cast<uint8_t>(InternalIpcMemoryType::IPC_HOST_UNIFIED_MEMORY)) {
allocationType = NEO::AllocationType::BUFFER_HOST_MEMORY;
} else {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
*ptr = getMemHandlePtr(hDevice,
handle,
NEO::AllocationType::BUFFER,
allocationType,
flags);
if (nullptr == *ptr) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
@@ -574,11 +592,13 @@ ze_result_t ContextImp::openIpcMemHandles(ze_device_handle_t hDevice,
handles.reserve(numIpcHandles);
for (uint32_t i = 0; i < numIpcHandles; i++) {
uint64_t handle = 0;
memcpy_s(&handle,
sizeof(handle),
reinterpret_cast<void *>(pIpcHandles[i].data),
sizeof(handle));
const IpcMemoryData &ipcData = *reinterpret_cast<const IpcMemoryData *>(pIpcHandles[i].data);
uint64_t handle = ipcData.handle;
if (ipcData.type != static_cast<uint8_t>(InternalIpcMemoryType::IPC_DEVICE_UNIFIED_MEMORY)) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
handles.push_back(static_cast<NEO::osHandle>(handle));
}
auto neoDevice = Device::fromHandle(hDevice)->getNEODevice()->getRootDevice();

View File

@@ -19,6 +19,14 @@ struct StructuresLookupTable;
struct DriverHandleImp;
struct Device;
#pragma pack(1)
struct IpcMemoryData {
uint64_t handle = 0;
uint8_t type = 0;
};
#pragma pack()
static_assert(sizeof(IpcMemoryData) <= ZE_MAX_IPC_HANDLE_SIZE, "IpcMemoryData is bigger than ZE_MAX_IPC_HANDLE_SIZE");
struct ContextImp : Context {
ContextImp(DriverHandle *driverHandle);
~ContextImp() override = default;

View File

@@ -377,10 +377,15 @@ struct ContextGetIpcHandleMock : public L0::ContextImp {
ze_result_t getIpcMemHandle(const void *ptr, ze_ipc_mem_handle_t *pIpcHandle) override {
uint64_t handle = driverHandle->mockFd;
memcpy_s(reinterpret_cast<void *>(pIpcHandle->data),
sizeof(ze_ipc_mem_handle_t),
&handle,
sizeof(handle));
NEO::SvmAllocationData *allocData = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
IpcMemoryData &ipcData = *reinterpret_cast<IpcMemoryData *>(pIpcHandle->data);
ipcData = {};
ipcData.handle = handle;
auto type = Context::parseUSMType(allocData->memoryType);
if (type == ZE_MEMORY_TYPE_HOST) {
ipcData.type = static_cast<uint8_t>(InternalIpcMemoryType::IPC_HOST_UNIFIED_MEMORY);
}
return ZE_RESULT_SUCCESS;
}
@@ -552,10 +557,15 @@ struct ContextIpcMock : public L0::ContextImp {
ze_result_t getIpcMemHandle(const void *ptr, ze_ipc_mem_handle_t *pIpcHandle) override {
uint64_t handle = mockFd;
memcpy_s(reinterpret_cast<void *>(pIpcHandle->data),
sizeof(ze_ipc_mem_handle_t),
&handle,
sizeof(handle));
NEO::SvmAllocationData *allocData = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
IpcMemoryData &ipcData = *reinterpret_cast<IpcMemoryData *>(pIpcHandle->data);
ipcData = {};
ipcData.handle = handle;
auto type = Context::parseUSMType(allocData->memoryType);
if (type == ZE_MEMORY_TYPE_HOST) {
ipcData.type = static_cast<uint8_t>(InternalIpcMemoryType::IPC_HOST_UNIFIED_MEMORY);
}
return ZE_RESULT_SUCCESS;
}

View File

@@ -109,6 +109,39 @@ TEST_F(MemoryExportImportImplicitScalingTest,
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<ze_ipc_mem_handle_t> 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<NEO::MockDriverModelDRM>());
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,
whenCallingOpenIpcHandlesWithIpcHandleThenDeviceAllocationIsReturned) {
size_t size = 10;
@@ -146,6 +179,78 @@ TEST_F(MemoryExportImportImplicitScalingTest,
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<NEO::MockDriverModelDRM>());
ze_ipc_memory_flags_t flags = {};
void *ipcPtr;
IpcMemoryData &ipcData = *reinterpret_cast<IpcMemoryData *>(ipcHandle.data);
ipcData.type = static_cast<uint8_t>(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;
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<NEO::MockDriverModelDRM>());
ze_ipc_memory_flags_t flags = {};
void *ipcPtr;
IpcMemoryData &ipcData = *reinterpret_cast<IpcMemoryData *>(ipcHandle.data);
ipcData.type = static_cast<uint8_t>(ZE_MEMORY_TYPE_HOST);
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,
whenCallingImportFdHandlesWithAllocationPointerThenAllocationIsReturned) {
size_t size = 10;

View File

@@ -46,6 +46,26 @@ TEST_F(MemoryIPCTests,
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
}
TEST_F(MemoryIPCTests,
whenCallingGetIpcHandleWithHostAllocationThenSuccessIsReturned) {
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);
ze_ipc_mem_handle_t ipcHandle = {};
result = context->getIpcMemHandle(ptr, &ipcHandle);
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
result = context->freeMem(ptr);
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
}
TEST_F(MemoryIPCTests,
whenCallingOpenIpcHandleWithIpcHandleThenDeviceAllocationIsReturned) {
size_t size = 10;

View File

@@ -17,6 +17,11 @@ enum InternalMemoryType : uint32_t {
SHARED_UNIFIED_MEMORY = 0b1000
};
enum class InternalIpcMemoryType : uint32_t {
IPC_DEVICE_UNIFIED_MEMORY = 0,
IPC_HOST_UNIFIED_MEMORY = 1
};
enum TransferType : uint32_t {
HOST_NON_USM_TO_HOST_USM = 0,
HOST_NON_USM_TO_DEVICE_USM = 1,