Skip to content

Commit 3f6dbf3

Browse files
authored
Merge pull request intel#2193 from aarongreig/aaron/clarifyIsNativeHandleOwned
Clarify spec around isNativeHandleOwned.
2 parents bf7a654 + dc294e4 commit 3f6dbf3

29 files changed

+274
-172
lines changed

include/ur_api.h

+27-27
Original file line numberDiff line numberDiff line change
@@ -1615,9 +1615,9 @@ typedef struct ur_platform_native_properties_t {
16151615
ur_structure_type_t stype;
16161616
/// [in,out][optional] pointer to extension-specific structure
16171617
void *pNext;
1618-
/// [in] Indicates UR owns the native handle or if it came from an
1619-
/// interoperability operation in the application that asked to not
1620-
/// transfer the ownership to the unified-runtime.
1618+
/// [in] If true then ownership of the native handle is transferred to
1619+
/// the resultant object. This means the object will be responsible for
1620+
/// releasing the native resources at the end of its lifetime.
16211621
bool isNativeHandleOwned;
16221622

16231623
} ur_platform_native_properties_t;
@@ -2699,9 +2699,9 @@ typedef struct ur_device_native_properties_t {
26992699
ur_structure_type_t stype;
27002700
/// [in,out][optional] pointer to extension-specific structure
27012701
void *pNext;
2702-
/// [in] Indicates UR owns the native handle or if it came from an
2703-
/// interoperability operation in the application that asked to not
2704-
/// transfer the ownership to the unified-runtime.
2702+
/// [in] If true then ownership of the native handle is transferred to
2703+
/// the resultant object. This means the object will be responsible for
2704+
/// releasing the native resources at the end of its lifetime.
27052705
bool isNativeHandleOwned;
27062706

27072707
} ur_device_native_properties_t;
@@ -3079,9 +3079,9 @@ typedef struct ur_context_native_properties_t {
30793079
ur_structure_type_t stype;
30803080
/// [in,out][optional] pointer to extension-specific structure
30813081
void *pNext;
3082-
/// [in] Indicates UR owns the native handle or if it came from an
3083-
/// interoperability operation in the application that asked to not transfer
3084-
/// the ownership to the unified-runtime.
3082+
/// [in] If true then ownership of the native handle is transferred to
3083+
/// the resultant object. This means the object will be responsible for
3084+
/// releasing the native resources at the end of its lifetime.
30853085
bool isNativeHandleOwned;
30863086

30873087
} ur_context_native_properties_t;
@@ -3700,9 +3700,9 @@ typedef struct ur_mem_native_properties_t {
37003700
ur_structure_type_t stype;
37013701
/// [in,out][optional] pointer to extension-specific structure
37023702
void *pNext;
3703-
/// [in] Indicates UR owns the native handle or if it came from an
3704-
/// interoperability operation in the application that asked to not
3705-
/// transfer the ownership to the unified-runtime.
3703+
/// [in] If true then ownership of the native handle is transferred to
3704+
/// the resultant object. This means the object will be responsible for
3705+
/// releasing the native resources at the end of its lifetime.
37063706
bool isNativeHandleOwned;
37073707

37083708
} ur_mem_native_properties_t;
@@ -4106,9 +4106,9 @@ typedef struct ur_sampler_native_properties_t {
41064106
ur_structure_type_t stype;
41074107
/// [in,out][optional] pointer to extension-specific structure
41084108
void *pNext;
4109-
/// [in] Indicates UR owns the native handle or if it came from an
4110-
/// interoperability operation in the application that asked to not
4111-
/// transfer the ownership to the unified-runtime.
4109+
/// [in] If true then ownership of the native handle is transferred to
4110+
/// the resultant object. This means the object will be responsible for
4111+
/// releasing the native resources at the end of its lifetime.
41124112
bool isNativeHandleOwned;
41134113

41144114
} ur_sampler_native_properties_t;
@@ -5821,9 +5821,9 @@ typedef struct ur_program_native_properties_t {
58215821
ur_structure_type_t stype;
58225822
/// [in,out][optional] pointer to extension-specific structure
58235823
void *pNext;
5824-
/// [in] Indicates UR owns the native handle or if it came from an
5825-
/// interoperability operation in the application that asked to not
5826-
/// transfer the ownership to the unified-runtime.
5824+
/// [in] If true then ownership of the native handle is transferred to
5825+
/// the resultant object. This means the object will be responsible for
5826+
/// releasing the native resources at the end of its lifetime.
58275827
bool isNativeHandleOwned;
58285828

58295829
} ur_program_native_properties_t;
@@ -6490,9 +6490,9 @@ typedef struct ur_kernel_native_properties_t {
64906490
ur_structure_type_t stype;
64916491
/// [in,out][optional] pointer to extension-specific structure
64926492
void *pNext;
6493-
/// [in] Indicates UR owns the native handle or if it came from an
6494-
/// interoperability operation in the application that asked to not transfer
6495-
/// the ownership to the unified-runtime.
6493+
/// [in] If true then ownership of the native handle is transferred to
6494+
/// the resultant object. This means the object will be responsible for
6495+
/// releasing the native resources at the end of its lifetime.
64966496
bool isNativeHandleOwned;
64976497

64986498
} ur_kernel_native_properties_t;
@@ -6885,9 +6885,9 @@ typedef struct ur_queue_native_properties_t {
68856885
ur_structure_type_t stype;
68866886
/// [in,out][optional] pointer to extension-specific structure
68876887
void *pNext;
6888-
/// [in] Indicates UR owns the native handle or if it came from an
6889-
/// interoperability operation in the application that asked to not transfer
6890-
/// the ownership to the unified-runtime.
6888+
/// [in] If true then ownership of the native handle is transferred to
6889+
/// the resultant object. This means the object will be responsible for
6890+
/// releasing the native resources at the end of its lifetime.
68916891
bool isNativeHandleOwned;
68926892

68936893
} ur_queue_native_properties_t;
@@ -7311,9 +7311,9 @@ typedef struct ur_event_native_properties_t {
73117311
ur_structure_type_t stype;
73127312
/// [in,out][optional] pointer to extension-specific structure
73137313
void *pNext;
7314-
/// [in] Indicates UR owns the native handle or if it came from an
7315-
/// interoperability operation in the application that asked to not transfer
7316-
/// the ownership to the unified-runtime.
7314+
/// [in] If true then ownership of the native handle is transferred to
7315+
/// the resultant object. This means the object will be responsible for
7316+
/// releasing the native resources at the end of its lifetime.
73177317
bool isNativeHandleOwned;
73187318

73197319
} ur_event_native_properties_t;

