Skip to content

Commit b977d7c

Browse files
committed
Add device handle to opencl
1 parent 0430133 commit b977d7c

File tree

9 files changed

+171
-183
lines changed

9 files changed

+171
-183
lines changed

source/adapters/opencl/context.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,22 @@ ur_result_t
1616
getDevicesFromContext(ur_context_handle_t hContext,
1717
std::unique_ptr<std::vector<cl_device_id>> &DevicesInCtx);
1818
}
19+
20+
// struct ur_context_handle_t_ {
21+
// using native_type = cl_context;
22+
// native_type Context;
23+
// std::atomic_uint32_t RefCount;
24+
// ur_platform_handle_t Platform;
25+
26+
// ur_context_handle_t_(native_type Ctx):Context(Ctx) {}
27+
28+
// ~ur_context_handle_t_() {}
29+
30+
// native_type get() { return Context; }
31+
32+
// uint32_t incrementReferenceCount() noexcept { return ++RefCount; }
33+
34+
// uint32_t decrementReferenceCount() noexcept { return --RefCount; }
35+
36+
// uint32_t getReferenceCount() const noexcept { return RefCount; }
37+
// };

source/adapters/opencl/device.cpp

Lines changed: 70 additions & 89 deletions
Large diffs are not rendered by default.

source/adapters/opencl/device.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,16 @@ ur_result_t checkDeviceExtensions(cl_device_id Dev,
1818
const std::vector<std::string> &Exts,
1919
bool &Supported);
2020
} // namespace cl_adapter
21+
22+
struct ur_device_handle_t_ {
23+
using native_type = cl_device_id;
24+
native_type Device;
25+
ur_platform_handle_t Platform;
26+
27+
ur_device_handle_t_(native_type Dev, ur_platform_handle_t Plat)
28+
: Device(Dev), Platform(Plat) {}
29+
30+
~ur_device_handle_t_() {}
31+
32+
native_type get() { return Device; }
33+
};

source/adapters/opencl/kernel.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//
99
//===----------------------------------------------------------------------===//
1010
#include "common.hpp"
11+
#include "device.hpp"
1112

1213
#include <algorithm>
1314
#include <memory>
@@ -135,16 +136,15 @@ urKernelGetGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice,
135136
// to deter naive use of the query.
136137
if (propName == UR_KERNEL_GROUP_INFO_GLOBAL_WORK_SIZE) {
137138
cl_device_type ClDeviceType;
138-
CL_RETURN_ON_FAILURE(
139-
clGetDeviceInfo(cl_adapter::cast<cl_device_id>(hDevice), CL_DEVICE_TYPE,
140-
sizeof(ClDeviceType), &ClDeviceType, nullptr));
139+
CL_RETURN_ON_FAILURE(clGetDeviceInfo(hDevice->get(), CL_DEVICE_TYPE,
140+
sizeof(ClDeviceType), &ClDeviceType,
141+
nullptr));
141142
if (ClDeviceType != CL_DEVICE_TYPE_CUSTOM) {
142143
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
143144
}
144145
}
145146
CL_RETURN_ON_FAILURE(clGetKernelWorkGroupInfo(
146-
cl_adapter::cast<cl_kernel>(hKernel),
147-
cl_adapter::cast<cl_device_id>(hDevice),
147+
cl_adapter::cast<cl_kernel>(hKernel), hDevice->get(),
148148
mapURKernelGroupInfoToCL(propName), propSize, pPropValue, pPropSizeRet));
149149

150150
return UR_RESULT_SUCCESS;
@@ -197,11 +197,10 @@ urKernelGetSubGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice,
197197
InputValueSize = MaxDims * sizeof(size_t);
198198
}
199199

200-
cl_int Ret = clGetKernelSubGroupInfo(cl_adapter::cast<cl_kernel>(hKernel),
201-
cl_adapter::cast<cl_device_id>(hDevice),
202-
mapURKernelSubGroupInfoToCL(propName),
203-
InputValueSize, InputValue.get(),
204-
sizeof(size_t), &RetVal, pPropSizeRet);
200+
cl_int Ret = clGetKernelSubGroupInfo(
201+
cl_adapter::cast<cl_kernel>(hKernel), hDevice->get(),
202+
mapURKernelSubGroupInfoToCL(propName), InputValueSize, InputValue.get(),
203+
sizeof(size_t), &RetVal, pPropSizeRet);
205204

