Skip to content

[UR][L0] Memory interop support given external buffer #17458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions unified-runtime/source/adapters/level_zero/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,8 +1115,8 @@ ur_result_t urDeviceGetInfo(
return ReturnValue(false);
}
case UR_DEVICE_INFO_EXTERNAL_MEMORY_IMPORT_SUPPORT_EXP: {
// L0 does not support importing external memory.
return ReturnValue(false);
// L0 supports importing external memory.
return ReturnValue(true);
}
case UR_DEVICE_INFO_EXTERNAL_SEMAPHORE_IMPORT_SUPPORT_EXP: {
return ReturnValue(Device->Platform->ZeExternalSemaphoreExt.Supported);
Expand Down
43 changes: 34 additions & 9 deletions unified-runtime/source/adapters/level_zero/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,15 +729,40 @@ ur_result_t urBindlessImagesMapExternalArrayExp(
ur_result_t urBindlessImagesMapExternalLinearMemoryExp(
ur_context_handle_t hContext, ur_device_handle_t hDevice, uint64_t offset,
uint64_t size, ur_exp_external_mem_handle_t hExternalMem, void **phRetMem) {
std::ignore = hContext;
std::ignore = hDevice;
std::ignore = size;
std::ignore = offset;
std::ignore = hExternalMem;
std::ignore = phRetMem;
logger::error("[UR][L0] {} function not implemented!",
"{} function not implemented!", __FUNCTION__);
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
UR_ASSERT(hContext && hDevice && hExternalMem,
UR_RESULT_ERROR_INVALID_NULL_HANDLE);
UR_ASSERT(offset && size, UR_RESULT_ERROR_INVALID_BUFFER_SIZE);

struct ur_ze_external_memory_data *externalMemoryData =
reinterpret_cast<ur_ze_external_memory_data *>(hExternalMem);
UR_ASSERT(externalMemoryData && externalMemoryData->importExtensionDesc,
UR_RESULT_ERROR_INVALID_NULL_POINTER);

ze_device_mem_alloc_desc_t allocDesc = {};
allocDesc.stype = ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC;
allocDesc.flags = 0;
allocDesc.pNext = externalMemoryData->importExtensionDesc;
void *mappedMemory;

ze_result_t zeResult = zeMemAllocDevice(hContext->ZeContext, &allocDesc, size,
1, hDevice->ZeDevice, &mappedMemory);
if (zeResult != ZE_RESULT_SUCCESS) {
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
}

zeResult = zeContextMakeMemoryResident(hContext->ZeContext, hDevice->ZeDevice,
mappedMemory, size);
if (zeResult != ZE_RESULT_SUCCESS) {
zeMemFree(hContext->ZeContext, mappedMemory);
return UR_RESULT_ERROR_UNKNOWN;
}
*phRetMem = reinterpret_cast<void *>(
reinterpret_cast<uintptr_t>(mappedMemory) + offset);

externalMemoryData->urMemoryHandle =
reinterpret_cast<ur_mem_handle_t>(*phRetMem);

return UR_RESULT_SUCCESS;
}

ur_result_t urBindlessImagesReleaseExternalMemoryExp(
Expand Down