Skip to content

Commit 0430133

Browse files
committed
Add platform handle to opencl
1 parent fe2735a commit 0430133

File tree

4 files changed

+220
-15
lines changed

4 files changed

+220
-15
lines changed

source/adapters/opencl/adapter.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters,
2222
uint32_t *pNumAdapters) {
2323
if (NumEntries > 0 && phAdapters) {
2424
std::lock_guard<std::mutex> Lock{adapter.Mutex};
25-
if (adapter.RefCount++ == 0) {
26-
cl_ext::ExtFuncPtrCache = std::make_unique<cl_ext::ExtFuncPtrCacheT>();
27-
}
28-
25+
adapter.RefCount++;
2926
*phAdapters = &adapter;
3027
}
3128

@@ -43,9 +40,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) {
4340

4441
UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) {
4542
std::lock_guard<std::mutex> Lock{adapter.Mutex};
46-
if (--adapter.RefCount == 0) {
47-
cl_ext::ExtFuncPtrCache.reset();
48-
}
43+
--adapter.RefCount;
4944
return UR_RESULT_SUCCESS;
5045
}
5146

@@ -66,7 +61,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t,
6661

6762
switch (propName) {
6863
case UR_ADAPTER_INFO_BACKEND:
69-
return ReturnValue(UR_ADAPTER_BACKEND_CUDA);
64+
return ReturnValue(UR_ADAPTER_BACKEND_OPENCL);
7065
case UR_ADAPTER_INFO_REFERENCE_COUNT:
7166
return ReturnValue(adapter.RefCount.load());
7267
default:

source/adapters/opencl/device.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGet(ur_platform_handle_t hPlatform,
8181
return UR_RESULT_ERROR_INVALID_ENUMERATION;
8282
}
8383

84-
cl_int Result = clGetDeviceIDs(cl_adapter::cast<cl_platform_id>(hPlatform),
84+
cl_int Result = clGetDeviceIDs(hPlatform->get(),
8585
Type, cl_adapter::cast<cl_uint>(NumEntries),
8686
cl_adapter::cast<cl_device_id *>(phDevices),
8787
cl_adapter::cast<cl_uint *>(pNumDevices));

source/adapters/opencl/platform.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ 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+
}
3148

3249
static cl_int mapURPlatformInfoToCL(ur_platform_info_t URPropName) {
3350

@@ -63,7 +80,7 @@ urPlatformGetInfo(ur_platform_handle_t hPlatform, ur_platform_info_t propName,
6380
case UR_PLATFORM_INFO_EXTENSIONS:
6481
case UR_PLATFORM_INFO_PROFILE: {
6582
CL_RETURN_ON_FAILURE(
66-
clGetPlatformInfo(cl_adapter::cast<cl_platform_id>(hPlatform),
83+
clGetPlatformInfo(hPlatform->get(),
6784
CLPropName, propSize, pPropValue, pSizeRet));
6885
return UR_RESULT_SUCCESS;
6986
}
@@ -83,32 +100,37 @@ UR_APIEXPORT ur_result_t UR_APICALL
83100
urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries,
84101
ur_platform_handle_t *phPlatforms, uint32_t *pNumPlatforms) {
85102

103+
std::vector<cl_platform_id> CLPlatforms(NumEntries);
86104
cl_int Result =
87105
clGetPlatformIDs(cl_adapter::cast<cl_uint>(NumEntries),
88-
cl_adapter::cast<cl_platform_id *>(phPlatforms),
106+
CLPlatforms.data(),
89107
cl_adapter::cast<cl_uint *>(pNumPlatforms));
90-
91108
/* Absorb the CL_PLATFORM_NOT_FOUND_KHR and just return 0 in num_platforms */
92109
if (Result == CL_PLATFORM_NOT_FOUND_KHR) {
93110
Result = CL_SUCCESS;
94111
if (pNumPlatforms) {
95112
*pNumPlatforms = 0;
96113
}
97114
}
98-
115+
if (NumEntries) {
116+
for (uint32_t i = 0; i < NumEntries; i++) {
117+
phPlatforms[i] = new ur_platform_handle_t_(CLPlatforms[i]);
118+
}
119+
}
99120
return mapCLErrorToUR(Result);
100121
}
101122

102123
UR_APIEXPORT ur_result_t UR_APICALL urPlatformGetNativeHandle(
103124
ur_platform_handle_t hPlatform, ur_native_handle_t *phNativePlatform) {
104-
*phNativePlatform = reinterpret_cast<ur_native_handle_t>(hPlatform);
125+
*phNativePlatform = reinterpret_cast<ur_native_handle_t>(hPlatform->get());
105126
return UR_RESULT_SUCCESS;
106127
}
107128

108129
UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle(
109130
ur_native_handle_t hNativePlatform, const ur_platform_native_properties_t *,
110131
ur_platform_handle_t *phPlatform) {
111-
*phPlatform = reinterpret_cast<ur_platform_handle_t>(hNativePlatform);
132+
cl_platform_id NativeHandle = reinterpret_cast<cl_platform_id>(hNativePlatform);
133+
*phPlatform = new ur_platform_handle_t_(NativeHandle);
112134
return UR_RESULT_SUCCESS;
113135
}
114136

source/adapters/opencl/platform.hpp

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,192 @@
1414
namespace cl_adapter {
1515
ur_result_t getPlatformVersion(cl_platform_id Plat,
1616
oclv::OpenCLVersion &Version);
17+
18+
// Older versions of GCC don't like "const" here
19+
#if defined(__GNUC__) && (__GNUC__ < 7 || (__GNU__C == 7 && __GNUC_MINOR__ < 2))
20+
#define CONSTFIX constexpr
21+
#else
22+
#define CONSTFIX const
23+
#endif
24+
25+
// Names of USM functions that are queried from OpenCL
26+
CONSTFIX char HostMemAllocName[] = "clHostMemAllocINTEL";
27+
CONSTFIX char DeviceMemAllocName[] = "clDeviceMemAllocINTEL";
28+
CONSTFIX char SharedMemAllocName[] = "clSharedMemAllocINTEL";
29+
CONSTFIX char MemBlockingFreeName[] = "clMemBlockingFreeINTEL";
30+
CONSTFIX char CreateBufferWithPropertiesName[] =
31+
"clCreateBufferWithPropertiesINTEL";
32+
CONSTFIX char SetKernelArgMemPointerName[] = "clSetKernelArgMemPointerINTEL";
33+
CONSTFIX char EnqueueMemFillName[] = "clEnqueueMemFillINTEL";
34+
CONSTFIX char EnqueueMemcpyName[] = "clEnqueueMemcpyINTEL";
35+
CONSTFIX char GetMemAllocInfoName[] = "clGetMemAllocInfoINTEL";
36+
CONSTFIX char SetProgramSpecializationConstantName[] =
37+
"clSetProgramSpecializationConstant";
38+
CONSTFIX char GetDeviceFunctionPointerName[] =
39+
"clGetDeviceFunctionPointerINTEL";
40+
CONSTFIX char EnqueueWriteGlobalVariableName[] =
41+
"clEnqueueWriteGlobalVariableINTEL";
42+
CONSTFIX char EnqueueReadGlobalVariableName[] =
43+
"clEnqueueReadGlobalVariableINTEL";
44+
// Names of host pipe functions queried from OpenCL
45+
CONSTFIX char EnqueueReadHostPipeName[] = "clEnqueueReadHostPipeINTEL";
46+
CONSTFIX char EnqueueWriteHostPipeName[] = "clEnqueueWriteHostPipeINTEL";
47+
// Names of command buffer functions queried from OpenCL
48+
CONSTFIX char CreateCommandBufferName[] = "clCreateCommandBufferKHR";
49+
CONSTFIX char RetainCommandBufferName[] = "clRetainCommandBufferKHR";
50+
CONSTFIX char ReleaseCommandBufferName[] = "clReleaseCommandBufferKHR";
51+
CONSTFIX char FinalizeCommandBufferName[] = "clFinalizeCommandBufferKHR";
52+
CONSTFIX char CommandNRRangeKernelName[] = "clCommandNDRangeKernelKHR";
53+
CONSTFIX char CommandCopyBufferName[] = "clCommandCopyBufferKHR";
54+
CONSTFIX char CommandCopyBufferRectName[] = "clCommandCopyBufferRectKHR";
55+
CONSTFIX char CommandFillBufferName[] = "clCommandFillBufferKHR";
56+
CONSTFIX char EnqueueCommandBufferName[] = "clEnqueueCommandBufferKHR";
57+
58+
#undef CONSTFIX
59+
60+
using clGetDeviceFunctionPointer_fn = CL_API_ENTRY
61+
cl_int(CL_API_CALL *)(cl_device_id device, cl_program program,
62+
const char *FuncName, cl_ulong *ret_ptr);
63+
64+
using clEnqueueWriteGlobalVariable_fn = CL_API_ENTRY
65+
cl_int(CL_API_CALL *)(cl_command_queue, cl_program, const char *, cl_bool,
66+
size_t, size_t, const void *, cl_uint, const cl_event *,
67+
cl_event *);
68+
69+
using clEnqueueReadGlobalVariable_fn = CL_API_ENTRY
70+
cl_int(CL_API_CALL *)(cl_command_queue, cl_program, const char *, cl_bool,
71+
size_t, size_t, void *, cl_uint, const cl_event *,
72+
cl_event *);
73+
74+
using clSetProgramSpecializationConstant_fn = CL_API_ENTRY
75+
cl_int(CL_API_CALL *)(cl_program program, cl_uint spec_id, size_t spec_size,
76+
const void *spec_value);
77+
78+
using clEnqueueReadHostPipeINTEL_fn = CL_API_ENTRY
79+
cl_int(CL_API_CALL *)(cl_command_queue queue, cl_program program,
80+
const char *pipe_symbol, cl_bool blocking, void *ptr,
81+
size_t size, cl_uint num_events_in_waitlist,
82+
const cl_event *events_waitlist, cl_event *event);
83+
84+
using clEnqueueWriteHostPipeINTEL_fn = CL_API_ENTRY
85+
cl_int(CL_API_CALL *)(cl_command_queue queue, cl_program program,
86+
const char *pipe_symbol, cl_bool blocking,
87+
const void *ptr, size_t size,
88+
cl_uint num_events_in_waitlist,
89+
const cl_event *events_waitlist, cl_event *event);
90+
91+
using clCreateCommandBufferKHR_fn = CL_API_ENTRY cl_command_buffer_khr(
92+
CL_API_CALL *)(cl_uint num_queues, const cl_command_queue *queues,
93+
const cl_command_buffer_properties_khr *properties,
94+
cl_int *errcode_ret);
95+
96+
using clRetainCommandBufferKHR_fn = CL_API_ENTRY
97+
cl_int(CL_API_CALL *)(cl_command_buffer_khr command_buffer);
98+
99+
using clReleaseCommandBufferKHR_fn = CL_API_ENTRY
100+
cl_int(CL_API_CALL *)(cl_command_buffer_khr command_buffer);
101+
102+
using clFinalizeCommandBufferKHR_fn = CL_API_ENTRY
103+
cl_int(CL_API_CALL *)(cl_command_buffer_khr command_buffer);
104+
105+
using clCommandNDRangeKernelKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)(
106+
cl_command_buffer_khr command_buffer, cl_command_queue command_queue,
107+
const cl_ndrange_kernel_command_properties_khr *properties,
108+
cl_kernel kernel, cl_uint work_dim, const size_t *global_work_offset,
109+
const size_t *global_work_size, const size_t *local_work_size,
110+
cl_uint num_sync_points_in_wait_list,
111+
const cl_sync_point_khr *sync_point_wait_list,
112+
cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle);
113+
114+
using clCommandCopyBufferKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)(
115+
cl_command_buffer_khr command_buffer, cl_command_queue command_queue,
116+
cl_mem src_buffer, cl_mem dst_buffer, size_t src_offset, size_t dst_offset,
117+
size_t size, cl_uint num_sync_points_in_wait_list,
118+
const cl_sync_point_khr *sync_point_wait_list,
119+
cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle);
120+
121+
using clCommandCopyBufferRectKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)(
122+
cl_command_buffer_khr command_buffer, cl_command_queue command_queue,
123+
cl_mem src_buffer, cl_mem dst_buffer, const size_t *src_origin,
124+
const size_t *dst_origin, const size_t *region, size_t src_row_pitch,
125+
size_t src_slice_pitch, size_t dst_row_pitch, size_t dst_slice_pitch,
126+
cl_uint num_sync_points_in_wait_list,
127+
const cl_sync_point_khr *sync_point_wait_list,
128+
cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle);
129+
130+
using clCommandFillBufferKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)(
131+
cl_command_buffer_khr command_buffer, cl_command_queue command_queue,
132+
cl_mem buffer, const void *pattern, size_t pattern_size, size_t offset,
133+
size_t size, cl_uint num_sync_points_in_wait_list,
134+
const cl_sync_point_khr *sync_point_wait_list,
135+
cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle);
136+
137+
using clEnqueueCommandBufferKHR_fn = CL_API_ENTRY
138+
cl_int(CL_API_CALL *)(cl_uint num_queues, cl_command_queue *queues,
139+
cl_command_buffer_khr command_buffer,
140+
cl_uint num_events_in_wait_list,
141+
const cl_event *event_wait_list, cl_event *event);
142+
143+
struct ExtFuncPtrT {
144+
clHostMemAllocINTEL_fn clHostMemAllocINTELCache;
145+
clDeviceMemAllocINTEL_fn clDeviceMemAllocINTELCache;
146+
clSharedMemAllocINTEL_fn clSharedMemAllocINTELCache;
147+
clGetDeviceFunctionPointer_fn clGetDeviceFunctionPointerCache;
148+
clCreateBufferWithPropertiesINTEL_fn
149+
clCreateBufferWithPropertiesINTELCache;
150+
clMemBlockingFreeINTEL_fn clMemBlockingFreeINTELCache;
151+
clSetKernelArgMemPointerINTEL_fn
152+
clSetKernelArgMemPointerINTELCache;
153+
clEnqueueMemFillINTEL_fn clEnqueueMemFillINTELCache;
154+
clEnqueueMemcpyINTEL_fn clEnqueueMemcpyINTELCache;
155+
clGetMemAllocInfoINTEL_fn clGetMemAllocInfoINTELCache;
156+
clEnqueueWriteGlobalVariable_fn
157+
clEnqueueWriteGlobalVariableCache;
158+
clEnqueueReadGlobalVariable_fn clEnqueueReadGlobalVariableCache;
159+
clEnqueueReadHostPipeINTEL_fn clEnqueueReadHostPipeINTELCache;
160+
clEnqueueWriteHostPipeINTEL_fn clEnqueueWriteHostPipeINTELCache;
161+
clSetProgramSpecializationConstant_fn
162+
clSetProgramSpecializationConstantCache;
163+
clCreateCommandBufferKHR_fn clCreateCommandBufferKHRCache;
164+
clRetainCommandBufferKHR_fn clRetainCommandBufferKHRCache;
165+
clReleaseCommandBufferKHR_fn clReleaseCommandBufferKHRCache;
166+
clFinalizeCommandBufferKHR_fn clFinalizeCommandBufferKHRCache;
167+
clCommandNDRangeKernelKHR_fn clCommandNDRangeKernelKHRCache;
168+
clCommandCopyBufferKHR_fn clCommandCopyBufferKHRCache;
169+
clCommandCopyBufferRectKHR_fn clCommandCopyBufferRectKHRCache;
170+
clCommandFillBufferKHR_fn clCommandFillBufferKHRCache;
171+
clEnqueueCommandBufferKHR_fn clEnqueueCommandBufferKHRCache;
172+
};
17173
}
174+
175+
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;
202+
}
203+
204+
native_type get() { return Platform; }
205+
};

0 commit comments

Comments
 (0)