Skip to content

Commit

Permalink
Add isNativeHandleOwned flag for handles
Browse files Browse the repository at this point in the history
  • Loading branch information
omarahmed1111 committed Oct 11, 2024
1 parent 70f7575 commit 50121a6
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 63 deletions.
11 changes: 3 additions & 8 deletions source/adapters/opencl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -123,11 +121,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreateWithNativeHandle(
cl_context NativeHandle = reinterpret_cast<cl_context>(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;
}

Expand Down
6 changes: 4 additions & 2 deletions source/adapters/opencl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct ur_context_handle_t_ {
std::vector<ur_device_handle_t> Devices;
uint32_t DeviceCount;
std::atomic<uint32_t> RefCount = 0;
bool IsNativeHandleOwned = true;

ur_context_handle_t_(native_type Ctx, uint32_t DevCount,
const ur_device_handle_t *phDevices)
Expand Down Expand Up @@ -60,7 +61,6 @@ struct ur_context_handle_t_ {
auto URContext =
std::make_unique<ur_context_handle_t_>(Ctx, DevCount, phDevices);
Context = URContext.release();
CL_RETURN_ON_FAILURE(clRetainContext(Ctx));
} catch (std::bad_alloc &) {
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
} catch (...) {
Expand All @@ -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; }
Expand Down
8 changes: 4 additions & 4 deletions source/adapters/opencl/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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;
Expand All @@ -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<cl_device_id>(hNativeDevice);

uint32_t NumPlatforms = 0;
Expand All @@ -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;
}
}
Expand Down
7 changes: 6 additions & 1 deletion source/adapters/opencl/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct ur_device_handle_t_ {
cl_device_type Type = 0;
ur_device_handle_t ParentDevice = nullptr;
std::atomic<uint32_t> RefCount = 0;
bool IsNativeHandleOwned = true;

ur_device_handle_t_(native_type Dev, ur_platform_handle_t Plat,
ur_device_handle_t Parent)
Expand All @@ -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; }

Expand Down
9 changes: 2 additions & 7 deletions source/adapters/opencl/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle(
try {
auto UREvent =
std::make_unique<ur_event_handle_t_>(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;
}

Expand All @@ -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;
}
Expand Down
5 changes: 4 additions & 1 deletion source/adapters/opencl/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct ur_event_handle_t_ {
ur_context_handle_t Context;
ur_queue_handle_t Queue;
std::atomic<uint32_t> RefCount = 0;
bool IsNativeHandleOwned = true;

ur_event_handle_t_(native_type Event, ur_context_handle_t Ctx,
ur_queue_handle_t Queue)
Expand All @@ -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; }
Expand Down
8 changes: 2 additions & 6 deletions source/adapters/opencl/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion source/adapters/opencl/kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct ur_kernel_handle_t_ {
ur_program_handle_t Program;
ur_context_handle_t Context;
std::atomic<uint32_t> RefCount = 0;
bool IsNativeHandleOwned = true;

ur_kernel_handle_t_(native_type Kernel, ur_program_handle_t Program,
ur_context_handle_t Context)
Expand All @@ -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; }
Expand Down
13 changes: 4 additions & 9 deletions source/adapters/opencl/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreateWithNativeHandle(
cl_mem NativeHandle = reinterpret_cast<cl_mem>(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;
}

Expand All @@ -382,9 +381,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreateWithNativeHandle(
cl_mem NativeHandle = reinterpret_cast<cl_mem>(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;
}

Expand Down Expand Up @@ -441,16 +439,13 @@ 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;
}

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;
}
5 changes: 4 additions & 1 deletion source/adapters/opencl/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct ur_mem_handle_t_ {
native_type Memory;
ur_context_handle_t Context;
std::atomic<uint32_t> RefCount = 0;
bool IsNativeHandleOwned = true;

ur_mem_handle_t_(native_type Mem, ur_context_handle_t Ctx)
: Memory(Mem), Context(Ctx) {
Expand All @@ -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; }
Expand Down
8 changes: 2 additions & 6 deletions source/adapters/opencl/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion source/adapters/opencl/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct ur_program_handle_t_ {
native_type Program;
ur_context_handle_t Context;
std::atomic<uint32_t> RefCount = 0;
bool IsNativeHandleOwned = true;

ur_program_handle_t_(native_type Prog, ur_context_handle_t Ctx)
: Program(Prog), Context(Ctx) {
Expand All @@ -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; }
Expand Down
8 changes: 2 additions & 6 deletions source/adapters/opencl/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -211,16 +210,13 @@ 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;
}

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;
}
5 changes: 4 additions & 1 deletion source/adapters/opencl/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct ur_queue_handle_t_ {
ur_context_handle_t Context;
ur_device_handle_t Device;
std::atomic<uint32_t> RefCount = 0;
bool IsNativeHandleOwned = true;

ur_queue_handle_t_(native_type Queue, ur_context_handle_t Ctx,
ur_device_handle_t Dev)
Expand Down Expand Up @@ -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; }
Expand Down
Loading

0 comments on commit 50121a6

Please sign in to comment.