Skip to content

Commit 536f165

Browse files
[UR][L0] Memory interop NPU to iGPU
Enable L0 to directly import the memory allocated for NPU and let SYCL kernel running on iGPU be able to directly access the data it will reduce the overhead of data movement between NPU and iGPU. Signed-off-by: Zhang, Winston <[email protected]>
1 parent b23d69e commit 536f165

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ ur_result_t urDeviceGetInfo(
11161116
}
11171117
case UR_DEVICE_INFO_EXTERNAL_MEMORY_IMPORT_SUPPORT_EXP: {
11181118
// L0 does not support importing external memory.
1119-
return ReturnValue(false);
1119+
return ReturnValue(true);
11201120
}
11211121
case UR_DEVICE_INFO_EXTERNAL_SEMAPHORE_IMPORT_SUPPORT_EXP: {
11221122
return ReturnValue(Device->Platform->ZeExternalSemaphoreExt.Supported);

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

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

743775
ur_result_t urBindlessImagesReleaseExternalMemoryExp(

0 commit comments

Comments
 (0)