diff --git a/source/adapters/opencl/context.cpp b/source/adapters/opencl/context.cpp index 48a7a9e3f5..020139d0aa 100644 --- a/source/adapters/opencl/context.cpp +++ b/source/adapters/opencl/context.cpp @@ -143,10 +143,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreateWithNativeHandle( ur_context_handle_t *phContext) { cl_context NativeHandle = reinterpret_cast(hNativeContext); - auto URContext = std::make_unique( - 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; } diff --git a/source/adapters/opencl/context.hpp b/source/adapters/opencl/context.hpp index e08501cb8a..5f6c186db3 100644 --- a/source/adapters/opencl/context.hpp +++ b/source/adapters/opencl/context.hpp @@ -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(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 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(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; } diff --git a/source/adapters/opencl/kernel.cpp b/source/adapters/opencl/kernel.cpp index 8993ee693f..51fa78dc2a 100644 --- a/source/adapters/opencl/kernel.cpp +++ b/source/adapters/opencl/kernel.cpp @@ -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(hNativeKernel); - auto URKernel = - std::make_unique(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); diff --git a/source/adapters/opencl/kernel.hpp b/source/adapters/opencl/kernel.hpp index 3323fb68c7..37e60e74b7 100644 --- a/source/adapters/opencl/kernel.hpp +++ b/source/adapters/opencl/kernel.hpp @@ -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(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(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(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; } diff --git a/source/adapters/opencl/memory.cpp b/source/adapters/opencl/memory.cpp index 6e2054c546..840b63180c 100644 --- a/source/adapters/opencl/memory.cpp +++ b/source/adapters/opencl/memory.cpp @@ -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 @@ -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(hNativeMem); - auto URMem = std::make_unique(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); } @@ -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(hNativeMem); - auto URMem = std::make_unique(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); } diff --git a/source/adapters/opencl/memory.hpp b/source/adapters/opencl/memory.hpp index bd3a4c994d..ff4cddec3e 100644 --- a/source/adapters/opencl/memory.hpp +++ b/source/adapters/opencl/memory.hpp @@ -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(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(CLContext); UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle( - NativeContext, 0, nullptr, nullptr, &Context)); + NativeContext, 0, nullptr, nullptr, &(URMem->Context))); } + Mem = URMem.release(); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/program.cpp b/source/adapters/opencl/program.cpp index 5c326d8097..d017e00498 100644 --- a/source/adapters/opencl/program.cpp +++ b/source/adapters/opencl/program.cpp @@ -349,10 +349,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithNativeHandle( ur_program_handle_t *phProgram) { cl_program NativeHandle = reinterpret_cast(hNativeProgram); - auto URProgram = - std::make_unique(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); } diff --git a/source/adapters/opencl/program.hpp b/source/adapters/opencl/program.hpp index 5c40cdc0b2..63a0f5bac0 100644 --- a/source/adapters/opencl/program.hpp +++ b/source/adapters/opencl/program.hpp @@ -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(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(CLContext); UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle( - NativeContext, 0, nullptr, nullptr, &Context)); + NativeContext, 0, nullptr, nullptr, &(URProgram->Context))); } + Program = URProgram.release(); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/queue.cpp b/source/adapters/opencl/queue.cpp index 4e387d7dbe..c21006fe90 100644 --- a/source/adapters/opencl/queue.cpp +++ b/source/adapters/opencl/queue.cpp @@ -169,10 +169,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle( cl_command_queue NativeHandle = reinterpret_cast(hNativeQueue); - auto URQueue = - std::make_unique(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)); diff --git a/source/adapters/opencl/queue.hpp b/source/adapters/opencl/queue.hpp index 4918fa0f31..80d620d1e4 100644 --- a/source/adapters/opencl/queue.hpp +++ b/source/adapters/opencl/queue.hpp @@ -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(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(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(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; }