Skip to content

Commit c3baef7

Browse files
committed
[Bindless][Exp] Add interop memory mapping to USM.
This patch introduces `urBindlessImagesMapExternalLinearMemoryExp` to allow mapping interop memory to USM regions.
1 parent 1b8ee0d commit c3baef7

25 files changed

+520
-0
lines changed

include/ur_api.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ typedef enum ur_function_t {
227227
UR_FUNCTION_BINDLESS_IMAGES_IMPORT_EXTERNAL_SEMAPHORE_EXP = 227, ///< Enumerator for ::urBindlessImagesImportExternalSemaphoreExp
228228
UR_FUNCTION_ENQUEUE_NATIVE_COMMAND_EXP = 228, ///< Enumerator for ::urEnqueueNativeCommandExp
229229
UR_FUNCTION_LOADER_CONFIG_SET_MOCKING_ENABLED = 229, ///< Enumerator for ::urLoaderConfigSetMockingEnabled
230+
UR_FUNCTION_BINDLESS_IMAGES_MAP_EXTERNAL_LINEAR_MEMORY_EXP = 230, ///< Enumerator for ::urBindlessImagesMapExternalLinearMemoryExp
230231
/// @cond
231232
UR_FUNCTION_FORCE_UINT32 = 0x7fffffff
232233
/// @endcond
@@ -7956,6 +7957,36 @@ urBindlessImagesMapExternalArrayExp(
79567957
ur_exp_image_mem_native_handle_t *phImageMem ///< [out] image memory handle to the externally allocated memory
79577958
);
79587959

7960+
///////////////////////////////////////////////////////////////////////////////
7961+
/// @brief Map an interop memory handle to a device memory region described by
7962+
/// void*
7963+
///
7964+
/// @returns
7965+
/// - ::UR_RESULT_SUCCESS
7966+
/// - ::UR_RESULT_ERROR_UNINITIALIZED
7967+
/// - ::UR_RESULT_ERROR_DEVICE_LOST
7968+
/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC
7969+
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
7970+
/// + `NULL == hContext`
7971+
/// + `NULL == hDevice`
7972+
/// + `NULL == hInteropMem`
7973+
/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER
7974+
/// + `NULL == ppRetMem`
7975+
/// - ::UR_RESULT_ERROR_INVALID_CONTEXT
7976+
/// - ::UR_RESULT_ERROR_INVALID_VALUE
7977+
/// - ::UR_RESULT_ERROR_INVALID_IMAGE_SIZE
7978+
/// - ::UR_RESULT_ERROR_INVALID_OPERATION
7979+
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
7980+
UR_APIEXPORT ur_result_t UR_APICALL
7981+
urBindlessImagesMapExternalLinearMemoryExp(
7982+
ur_context_handle_t hContext, ///< [in] handle of the context object
7983+
ur_device_handle_t hDevice, ///< [in] handle of the device object
7984+
uint64_t offset, ///< [in] offset into memory region to map
7985+
uint64_t size, ///< [in] size of memory region to map
7986+
ur_exp_interop_mem_handle_t hInteropMem, ///< [in] interop memory handle to the external memory
7987+
void **ppRetMem ///< [out] pointer of the externally allocated memory
7988+
);
7989+
79597990
///////////////////////////////////////////////////////////////////////////////
79607991
/// @brief Release interop memory
79617992
///
@@ -11224,6 +11255,19 @@ typedef struct ur_bindless_images_map_external_array_exp_params_t {
1122411255
ur_exp_image_mem_native_handle_t **pphImageMem;
1122511256
} ur_bindless_images_map_external_array_exp_params_t;
1122611257

11258+
///////////////////////////////////////////////////////////////////////////////
11259+
/// @brief Function parameters for urBindlessImagesMapExternalLinearMemoryExp
11260+
/// @details Each entry is a pointer to the parameter passed to the function;
11261+
/// allowing the callback the ability to modify the parameter's value
11262+
typedef struct ur_bindless_images_map_external_linear_memory_exp_params_t {
11263+
ur_context_handle_t *phContext;
11264+
ur_device_handle_t *phDevice;
11265+
uint64_t *poffset;
11266+
uint64_t *psize;
11267+
ur_exp_interop_mem_handle_t *phInteropMem;
11268+
void ***pppRetMem;
11269+
} ur_bindless_images_map_external_linear_memory_exp_params_t;
11270+
1122711271
///////////////////////////////////////////////////////////////////////////////
1122811272
/// @brief Function parameters for urBindlessImagesReleaseInteropExp
1122911273
/// @details Each entry is a pointer to the parameter passed to the function;

include/ur_ddi.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,16 @@ typedef ur_result_t(UR_APICALL *ur_pfnBindlessImagesMapExternalArrayExp_t)(
16381638
ur_exp_interop_mem_handle_t,
16391639
ur_exp_image_mem_native_handle_t *);
16401640

1641+
///////////////////////////////////////////////////////////////////////////////
1642+
/// @brief Function-pointer for urBindlessImagesMapExternalLinearMemoryExp
1643+
typedef ur_result_t(UR_APICALL *ur_pfnBindlessImagesMapExternalLinearMemoryExp_t)(
1644+
ur_context_handle_t,
1645+
ur_device_handle_t,
1646+
uint64_t,
1647+
uint64_t,
1648+
ur_exp_interop_mem_handle_t,
1649+
void **);
1650+
16411651
///////////////////////////////////////////////////////////////////////////////
16421652
/// @brief Function-pointer for urBindlessImagesReleaseInteropExp
16431653
typedef ur_result_t(UR_APICALL *ur_pfnBindlessImagesReleaseInteropExp_t)(
@@ -1698,6 +1708,7 @@ typedef struct ur_bindless_images_exp_dditable_t {
16981708
ur_pfnBindlessImagesMipmapFreeExp_t pfnMipmapFreeExp;
16991709
ur_pfnBindlessImagesImportExternalMemoryExp_t pfnImportExternalMemoryExp;
17001710
ur_pfnBindlessImagesMapExternalArrayExp_t pfnMapExternalArrayExp;
1711+
ur_pfnBindlessImagesMapExternalLinearMemoryExp_t pfnMapExternalLinearMemoryExp;
17011712
ur_pfnBindlessImagesReleaseInteropExp_t pfnReleaseInteropExp;
17021713
ur_pfnBindlessImagesImportExternalSemaphoreExp_t pfnImportExternalSemaphoreExp;
17031714
ur_pfnBindlessImagesReleaseExternalSemaphoreExp_t pfnReleaseExternalSemaphoreExp;

include/ur_print.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urPrintBindlessImagesImportExternalMemoryExp
21462146
/// - `buff_size < out_size`
21472147
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);
21482148

2149+
///////////////////////////////////////////////////////////////////////////////
2150+
/// @brief Print ur_bindless_images_map_external_linear_memory_exp_params_t struct
2151+
/// @returns
2152+
/// - ::UR_RESULT_SUCCESS
2153+
/// - ::UR_RESULT_ERROR_INVALID_SIZE
2154+
/// - `buff_size < out_size`
2155+
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);
2156+
21492157
///////////////////////////////////////////////////////////////////////////////
21502158
/// @brief Print ur_bindless_images_release_interop_exp_params_t struct
21512159
/// @returns

include/ur_print.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,9 @@ inline std::ostream &operator<<(std::ostream &os, enum ur_function_t value) {
942942
case UR_FUNCTION_LOADER_CONFIG_SET_MOCKING_ENABLED:
943943
os << "UR_FUNCTION_LOADER_CONFIG_SET_MOCKING_ENABLED";
944944
break;
945+
case UR_FUNCTION_BINDLESS_IMAGES_MAP_EXTERNAL_LINEAR_MEMORY_EXP:
946+
os << "UR_FUNCTION_BINDLESS_IMAGES_MAP_EXTERNAL_LINEAR_MEMORY_EXP";
947+
break;
945948
default:
946949
os << "unknown enumerator";
947950
break;
@@ -15169,6 +15172,48 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1516915172
return os;
1517015173
}
1517115174

15175+
///////////////////////////////////////////////////////////////////////////////
15176+
/// @brief Print operator for the ur_bindless_images_map_external_linear_memory_exp_params_t type
15177+
/// @returns
15178+
/// std::ostream &
15179+
inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct ur_bindless_images_map_external_linear_memory_exp_params_t *params) {
15180+
15181+
os << ".hContext = ";
15182+
15183+
ur::details::printPtr(os,
15184+
*(params->phContext));
15185+
15186+
os << ", ";
15187+
os << ".hDevice = ";
15188+
15189+
ur::details::printPtr(os,
15190+
*(params->phDevice));
15191+
15192+
os << ", ";
15193+
os << ".offset = ";
15194+
15195+
os << *(params->poffset);
15196+
15197+
os << ", ";
15198+
os << ".size = ";
15199+
15200+
os << *(params->psize);
15201+
15202+
os << ", ";
15203+
os << ".hInteropMem = ";
15204+
15205+
ur::details::printPtr(os,
15206+
*(params->phInteropMem));
15207+
15208+
os << ", ";
15209+
os << ".ppRetMem = ";
15210+
15211+
ur::details::printPtr(os,
15212+
*(params->pppRetMem));
15213+
15214+
return os;
15215+
}
15216+
1517215217
///////////////////////////////////////////////////////////////////////////////
1517315218
/// @brief Print operator for the ur_bindless_images_release_interop_exp_params_t type
1517415219
/// @returns
@@ -17783,6 +17828,9 @@ inline ur_result_t UR_APICALL printFunctionParams(std::ostream &os, ur_function_
1778317828
case UR_FUNCTION_BINDLESS_IMAGES_MAP_EXTERNAL_ARRAY_EXP: {
1778417829
os << (const struct ur_bindless_images_map_external_array_exp_params_t *)params;
1778517830
} break;
17831+
case UR_FUNCTION_BINDLESS_IMAGES_MAP_EXTERNAL_LINEAR_MEMORY_EXP: {
17832+
os << (const struct ur_bindless_images_map_external_linear_memory_exp_params_t *)params;
17833+
} break;
1778617834
case UR_FUNCTION_BINDLESS_IMAGES_RELEASE_INTEROP_EXP: {
1778717835
os << (const struct ur_bindless_images_release_interop_exp_params_t *)params;
1778817836
} break;

scripts/core/EXP-BINDLESS-IMAGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ Functions
180180
* Interop
181181
* ${x}BindlessImagesImportExternalMemoryExp
182182
* ${x}BindlessImagesMapExternalArrayExp
183+
* ${x}BindlessImagesMapExternalLinearMemoryExp
183184
* ${x}BindlessImagesReleaseInteropExp
184185
* ${x}BindlessImagesImportExternalSemaphoreExp
185186
* ${x}BindlessImagesReleaseExternalSemaphoreExp
@@ -242,6 +243,8 @@ Changelog
242243
| || BindlessImagesReleaseExternalSemaphoreExp |
243244
+------------------------------------------------------------------------+
244245
| 15.0 | Added structures for supporting copying. |
246+
+------------------------------------------------------------------------+
247+
| 16.0 | Added BindlessImagesMapExternalLinearMemoryExp function. |
245248
+----------+-------------------------------------------------------------+
246249

247250
Contributors

scripts/core/exp-bindless-images.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,37 @@ returns:
734734
- $X_RESULT_ERROR_OUT_OF_RESOURCES
735735
--- #--------------------------------------------------------------------------
736736
type: function
737+
desc: "Map an interop memory handle to a device memory region described by void*"
738+
class: $xBindlessImages
739+
name: MapExternalLinearMemoryExp
740+
ordinal: "0"
741+
params:
742+
- type: $x_context_handle_t
743+
name: hContext
744+
desc: "[in] handle of the context object"
745+
- type: $x_device_handle_t
746+
name: hDevice
747+
desc: "[in] handle of the device object"
748+
- type: uint64_t
749+
name: offset
750+
desc: "[in] offset into memory region to map"
751+
- type: uint64_t
752+
name: size
753+
desc: "[in] size of memory region to map"
754+
- type: $x_exp_interop_mem_handle_t
755+
name: hInteropMem
756+
desc: "[in] interop memory handle to the external memory"
757+
- type: void**
758+
name: ppRetMem
759+
desc: "[out] pointer of the externally allocated memory"
760+
returns:
761+
- $X_RESULT_ERROR_INVALID_CONTEXT
762+
- $X_RESULT_ERROR_INVALID_VALUE
763+
- $X_RESULT_ERROR_INVALID_IMAGE_SIZE
764+
- $X_RESULT_ERROR_INVALID_OPERATION
765+
- $X_RESULT_ERROR_OUT_OF_RESOURCES
766+
--- #--------------------------------------------------------------------------
767+
type: function
737768
desc: "Release interop memory"
738769
class: $xBindlessImages
739770
name: ReleaseInteropExp

scripts/core/registry.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,9 @@ etors:
595595
- name: LOADER_CONFIG_SET_MOCKING_ENABLED
596596
desc: Enumerator for $xLoaderConfigSetMockingEnabled
597597
value: '229'
598+
- name: BINDLESS_IMAGES_MAP_EXTERNAL_LINEAR_MEMORY_EXP
599+
desc: Enumerator for $xBindlessImagesMapExternalLinearMemoryExp
600+
value: '230'
598601
---
599602
type: enum
600603
desc: Defines structure types

source/adapters/cuda/image.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,36 @@ UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesMapExternalArrayExp(
11691169
return UR_RESULT_SUCCESS;
11701170
}
11711171

1172+
UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesMapExternalLinearMemoryExp(
1173+
ur_context_handle_t hContext, ur_device_handle_t hDevice, uint64_t offset,
1174+
uint64_t size, ur_exp_interop_mem_handle_t hInteropMem, void **ppRetMem) {
1175+
UR_ASSERT(std::find(hContext->getDevices().begin(),
1176+
hContext->getDevices().end(),
1177+
hDevice) != hContext->getDevices().end(),
1178+
UR_RESULT_ERROR_INVALID_CONTEXT);
1179+
1180+
try {
1181+
ScopedContext Active(hDevice);
1182+
1183+
CUDA_EXTERNAL_MEMORY_BUFFER_DESC BufferDesc = {};
1184+
BufferDesc.size = size;
1185+
BufferDesc.offset = offset;
1186+
BufferDesc.flags = 0;
1187+
1188+
CUdeviceptr retMem;
1189+
UR_CHECK_ERROR(cuExternalMemoryGetMappedBuffer(
1190+
&retMem, (CUexternalMemory)hInteropMem, &BufferDesc));
1191+
1192+
*ppRetMem = (void *)retMem;
1193+
1194+
} catch (ur_result_t Err) {
1195+
return Err;
1196+
} catch (...) {
1197+
return UR_RESULT_ERROR_UNKNOWN;
1198+
}
1199+
return UR_RESULT_SUCCESS;
1200+
}
1201+
11721202
UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesReleaseInteropExp(
11731203
ur_context_handle_t hContext, ur_device_handle_t hDevice,
11741204
ur_exp_interop_mem_handle_t hInteropMem) {

source/adapters/cuda/ur_interface_loader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetBindlessImagesExpProcAddrTable(
340340
pDdiTable->pfnImportExternalMemoryExp =
341341
urBindlessImagesImportExternalMemoryExp;
342342
pDdiTable->pfnMapExternalArrayExp = urBindlessImagesMapExternalArrayExp;
343+
pDdiTable->pfnMapExternalLinearMemoryExp =
344+
urBindlessImagesMapExternalLinearMemoryExp;
343345
pDdiTable->pfnReleaseInteropExp = urBindlessImagesReleaseInteropExp;
344346
pDdiTable->pfnImportExternalSemaphoreExp =
345347
urBindlessImagesImportExternalSemaphoreExp;

source/adapters/hip/image.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesMapExternalArrayExp(
132132
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
133133
}
134134

135+
UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesMapExternalLinearMemoryExp(
136+
[[maybe_unused]] ur_context_handle_t hContext,
137+
[[maybe_unused]] ur_device_handle_t hDevice,
138+
[[maybe_unused]] uint64_t offset, [[maybe_unused]] uint64_t size,
139+
[[maybe_unused]] ur_exp_interop_mem_handle_t hInteropMem,
140+
[[maybe_unused]] void **phRetMem) {
141+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
142+
}
143+
135144
UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesReleaseInteropExp(
136145
[[maybe_unused]] ur_context_handle_t hContext,
137146
[[maybe_unused]] ur_device_handle_t hDevice,

0 commit comments

Comments
 (0)