Skip to content

Commit 6f5716e

Browse files
winstonzhang-intelProGTX
authored andcommitted
[UR][L0] Memory interop support given external buffer (#17458)
Enable L0 to directly import the memory allocated for NPU/iGPU and let SYCL kernel running on iGPU be able to directly access the data it will reduce the overhead of data movement between NPU/iGPU and iGPU. --------- Signed-off-by: Zhang, Winston <[email protected]> Co-authored-by: Peter Žužek <[email protected]>
1 parent c62bed5 commit 6f5716e

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

unified-runtime/source/adapters/level_zero/device.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,8 +1119,8 @@ ur_result_t urDeviceGetInfo(
11191119
return ReturnValue(false);
11201120
}
11211121
case UR_DEVICE_INFO_EXTERNAL_MEMORY_IMPORT_SUPPORT_EXP: {
1122-
// L0 does not support importing external memory.
1123-
return ReturnValue(false);
1122+
// L0 supports importing external memory.
1123+
return ReturnValue(true);
11241124
}
11251125
case UR_DEVICE_INFO_EXTERNAL_SEMAPHORE_IMPORT_SUPPORT_EXP: {
11261126
return ReturnValue(Device->Platform->ZeExternalSemaphoreExt.Supported);

unified-runtime/source/adapters/level_zero/image.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -731,15 +731,40 @@ ur_result_t urBindlessImagesMapExternalArrayExp(
731731
ur_result_t urBindlessImagesMapExternalLinearMemoryExp(
732732
ur_context_handle_t hContext, ur_device_handle_t hDevice, uint64_t offset,
733733
uint64_t size, ur_exp_external_mem_handle_t hExternalMem, void **phRetMem) {
734-
std::ignore = hContext;
735-
std::ignore = hDevice;
736-
std::ignore = size;
737-
std::ignore = offset;
738-
std::ignore = hExternalMem;
739-
std::ignore = phRetMem;
740-
logger::error("[UR][L0] {} function not implemented!",
741-
"{} function not implemented!", __FUNCTION__);
742-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
734+
UR_ASSERT(hContext && hDevice && hExternalMem,
735+
UR_RESULT_ERROR_INVALID_NULL_HANDLE);
736+
UR_ASSERT(offset && size, UR_RESULT_ERROR_INVALID_BUFFER_SIZE);
737+
738+
struct ur_ze_external_memory_data *externalMemoryData =
739+
reinterpret_cast<ur_ze_external_memory_data *>(hExternalMem);
740+
UR_ASSERT(externalMemoryData && externalMemoryData->importExtensionDesc,
741+
UR_RESULT_ERROR_INVALID_NULL_POINTER);
742+
743+
ze_device_mem_alloc_desc_t allocDesc = {};
744+
allocDesc.stype = ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC;
745+
allocDesc.flags = 0;
746+
allocDesc.pNext = externalMemoryData->importExtensionDesc;
747+
void *mappedMemory;
748+
749+
ze_result_t zeResult = zeMemAllocDevice(hContext->ZeContext, &allocDesc, size,
750+
1, hDevice->ZeDevice, &mappedMemory);
751+
if (zeResult != ZE_RESULT_SUCCESS) {
752+
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
753+
}
754+
755+
zeResult = zeContextMakeMemoryResident(hContext->ZeContext, hDevice->ZeDevice,
756+
mappedMemory, size);
757+
if (zeResult != ZE_RESULT_SUCCESS) {
758+
zeMemFree(hContext->ZeContext, mappedMemory);
759+
return UR_RESULT_ERROR_UNKNOWN;
760+
}
761+
*phRetMem = reinterpret_cast<void *>(
762+
reinterpret_cast<uintptr_t>(mappedMemory) + offset);
763+
764+
externalMemoryData->urMemoryHandle =
765+
reinterpret_cast<ur_mem_handle_t>(*phRetMem);
766+
767+
return UR_RESULT_SUCCESS;
743768
}
744769

745770
ur_result_t urBindlessImagesFreeMappedLinearMemoryExp(

0 commit comments

Comments
 (0)