206205
if (Ret == CL_INVALID_OPERATION) {
207206
// clGetKernelSubGroupInfo returns CL_INVALID_OPERATION if the device does

source/adapters/opencl/platform.cpp

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,6 @@ ur_result_t cl_adapter::getPlatformVersion(cl_platform_id Plat,
2828

2929
return UR_RESULT_SUCCESS;
3030
}
31-
ur_result_t ur_platform_handle_t_::getPlatformVersion(oclv::OpenCLVersion &Version) {
32-
33-
size_t PlatVerSize = 0;
34-
CL_RETURN_ON_FAILURE(
35-
clGetPlatformInfo(Platform, CL_PLATFORM_VERSION, 0, nullptr, &PlatVerSize));
36-
37-
std::string PlatVer(PlatVerSize, '\0');
38-
CL_RETURN_ON_FAILURE(clGetPlatformInfo(Platform, CL_PLATFORM_VERSION, PlatVerSize,
39-
PlatVer.data(), nullptr));
40-
41-
Version = oclv::OpenCLVersion(PlatVer);
42-
if (!Version.isValid()) {
43-
return UR_RESULT_ERROR_INVALID_PLATFORM;
44-
}
45-
46-
return UR_RESULT_SUCCESS;
47-
}
4831

4932
static cl_int mapURPlatformInfoToCL(ur_platform_info_t URPropName) {
5033

@@ -79,9 +62,13 @@ urPlatformGetInfo(ur_platform_handle_t hPlatform, ur_platform_info_t propName,
7962
case UR_PLATFORM_INFO_VERSION:
8063
case UR_PLATFORM_INFO_EXTENSIONS:
8164
case UR_PLATFORM_INFO_PROFILE: {
82-
CL_RETURN_ON_FAILURE(
83-
clGetPlatformInfo(hPlatform->get(),
84-
CLPropName, propSize, pPropValue, pSizeRet));
65+
cl_platform_id Plat = nullptr;
66+
if (hPlatform) {
67+
Plat = hPlatform->get();
68+
}
69+
CL_RETURN_ON_FAILURE(clGetPlatformInfo(Plat, CLPropName,
70+
propSize, pPropValue, pSizeRet));
71+
8572
return UR_RESULT_SUCCESS;
8673
}
8774
default:
@@ -101,18 +88,17 @@ urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries,
10188
ur_platform_handle_t *phPlatforms, uint32_t *pNumPlatforms) {
10289

10390
std::vector<cl_platform_id> CLPlatforms(NumEntries);
104-
cl_int Result =
105-
clGetPlatformIDs(cl_adapter::cast<cl_uint>(NumEntries),
106-
CLPlatforms.data(),
107-
cl_adapter::cast<cl_uint *>(pNumPlatforms));
91+
cl_int Result = clGetPlatformIDs(cl_adapter::cast<cl_uint>(NumEntries),
92+
CLPlatforms.data(),
93+
cl_adapter::cast<cl_uint *>(pNumPlatforms));
10894
/* Absorb the CL_PLATFORM_NOT_FOUND_KHR and just return 0 in num_platforms */
10995
if (Result == CL_PLATFORM_NOT_FOUND_KHR) {
11096
Result = CL_SUCCESS;
11197
if (pNumPlatforms) {
11298
*pNumPlatforms = 0;
11399
}
114100
}
115-
if (NumEntries) {
101+
if (NumEntries && phPlatforms) {
116102
for (uint32_t i = 0; i < NumEntries; i++) {
117103
phPlatforms[i] = new ur_platform_handle_t_(CLPlatforms[i]);
118104
}
@@ -129,7 +115,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGetNativeHandle(
129115
UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle(
130116
ur_native_handle_t hNativePlatform, const ur_platform_native_properties_t *,
131117
ur_platform_handle_t *phPlatform) {
132-
cl_platform_id NativeHandle = reinterpret_cast<cl_platform_id>(hNativePlatform);
118+
cl_platform_id NativeHandle =
119+
reinterpret_cast<cl_platform_id>(hNativePlatform);
133120
*phPlatform = new ur_platform_handle_t_(NativeHandle);
134121
return UR_RESULT_SUCCESS;
135122
}

source/adapters/opencl/platform.hpp

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,17 @@ struct ExtFuncPtrT {
145145
clDeviceMemAllocINTEL_fn clDeviceMemAllocINTELCache;
146146
clSharedMemAllocINTEL_fn clSharedMemAllocINTELCache;
147147
clGetDeviceFunctionPointer_fn clGetDeviceFunctionPointerCache;
148-
clCreateBufferWithPropertiesINTEL_fn
149-
clCreateBufferWithPropertiesINTELCache;
148+
clCreateBufferWithPropertiesINTEL_fn clCreateBufferWithPropertiesINTELCache;
150149
clMemBlockingFreeINTEL_fn clMemBlockingFreeINTELCache;
151-
clSetKernelArgMemPointerINTEL_fn
152-
clSetKernelArgMemPointerINTELCache;
150+
clSetKernelArgMemPointerINTEL_fn clSetKernelArgMemPointerINTELCache;
153151
clEnqueueMemFillINTEL_fn clEnqueueMemFillINTELCache;
154152
clEnqueueMemcpyINTEL_fn clEnqueueMemcpyINTELCache;
155153
clGetMemAllocInfoINTEL_fn clGetMemAllocInfoINTELCache;
156-
clEnqueueWriteGlobalVariable_fn
157-
clEnqueueWriteGlobalVariableCache;
154+
clEnqueueWriteGlobalVariable_fn clEnqueueWriteGlobalVariableCache;
158155
clEnqueueReadGlobalVariable_fn clEnqueueReadGlobalVariableCache;
159156
clEnqueueReadHostPipeINTEL_fn clEnqueueReadHostPipeINTELCache;
160157
clEnqueueWriteHostPipeINTEL_fn clEnqueueWriteHostPipeINTELCache;
161-
clSetProgramSpecializationConstant_fn
162-
clSetProgramSpecializationConstantCache;
158+
clSetProgramSpecializationConstant_fn clSetProgramSpecializationConstantCache;
163159
clCreateCommandBufferKHR_fn clCreateCommandBufferKHRCache;
164160
clRetainCommandBufferKHR_fn clRetainCommandBufferKHRCache;
165161
clReleaseCommandBufferKHR_fn clReleaseCommandBufferKHRCache;
@@ -170,36 +166,32 @@ struct ExtFuncPtrT {
170166
clCommandFillBufferKHR_fn clCommandFillBufferKHRCache;
171167
clEnqueueCommandBufferKHR_fn clEnqueueCommandBufferKHRCache;
172168
};
173-
}
169+
} // namespace cl_adapter
174170

175171
struct ur_platform_handle_t_ {
176-
using native_type = cl_platform_id;
177-
native_type Platform;
178-
std::unique_ptr<cl_adapter::ExtFuncPtrT> ExtFuncPtr;
179-
180-
ur_platform_handle_t_(native_type Plat):Platform(Plat) {
181-
std::make_unique<cl_adapter::ExtFuncPtrT>();
182-
}
183-
184-
~ur_platform_handle_t_() {
185-
ExtFuncPtr.reset();
186-
}
187-
188-
ur_result_t getPlatformVersion(oclv::OpenCLVersion &Version);
189-
190-
template <typename T>
191-
ur_result_t getExtFunc(T CachedExtFunc, const char *FuncName, T *Fptr) {
192-
if (!CachedExtFunc) {
193-
// TODO: check that the function is available
194-
CachedExtFunc = reinterpret_cast<T>(
195-
clGetExtensionFunctionAddressForPlatform(Platform, FuncName));
196-
if (!CachedExtFunc) {
197-
return UR_RESULT_ERROR_INVALID_VALUE;
198-
}
199-
}
200-
*Fptr = CachedExtFunc;
201-
return UR_RESULT_SUCCESS;
172+
using native_type = cl_platform_id;
173+
native_type Platform;
174+
std::unique_ptr<cl_adapter::ExtFuncPtrT> ExtFuncPtr;
175+
176+
ur_platform_handle_t_(native_type Plat) : Platform(Plat) {
177+
std::make_unique<cl_adapter::ExtFuncPtrT>();
178+
}
179+
180+
~ur_platform_handle_t_() { ExtFuncPtr.reset(); }
181+
182+
template <typename T>
183+
ur_result_t getExtFunc(T CachedExtFunc, const char *FuncName, T *Fptr) {
184+
if (!CachedExtFunc) {
185+
// TODO: check that the function is available
186+
CachedExtFunc = reinterpret_cast<T>(
187+
clGetExtensionFunctionAddressForPlatform(Platform, FuncName));
188+
if (!CachedExtFunc) {
189+
return UR_RESULT_ERROR_INVALID_VALUE;
190+
}
202191
}
192+
*Fptr = CachedExtFunc;
193+
return UR_RESULT_SUCCESS;
194+
}
203195

204-
native_type get() { return Platform; }
196+
native_type get() { return Platform; }
205197
};

source/adapters/opencl/program.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinary(
120120
const uint8_t *pBinary, const ur_program_properties_t *,
121121
ur_program_handle_t *phProgram) {
122122

123-
const cl_device_id Devices[1] = {cl_adapter::cast<cl_device_id>(hDevice)};
123+
const cl_device_id Devices[1] = {hDevice->get()};
124124
const size_t Lengths[1] = {size};
125125
cl_int BinaryStatus[1];
126126
cl_int CLResult;
@@ -280,17 +280,16 @@ urProgramGetBuildInfo(ur_program_handle_t hProgram, ur_device_handle_t hDevice,
280280
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);
281281
cl_program_binary_type BinaryType;
282282
CL_RETURN_ON_FAILURE(clGetProgramBuildInfo(
283-
cl_adapter::cast<cl_program>(hProgram),
284-
cl_adapter::cast<cl_device_id>(hDevice),
283+
cl_adapter::cast<cl_program>(hProgram), hDevice->get(),
285284
mapURProgramBuildInfoToCL(propName), sizeof(cl_program_binary_type),
286285
&BinaryType, nullptr));
287286
return ReturnValue(mapCLBinaryTypeToUR(BinaryType));
288287
}
289288
size_t CheckPropSize = 0;
290-
cl_int ClErr = clGetProgramBuildInfo(cl_adapter::cast<cl_program>(hProgram),
291-
cl_adapter::cast<cl_device_id>(hDevice),
292-
mapURProgramBuildInfoToCL(propName),
293-
propSize, pPropValue, &CheckPropSize);
289+
cl_int ClErr =
290+
clGetProgramBuildInfo(cl_adapter::cast<cl_program>(hProgram),
291+
hDevice->get(), mapURProgramBuildInfoToCL(propName),
292+
propSize, pPropValue, &CheckPropSize);
294293
if (pPropValue && CheckPropSize != propSize) {
295294
return UR_RESULT_ERROR_INVALID_SIZE;
296295
}
@@ -472,9 +471,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramGetFunctionPointer(
472471
}
473472

474473
const cl_int CLResult =
475-
FuncT(cl_adapter::cast<cl_device_id>(hDevice),
476-
cl_adapter::cast<cl_program>(hProgram), pFunctionName,
477-
reinterpret_cast<cl_ulong *>(ppFunctionPointer));
474+
FuncT(hDevice->get(), cl_adapter::cast<cl_program>(hProgram),
475+
pFunctionName, reinterpret_cast<cl_ulong *>(ppFunctionPointer));
478476
// GPU runtime sometimes returns CL_INVALID_ARG_VALUE if the function address
479477
// cannot be found but the kernel exists. As the kernel does exist, return
480478
// that the function name is invalid.

source/adapters/opencl/queue.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===-----------------------------------------------------------------===//
88

99
#include "common.hpp"
10+
#include "device.hpp"
1011
#include "platform.hpp"
1112

1213
cl_command_queue_info mapURQueueInfoToCL(const ur_queue_info_t PropName) {
@@ -73,9 +74,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreate(
7374

7475
cl_platform_id CurPlatform;
7576
CL_RETURN_ON_FAILURE_AND_SET_NULL(
76-
clGetDeviceInfo(cl_adapter::cast<cl_device_id>(hDevice),
77-
CL_DEVICE_PLATFORM, sizeof(cl_platform_id), &CurPlatform,
78-
nullptr),
77+
clGetDeviceInfo(hDevice->get(), CL_DEVICE_PLATFORM,
78+
sizeof(cl_platform_id), &CurPlatform, nullptr),
7979
phQueue);
8080

8181
cl_command_queue_properties CLProperties =
@@ -93,10 +93,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreate(
9393
cl_int RetErr = CL_INVALID_OPERATION;
9494

9595
if (Version < oclv::V2_0) {
96-
*phQueue = cl_adapter::cast<ur_queue_handle_t>(
97-
clCreateCommandQueue(cl_adapter::cast<cl_context>(hContext),
98-
cl_adapter::cast<cl_device_id>(hDevice),
99-
CLProperties & SupportByOpenCL, &RetErr));
96+
*phQueue = cl_adapter::cast<ur_queue_handle_t>(clCreateCommandQueue(
97+
cl_adapter::cast<cl_context>(hContext), hDevice->get(),
98+
CLProperties & SupportByOpenCL, &RetErr));
10099
CL_RETURN_ON_FAILURE(RetErr);
101100
return UR_RESULT_SUCCESS;
102101
}
@@ -106,9 +105,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreate(
106105
CL_QUEUE_PROPERTIES, CLProperties & SupportByOpenCL, 0};
107106
*phQueue =
108107
cl_adapter::cast<ur_queue_handle_t>(clCreateCommandQueueWithProperties(
109-
cl_adapter::cast<cl_context>(hContext),
110-
cl_adapter::cast<cl_device_id>(hDevice), CreationFlagProperties,
111-
&RetErr));
108+
cl_adapter::cast<cl_context>(hContext), hDevice->get(),
109+
CreationFlagProperties, &RetErr));
112110
CL_RETURN_ON_FAILURE(RetErr);
113111
return UR_RESULT_SUCCESS;
114112
}

source/adapters/opencl/usm.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "common.hpp"
12+
#include "device.hpp"
1213

1314
UR_APIEXPORT ur_result_t UR_APICALL
1415
urUSMHostAlloc(ur_context_handle_t hContext, const ur_usm_desc_t *pUSMDesc,
@@ -103,7 +104,7 @@ urUSMDeviceAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,
103104

104105
if (FuncPtr) {
105106
cl_int ClResult = CL_SUCCESS;
106-
Ptr = FuncPtr(CLContext, cl_adapter::cast<cl_device_id>(hDevice),
107+
Ptr = FuncPtr(CLContext, hDevice->get(),
107108
cl_adapter::cast<cl_mem_properties_intel *>(Properties), size,
108109
Alignment, &ClResult);
109110
if (ClResult == CL_INVALID_BUFFER_SIZE) {
@@ -171,7 +172,7 @@ urUSMSharedAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,
171172

172173
if (FuncPtr) {
173174
cl_int ClResult = CL_SUCCESS;
174-
Ptr = FuncPtr(CLContext, cl_adapter::cast<cl_device_id>(hDevice),
175+
Ptr = FuncPtr(CLContext, hDevice->get(),
175176
cl_adapter::cast<cl_mem_properties_intel *>(Properties), size,
176177
Alignment, cl_adapter::cast<cl_int *>(&ClResult));
177178
if (ClResult == CL_INVALID_BUFFER_SIZE) {

0 commit comments

Comments
 (0)