scripts/core/PROG.rst

+35-11
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,6 @@ ${x}QueueRetain. An application must call ${x}QueueRelease
287287
when a queue object is no longer needed. When a queue object's reference count becomes
288288
zero, it is deleted by the runtime.
289289

290-
Native Driver Access
291-
----------------------------------
292-
293-
The runtime API provides accessors for native handles.
294-
For example, given a ${x}_program_handle_t, we can
295-
call ${x}ProgramGetNativeHandle to retrieve a ${x}_native_handle_t.
296-
We can then leverage a platform extension to convert the
297-
native handle to a driver handle. For example, OpenCL platform
298-
may expose an extension ${x}ProgramCreateWithNativeHandle to retrieve
299-
a cl_program.
300-
301290
Memory
302291
======
303292

@@ -340,3 +329,38 @@ through ${x}_usm_desc_t structure. Allocations that specify different pool handl
340329
isolated and not reside on the same page. Memory pool is subject to limits specified during pool creation.
341330

342331
Even if no ${x}_usm_pool_handle_t is provided to an allocation function, each adapter may still perform memory pooling.
332+
333+
Native Handles
334+
==============
335+
336+
In addition to the regular object creation APIs, ${X} objects can be
337+
constructed with handles obtained directly from an adapter's associated
338+
backend. This is achieved by casting the backend handle to a
339+
${x}_native_handle_t and passing it to the relevant ``CreateWithNativeHandle``
340+
entry point.
341+
342+
343+
.. note::
344+
Not all backends have a 1:1 equivalent for every ${X} handle type, as such
345+
any ``CreateWithNativeHandle`` or ``GetNativeHandle`` entry point *may* fail
346+
with the error code ${X}_RESULT_ERROR_UNSUPPORTED_FEATURE for a given
347+
adapter.
348+
349+
Native Handle Ownership
350+
-----------------------
351+
352+
By default a ${X} object constructed from a native handle doesn't own the
353+
native handle, it is guaranteed not to retain a reference to the native handle,
354+
or cause its resources to be released. A ${X} object that doesn't own its
355+
associated native handle **must** be destroyed before the native handle is.
356+
357+
Ownership of the native handle can be transferred to the ${X} object by passing
358+
``isNativeHandleOwned = true`` in the native properties struct when calling the
359+
``CreateWithNativeHandle`` entry point. A ${X} object that owns a native handle
360+
will attempt to release the native resources associated with that handle on
361+
destruction. The same native handle **must not** have its ownership transferred
362+
to more than one ${X} object.
363+
364+
Ownership of a native handle obtained from a ${X} object via a
365+
``GetNativeHandle`` entry point **must not** be transferred to a new ${X}
366+
object.

