Skip to content

Commit 9a67949

Browse files
author
Steffen Larsen
committed
[SYCL][PI] PI objects created with native handles takes ownership
PI objects created with the associated piextCreate*WithNativeHandle function takes ownership of the native handle passed to it. For OpenCL this means that the native OpenCL object is not retained, so the PI object effectively owns the reference of the caller. The OpenCL interoperability have been changed to retain the native OpenCL handle as the implementation no longer does it. This is required by the SYCL 1.2.1 specification (Rev6, section 4.3.1.) Signed-off-by: Steffen Larsen <[email protected]>
1 parent f67949a commit 9a67949

File tree

5 files changed

+22
-62
lines changed

5 files changed

+22
-62
lines changed

sycl/include/CL/sycl/detail/pi.h

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,7 @@ pi_result piextDeviceGetNativeHandle(pi_device device,
788788
pi_native_handle *nativeHandle);
789789

790790
/// Creates PI device object from a native handle.
791-
/// NOTE: If the created PI object shares reference count with the native object
792-
/// the reference count is increased by one. Otherwise the newly created
793-
/// PI object has a reference count of 1.
791+
/// NOTE: The created PI object takes ownership of the native handle.
794792
///
795793
/// \param nativeHandle is the native handle to create PI device from.
796794
/// \param device is the PI device created from the native handle.
@@ -848,9 +846,7 @@ pi_result piextContextGetNativeHandle(pi_context context,
848846
pi_native_handle *nativeHandle);
849847

850848
/// Creates PI context object from a native handle.
851-
/// NOTE: If the created PI object shares reference count with the native object
852-
/// the reference count is increased by one. Otherwise the newly created
853-
/// PI object has a reference count of 1.
849+
/// NOTE: The created PI object takes ownership of the native handle.
854850
///
855851
/// \param nativeHandle is the native handle to create PI context from.
856852
/// \param context is the PI context created from the native handle.
@@ -881,9 +877,7 @@ pi_result piextQueueGetNativeHandle(pi_queue queue,
881877
pi_native_handle *nativeHandle);
882878

