Skip to content

Commit

Permalink
Refactor makeWithNative
Browse files Browse the repository at this point in the history
  • Loading branch information
omarahmed1111 committed Jan 12, 2024
1 parent 4777b93 commit ff193b2
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 52 deletions.
7 changes: 3 additions & 4 deletions source/adapters/opencl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreateWithNativeHandle(
ur_context_handle_t *phContext) {

cl_context NativeHandle = reinterpret_cast<cl_context>(hNativeContext);
auto URContext = std::make_unique<ur_context_handle_t_>(
NativeHandle, numDevices, phDevices);
UR_RETURN_ON_FAILURE(URContext->initWithNative());
*phContext = URContext.release();
UR_RETURN_ON_FAILURE(ur_context_handle_t_::makeWithNative(
NativeHandle, numDevices, phDevices, *phContext));

return UR_RESULT_SUCCESS;
}

Expand Down
22 changes: 15 additions & 7 deletions source/adapters/opencl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,31 @@ struct ur_context_handle_t_ {
Devices.emplace_back(phDevices[i]);
}
}
ur_result_t initWithNative() {

static ur_result_t makeWithNative(native_type Ctx, uint32_t DevCount,
const ur_device_handle_t *phDevices,
ur_context_handle_t &Context) {
auto URContext =
std::make_unique<ur_context_handle_t_>(Ctx, DevCount, phDevices);
native_type &NativeContext = URContext->Context;
uint32_t &DeviceCount = URContext->DeviceCount;
if (!DeviceCount) {
CL_RETURN_ON_FAILURE(clGetContextInfo(Context, CL_CONTEXT_NUM_DEVICES,
sizeof(DeviceCount), &DeviceCount,
nullptr));
CL_RETURN_ON_FAILURE(
clGetContextInfo(NativeContext, CL_CONTEXT_NUM_DEVICES,
sizeof(DeviceCount), &DeviceCount, nullptr));
std::vector<cl_device_id> CLDevices(DeviceCount);
CL_RETURN_ON_FAILURE(clGetContextInfo(Context, CL_CONTEXT_DEVICES,
CL_RETURN_ON_FAILURE(clGetContextInfo(NativeContext, CL_CONTEXT_DEVICES,
sizeof(CLDevices), CLDevices.data(),
nullptr));
Devices.resize(DeviceCount);
URContext->Devices.resize(DeviceCount);
for (uint32_t i = 0; i < DeviceCount; i++) {
ur_native_handle_t NativeDevice =
reinterpret_cast<ur_native_handle_t>(CLDevices[i]);
UR_RETURN_ON_FAILURE(urDeviceCreateWithNativeHandle(
NativeDevice, nullptr, nullptr, &Devices[i]));
NativeDevice, nullptr, nullptr, &(URContext->Devices[i])));
}
}
Context = URContext.release();
return UR_RESULT_SUCCESS;
}

Expand Down
7 changes: 3 additions & 4 deletions source/adapters/opencl/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelCreateWithNativeHandle(
const ur_kernel_native_properties_t *pProperties,
ur_kernel_handle_t *phKernel) {
cl_kernel NativeHandle = reinterpret_cast<cl_kernel>(hNativeKernel);
auto URKernel =
std::make_unique<ur_kernel_handle_t_>(NativeHandle, hProgram, hContext);
UR_RETURN_ON_FAILURE(URKernel->initWithNative());
*phKernel = URKernel.release();

UR_RETURN_ON_FAILURE(ur_kernel_handle_t_::makeWithNative(
NativeHandle, hProgram, hContext, *phKernel));

if (!pProperties || !pProperties->isNativeHandleOwned) {
return urKernelRetain(*phKernel);
Expand Down
22 changes: 15 additions & 7 deletions source/adapters/opencl/kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,35 @@ struct ur_kernel_handle_t_ {

~ur_kernel_handle_t_() {}

ur_result_t initWithNative() {
static ur_result_t makeWithNative(native_type NativeKernel,
ur_program_handle_t Program,
ur_context_handle_t Context,
ur_kernel_handle_t &Kernel) {
auto URKernel =
std::make_unique<ur_kernel_handle_t_>(NativeKernel, Program, Context);
if (!Program) {
cl_program CLProgram;
CL_RETURN_ON_FAILURE(clGetKernelInfo(
Kernel, CL_KERNEL_PROGRAM, sizeof(CLProgram), &CLProgram, nullptr));
CL_RETURN_ON_FAILURE(clGetKernelInfo(NativeKernel, CL_KERNEL_PROGRAM,
sizeof(CLProgram), &CLProgram,
nullptr));
ur_native_handle_t NativeProgram =
reinterpret_cast<ur_native_handle_t>(CLProgram);
UR_RETURN_ON_FAILURE(urProgramCreateWithNativeHandle(
NativeProgram, nullptr, nullptr, &Program));
NativeProgram, nullptr, nullptr, &(URKernel->Program)));
}
cl_context CLContext;
CL_RETURN_ON_FAILURE(clGetKernelInfo(
Kernel, CL_KERNEL_CONTEXT, sizeof(CLContext), &CLContext, nullptr));
CL_RETURN_ON_FAILURE(clGetKernelInfo(NativeKernel, CL_KERNEL_CONTEXT,
sizeof(CLContext), &CLContext,
nullptr));
if (!Context) {
ur_native_handle_t NativeContext =
reinterpret_cast<ur_native_handle_t>(CLContext);
UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle(
NativeContext, 0, nullptr, nullptr, &Context));
NativeContext, 0, nullptr, nullptr, &(URKernel->Context)));
} else if (Context->get() != CLContext) {
return UR_RESULT_ERROR_INVALID_CONTEXT;
}
Kernel = URKernel.release();
return UR_RESULT_SUCCESS;
}