scripts/core/context.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ members:
209209
- type: bool
210210
name: isNativeHandleOwned
211211
desc: |
212-
[in] Indicates UR owns the native handle or if it came from an interoperability
213-
operation in the application that asked to not transfer the ownership to
214-
the unified-runtime.
212+
[in] If true then ownership of the native handle is transferred to
213+
the resultant object. This means the object will be responsible for
214+
releasing the native resources at the end of its lifetime.
215215
--- #--------------------------------------------------------------------------
216216
type: function
217217
desc: "Create runtime context object from native context handle."

scripts/core/device.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -805,10 +805,10 @@ base: $x_base_properties_t
805805
members:
806806
- type: bool
807807
name: isNativeHandleOwned
808-
desc: >
809-
[in] Indicates UR owns the native handle or if it came from an
810-
interoperability operation in the application that asked to not
811-
transfer the ownership to the unified-runtime.
808+
desc: |
809+
[in] If true then ownership of the native handle is transferred to
810+
the resultant object. This means the object will be responsible for
811+
releasing the native resources at the end of its lifetime.
812812
--- #--------------------------------------------------------------------------
813813
type: function
814814
desc: "Create runtime device object from native device handle."

scripts/core/event.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ members:
289289
- type: bool
290290
name: isNativeHandleOwned
291291
desc: |
292-
[in] Indicates UR owns the native handle or if it came from an interoperability
293-
operation in the application that asked to not transfer the ownership to
294-
the unified-runtime.
292+
[in] If true then ownership of the native handle is transferred to
293+
the resultant object. This means the object will be responsible for
294+
releasing the native resources at the end of its lifetime.
295295
--- #--------------------------------------------------------------------------
296296
type: function
297297
desc: "Create runtime event object from native event handle."

scripts/core/kernel.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,9 @@ members:
513513
- type: bool
514514
name: isNativeHandleOwned
515515
desc: |
516-
[in] Indicates UR owns the native handle or if it came from an interoperability
517-
operation in the application that asked to not transfer the ownership to
518-
the unified-runtime.
516+
[in] If true then ownership of the native handle is transferred to
517+
the resultant object. This means the object will be responsible for
518+
releasing the native resources at the end of its lifetime.
519519
--- #--------------------------------------------------------------------------
520520
type: function
521521
desc: "Create runtime kernel object from native kernel handle."

