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 16, 2024
1 parent be00944 commit f56cf10
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 81 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
35 changes: 26 additions & 9 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 @@ -49,18 +50,32 @@ struct ur_context_handle_t_ {
CL_RETURN_ON_FAILURE(clGetContextInfo(Ctx, CL_CONTEXT_DEVICES,
sizeof(CLDevices), CLDevices.data(),
nullptr));
if (DevCount != CLDeviceCount) {
return UR_RESULT_ERROR_INVALID_CONTEXT;
}
for (uint32_t i = 0; i < DevCount; i++) {
if (phDevices[i]->get() != CLDevices[i]) {
std::vector<ur_device_handle_t> URDevices;
if (DevCount) {
if (DevCount != CLDeviceCount) {
return UR_RESULT_ERROR_INVALID_CONTEXT;
}
for (uint32_t i = 0; i < DevCount; i++) {
if (phDevices[i]->get() != CLDevices[i]) {
return UR_RESULT_ERROR_INVALID_CONTEXT;
}
URDevices.push_back(phDevices[i]);
}
} else {
DevCount = CLDeviceCount;
for (uint32_t i = 0; i < CLDeviceCount; i++) {
ur_device_handle_t UrDevice = nullptr;
ur_native_handle_t hNativeHandle =
reinterpret_cast<ur_native_handle_t>(CLDevices[i]);
UR_RETURN_ON_FAILURE(urDeviceCreateWithNativeHandle(
hNativeHandle, nullptr, nullptr, &UrDevice));
URDevices.push_back(UrDevice);
}
}
auto URContext =
std::make_unique<ur_context_handle_t_>(Ctx, DevCount, phDevices);

auto URContext = std::make_unique<ur_context_handle_t_>(Ctx, DevCount,
URDevices.data());
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 +89,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 @@ -1105,7 +1105,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 @@ -1118,8 +1117,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 @@ -1134,7 +1131,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 @@ -1154,6 +1152,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 @@ -403,9 +400,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
28 changes: 20 additions & 8 deletions source/adapters/opencl/kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@ 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)
: Kernel(Kernel), Program(Program), Context(Context) {
RefCount = 1;
urProgramRetain(Program);
if (Program) {
urProgramRetain(Program);
}
urContextRetain(Context);
}

~ur_kernel_handle_t_() {
clReleaseKernel(Kernel);
urProgramRelease(Program);
if (Program) {
urProgramRelease(Program);
}
urContextRelease(Context);
if (IsNativeHandleOwned) {
clReleaseKernel(Kernel);
}
}

uint32_t incrementReferenceCount() noexcept { return ++RefCount; }
Expand All @@ -46,9 +53,6 @@ struct ur_kernel_handle_t_ {
ur_program_handle_t Program,
ur_context_handle_t Context,
ur_kernel_handle_t &Kernel) {
if (!Program || !Context) {
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
}
try {
cl_context CLContext;
CL_RETURN_ON_FAILURE(clGetKernelInfo(NativeKernel, CL_KERNEL_CONTEXT,
Expand All @@ -62,9 +66,17 @@ struct ur_kernel_handle_t_ {
if (Context->get() != CLContext) {
return UR_RESULT_ERROR_INVALID_CONTEXT;
}
if (Program->get() != CLProgram) {
return UR_RESULT_ERROR_INVALID_PROGRAM;
if (Program) {
if (Program->get() != CLProgram) {
return UR_RESULT_ERROR_INVALID_PROGRAM;
}
} else {
ur_native_handle_t hNativeHandle =
reinterpret_cast<ur_native_handle_t>(CLProgram);
UR_RETURN_ON_FAILURE(urProgramCreateWithNativeHandle(
hNativeHandle, Context, nullptr, &Program));
}

auto URKernel =
std::make_unique<ur_kernel_handle_t_>(NativeKernel, Program, Context);
Kernel = URKernel.release();
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
Loading

0 comments on commit f56cf10

Please sign in to comment.