diff --git a/source/adapters/opencl/context.cpp b/source/adapters/opencl/context.cpp index ae77169817..27bb3cf7ba 100644 --- a/source/adapters/opencl/context.cpp +++ b/source/adapters/opencl/context.cpp @@ -94,15 +94,13 @@ urContextRelease(ur_context_handle_t hContext) { if (hContext->decrementReferenceCount() == 0) { delete hContext; - } else { - CL_RETURN_ON_FAILURE(clReleaseContext(hContext->get())); } + return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urContextRetain(ur_context_handle_t hContext) { - CL_RETURN_ON_FAILURE(clRetainContext(hContext->get())); hContext->incrementReferenceCount(); return UR_RESULT_SUCCESS; } @@ -123,11 +121,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreateWithNativeHandle( cl_context NativeHandle = reinterpret_cast(hNativeContext); UR_RETURN_ON_FAILURE(ur_context_handle_t_::makeWithNative( NativeHandle, numDevices, phDevices, *phContext)); - - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainContext(NativeHandle)); - } - + (*phContext)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/context.hpp b/source/adapters/opencl/context.hpp index 66ab0e2518..bf877dfe95 100644 --- a/source/adapters/opencl/context.hpp +++ b/source/adapters/opencl/context.hpp @@ -20,6 +20,7 @@ struct ur_context_handle_t_ { std::vector Devices; uint32_t DeviceCount; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_context_handle_t_(native_type Ctx, uint32_t DevCount, const ur_device_handle_t *phDevices) @@ -60,7 +61,6 @@ struct ur_context_handle_t_ { auto URContext = std::make_unique(Ctx, DevCount, phDevices); Context = URContext.release(); - CL_RETURN_ON_FAILURE(clRetainContext(Ctx)); } catch (std::bad_alloc &) { return UR_RESULT_ERROR_OUT_OF_RESOURCES; } catch (...) { @@ -74,7 +74,9 @@ struct ur_context_handle_t_ { for (uint32_t i = 0; i < DeviceCount; i++) { urDeviceRelease(Devices[i]); } - clReleaseContext(Context); + if (IsNativeHandleOwned) { + clReleaseContext(Context); + } } native_type get() { return Context; } diff --git a/source/adapters/opencl/device.cpp b/source/adapters/opencl/device.cpp index a0dbfa73e9..23d4a7cd38 100644 --- a/source/adapters/opencl/device.cpp +++ b/source/adapters/opencl/device.cpp @@ -1103,7 +1103,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition( // Root devices ref count are unchanged through out the program lifetime. UR_APIEXPORT ur_result_t UR_APICALL urDeviceRetain(ur_device_handle_t hDevice) { if (hDevice->ParentDevice) { - CL_RETURN_ON_FAILURE(clRetainDevice(hDevice->get())); hDevice->incrementReferenceCount(); } @@ -1116,8 +1115,6 @@ urDeviceRelease(ur_device_handle_t hDevice) { if (hDevice->ParentDevice) { if (hDevice->decrementReferenceCount() == 0) { delete hDevice; - } else { - CL_RETURN_ON_FAILURE(clReleaseDevice(hDevice->get())); } } return UR_RESULT_SUCCESS; @@ -1132,7 +1129,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetNativeHandle( UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle( ur_native_handle_t hNativeDevice, ur_adapter_handle_t, - const ur_device_native_properties_t *, ur_device_handle_t *phDevice) { + const ur_device_native_properties_t *pProperties, + ur_device_handle_t *phDevice) { cl_device_id NativeHandle = reinterpret_cast(hNativeDevice); uint32_t NumPlatforms = 0; @@ -1152,6 +1150,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle( for (auto &Device : Devices) { if (Device->get() == NativeHandle) { *phDevice = Device; + (*phDevice)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } } diff --git a/source/adapters/opencl/device.hpp b/source/adapters/opencl/device.hpp index 7420f58665..8527f9b518 100644 --- a/source/adapters/opencl/device.hpp +++ b/source/adapters/opencl/device.hpp @@ -19,6 +19,7 @@ struct ur_device_handle_t_ { cl_device_type Type = 0; ur_device_handle_t ParentDevice = nullptr; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_device_handle_t_(native_type Dev, ur_platform_handle_t Plat, ur_device_handle_t Parent) @@ -32,7 +33,11 @@ struct ur_device_handle_t_ { } } - ~ur_device_handle_t_() {} + ~ur_device_handle_t_() { + if (ParentDevice && IsNativeHandleOwned) { + clReleaseDevice(Device); + } + } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/source/adapters/opencl/event.cpp b/source/adapters/opencl/event.cpp index a03ecbb7eb..b432959a75 100644 --- a/source/adapters/opencl/event.cpp +++ b/source/adapters/opencl/event.cpp @@ -119,16 +119,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle( try { auto UREvent = std::make_unique(NativeHandle, hContext, nullptr); + UREvent->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; *phEvent = UREvent.release(); } catch (std::bad_alloc &) { return UR_RESULT_ERROR_OUT_OF_RESOURCES; } catch (...) { return UR_RESULT_ERROR_UNKNOWN; } - - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainEvent(NativeHandle)); - } return UR_RESULT_SUCCESS; } @@ -140,14 +138,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetNativeHandle( UR_APIEXPORT ur_result_t UR_APICALL urEventRelease(ur_event_handle_t hEvent) { if (hEvent->decrementReferenceCount() == 0) { delete hEvent; - } else { - CL_RETURN_ON_FAILURE(clReleaseEvent(hEvent->get())); } return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urEventRetain(ur_event_handle_t hEvent) { - CL_RETURN_ON_FAILURE(clRetainEvent(hEvent->get())); hEvent->incrementReferenceCount(); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/event.hpp b/source/adapters/opencl/event.hpp index f7f17e7e1e..18d7c83f6d 100644 --- a/source/adapters/opencl/event.hpp +++ b/source/adapters/opencl/event.hpp @@ -19,6 +19,7 @@ struct ur_event_handle_t_ { ur_context_handle_t Context; ur_queue_handle_t Queue; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_event_handle_t_(native_type Event, ur_context_handle_t Ctx, ur_queue_handle_t Queue) @@ -35,7 +36,9 @@ struct ur_event_handle_t_ { if (Queue) { urQueueRelease(Queue); } - clReleaseEvent(Event); + if (IsNativeHandleOwned) { + clReleaseEvent(Event); + } } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/source/adapters/opencl/kernel.cpp b/source/adapters/opencl/kernel.cpp index 52e24770a5..0a76c5de7f 100644 --- a/source/adapters/opencl/kernel.cpp +++ b/source/adapters/opencl/kernel.cpp @@ -262,7 +262,6 @@ urKernelGetSubGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice, } UR_APIEXPORT ur_result_t UR_APICALL urKernelRetain(ur_kernel_handle_t hKernel) { - CL_RETURN_ON_FAILURE(clRetainKernel(hKernel->get())); hKernel->incrementReferenceCount(); return UR_RESULT_SUCCESS; } @@ -271,8 +270,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelRelease(ur_kernel_handle_t hKernel) { if (hKernel->decrementReferenceCount() == 0) { delete hKernel; - } else { - CL_RETURN_ON_FAILURE(clReleaseKernel(hKernel->get())); } return UR_RESULT_SUCCESS; } @@ -405,9 +402,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelCreateWithNativeHandle( UR_RETURN_ON_FAILURE(ur_kernel_handle_t_::makeWithNative( NativeHandle, hProgram, hContext, *phKernel)); - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainKernel(NativeHandle)); - } + (*phKernel)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/kernel.hpp b/source/adapters/opencl/kernel.hpp index 44651ebfc7..4dd4049e6d 100644 --- a/source/adapters/opencl/kernel.hpp +++ b/source/adapters/opencl/kernel.hpp @@ -21,6 +21,7 @@ struct ur_kernel_handle_t_ { ur_program_handle_t Program; ur_context_handle_t Context; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_kernel_handle_t_(native_type Kernel, ur_program_handle_t Program, ur_context_handle_t Context) @@ -31,9 +32,11 @@ struct ur_kernel_handle_t_ { } ~ur_kernel_handle_t_() { - clReleaseKernel(Kernel); urProgramRelease(Program); urContextRelease(Context); + if (IsNativeHandleOwned) { + clReleaseKernel(Kernel); + } } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/source/adapters/opencl/memory.cpp b/source/adapters/opencl/memory.cpp index 064feba00d..4d41a9463f 100644 --- a/source/adapters/opencl/memory.cpp +++ b/source/adapters/opencl/memory.cpp @@ -368,9 +368,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreateWithNativeHandle( cl_mem NativeHandle = reinterpret_cast(hNativeMem); UR_RETURN_ON_FAILURE( ur_mem_handle_t_::makeWithNative(NativeHandle, hContext, *phMem)); - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainMemObject((*phMem)->get())); - } + (*phMem)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } @@ -382,9 +381,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreateWithNativeHandle( cl_mem NativeHandle = reinterpret_cast(hNativeMem); UR_RETURN_ON_FAILURE( ur_mem_handle_t_::makeWithNative(NativeHandle, hContext, *phMem)); - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainMemObject(NativeHandle)); - } + (*phMem)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } @@ -441,7 +439,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageGetInfo(ur_mem_handle_t hMemory, } UR_APIEXPORT ur_result_t UR_APICALL urMemRetain(ur_mem_handle_t hMem) { - CL_RETURN_ON_FAILURE(clRetainMemObject(hMem->get())); hMem->incrementReferenceCount(); return UR_RESULT_SUCCESS; } @@ -449,8 +446,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemRetain(ur_mem_handle_t hMem) { UR_APIEXPORT ur_result_t UR_APICALL urMemRelease(ur_mem_handle_t hMem) { if (hMem->decrementReferenceCount() == 0) { delete hMem; - } else { - CL_RETURN_ON_FAILURE(clReleaseMemObject(hMem->get())); } return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/memory.hpp b/source/adapters/opencl/memory.hpp index df8794c897..f54d9b1347 100644 --- a/source/adapters/opencl/memory.hpp +++ b/source/adapters/opencl/memory.hpp @@ -19,6 +19,7 @@ struct ur_mem_handle_t_ { native_type Memory; ur_context_handle_t Context; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_mem_handle_t_(native_type Mem, ur_context_handle_t Ctx) : Memory(Mem), Context(Ctx) { @@ -27,8 +28,10 @@ struct ur_mem_handle_t_ { } ~ur_mem_handle_t_() { - clReleaseMemObject(Memory); urContextRelease(Context); + if (IsNativeHandleOwned) { + clReleaseMemObject(Memory); + } } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/source/adapters/opencl/program.cpp b/source/adapters/opencl/program.cpp index 08ec3f78d4..74c226a9de 100644 --- a/source/adapters/opencl/program.cpp +++ b/source/adapters/opencl/program.cpp @@ -356,7 +356,6 @@ urProgramGetBuildInfo(ur_program_handle_t hProgram, ur_device_handle_t hDevice, UR_APIEXPORT ur_result_t UR_APICALL urProgramRetain(ur_program_handle_t hProgram) { - CL_RETURN_ON_FAILURE(clRetainProgram(hProgram->get())); hProgram->incrementReferenceCount(); return UR_RESULT_SUCCESS; } @@ -365,8 +364,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramRelease(ur_program_handle_t hProgram) { if (hProgram->decrementReferenceCount() == 0) { delete hProgram; - } else { - CL_RETURN_ON_FAILURE(clReleaseProgram(hProgram->get())); } return UR_RESULT_SUCCESS; } @@ -386,9 +383,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithNativeHandle( UR_RETURN_ON_FAILURE( ur_program_handle_t_::makeWithNative(NativeHandle, hContext, *phProgram)); - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainProgram(NativeHandle)); - } + (*phProgram)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/program.hpp b/source/adapters/opencl/program.hpp index 1c6bae2e8d..f2c065d895 100644 --- a/source/adapters/opencl/program.hpp +++ b/source/adapters/opencl/program.hpp @@ -19,6 +19,7 @@ struct ur_program_handle_t_ { native_type Program; ur_context_handle_t Context; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_program_handle_t_(native_type Prog, ur_context_handle_t Ctx) : Program(Prog), Context(Ctx) { @@ -27,8 +28,10 @@ struct ur_program_handle_t_ { } ~ur_program_handle_t_() { - clReleaseProgram(Program); urContextRelease(Context); + if (IsNativeHandleOwned) { + clReleaseProgram(Program); + } } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/source/adapters/opencl/queue.cpp b/source/adapters/opencl/queue.cpp index 2cbeb6f738..68f59d4563 100644 --- a/source/adapters/opencl/queue.cpp +++ b/source/adapters/opencl/queue.cpp @@ -191,9 +191,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle( UR_RETURN_ON_FAILURE(ur_queue_handle_t_::makeWithNative( NativeHandle, hContext, hDevice, *phQueue)); - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainCommandQueue(NativeHandle)); - } + (*phQueue)->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; return UR_RESULT_SUCCESS; } @@ -211,7 +210,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueFlush(ur_queue_handle_t hQueue) { } UR_APIEXPORT ur_result_t UR_APICALL urQueueRetain(ur_queue_handle_t hQueue) { - CL_RETURN_ON_FAILURE(clRetainCommandQueue(hQueue->get())); hQueue->incrementReferenceCount(); return UR_RESULT_SUCCESS; } @@ -219,8 +217,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueRetain(ur_queue_handle_t hQueue) { UR_APIEXPORT ur_result_t UR_APICALL urQueueRelease(ur_queue_handle_t hQueue) { if (hQueue->decrementReferenceCount() == 0) { delete hQueue; - } else { - CL_RETURN_ON_FAILURE(clReleaseCommandQueue(hQueue->get())); } return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/queue.hpp b/source/adapters/opencl/queue.hpp index 6ea50402a9..ed22c8d6b2 100644 --- a/source/adapters/opencl/queue.hpp +++ b/source/adapters/opencl/queue.hpp @@ -21,6 +21,7 @@ struct ur_queue_handle_t_ { ur_context_handle_t Context; ur_device_handle_t Device; std::atomic RefCount = 0; + bool IsNativeHandleOwned = true; ur_queue_handle_t_(native_type Queue, ur_context_handle_t Ctx, ur_device_handle_t Dev) @@ -60,9 +61,11 @@ struct ur_queue_handle_t_ { } ~ur_queue_handle_t_() { - clReleaseCommandQueue(Queue); urDeviceRelease(Device); urContextRelease(Context); + if (IsNativeHandleOwned) { + clReleaseCommandQueue(Queue); + } } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/source/adapters/opencl/sampler.cpp b/source/adapters/opencl/sampler.cpp index 9dee3065f3..7a8ebeba2f 100644 --- a/source/adapters/opencl/sampler.cpp +++ b/source/adapters/opencl/sampler.cpp @@ -220,7 +220,6 @@ urSamplerGetInfo(ur_sampler_handle_t hSampler, ur_sampler_info_t propName, UR_APIEXPORT ur_result_t UR_APICALL urSamplerRetain(ur_sampler_handle_t hSampler) { - CL_RETURN_ON_FAILURE(clRetainSampler(hSampler->get())); hSampler->incrementReferenceCount(); return UR_RESULT_SUCCESS; } @@ -229,8 +228,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urSamplerRelease(ur_sampler_handle_t hSampler) { if (hSampler->decrementReferenceCount() == 0) { delete hSampler; - } else { - CL_RETURN_ON_FAILURE(clRetainSampler(hSampler->get())); } return UR_RESULT_SUCCESS; } @@ -249,6 +246,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urSamplerCreateWithNativeHandle( try { auto URSampler = std::make_unique(NativeHandle, hContext); + URSampler->IsNativeHandleOwned = + pProperties ? pProperties->isNativeHandleOwned : false; *phSampler = URSampler.release(); } catch (std::bad_alloc &) { return UR_RESULT_ERROR_OUT_OF_RESOURCES; @@ -256,9 +255,5 @@ UR_APIEXPORT ur_result_t UR_APICALL urSamplerCreateWithNativeHandle( return UR_RESULT_ERROR_UNKNOWN; } - if (!pProperties || !pProperties->isNativeHandleOwned) { - CL_RETURN_ON_FAILURE(clRetainSampler(NativeHandle)); - } - return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/sampler.hpp b/source/adapters/opencl/sampler.hpp index 238ee1cecc..49f8ec43eb 100644 --- a/source/adapters/opencl/sampler.hpp +++ b/source/adapters/opencl/sampler.hpp @@ -18,6 +18,7 @@ struct ur_sampler_handle_t_ { native_type Sampler; ur_context_handle_t Context; std::atomic RefCount = 0; + bool IsNativeHandleOwned = false; ur_sampler_handle_t_(native_type Sampler, ur_context_handle_t Ctx) : Sampler(Sampler), Context(Ctx) { @@ -26,8 +27,10 @@ struct ur_sampler_handle_t_ { } ~ur_sampler_handle_t_() { - clReleaseSampler(Sampler); urContextRelease(Context); + if (IsNativeHandleOwned) { + clReleaseSampler(Sampler); + } } uint32_t incrementReferenceCount() noexcept { return ++RefCount; } diff --git a/test/conformance/context/context_adapter_opencl.match b/test/conformance/context/context_adapter_opencl.match deleted file mode 100644 index 869f11cd42..0000000000 --- a/test/conformance/context/context_adapter_opencl.match +++ /dev/null @@ -1 +0,0 @@ -urContextCreateWithNativeHandleTest.SuccessWithUnOwnedNativeHandle/Intel_R__OpenCL___{{.*}}_