Skip to content

Commit 59d9cbc

Browse files
authored
Merge pull request #433 from jandres742/url0_loader_urContextGetNativeHandle
Expand urContextGetNativeHandle
2 parents 4630e1e + 1cfe415 commit 59d9cbc

File tree

14 files changed

+196
-19
lines changed

14 files changed

+196
-19
lines changed

include/ur.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ class ur_structure_type_v(IntEnum):
215215
SAMPLER_DESC = 13 ## ::ur_sampler_desc_t
216216
QUEUE_PROPERTIES = 14 ## ::ur_queue_properties_t
217217
QUEUE_INDEX_PROPERTIES = 15 ## ::ur_queue_properties_t
218+
CONTEXT_NATIVE_PROPERTIES = 16 ## ::ur_context_native_properties_t
218219

219220
class ur_structure_type_t(c_int):
220221
def __str__(self):
@@ -706,6 +707,18 @@ def __str__(self):
706707
return str(ur_context_info_v(self.value))
707708

708709

710+
###############################################################################
711+
## @brief Properties for for ::urContextCreateWithNativeHandle.
712+
class ur_context_native_properties_t(Structure):
713+
_fields_ = [
714+
("stype", ur_structure_type_t), ## [in] type of this structure, must be
715+
## ::UR_STRUCTURE_TYPE_CONTEXT_NATIVE_PROPERTIES
716+
("pNext", c_void_p), ## [in,out][optional] pointer to extension-specific structure
717+
("isNativeHandleOwned", c_bool) ## [in] Indicates UR owns the native handle or if it came from an interoperability
718+
## operation in the application that asked to not transfer the ownership to
719+
## the unified-runtime.
720+
]
721+
709722
###############################################################################
710723
## @brief Context's extended deleter callback function with user data.
711724
def ur_context_extended_deleter_t(user_defined_callback):
@@ -1715,9 +1728,9 @@ class ur_platform_dditable_t(Structure):
17151728
###############################################################################
17161729
## @brief Function-pointer for urContextCreateWithNativeHandle
17171730
if __use_win_types:
1718-
_urContextCreateWithNativeHandle_t = WINFUNCTYPE( ur_result_t, ur_native_handle_t, POINTER(ur_context_handle_t) )
1731+
_urContextCreateWithNativeHandle_t = WINFUNCTYPE( ur_result_t, ur_native_handle_t, c_ulong, POINTER(ur_device_handle_t), POINTER(ur_context_native_properties_t), POINTER(ur_context_handle_t) )
17191732
else:
1720-
_urContextCreateWithNativeHandle_t = CFUNCTYPE( ur_result_t, ur_native_handle_t, POINTER(ur_context_handle_t) )
1733+
_urContextCreateWithNativeHandle_t = CFUNCTYPE( ur_result_t, ur_native_handle_t, c_ulong, POINTER(ur_device_handle_t), POINTER(ur_context_native_properties_t), POINTER(ur_context_handle_t) )
17211734

17221735
###############################################################################
17231736
## @brief Function-pointer for urContextSetExtendedDeleter