scripts/core/memory.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,10 @@ base: $x_base_properties_t
474474
members:
475475
- type: bool
476476
name: isNativeHandleOwned
477-
desc: >
478-
[in] Indicates UR owns the native handle or if it came from an
479-
interoperability operation in the application that asked to not
480-
transfer the ownership to the unified-runtime.
477+
desc: |
478+
[in] If true then ownership of the native handle is transferred to
479+
the resultant object. This means the object will be responsible for
480+
releasing the native resources at the end of its lifetime.
481481
--- #--------------------------------------------------------------------------
482482
type: function
483483
desc: "Create runtime buffer memory object from native memory handle."

scripts/core/platform.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ base: $x_base_properties_t
202202
members:
203203
- type: bool
204204
name: isNativeHandleOwned
205-
desc: >
206-
[in] Indicates UR owns the native handle or if it came from an
207-
interoperability operation in the application that asked to not
208-
transfer the ownership to the unified-runtime.
205+
desc: |
206+
[in] If true then ownership of the native handle is transferred to
207+
the resultant object. This means the object will be responsible for
208+
releasing the native resources at the end of its lifetime.
209209
--- #--------------------------------------------------------------------------
210210
type: function
211211
desc: "Create runtime platform object from native platform handle."

scripts/core/program.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,10 @@ base: $x_base_properties_t
587587
members:
588588
- type: bool
589589
name: isNativeHandleOwned
590-
desc: >
591-
[in] Indicates UR owns the native handle or if it came from an
592-
interoperability operation in the application that asked to not
593-
transfer the ownership to the unified-runtime.
590+
desc: |
591+
[in] If true then ownership of the native handle is transferred to
592+
the resultant object. This means the object will be responsible for
593+
releasing the native resources at the end of its lifetime.
594594
--- #--------------------------------------------------------------------------
595595
type: function
596596
desc: "Create runtime program object from native program handle."

scripts/core/queue.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ members:
262262
- type: bool
263263
name: isNativeHandleOwned
264264
desc: |
265-
[in] Indicates UR owns the native handle or if it came from an interoperability
266-
operation in the application that asked to not transfer the ownership to
267-
the unified-runtime.
265+
[in] If true then ownership of the native handle is transferred to
266+
the resultant object. This means the object will be responsible for
267+
releasing the native resources at the end of its lifetime.
268268
--- #--------------------------------------------------------------------------
269269
type: function
270270
desc: "Create runtime queue object from native queue handle."

scripts/core/sampler.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ base: $x_base_properties_t
206206
members:
207207
- type: bool
208208
name: isNativeHandleOwned
209-
desc: >
210-
[in] Indicates UR owns the native handle or if it came from an
211-
interoperability operation in the application that asked to not
212-
transfer the ownership to the unified-runtime.
209+
desc: |
210+
[in] If true then ownership of the native handle is transferred to
211+
the resultant object. This means the object will be responsible for
212+
releasing the native resources at the end of its lifetime.
213213
--- #--------------------------------------------------------------------------
214214
type: function
215215
desc: "Create runtime sampler object from native sampler handle."