883879
/// Creates PI queue object from a native handle.
884-
/// NOTE: If the created PI object shares reference count with the native object
885-
/// the reference count is increased by one. Otherwise the newly created
886-
/// PI object has a reference count of 1.
880+
/// NOTE: The created PI object takes ownership of the native handle.
887881
///
888882
/// \param nativeHandle is the native handle to create PI queue from.
889883
/// \param queue is the PI queue created from the native handle.
@@ -925,9 +919,7 @@ pi_result piMemBufferPartition(pi_mem buffer, pi_mem_flags flags,
925919
pi_result piextMemGetNativeHandle(pi_mem mem, pi_native_handle *nativeHandle);
926920

927921
/// Creates PI mem object from a native handle.
928-
/// NOTE: If the created PI object shares reference count with the native object
929-
/// the reference count is increased by one. Otherwise the newly created
930-
/// PI object has a reference count of 1.
922+
/// NOTE: The created PI object takes ownership of the native handle.
931923
///
932924
/// \param nativeHandle is the native handle to create PI mem from.
933925
/// \param mem is the PI mem created from the native handle.
@@ -1004,9 +996,7 @@ pi_result piextProgramGetNativeHandle(pi_program program,
1004996
pi_native_handle *nativeHandle);
1005997

1006998
/// Creates PI program object from a native handle.
1007-
/// NOTE: If the created PI object shares reference count with the native object
1008-
/// the reference count is increased by one. Otherwise the newly created
1009-
/// PI object has a reference count of 1.
999+
/// NOTE: The created PI object takes ownership of the native handle.
10101000
///
10111001
/// \param nativeHandle is the native handle to create PI program from.
10121002
/// \param program is the PI program created from the native handle.
@@ -1112,9 +1102,7 @@ pi_result piextEventGetNativeHandle(pi_event event,
11121102
pi_native_handle *nativeHandle);
11131103

11141104
/// Creates PI event object from a native handle.
1115-
/// NOTE: If the created PI object shares reference count with the native object
1116-
/// the reference count is increased by one. Otherwise the newly created
1117-
/// PI object has a reference count of 1.
1105+
/// NOTE: The created PI object takes ownership of the native handle.
11181106
///
11191107
/// \param nativeHandle is the native handle to create PI event from.
11201108
/// \param event is the PI event created from the native handle.

sycl/plugins/cuda/pi_cuda.cpp

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,12 +1366,7 @@ pi_result cuda_piextDeviceGetNativeHandle(pi_device device,
13661366

13671367
/// Created a PI device object from a CUDA device handle.
13681368
/// TODO: Implement this.
1369-
/// NOTE: Approaches for native object lifetime:
1370-
/// 1. Make the created PI object the owner of the native object,
1371-
/// deleting it upon its own destruction.
1372-
/// 2. Make a special-case where the PI object does not destroy the
1373-
/// native object.
1374-
/// Both options may make lifetime tracking difficult for the user.
1369+
/// NOTE: The created PI object takes ownership of the native handle.
13751370
///
13761371
/// \param[in] nativeHandle The native handle to create PI device object from.
13771372
/// \param[out] device Set to the PI device object created from native handle.
@@ -1533,12 +1528,7 @@ pi_result cuda_piextContextGetNativeHandle(pi_context context,
15331528

15341529
/// Created a PI context object from a CUDA context handle.
15351530
/// TODO: Implement this.
1536-
/// NOTE: Approaches for native object lifetime:
1537-
/// 1. Make the created PI object the owner of the native object,
1538-
/// deleting it upon its own destruction.
1539-
/// 2. Make a special-case where the PI object does not destroy the
1540-
/// native object.
1541-
/// Both options may make lifetime tracking difficult for the user.
1531+
/// NOTE: The created PI object takes ownership of the native handle.
15421532
///
15431533
/// \param[in] nativeHandle The native handle to create PI context object from.
15441534
/// \param[out] context Set to the PI context object created from native handle.
@@ -1747,12 +1737,7 @@ pi_result cuda_piextMemGetNativeHandle(pi_mem mem,
17471737

17481738
/// Created a PI mem object from a CUDA mem handle.
17491739
/// TODO: Implement this.
1750-
/// NOTE: Approaches for native object lifetime:
1751-
/// 1. Make the created PI object the owner of the native object,
1752-
/// deleting it upon its own destruction.
1753-
/// 2. Make a special-case where the PI object does not destroy the
1754-
/// native object.
1755-
/// Both options may make lifetime tracking difficult for the user.
1740+
/// NOTE: The created PI object takes ownership of the native handle.
17561741
///
17571742
/// \param[in] nativeHandle The native handle to create PI mem object from.
17581743
/// \param[out] mem Set to the PI mem object created from native handle.
@@ -1914,12 +1899,7 @@ pi_result cuda_piextQueueGetNativeHandle(pi_queue queue,
19141899

19151900
/// Created a PI queue object from a CUDA queue handle.
19161901
/// TODO: Implement this.
1917-
/// NOTE: Approaches for native object lifetime:
1918-
/// 1. Make the created PI object the owner of the native object,
1919-
/// deleting it upon its own destruction.
1920-
/// 2. Make a special-case where the PI object does not destroy the
1921-
/// native object.
1922-
/// Both options may make lifetime tracking difficult for the user.
1902+
/// NOTE: The created PI object takes ownership of the native handle.
19231903
///
19241904
/// \param[in] nativeHandle The native handle to create PI queue object from.
19251905
/// \param[out] queue Set to the PI queue object created from native handle.
@@ -2533,12 +2513,7 @@ pi_result cuda_piextProgramGetNativeHandle(pi_program program,
25332513

25342514
/// Created a PI program object from a CUDA program handle.
25352515
/// TODO: Implement this.
2536-
/// NOTE: Approaches for native object lifetime:
2537-
/// 1. Make the created PI object the owner of the native object,
2538-
/// deleting it upon its own destruction.
2539-
/// 2. Make a special-case where the PI object does not destroy the
2540-
/// native object.
2541-
/// Both options may make lifetime tracking difficult for the user.
2516+
/// NOTE: The created PI object takes ownership of the native handle.
25422517
///
25432518
/// \param[in] nativeHandle The native handle to create PI program object from.
25442519
/// \param[out] program Set to the PI program object created from native handle.
@@ -2923,12 +2898,7 @@ pi_result cuda_piextEventGetNativeHandle(pi_event event,
29232898

29242899
/// Created a PI event object from a CUDA event handle.
29252900
/// TODO: Implement this.
2926-
/// NOTE: Approaches for native object lifetime:
2927-
/// 1. Make the created PI object the owner of the native object,
2928-
/// deleting it upon its own destruction.
2929-
/// 2. Make a special-case where the PI object does not destroy the
2930-
/// native object.
2931-
/// Both options may make lifetime tracking difficult for the user.
2901+
/// NOTE: The created PI object takes ownership of the native handle.
29322902
///
29332903
/// \param[in] nativeHandle The native handle to create PI event object from.
29342904
/// \param[out] event Set to the PI event object created from native handle.

sycl/plugins/opencl/pi_opencl.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ pi_result OCL(piextDeviceCreateWithNativeHandle)(pi_native_handle nativeHandle,
273273
pi_device *piDevice) {
274274
assert(piDevice != nullptr);
275275
*piDevice = reinterpret_cast<pi_device>(nativeHandle);
276-
clRetainDevice(cast<cl_device_id>(*piDevice));
277276
return PI_SUCCESS;
278277
}
279278

@@ -321,7 +320,6 @@ pi_result OCL(piextQueueCreateWithNativeHandle)(pi_native_handle nativeHandle,
321320
pi_queue *piQueue) {
322321
assert(piQueue != nullptr);
323322
*piQueue = reinterpret_cast<pi_queue>(nativeHandle);
324-
clRetainCommandQueue(cast<cl_command_queue>(*piQueue));
325323
return PI_SUCCESS;
326324
}
327325

@@ -407,7 +405,6 @@ pi_result OCL(piextProgramCreateWithNativeHandle)(pi_native_handle nativeHandle,
407405
pi_program *piProgram) {
408406
assert(piProgram != nullptr);
409407
*piProgram = reinterpret_cast<pi_program>(nativeHandle);
410-
clRetainProgram(cast<cl_program>(*piProgram));
411408
return PI_SUCCESS;
412409
}
413410

@@ -506,7 +503,6 @@ pi_result OCL(piextContextCreateWithNativeHandle)(pi_native_handle nativeHandle,
506503
pi_context *piContext) {
507504
assert(piContext != nullptr);
508505
*piContext = reinterpret_cast<pi_context>(nativeHandle);
509-
clRetainContext(cast<cl_context>(*piContext));
510506
return PI_SUCCESS;
511507
}
512508

@@ -550,7 +546,6 @@ pi_result OCL(piextMemCreateWithNativeHandle)(pi_native_handle nativeHandle,
550546
pi_mem *piMem) {
551547
assert(piMem != nullptr);
552548
*piMem = reinterpret_cast<pi_mem>(nativeHandle);
553-
clRetainMemObject(cast<cl_mem>(*piMem));
554549
return PI_SUCCESS;
555550
}
556551

@@ -619,7 +614,6 @@ pi_result OCL(piextEventCreateWithNativeHandle)(pi_native_handle nativeHandle,
619614
pi_event *piEvent) {
620615
assert(piEvent != nullptr);
621616
*piEvent = reinterpret_cast<pi_event>(nativeHandle);
622-
clRetainEvent(cast<cl_event>(*piEvent));
623617
return PI_SUCCESS;
624618
}
625619

sycl/source/device.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ device::device() : impl(std::make_shared<detail::device_impl>()) {}
3030

3131
device::device(cl_device_id deviceId)
3232
: impl(std::make_shared<detail::device_impl>(
33-
detail::pi::cast<pi_native_handle>(deviceId), *RT::GlobalPlugin)) {}
33+
detail::pi::cast<pi_native_handle>(deviceId), *RT::GlobalPlugin)) {
34+
// The implementation constructor takes ownership of the native handle so we
35+
// must retain it in order to adhere to SYCL 1.2.1 spec (Rev6, section 4.3.1.)
36+
clRetainDevice(deviceId);
37+
}
3438

3539
device::device(const device_selector &deviceSelector) {
3640
*this = deviceSelector.select_device();

sycl/source/program.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ program::program(vector_class<program> programList, string_class linkOptions) {
3030
program::program(const context &context, cl_program clProgram)
3131
: impl(std::make_shared<detail::program_impl>(
3232
detail::getSyclObjImpl(context),
33-
detail::pi::cast<pi_native_handle>(clProgram))) {}
33+
detail::pi::cast<pi_native_handle>(clProgram))) {
34+
// The implementation constructor takes ownership of the native handle so we
35+
// must retain it in order to adhere to SYCL 1.2.1 spec (Rev6, section 4.3.1.)
36+
clRetainProgram(clProgram);
37+
}
3438
program::program(std::shared_ptr<detail::program_impl> impl) : impl(impl) {}
3539

3640
cl_program program::get() const { return impl->get(); }

0 commit comments

Comments
 (0)