Skip to content

Commit 5726072

Browse files
[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 449c008 commit 5726072

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

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

+2-2
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

+34-9
Original file line numberDiff line numberDiff line change
@@ -729,15 +729,40 @@ 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+
ze_device_mem_alloc_desc_t allocDesc = {};
742+
allocDesc.stype = ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC;
743+
allocDesc.flags = 0;
744+
allocDesc.pNext = externalMemoryData->importExtensionDesc;
745+
void *mappedMemory;
746+
747+
ze_result_t zeResult = zeMemAllocDevice(hContext->ZeContext, &allocDesc, size,
748+
1, hDevice->ZeDevice, &mappedMemory);
749+
if (zeResult != ZE_RESULT_SUCCESS) {
750+
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
751+
}
752+
753+
zeResult = zeContextMakeMemoryResident(hContext->ZeContext, hDevice->ZeDevice,
754+
mappedMemory, size);
755+
if (zeResult != ZE_RESULT_SUCCESS) {
756+
zeMemFree(hContext->ZeContext, mappedMemory);
757+
return UR_RESULT_ERROR_UNKNOWN;
758+
}
759+
*phRetMem = reinterpret_cast<void *>(
760+
reinterpret_cast<uintptr_t>(mappedMemory) + offset);
761+
762+
externalMemoryData->urMemoryHandle =
763+
reinterpret_cast<ur_mem_handle_t>(*phRetMem);
764+
765+
return UR_RESULT_SUCCESS;
741766
}
742767

743768
ur_result_t urBindlessImagesReleaseExternalMemoryExp(

0 commit comments

Comments
 (0)