source/adapters/cuda/queue.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -284,17 +284,19 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle(
284284
std::vector<CUstream> ComputeCuStreams(1, CuStream);
285285
std::vector<CUstream> TransferCuStreams(0);
286286

287+
auto isNativeHandleOwned =
288+
pProperties ? pProperties->isNativeHandleOwned : false;
289+
287290
// Create queue and set num_compute_streams to 1, as computeCuStreams has
288291
// valid stream
289-
*phQueue =
290-
new ur_queue_handle_t_{std::move(ComputeCuStreams),
291-
std::move(TransferCuStreams),
292-
hContext,
293-
hDevice,
294-
CuFlags,
295-
Flags,
296-
/*priority*/ 0,
297-
/*backend_owns*/ pProperties->isNativeHandleOwned};
292+
*phQueue = new ur_queue_handle_t_{std::move(ComputeCuStreams),
293+
std::move(TransferCuStreams),
294+
hContext,
295+
hDevice,
296+
CuFlags,
297+
Flags,
298+
/*priority*/ 0,
299+
/*backend_owns*/ isNativeHandleOwned};
298300
(*phQueue)->NumComputeStreams = 1;
299301

300302
return UR_RESULT_SUCCESS;

source/adapters/hip/memory.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ UR_APIEXPORT ur_result_t UR_APICALL
302302
urMemGetNativeHandle(ur_mem_handle_t hMem, ur_device_handle_t Device,
303303
ur_native_handle_t *phNativeMem) {
304304
UR_ASSERT(Device != nullptr, UR_RESULT_ERROR_INVALID_NULL_HANDLE);
305+
if (hMem->isImage()) {
306+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
307+
}
305308
#if defined(__HIP_PLATFORM_NVIDIA__)
306309
if (sizeof(BufferMem::native_type) > sizeof(ur_native_handle_t)) {
307310
// Check that all the upper bits that cannot be represented by

source/adapters/hip/queue.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,19 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle(
318318
std::vector<hipStream_t> ComputeHIPStreams(1, HIPStream);
319319
std::vector<hipStream_t> TransferHIPStreams(0);
320320

321+
auto isNativeHandleOwned =
322+
pProperties ? pProperties->isNativeHandleOwned : false;
323+
321324
// Create queue and set num_compute_streams to 1, as computeHIPStreams has
322325
// valid stream
323-
*phQueue =
324-
new ur_queue_handle_t_{std::move(ComputeHIPStreams),
325-
std::move(TransferHIPStreams),
326-
hContext,
327-
hDevice,
328-
HIPFlags,
329-
Flags,
330-
/*priority*/ 0,
331-
/*backend_owns*/ pProperties->isNativeHandleOwned};
326+
*phQueue = new ur_queue_handle_t_{std::move(ComputeHIPStreams),
327+
std::move(TransferHIPStreams),
328+
hContext,
329+
hDevice,
330+
HIPFlags,
331+
Flags,
332+
/*priority*/ 0,
333+
/*backend_owns*/ isNativeHandleOwned};
332334
(*phQueue)->NumComputeStreams = 1;
333335

334336
return UR_RESULT_SUCCESS;

source/adapters/level_zero/context.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ ur_result_t urContextCreateWithNativeHandle(
159159
const ur_context_native_properties_t *Properties,
160160
/// [out] pointer to the handle of the context object created.
161161
ur_context_handle_t *Context) {
162-
bool OwnNativeHandle = Properties->isNativeHandleOwned;
162+
bool OwnNativeHandle = Properties ? Properties->isNativeHandleOwned : false;
163163
try {
164164
ze_context_handle_t ZeContext =
165165
reinterpret_cast<ze_context_handle_t>(NativeContext);

source/adapters/level_zero/kernel.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1118,9 +1118,9 @@ ur_result_t urKernelCreateWithNativeHandle(
11181118
ze_kernel_handle_t ZeKernel = ur_cast<ze_kernel_handle_t>(NativeKernel);
11191119
ur_kernel_handle_t_ *Kernel = nullptr;
11201120
try {
1121-
Kernel = new ur_kernel_handle_t_(ZeKernel, Properties->isNativeHandleOwned,
1122-
Context);
1123-
if (Properties->isNativeHandleOwned) {
1121+
auto OwnNativeHandle = Properties ? Properties->isNativeHandleOwned : false;
1122+
Kernel = new ur_kernel_handle_t_(ZeKernel, OwnNativeHandle, Context);
1123+
if (OwnNativeHandle) {
11241124
// If ownership is passed to the adapter we need to pass the kernel
11251125
// to this vector which is then used during ZeKernelRelease.
11261126
Kernel->ZeKernels.push_back(ZeKernel);

0 commit comments

Comments
 (0)