Skip to content

Commit 11b092b

Browse files
committed
Refactor makeWithNative
1 parent 4777b93 commit 11b092b

File tree

10 files changed

+78
-52
lines changed

10 files changed

+78
-52
lines changed

source/adapters/opencl/context.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreateWithNativeHandle(
143143
ur_context_handle_t *phContext) {
144144

145145
cl_context NativeHandle = reinterpret_cast<cl_context>(hNativeContext);
146-
auto URContext = std::make_unique<ur_context_handle_t_>(
147-
NativeHandle, numDevices, phDevices);
148-
UR_RETURN_ON_FAILURE(URContext->initWithNative());
149-
*phContext = URContext.release();
146+
UR_RETURN_ON_FAILURE(ur_context_handle_t_::makeWithNative(
147+
NativeHandle, numDevices, phDevices, *phContext));
148+
150149
return UR_RESULT_SUCCESS;
151150
}
152151

source/adapters/opencl/context.hpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,31 @@ struct ur_context_handle_t_ {
3333
Devices.emplace_back(phDevices[i]);
3434
}
3535
}
36-
ur_result_t initWithNative() {
36+
37+
static ur_result_t makeWithNative(native_type Ctx, uint32_t DevCount,
38+
const ur_device_handle_t *phDevices,
39+
ur_context_handle_t &Context) {
40+
auto URContext =
41+
std::make_unique<ur_context_handle_t_>(Ctx, DevCount, phDevices);
42+
native_type &NativeContext = URContext->Context;
43+
uint32_t &DeviceCount = URContext->DeviceCount;
3744
if (!DeviceCount) {
38-
CL_RETURN_ON_FAILURE(clGetContextInfo(Context, CL_CONTEXT_NUM_DEVICES,
39-
sizeof(DeviceCount), &DeviceCount,
40-
nullptr));
45+
CL_RETURN_ON_FAILURE(
46+
clGetContextInfo(NativeContext, CL_CONTEXT_NUM_DEVICES,
47+
sizeof(DeviceCount), &DeviceCount, nullptr));
4148
std::vector<cl_device_id> CLDevices(DeviceCount);
42-
CL_RETURN_ON_FAILURE(clGetContextInfo(Context, CL_CONTEXT_DEVICES,
49+
CL_RETURN_ON_FAILURE(clGetContextInfo(NativeContext, CL_CONTEXT_DEVICES,
4350
sizeof(CLDevices), CLDevices.data(),
4451
nullptr));
45-
Devices.resize(DeviceCount);
52+
URContext->Devices.resize(DeviceCount);
4653
for (uint32_t i = 0; i < DeviceCount; i++) {
4754
ur_native_handle_t NativeDevice =
4855
reinterpret_cast<ur_native_handle_t>(CLDevices[i]);
4956
UR_RETURN_ON_FAILURE(urDeviceCreateWithNativeHandle(
50-
NativeDevice, nullptr, nullptr, &Devices[i]));
57+
NativeDevice, nullptr, nullptr, &(URContext->Devices[i])));
5158
}
5259
}
60+
Context = URContext.release();
5361
return UR_RESULT_SUCCESS;
5462
}
5563

source/adapters/opencl/kernel.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelCreateWithNativeHandle(
385385
const ur_kernel_native_properties_t *pProperties,
386386
ur_kernel_handle_t *phKernel) {
387387
cl_kernel NativeHandle = reinterpret_cast<cl_kernel>(hNativeKernel);
388-
auto URKernel =
389-
std::make_unique<ur_kernel_handle_t_>(NativeHandle, hProgram, hContext);
390-
UR_RETURN_ON_FAILURE(URKernel->initWithNative());
391-
*phKernel = URKernel.release();
388+
389+
UR_RETURN_ON_FAILURE(ur_kernel_handle_t_::makeWithNative(
390+
NativeHandle, hProgram, hContext, *phKernel));
392391

393392
if (!pProperties || !pProperties->isNativeHandleOwned) {
394393
return urKernelRetain(*phKernel);

source/adapters/opencl/kernel.hpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,35 @@ struct ur_kernel_handle_t_ {
2626

2727
~ur_kernel_handle_t_() {}
2828

29-
ur_result_t initWithNative() {
29+
static ur_result_t makeWithNative(native_type NativeKernel,
30+
ur_program_handle_t Program,
31+
ur_context_handle_t Context,
32+
ur_kernel_handle_t &Kernel) {
33+
auto URKernel =
34+
std::make_unique<ur_kernel_handle_t_>(NativeKernel, Program, Context);
3035
if (!Program) {
3136
cl_program CLProgram;
32-
CL_RETURN_ON_FAILURE(clGetKernelInfo(
33-
Kernel, CL_KERNEL_PROGRAM, sizeof(CLProgram), &CLProgram, nullptr));
37+
CL_RETURN_ON_FAILURE(clGetKernelInfo(NativeKernel, CL_KERNEL_PROGRAM,
38+
sizeof(CLProgram), &CLProgram,
39+
nullptr));
3440
ur_native_handle_t NativeProgram =
3541
reinterpret_cast<ur_native_handle_t>(CLProgram);
3642
UR_RETURN_ON_FAILURE(urProgramCreateWithNativeHandle(
37-
NativeProgram, nullptr, nullptr, &Program));
43+
NativeProgram, nullptr, nullptr, &(URKernel->Program)));
3844
}
3945
cl_context CLContext;
40-
CL_RETURN_ON_FAILURE(clGetKernelInfo(
41-
Kernel, CL_KERNEL_CONTEXT, sizeof(CLContext), &CLContext, nullptr));
46+
CL_RETURN_ON_FAILURE(clGetKernelInfo(NativeKernel, CL_KERNEL_CONTEXT,
47+
sizeof(CLContext), &CLContext,
48+
nullptr));
4249
if (!Context) {
4350
ur_native_handle_t NativeContext =
4451
reinterpret_cast<ur_native_handle_t>(CLContext);
4552
UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle(
46-
NativeContext, 0, nullptr, nullptr, &Context));
53+
NativeContext, 0, nullptr, nullptr, &(URKernel->Context)));
4754
} else if (Context->get() != CLContext) {
4855
return UR_RESULT_ERROR_INVALID_CONTEXT;
4956
}
57+
Kernel = URKernel.release();
5058
return UR_RESULT_SUCCESS;
5159
}
5260