Expand Down
12 changes: 5 additions & 7 deletions source/adapters/opencl/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
ur_context_handle_t hContext, ur_mem_flags_t flags, size_t size,
const ur_buffer_properties_t *pProperties, ur_mem_handle_t *phBuffer) {
cl_int RetErr = CL_INVALID_OPERATION;
UR_RETURN_ON_FAILURE(urContextRetain(hContext));
// UR_RETURN_ON_FAILURE(urContextRetain(hContext));
if (pProperties) {
// TODO: need to check if all properties are supported by OpenCL RT and
// ignore unsupported
Expand Down Expand Up @@ -348,9 +348,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreateWithNativeHandle(
ur_native_handle_t hNativeMem, ur_context_handle_t hContext,
const ur_mem_native_properties_t *pProperties, ur_mem_handle_t *phMem) {
cl_mem NativeHandle = reinterpret_cast<cl_mem>(hNativeMem);
auto URMem = std::make_unique<ur_mem_handle_t_>(NativeHandle, hContext);
UR_RETURN_ON_FAILURE(URMem->initWithNative());
*phMem = URMem.release();
UR_RETURN_ON_FAILURE(
ur_mem_handle_t_::makeWithNative(NativeHandle, hContext, *phMem));
if (!pProperties || !pProperties->isNativeHandleOwned) {
return urMemRetain(*phMem);
}
Expand All @@ -363,9 +362,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreateWithNativeHandle(
[[maybe_unused]] const ur_image_desc_t *pImageDesc,
const ur_mem_native_properties_t *pProperties, ur_mem_handle_t *phMem) {
cl_mem NativeHandle = reinterpret_cast<cl_mem>(hNativeMem);
auto URMem = std::make_unique<ur_mem_handle_t_>(NativeHandle, hContext);
UR_RETURN_ON_FAILURE(URMem->initWithNative());
*phMem = URMem.release();
UR_RETURN_ON_FAILURE(
ur_mem_handle_t_::makeWithNative(NativeHandle, hContext, *phMem));
if (!pProperties || !pProperties->isNativeHandleOwned) {
return urMemRetain(*phMem);
}
Expand Down
12 changes: 8 additions & 4 deletions source/adapters/opencl/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ struct ur_mem_handle_t_ {

~ur_mem_handle_t_() {}

ur_result_t initWithNative() {
if (!Context) {
static ur_result_t makeWithNative(native_type NativeMem,
ur_context_handle_t Ctx,
ur_mem_handle_t &Mem) {
auto URMem = std::make_unique<ur_mem_handle_t_>(NativeMem, Ctx);
if (!Ctx) {
cl_context CLContext;
CL_RETURN_ON_FAILURE(clGetMemObjectInfo(
Memory, CL_MEM_CONTEXT, sizeof(CLContext), &CLContext, nullptr));
NativeMem, CL_MEM_CONTEXT, sizeof(CLContext), &CLContext, nullptr));
ur_native_handle_t NativeContext =
reinterpret_cast<ur_native_handle_t>(CLContext);
UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle(
NativeContext, 0, nullptr, nullptr, &Context));
NativeContext, 0, nullptr, nullptr, &(URMem->Context)));
}
Mem = URMem.release();
return UR_RESULT_SUCCESS;
}

Expand Down
6 changes: 2 additions & 4 deletions source/adapters/opencl/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithNativeHandle(
ur_program_handle_t *phProgram) {
cl_program NativeHandle = reinterpret_cast<cl_program>(hNativeProgram);

auto URProgram =
std::make_unique<ur_program_handle_t_>(NativeHandle, hContext);
UR_RETURN_ON_FAILURE(URProgram->initWithNative());
*phProgram = URProgram.release();
UR_RETURN_ON_FAILURE(
ur_program_handle_t_::makeWithNative(NativeHandle, hContext, *phProgram));
if (!pProperties || !pProperties->isNativeHandleOwned) {
return urProgramRetain(*phProgram);
}
Expand Down
14 changes: 10 additions & 4 deletions source/adapters/opencl/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ struct ur_program_handle_t_ {

~ur_program_handle_t_() {}

ur_result_t initWithNative() {
static ur_result_t makeWithNative(native_type NativeProg,
ur_context_handle_t Context,
ur_program_handle_t &Program) {
auto URProgram =
std::make_unique<ur_program_handle_t_>(NativeProg, Context);
if (!Context) {
cl_context CLContext;
CL_RETURN_ON_FAILURE(clGetProgramInfo(
Program, CL_PROGRAM_CONTEXT, sizeof(CLContext), &CLContext, nullptr));
CL_RETURN_ON_FAILURE(clGetProgramInfo(NativeProg, CL_PROGRAM_CONTEXT,
sizeof(CLContext), &CLContext,
nullptr));
ur_native_handle_t NativeContext =
reinterpret_cast<ur_native_handle_t>(CLContext);
UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle(
NativeContext, 0, nullptr, nullptr, &Context));
NativeContext, 0, nullptr, nullptr, &(URProgram->Context)));
}
Program = URProgram.release();
return UR_RESULT_SUCCESS;
}

Expand Down
7 changes: 3 additions & 4 deletions source/adapters/opencl/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle(

cl_command_queue NativeHandle =
reinterpret_cast<cl_command_queue>(hNativeQueue);
auto URQueue =
std::make_unique<ur_queue_handle_t_>(NativeHandle, hContext, hDevice);
UR_RETURN_ON_FAILURE(URQueue->initWithNative());
*phQueue = URQueue.release();

UR_RETURN_ON_FAILURE(ur_queue_handle_t_::makeWithNative(
NativeHandle, hContext, hDevice, *phQueue));

CL_RETURN_ON_FAILURE(clRetainCommandQueue(NativeHandle));

Expand Down
21 changes: 14 additions & 7 deletions source/adapters/opencl/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,32 @@ struct ur_queue_handle_t_ {
ur_device_handle_t Dev)
: Queue(Queue), Context(Ctx), Device(Dev) {}

ur_result_t initWithNative() {
static ur_result_t makeWithNative(native_type NativeQueue,
ur_context_handle_t Context,
ur_device_handle_t Device,
ur_queue_handle_t &Queue) {
auto URQueue =
std::make_unique<ur_queue_handle_t_>(NativeQueue, Context, Device);
if (!Context) {
cl_context CLContext;
CL_RETURN_ON_FAILURE(clGetCommandQueueInfo(
Queue, CL_QUEUE_CONTEXT, sizeof(CLContext), &CLContext, nullptr));
CL_RETURN_ON_FAILURE(clGetCommandQueueInfo(NativeQueue, CL_QUEUE_CONTEXT,
sizeof(CLContext), &CLContext,
nullptr));
ur_native_handle_t NativeContext =
reinterpret_cast<ur_native_handle_t>(CLContext);
UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle(
NativeContext, 0, nullptr, nullptr, &Context));
NativeContext, 0, nullptr, nullptr, &(URQueue->Context)));
}
if (!Device) {
cl_device_id CLDevice;
CL_RETURN_ON_FAILURE(clGetCommandQueueInfo(
Queue, CL_QUEUE_DEVICE, sizeof(CLDevice), &CLDevice, nullptr));
NativeQueue, CL_QUEUE_DEVICE, sizeof(CLDevice), &CLDevice, nullptr));
ur_native_handle_t NativeDevice =
reinterpret_cast<ur_native_handle_t>(CLDevice);
UR_RETURN_ON_FAILURE(urDeviceCreateWithNativeHandle(NativeDevice, nullptr,
nullptr, &Device));
UR_RETURN_ON_FAILURE(urDeviceCreateWithNativeHandle(
NativeDevice, nullptr, nullptr, &(URQueue->Device)));
}
Queue = URQueue.release();
return UR_RESULT_SUCCESS;
}

Expand Down

0 comments on commit ff193b2

Please sign in to comment.