include/ur_api.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ typedef enum ur_structure_type_t {
239239
UR_STRUCTURE_TYPE_SAMPLER_DESC = 13, ///< ::ur_sampler_desc_t
240240
UR_STRUCTURE_TYPE_QUEUE_PROPERTIES = 14, ///< ::ur_queue_properties_t
241241
UR_STRUCTURE_TYPE_QUEUE_INDEX_PROPERTIES = 15, ///< ::ur_queue_properties_t
242+
UR_STRUCTURE_TYPE_CONTEXT_NATIVE_PROPERTIES = 16, ///< ::ur_context_native_properties_t
242243
/// @cond
243244
UR_STRUCTURE_TYPE_FORCE_UINT32 = 0x7fffffff
244245
/// @endcond
@@ -1456,6 +1457,18 @@ urContextGetNativeHandle(
14561457
ur_native_handle_t *phNativeContext ///< [out] a pointer to the native handle of the context.
14571458
);
14581459

1460+
///////////////////////////////////////////////////////////////////////////////
1461+
/// @brief Properties for for ::urContextCreateWithNativeHandle.
1462+
typedef struct ur_context_native_properties_t {
1463+
ur_structure_type_t stype; ///< [in] type of this structure, must be
1464+
///< ::UR_STRUCTURE_TYPE_CONTEXT_NATIVE_PROPERTIES
1465+
void *pNext; ///< [in,out][optional] pointer to extension-specific structure
1466+
bool isNativeHandleOwned; ///< [in] Indicates UR owns the native handle or if it came from an interoperability
1467+
///< operation in the application that asked to not transfer the ownership to
1468+
///< the unified-runtime.
1469+
1470+
} ur_context_native_properties_t;
1471+
14591472
///////////////////////////////////////////////////////////////////////////////
14601473
/// @brief Create runtime context object from native context handle.
14611474
///
@@ -1472,11 +1485,16 @@ urContextGetNativeHandle(
14721485
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
14731486
/// + `NULL == hNativeContext`
14741487
/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER
1488+
/// + `NULL == phDevices`
1489+
/// + `NULL == pProperties`
14751490
/// + `NULL == phContext`
14761491
UR_APIEXPORT ur_result_t UR_APICALL
14771492
urContextCreateWithNativeHandle(
1478-
ur_native_handle_t hNativeContext, ///< [in] the native handle of the context.
1479-
ur_context_handle_t *phContext ///< [out] pointer to the handle of the context object created.
1493+
ur_native_handle_t hNativeContext, ///< [in] the native handle of the context.
1494+
uint32_t numDevices, ///< [in] number of devices associated with the context
1495+
const ur_device_handle_t *phDevices, ///< [in][range(0, numDevices)] list of devices associated with the context
1496+
const ur_context_native_properties_t *pProperties, ///< [in] pointer to properties struct
1497+
ur_context_handle_t *phContext ///< [out] pointer to the handle of the context object created.
14801498
);
14811499

14821500
///////////////////////////////////////////////////////////////////////////////
@@ -5732,6 +5750,9 @@ typedef void(UR_APICALL *ur_pfnContextGetNativeHandleCb_t)(
57325750
/// allowing the callback the ability to modify the parameter's value
57335751
typedef struct ur_context_create_with_native_handle_params_t {
57345752
ur_native_handle_t *phNativeContext;
5753+
uint32_t *pnumDevices;
5754+
const ur_device_handle_t **pphDevices;
5755+
const ur_context_native_properties_t **ppProperties;
57355756
ur_context_handle_t **pphContext;
57365757
} ur_context_create_with_native_handle_params_t;
57375758

include/ur_ddi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ typedef ur_result_t(UR_APICALL *ur_pfnContextGetNativeHandle_t)(
129129
/// @brief Function-pointer for urContextCreateWithNativeHandle
130130
typedef ur_result_t(UR_APICALL *ur_pfnContextCreateWithNativeHandle_t)(
131131
ur_native_handle_t,
132+
uint32_t,
133+
const ur_device_handle_t *,
134+
const ur_context_native_properties_t *,
132135
ur_context_handle_t *);
133136

134137
///////////////////////////////////////////////////////////////////////////////

scripts/core/common.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ etors:
294294
desc: $x_queue_properties_t
295295
- name: QUEUE_INDEX_PROPERTIES
296296
desc: $x_queue_properties_t
297+
- name: CONTEXT_NATIVE_PROPERTIES
298+
desc: $x_context_native_properties_t
297299
--- #--------------------------------------------------------------------------
298300
type: struct
299301
desc: "Base for all properties types"

scripts/core/context.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,19 @@ params:
183183
desc: |
184184
[out] a pointer to the native handle of the context.
185185
--- #--------------------------------------------------------------------------
186+
type: struct
187+
desc: "Properties for for $xContextCreateWithNativeHandle."
188+
class: $xContext
189+
name: $x_context_native_properties_t
190+
base: $x_base_properties_t
191+
members:
192+
- type: bool
193+
name: isNativeHandleOwned
194+
desc: |
195+
[in] Indicates UR owns the native handle or if it came from an interoperability
196+
operation in the application that asked to not transfer the ownership to
197+
the unified-runtime.
198+
--- #--------------------------------------------------------------------------
186199
type: function
187200
desc: "Create runtime context object from native context handle."
188201
class: $xContext
@@ -198,6 +211,15 @@ params:
198211
name: hNativeContext
199212
desc: |
200213
[in] the native handle of the context.
214+
- type: uint32_t
215+
name: numDevices
216+
desc: "[in] number of devices associated with the context"
217+
- type: "const $x_device_handle_t*"
218+
name: phDevices
219+
desc: "[in][range(0, numDevices)] list of devices associated with the context"
220+
- type: "const $x_context_native_properties_t*"
221+
name: pProperties
222+
desc: "[in] pointer to properties struct"
201223
- type: "$x_context_handle_t*"
202224
name: phContext
203225
desc: |

source/common/ur_params.hpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ inline std::ostream &operator<<(std::ostream &os, enum ur_context_flag_t value);
156156
inline std::ostream &operator<<(std::ostream &os,
157157
const struct ur_context_properties_t params);
158158
inline std::ostream &operator<<(std::ostream &os, enum ur_context_info_t value);
159+
inline std::ostream &
160+
operator<<(std::ostream &os,
161+
const struct ur_context_native_properties_t params);
159162
inline std::ostream &operator<<(std::ostream &os, enum ur_mem_flag_t value);
160163
inline std::ostream &operator<<(std::ostream &os, enum ur_mem_type_t value);
161164
inline std::ostream &operator<<(std::ostream &os, enum ur_mem_info_t value);
@@ -605,6 +608,10 @@ inline std::ostream &operator<<(std::ostream &os,
605608
case UR_STRUCTURE_TYPE_QUEUE_INDEX_PROPERTIES:
606609
os << "UR_STRUCTURE_TYPE_QUEUE_INDEX_PROPERTIES";
607610
break;
611+
612+
case UR_STRUCTURE_TYPE_CONTEXT_NATIVE_PROPERTIES:
613+
os << "UR_STRUCTURE_TYPE_CONTEXT_NATIVE_PROPERTIES";
614+
break;
608615
default:
609616
os << "unknown enumerator";
610617
break;
@@ -708,6 +715,12 @@ inline void serializeStruct(std::ostream &os, const void *ptr) {
708715
(const ur_queue_properties_t *)ptr;
709716
ur_params::serializePtr(os, pstruct);
710717
} break;
718+
719+
case UR_STRUCTURE_TYPE_CONTEXT_NATIVE_PROPERTIES: {
720+
const ur_context_native_properties_t *pstruct =
721+
(const ur_context_native_properties_t *)ptr;
722+
ur_params::serializePtr(os, pstruct);
723+
} break;
711724
default:
712725
os << "unknown enumerator";
713726
break;
@@ -3747,6 +3760,28 @@ inline void serializeTaggedTyped_ur_context_info_t(std::ostream &os,
37473760
}
37483761
}
37493762
} // namespace ur_params
3763+
inline std::ostream &
3764+
operator<<(std::ostream &os,
3765+
const struct ur_context_native_properties_t params) {
3766+
os << "(struct ur_context_native_properties_t){";
3767+
3768+
os << ".stype = ";
3769+
3770+
os << (params.stype);
3771+
3772+
os << ", ";
3773+
os << ".pNext = ";
3774+
3775+
ur_params::serializeStruct(os, (params.pNext));
3776+
3777+
os << ", ";
3778+
os << ".isNativeHandleOwned = ";
3779+
3780+
os << (params.isNativeHandleOwned);
3781+
3782+
os << "}";
3783+
return os;
3784+
}
37503785
inline std::ostream &operator<<(std::ostream &os, enum ur_mem_flag_t value) {
37513786
switch (value) {
37523787

@@ -7515,6 +7550,28 @@ operator<<(std::ostream &os,
75157550

75167551
ur_params::serializePtr(os, *(params->phNativeContext));
75177552

7553+
os << ", ";
7554+
os << ".numDevices = ";
7555+
7556+
os << *(params->pnumDevices);
7557+
7558+
os << ", ";
7559+
os << ".phDevices = [";
7560+
for (size_t i = 0;
7561+
*(params->pphDevices) != NULL && i < *params->pnumDevices; ++i) {
7562+
if (i != 0) {
7563+
os << ", ";
7564+
}
7565+
7566+
ur_params::serializePtr(os, (*(params->pphDevices))[i]);
7567+
}
7568+
os << "]";
7569+
7570+
os << ", ";
7571+
os << ".pProperties = ";
7572+
7573+
ur_params::serializePtr(os, *(params->ppProperties));
7574+
75187575
os << ", ";
75197576
os << ".phContext = ";
75207577

source/drivers/null/ur_nullddi.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,12 @@ __urdlllocal ur_result_t UR_APICALL urContextGetNativeHandle(
570570
/// @brief Intercept function for urContextCreateWithNativeHandle
571571
__urdlllocal ur_result_t UR_APICALL urContextCreateWithNativeHandle(
572572
ur_native_handle_t
573-
hNativeContext, ///< [in] the native handle of the context.
573+
hNativeContext, ///< [in] the native handle of the context.
574+
uint32_t numDevices, ///< [in] number of devices associated with the context
575+
const ur_device_handle_t *
576+
phDevices, ///< [in][range(0, numDevices)] list of devices associated with the context
577+
const ur_context_native_properties_t
578+
*pProperties, ///< [in] pointer to properties struct
574579
ur_context_handle_t *
575580
phContext ///< [out] pointer to the handle of the context object created.
576581
) {
@@ -580,7 +585,8 @@ __urdlllocal ur_result_t UR_APICALL urContextCreateWithNativeHandle(
580585
auto pfnCreateWithNativeHandle =
581586
d_context.urDdiTable.Context.pfnCreateWithNativeHandle;
582587
if (nullptr != pfnCreateWithNativeHandle) {
583-
result = pfnCreateWithNativeHandle(hNativeContext, phContext);
588+
result = pfnCreateWithNativeHandle(hNativeContext, numDevices,
589+
phDevices, pProperties, phContext);
584590
} else {
585591
// generic implementation
586592
*phContext = reinterpret_cast<ur_context_handle_t>(d_context.get());

source/loader/layers/tracing/ur_trcddi.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,12 @@ __urdlllocal ur_result_t UR_APICALL urContextGetNativeHandle(
694694
/// @brief Intercept function for urContextCreateWithNativeHandle
695695
__urdlllocal ur_result_t UR_APICALL urContextCreateWithNativeHandle(
696696
ur_native_handle_t
697-
hNativeContext, ///< [in] the native handle of the context.
697+
hNativeContext, ///< [in] the native handle of the context.
698+
uint32_t numDevices, ///< [in] number of devices associated with the context
699+
const ur_device_handle_t *
700+
phDevices, ///< [in][range(0, numDevices)] list of devices associated with the context
701+
const ur_context_native_properties_t
702+
*pProperties, ///< [in] pointer to properties struct
698703
ur_context_handle_t *
699704
phContext ///< [out] pointer to the handle of the context object created.
700705
) {
@@ -705,13 +710,14 @@ __urdlllocal ur_result_t UR_APICALL urContextCreateWithNativeHandle(
705710
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
706711
}
707712

708-
ur_context_create_with_native_handle_params_t params = {&hNativeContext,
709-
&phContext};
713+
ur_context_create_with_native_handle_params_t params = {
714+
&hNativeContext, &numDevices, &phDevices, &pProperties, &phContext};
710715
uint64_t instance =
711716
context.notify_begin(UR_FUNCTION_CONTEXT_CREATE_WITH_NATIVE_HANDLE,
712717
"urContextCreateWithNativeHandle", &params);
713718

714-
ur_result_t result = pfnCreateWithNativeHandle(hNativeContext, phContext);
719+
ur_result_t result = pfnCreateWithNativeHandle(
720+
hNativeContext, numDevices, phDevices, pProperties, phContext);
715721

716722
context.notify_end(UR_FUNCTION_CONTEXT_CREATE_WITH_NATIVE_HANDLE,
717723
"urContextCreateWithNativeHandle", &params, &result,

source/loader/layers/validation/ur_valddi.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,12 @@ __urdlllocal ur_result_t UR_APICALL urContextGetNativeHandle(
747747
/// @brief Intercept function for urContextCreateWithNativeHandle
748748
__urdlllocal ur_result_t UR_APICALL urContextCreateWithNativeHandle(
749749
ur_native_handle_t
750-
hNativeContext, ///< [in] the native handle of the context.
750+
hNativeContext, ///< [in] the native handle of the context.
751+
uint32_t numDevices, ///< [in] number of devices associated with the context
752+
const ur_device_handle_t *
753+
phDevices, ///< [in][range(0, numDevices)] list of devices associated with the context
754+
const ur_context_native_properties_t
755+
*pProperties, ///< [in] pointer to properties struct
751756
ur_context_handle_t *
752757
phContext ///< [out] pointer to the handle of the context object created.
753758
) {
@@ -763,12 +768,21 @@ __urdlllocal ur_result_t UR_APICALL urContextCreateWithNativeHandle(
763768
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
764769
}
765770

771+
if (NULL == phDevices) {
772+
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
773+
}
774+
775+
if (NULL == pProperties) {
776+
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
777+
}
778+
766779
if (NULL == phContext) {
767780
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
768781
}
769782
}
770783

771-
ur_result_t result = pfnCreateWithNativeHandle(hNativeContext, phContext);
784+
ur_result_t result = pfnCreateWithNativeHandle(
785+
hNativeContext, numDevices, phDevices, pProperties, phContext);
772786

773787
if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) {
774788
refCountContext.createRefCount(*phContext);

source/loader/ur_ldrddi.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,12 @@ __urdlllocal ur_result_t UR_APICALL urContextGetNativeHandle(
821821
/// @brief Intercept function for urContextCreateWithNativeHandle
822822
__urdlllocal ur_result_t UR_APICALL urContextCreateWithNativeHandle(
823823
ur_native_handle_t
824-
hNativeContext, ///< [in] the native handle of the context.
824+
hNativeContext, ///< [in] the native handle of the context.
825+
uint32_t numDevices, ///< [in] number of devices associated with the context
826+
const ur_device_handle_t *
827+
phDevices, ///< [in][range(0, numDevices)] list of devices associated with the context
828+
const ur_context_native_properties_t
829+
*pProperties, ///< [in] pointer to properties struct
825830
ur_context_handle_t *
826831
phContext ///< [out] pointer to the handle of the context object created.
827832
) {
@@ -840,8 +845,17 @@ __urdlllocal ur_result_t UR_APICALL urContextCreateWithNativeHandle(
840845
hNativeContext =
841846
reinterpret_cast<ur_native_object_t *>(hNativeContext)->handle;
842847

848+
// convert loader handles to platform handles
849+
auto phDevicesLocal = new ur_device_handle_t[numDevices];
850+
for (size_t i = 0; (nullptr != phDevices) && (i < numDevices); ++i) {
851+
phDevicesLocal[i] =
852+
reinterpret_cast<ur_device_object_t *>(phDevices[i])->handle;
853+
}
854+
843855
// forward to device-platform
844-
result = pfnCreateWithNativeHandle(hNativeContext, phContext);
856+
result = pfnCreateWithNativeHandle(hNativeContext, numDevices, phDevices,
857+
pProperties, phContext);
858+
delete[] phDevicesLocal;
845859

846860
if (UR_RESULT_SUCCESS != result) {
847861
return result;

0 commit comments

Comments
 (0)