source/adapters/opencl/memory.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
227227
ur_context_handle_t hContext, ur_mem_flags_t flags, size_t size,
228228
const ur_buffer_properties_t *pProperties, ur_mem_handle_t *phBuffer) {
229229
cl_int RetErr = CL_INVALID_OPERATION;
230-
UR_RETURN_ON_FAILURE(urContextRetain(hContext));
230+
// UR_RETURN_ON_FAILURE(urContextRetain(hContext));
231231
if (pProperties) {
232232
// TODO: need to check if all properties are supported by OpenCL RT and
233233
// ignore unsupported
@@ -348,9 +348,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreateWithNativeHandle(
348348
ur_native_handle_t hNativeMem, ur_context_handle_t hContext,
349349
const ur_mem_native_properties_t *pProperties, ur_mem_handle_t *phMem) {
350350
cl_mem NativeHandle = reinterpret_cast<cl_mem>(hNativeMem);
351-
auto URMem = std::make_unique<ur_mem_handle_t_>(NativeHandle, hContext);
352-
UR_RETURN_ON_FAILURE(URMem->initWithNative());
353-
*phMem = URMem.release();
351+
UR_RETURN_ON_FAILURE(
352+
ur_mem_handle_t_::makeWithNative(NativeHandle, hContext, *phMem));
354353
if (!pProperties || !pProperties->isNativeHandleOwned) {
355354
return urMemRetain(*phMem);
356355
}
@@ -363,9 +362,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreateWithNativeHandle(
363362
[[maybe_unused]] const ur_image_desc_t *pImageDesc,
364363
const ur_mem_native_properties_t *pProperties, ur_mem_handle_t *phMem) {
365364
cl_mem NativeHandle = reinterpret_cast<cl_mem>(hNativeMem);
366-
auto URMem = std::make_unique<ur_mem_handle_t_>(NativeHandle, hContext);
367-
UR_RETURN_ON_FAILURE(URMem->initWithNative());
368-
*phMem = URMem.release();
365+
UR_RETURN_ON_FAILURE(
366+
ur_mem_handle_t_::makeWithNative(NativeHandle, hContext, *phMem));
369367
if (!pProperties || !pProperties->isNativeHandleOwned) {
370368
return urMemRetain(*phMem);
371369
}

source/adapters/opencl/memory.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@ struct ur_mem_handle_t_ {
2323

2424
~ur_mem_handle_t_() {}
2525

26-
ur_result_t initWithNative() {
27-
if (!Context) {
26+
static ur_result_t makeWithNative(native_type NativeMem,
27+
ur_context_handle_t Ctx,
28+
ur_mem_handle_t &Mem) {
29+
auto URMem = std::make_unique<ur_mem_handle_t_>(NativeMem, Ctx);
30+
if (!Ctx) {
2831
cl_context CLContext;
2932
CL_RETURN_ON_FAILURE(clGetMemObjectInfo(
30-
Memory, CL_MEM_CONTEXT, sizeof(CLContext), &CLContext, nullptr));
33+
NativeMem, CL_MEM_CONTEXT, sizeof(CLContext), &CLContext, nullptr));
3134
ur_native_handle_t NativeContext =
3235
reinterpret_cast<ur_native_handle_t>(CLContext);
3336
UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle(
34-
NativeContext, 0, nullptr, nullptr, &Context));
37+
NativeContext, 0, nullptr, nullptr, &(URMem->Context)));
3538
}
39+
Mem = URMem.release();
3640
return UR_RESULT_SUCCESS;
3741
}
3842

source/adapters/opencl/program.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithNativeHandle(
349349
ur_program_handle_t *phProgram) {
350350
cl_program NativeHandle = reinterpret_cast<cl_program>(hNativeProgram);
351351

352-
auto URProgram =
353-
std::make_unique<ur_program_handle_t_>(NativeHandle, hContext);
354-
UR_RETURN_ON_FAILURE(URProgram->initWithNative());
355-
*phProgram = URProgram.release();
352+
UR_RETURN_ON_FAILURE(
353+
ur_program_handle_t_::makeWithNative(NativeHandle, hContext, *phProgram));
356354
if (!pProperties || !pProperties->isNativeHandleOwned) {
357355
return urProgramRetain(*phProgram);
358356
}

source/adapters/opencl/program.hpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,22 @@ struct ur_program_handle_t_ {
2323

2424
~ur_program_handle_t_() {}
2525

26-
ur_result_t initWithNative() {
26+
static ur_result_t makeWithNative(native_type NativeProg,
27+
ur_context_handle_t Context,
28+
ur_program_handle_t Program) {
29+
auto URProgram =
30+
std::make_unique<ur_program_handle_t_>(NativeProg, Context);
2731
if (!Context) {
2832
cl_context CLContext;
29-
CL_RETURN_ON_FAILURE(clGetProgramInfo(
30-
Program, CL_PROGRAM_CONTEXT, sizeof(CLContext), &CLContext, nullptr));
33+
CL_RETURN_ON_FAILURE(clGetProgramInfo(NativeProg, CL_PROGRAM_CONTEXT,
34+
sizeof(CLContext), &CLContext,
35+
nullptr));
3136
ur_native_handle_t NativeContext =
3237
reinterpret_cast<ur_native_handle_t>(CLContext);
3338
UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle(
34-
NativeContext, 0, nullptr, nullptr, &Context));
39+
NativeContext, 0, nullptr, nullptr, &(URProgram->Context)));
3540
}
41+
Program = URProgram.release();
3642
return UR_RESULT_SUCCESS;
3743
}
3844

source/adapters/opencl/queue.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle(
169169

170170
cl_command_queue NativeHandle =
171171
reinterpret_cast<cl_command_queue>(hNativeQueue);
172-
auto URQueue =
173-
std::make_unique<ur_queue_handle_t_>(NativeHandle, hContext, hDevice);
174-
UR_RETURN_ON_FAILURE(URQueue->initWithNative());
175-
*phQueue = URQueue.release();
172+
173+
UR_RETURN_ON_FAILURE(ur_queue_handle_t_::makeWithNative(
174+
NativeHandle, hContext, hDevice, *phQueue));
176175

177176
CL_RETURN_ON_FAILURE(clRetainCommandQueue(NativeHandle));
178177

source/adapters/opencl/queue.hpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,32 @@ struct ur_queue_handle_t_ {
2323
ur_device_handle_t Dev)
2424
: Queue(Queue), Context(Ctx), Device(Dev) {}
2525

26-
ur_result_t initWithNative() {
26+
static ur_result_t makeWithNative(native_type NativeQueue,
27+
ur_context_handle_t Context,
28+
ur_device_handle_t Device,
29+
ur_queue_handle_t Queue) {
30+
auto URQueue =
31+
std::make_unique<ur_queue_handle_t_>(NativeQueue, Context, Device);
2732
if (!Context) {
2833
cl_context CLContext;
29-
CL_RETURN_ON_FAILURE(clGetCommandQueueInfo(
30-
Queue, CL_QUEUE_CONTEXT, sizeof(CLContext), &CLContext, nullptr));
34+
CL_RETURN_ON_FAILURE(clGetCommandQueueInfo(NativeQueue, CL_QUEUE_CONTEXT,
35+
sizeof(CLContext), &CLContext,
36+
nullptr));
3137
ur_native_handle_t NativeContext =
3238
reinterpret_cast<ur_native_handle_t>(CLContext);
3339
UR_RETURN_ON_FAILURE(urContextCreateWithNativeHandle(
34-
NativeContext, 0, nullptr, nullptr, &Context));
40+
NativeContext, 0, nullptr, nullptr, &(URQueue->Context)));
3541
}
3642
if (!Device) {
3743
cl_device_id CLDevice;
3844
CL_RETURN_ON_FAILURE(clGetCommandQueueInfo(
39-
Queue, CL_QUEUE_DEVICE, sizeof(CLDevice), &CLDevice, nullptr));
45+
NativeQueue, CL_QUEUE_DEVICE, sizeof(CLDevice), &CLDevice, nullptr));
4046
ur_native_handle_t NativeDevice =
4147
reinterpret_cast<ur_native_handle_t>(CLDevice);
42-
UR_RETURN_ON_FAILURE(urDeviceCreateWithNativeHandle(NativeDevice, nullptr,
43-
nullptr, &Device));
48+
UR_RETURN_ON_FAILURE(urDeviceCreateWithNativeHandle(
49+
NativeDevice, nullptr, nullptr, &(URQueue->Device)));
4450
}
51+
Queue = URQueue.release();
4552
return UR_RESULT_SUCCESS;
4653
}
4754

0 commit comments

Comments
 (0)