Skip to content

Commit

Permalink
Merge pull request oneapi-src#1882 from przemektmalon/przemek/interop…
Browse files Browse the repository at this point in the history
…-map-memory

[Bindless][Exp] Add interop memory mapping to USM.
  • Loading branch information
omarahmed1111 authored Jul 31, 2024
2 parents 99489ad + c86beb6 commit c5d2175
Show file tree
Hide file tree
Showing 25 changed files with 520 additions and 0 deletions.
44 changes: 44 additions & 0 deletions include/ur_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ typedef enum ur_function_t {
UR_FUNCTION_ENQUEUE_NATIVE_COMMAND_EXP = 228, ///< Enumerator for ::urEnqueueNativeCommandExp
UR_FUNCTION_LOADER_CONFIG_SET_MOCKING_ENABLED = 229, ///< Enumerator for ::urLoaderConfigSetMockingEnabled
UR_FUNCTION_BINDLESS_IMAGES_RELEASE_EXTERNAL_MEMORY_EXP = 230, ///< Enumerator for ::urBindlessImagesReleaseExternalMemoryExp
UR_FUNCTION_BINDLESS_IMAGES_MAP_EXTERNAL_LINEAR_MEMORY_EXP = 231, ///< Enumerator for ::urBindlessImagesMapExternalLinearMemoryExp
/// @cond
UR_FUNCTION_FORCE_UINT32 = 0x7fffffff
/// @endcond
Expand Down Expand Up @@ -7962,6 +7963,36 @@ urBindlessImagesMapExternalArrayExp(
ur_exp_image_mem_native_handle_t *phImageMem ///< [out] image memory handle to the externally allocated memory
);

///////////////////////////////////////////////////////////////////////////////
/// @brief Map an external memory handle to a device memory region described by
/// void*
///
/// @returns
/// - ::UR_RESULT_SUCCESS
/// - ::UR_RESULT_ERROR_UNINITIALIZED
/// - ::UR_RESULT_ERROR_DEVICE_LOST
/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
/// + `NULL == hContext`
/// + `NULL == hDevice`
/// + `NULL == hExternalMem`
/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER
/// + `NULL == ppRetMem`
/// - ::UR_RESULT_ERROR_INVALID_CONTEXT
/// - ::UR_RESULT_ERROR_INVALID_VALUE
/// - ::UR_RESULT_ERROR_INVALID_IMAGE_SIZE
/// - ::UR_RESULT_ERROR_INVALID_OPERATION
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
UR_APIEXPORT ur_result_t UR_APICALL
urBindlessImagesMapExternalLinearMemoryExp(
ur_context_handle_t hContext, ///< [in] handle of the context object
ur_device_handle_t hDevice, ///< [in] handle of the device object
uint64_t offset, ///< [in] offset into memory region to map
uint64_t size, ///< [in] size of memory region to map
ur_exp_external_mem_handle_t hExternalMem, ///< [in] external memory handle to the external memory
void **ppRetMem ///< [out] pointer of the externally allocated memory
);

///////////////////////////////////////////////////////////////////////////////
/// @brief Release external memory
///
Expand Down Expand Up @@ -11231,6 +11262,19 @@ typedef struct ur_bindless_images_map_external_array_exp_params_t {
ur_exp_image_mem_native_handle_t **pphImageMem;
} ur_bindless_images_map_external_array_exp_params_t;

///////////////////////////////////////////////////////////////////////////////
/// @brief Function parameters for urBindlessImagesMapExternalLinearMemoryExp
/// @details Each entry is a pointer to the parameter passed to the function;
/// allowing the callback the ability to modify the parameter's value
typedef struct ur_bindless_images_map_external_linear_memory_exp_params_t {
ur_context_handle_t *phContext;
ur_device_handle_t *phDevice;
uint64_t *poffset;
uint64_t *psize;
ur_exp_external_mem_handle_t *phExternalMem;
void ***pppRetMem;
} ur_bindless_images_map_external_linear_memory_exp_params_t;

///////////////////////////////////////////////////////////////////////////////
/// @brief Function parameters for urBindlessImagesReleaseExternalMemoryExp
/// @details Each entry is a pointer to the parameter passed to the function;
Expand Down
11 changes: 11 additions & 0 deletions include/ur_ddi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,16 @@ typedef ur_result_t(UR_APICALL *ur_pfnBindlessImagesMapExternalArrayExp_t)(
ur_exp_external_mem_handle_t,
ur_exp_image_mem_native_handle_t *);

///////////////////////////////////////////////////////////////////////////////
/// @brief Function-pointer for urBindlessImagesMapExternalLinearMemoryExp
typedef ur_result_t(UR_APICALL *ur_pfnBindlessImagesMapExternalLinearMemoryExp_t)(
ur_context_handle_t,
ur_device_handle_t,
uint64_t,
uint64_t,
ur_exp_external_mem_handle_t,
void **);

///////////////////////////////////////////////////////////////////////////////
/// @brief Function-pointer for urBindlessImagesReleaseExternalMemoryExp
typedef ur_result_t(UR_APICALL *ur_pfnBindlessImagesReleaseExternalMemoryExp_t)(
Expand Down Expand Up @@ -1699,6 +1709,7 @@ typedef struct ur_bindless_images_exp_dditable_t {
ur_pfnBindlessImagesMipmapFreeExp_t pfnMipmapFreeExp;
ur_pfnBindlessImagesImportExternalMemoryExp_t pfnImportExternalMemoryExp;
ur_pfnBindlessImagesMapExternalArrayExp_t pfnMapExternalArrayExp;
ur_pfnBindlessImagesMapExternalLinearMemoryExp_t pfnMapExternalLinearMemoryExp;
ur_pfnBindlessImagesReleaseExternalMemoryExp_t pfnReleaseExternalMemoryExp;
ur_pfnBindlessImagesImportExternalSemaphoreExp_t pfnImportExternalSemaphoreExp;
ur_pfnBindlessImagesReleaseExternalSemaphoreExp_t pfnReleaseExternalSemaphoreExp;
Expand Down
8 changes: 8 additions & 0 deletions include/ur_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urPrintBindlessImagesImportExternalMemoryExp
/// - `buff_size < out_size`
UR_APIEXPORT ur_result_t UR_APICALL urPrintBindlessImagesMapExternalArrayExpParams(const struct ur_bindless_images_map_external_array_exp_params_t *params, char *buffer, const size_t buff_size, size_t *out_size);

///////////////////////////////////////////////////////////////////////////////
/// @brief Print ur_bindless_images_map_external_linear_memory_exp_params_t struct
/// @returns
/// - ::UR_RESULT_SUCCESS
/// - ::UR_RESULT_ERROR_INVALID_SIZE
/// - `buff_size < out_size`
UR_APIEXPORT ur_result_t UR_APICALL urPrintBindlessImagesMapExternalLinearMemoryExpParams(const struct ur_bindless_images_map_external_linear_memory_exp_params_t *params, char *buffer, const size_t buff_size, size_t *out_size);

///////////////////////////////////////////////////////////////////////////////
/// @brief Print ur_bindless_images_release_external_memory_exp_params_t struct
/// @returns
Expand Down
48 changes: 48 additions & 0 deletions include/ur_print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,9 @@ inline std::ostream &operator<<(std::ostream &os, enum ur_function_t value) {
case UR_FUNCTION_BINDLESS_IMAGES_RELEASE_EXTERNAL_MEMORY_EXP:
os << "UR_FUNCTION_BINDLESS_IMAGES_RELEASE_EXTERNAL_MEMORY_EXP";
break;
case UR_FUNCTION_BINDLESS_IMAGES_MAP_EXTERNAL_LINEAR_MEMORY_EXP:
os << "UR_FUNCTION_BINDLESS_IMAGES_MAP_EXTERNAL_LINEAR_MEMORY_EXP";
break;
default:
os << "unknown enumerator";
break;
Expand Down Expand Up @@ -15190,6 +15193,48 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
return os;
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Print operator for the ur_bindless_images_map_external_linear_memory_exp_params_t type
/// @returns
/// std::ostream &
inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct ur_bindless_images_map_external_linear_memory_exp_params_t *params) {

os << ".hContext = ";

ur::details::printPtr(os,
*(params->phContext));

os << ", ";
os << ".hDevice = ";

ur::details::printPtr(os,
*(params->phDevice));

os << ", ";
os << ".offset = ";

os << *(params->poffset);

os << ", ";
os << ".size = ";

os << *(params->psize);

os << ", ";
os << ".hExternalMem = ";

ur::details::printPtr(os,
*(params->phExternalMem));

os << ", ";
os << ".ppRetMem = ";

ur::details::printPtr(os,
*(params->pppRetMem));

return os;
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Print operator for the ur_bindless_images_release_external_memory_exp_params_t type
/// @returns
Expand Down Expand Up @@ -17804,6 +17849,9 @@ inline ur_result_t UR_APICALL printFunctionParams(std::ostream &os, ur_function_
case UR_FUNCTION_BINDLESS_IMAGES_MAP_EXTERNAL_ARRAY_EXP: {
os << (const struct ur_bindless_images_map_external_array_exp_params_t *)params;
} break;
case UR_FUNCTION_BINDLESS_IMAGES_MAP_EXTERNAL_LINEAR_MEMORY_EXP: {
os << (const struct ur_bindless_images_map_external_linear_memory_exp_params_t *)params;
} break;
case UR_FUNCTION_BINDLESS_IMAGES_RELEASE_EXTERNAL_MEMORY_EXP: {
os << (const struct ur_bindless_images_release_external_memory_exp_params_t *)params;
} break;
Expand Down
3 changes: 3 additions & 0 deletions scripts/core/EXP-BINDLESS-IMAGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Functions
* Interop
* ${x}BindlessImagesImportExternalMemoryExp
* ${x}BindlessImagesMapExternalArrayExp
* ${x}BindlessImagesMapExternalLinearMemoryExp
* ${x}BindlessImagesReleaseExternalMemoryExp
* ${x}BindlessImagesImportExternalSemaphoreExp
* ${x}BindlessImagesReleaseExternalSemaphoreExp
Expand Down Expand Up @@ -250,6 +251,8 @@ Changelog
| 17.0 || Rename interop related structs and funcs with "external" |
| || keyword over "interop". |
+----------+-------------------------------------------------------------+
| 18.0 | Added BindlessImagesMapExternalLinearMemoryExp function. |
+----------+-------------------------------------------------------------+

Contributors
--------------------------------------------------------------------------------
Expand Down
31 changes: 31 additions & 0 deletions scripts/core/exp-bindless-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,37 @@ returns:
- $X_RESULT_ERROR_OUT_OF_RESOURCES
--- #--------------------------------------------------------------------------
type: function
desc: "Map an external memory handle to a device memory region described by void*"
class: $xBindlessImages
name: MapExternalLinearMemoryExp
ordinal: "0"
params:
- type: $x_context_handle_t
name: hContext
desc: "[in] handle of the context object"
- type: $x_device_handle_t
name: hDevice
desc: "[in] handle of the device object"
- type: uint64_t
name: offset
desc: "[in] offset into memory region to map"
- type: uint64_t
name: size
desc: "[in] size of memory region to map"
- type: $x_exp_external_mem_handle_t
name: hExternalMem
desc: "[in] external memory handle to the external memory"
- type: void**
name: ppRetMem
desc: "[out] pointer of the externally allocated memory"
returns:
- $X_RESULT_ERROR_INVALID_CONTEXT
- $X_RESULT_ERROR_INVALID_VALUE
- $X_RESULT_ERROR_INVALID_IMAGE_SIZE
- $X_RESULT_ERROR_INVALID_OPERATION
- $X_RESULT_ERROR_OUT_OF_RESOURCES
--- #--------------------------------------------------------------------------
type: function
desc: "Release external memory"
class: $xBindlessImages
name: ReleaseExternalMemoryExp
Expand Down
3 changes: 3 additions & 0 deletions scripts/core/registry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,9 @@ etors:
- name: BINDLESS_IMAGES_RELEASE_EXTERNAL_MEMORY_EXP
desc: Enumerator for $xBindlessImagesReleaseExternalMemoryExp
value: '230'
- name: BINDLESS_IMAGES_MAP_EXTERNAL_LINEAR_MEMORY_EXP
desc: Enumerator for $xBindlessImagesMapExternalLinearMemoryExp
value: '231'
---
type: enum
desc: Defines structure types
Expand Down
30 changes: 30 additions & 0 deletions source/adapters/cuda/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,36 @@ UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesMapExternalArrayExp(
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL 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 **ppRetMem) {
UR_ASSERT(std::find(hContext->getDevices().begin(),
hContext->getDevices().end(),
hDevice) != hContext->getDevices().end(),
UR_RESULT_ERROR_INVALID_CONTEXT);

try {
ScopedContext Active(hDevice);

CUDA_EXTERNAL_MEMORY_BUFFER_DESC BufferDesc = {};
BufferDesc.size = size;
BufferDesc.offset = offset;
BufferDesc.flags = 0;

CUdeviceptr retMem;
UR_CHECK_ERROR(cuExternalMemoryGetMappedBuffer(
&retMem, (CUexternalMemory)hExternalMem, &BufferDesc));

*ppRetMem = (void *)retMem;

} catch (ur_result_t Err) {
return Err;
} catch (...) {
return UR_RESULT_ERROR_UNKNOWN;
}
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesReleaseExternalMemoryExp(
ur_context_handle_t hContext, ur_device_handle_t hDevice,
ur_exp_external_mem_handle_t hExternalMem) {
Expand Down
2 changes: 2 additions & 0 deletions source/adapters/cuda/ur_interface_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetBindlessImagesExpProcAddrTable(
pDdiTable->pfnImportExternalMemoryExp =
urBindlessImagesImportExternalMemoryExp;
pDdiTable->pfnMapExternalArrayExp = urBindlessImagesMapExternalArrayExp;
pDdiTable->pfnMapExternalLinearMemoryExp =
urBindlessImagesMapExternalLinearMemoryExp;
pDdiTable->pfnReleaseExternalMemoryExp =
urBindlessImagesReleaseExternalMemoryExp;
pDdiTable->pfnImportExternalSemaphoreExp =
Expand Down
9 changes: 9 additions & 0 deletions source/adapters/hip/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesMapExternalArrayExp(
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesMapExternalLinearMemoryExp(
[[maybe_unused]] ur_context_handle_t hContext,
[[maybe_unused]] ur_device_handle_t hDevice,
[[maybe_unused]] uint64_t offset, [[maybe_unused]] uint64_t size,
[[maybe_unused]] ur_exp_external_mem_handle_t hExternalMem,
[[maybe_unused]] void **phRetMem) {
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesReleaseExternalMemoryExp(
[[maybe_unused]] ur_context_handle_t hContext,
[[maybe_unused]] ur_device_handle_t hDevice,
Expand Down
14 changes: 14 additions & 0 deletions source/adapters/level_zero/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,20 @@ UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesMapExternalArrayExp(
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL 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_APIEXPORT ur_result_t UR_APICALL urBindlessImagesReleaseExternalMemoryExp(
ur_context_handle_t hContext, ur_device_handle_t hDevice,
ur_exp_external_mem_handle_t hExternalMem) {
Expand Down
2 changes: 2 additions & 0 deletions source/adapters/level_zero/ur_interface_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetBindlessImagesExpProcAddrTable(
pDdiTable->pfnImportExternalMemoryExp =
urBindlessImagesImportExternalMemoryExp;
pDdiTable->pfnMapExternalArrayExp = urBindlessImagesMapExternalArrayExp;
pDdiTable->pfnMapExternalLinearMemoryExp =
urBindlessImagesMapExternalLinearMemoryExp;
pDdiTable->pfnReleaseExternalMemoryExp =
urBindlessImagesReleaseExternalMemoryExp;
pDdiTable->pfnImportExternalSemaphoreExp =
Expand Down
55 changes: 55 additions & 0 deletions source/adapters/mock/ur_mockddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7805,6 +7805,58 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesMapExternalArrayExp(
return exceptionToResult(std::current_exception());
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urBindlessImagesMapExternalLinearMemoryExp
__urdlllocal ur_result_t UR_APICALL urBindlessImagesMapExternalLinearMemoryExp(
ur_context_handle_t hContext, ///< [in] handle of the context object
ur_device_handle_t hDevice, ///< [in] handle of the device object
uint64_t offset, ///< [in] offset into memory region to map
uint64_t size, ///< [in] size of memory region to map
ur_exp_external_mem_handle_t
hExternalMem, ///< [in] external memory handle to the external memory
void **ppRetMem ///< [out] pointer of the externally allocated memory
) try {
ur_result_t result = UR_RESULT_SUCCESS;

ur_bindless_images_map_external_linear_memory_exp_params_t params = {
&hContext, &hDevice, &offset, &size, &hExternalMem, &ppRetMem};

auto beforeCallback = reinterpret_cast<ur_mock_callback_t>(
mock::getCallbacks().get_before_callback(
"urBindlessImagesMapExternalLinearMemoryExp"));
if (beforeCallback) {
result = beforeCallback(&params);
if (result != UR_RESULT_SUCCESS) {
return result;
}
}

auto replaceCallback = reinterpret_cast<ur_mock_callback_t>(
mock::getCallbacks().get_replace_callback(
"urBindlessImagesMapExternalLinearMemoryExp"));
if (replaceCallback) {
result = replaceCallback(&params);
} else {

result = UR_RESULT_SUCCESS;
}

if (result != UR_RESULT_SUCCESS) {
return result;
}

auto afterCallback = reinterpret_cast<ur_mock_callback_t>(
mock::getCallbacks().get_after_callback(
"urBindlessImagesMapExternalLinearMemoryExp"));
if (afterCallback) {
return afterCallback(&params);
}

return result;
} catch (...) {
return exceptionToResult(std::current_exception());
}

///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urBindlessImagesReleaseExternalMemoryExp
__urdlllocal ur_result_t UR_APICALL urBindlessImagesReleaseExternalMemoryExp(
Expand Down Expand Up @@ -10272,6 +10324,9 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetBindlessImagesExpProcAddrTable(
pDdiTable->pfnMapExternalArrayExp =
driver::urBindlessImagesMapExternalArrayExp;

pDdiTable->pfnMapExternalLinearMemoryExp =
driver::urBindlessImagesMapExternalLinearMemoryExp;

pDdiTable->pfnReleaseExternalMemoryExp =
driver::urBindlessImagesReleaseExternalMemoryExp;

Expand Down
Loading

0 comments on commit c5d2175

Please sign in to comment.