6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
#include " CL/opencl.h"
9
- #include < CL/sycl/detail/pi.hpp >
9
+ #include < CL/sycl/detail/pi.h >
10
10
#include < cassert>
11
11
#include < cstring>
12
-
13
- namespace PI = cl::sycl::detail::pi;
14
-
15
- #define CHECK_ERR_SET_NULL_RET (err,ptr,reterr ) \
16
- if (err != CL_SUCCESS) { \
17
- if (ptr != nullptr ) \
18
- *ptr = nullptr ; \
19
- return PI:: cast<pi_result>(reterr); \
12
+ # include < string >
13
+ # include < vector >
14
+
15
+ #define CHECK_ERR_SET_NULL_RET (err, ptr, reterr ) \
16
+ if (err != CL_SUCCESS) { \
17
+ if (ptr != nullptr ) \
18
+ *ptr = nullptr ; \
19
+ return cast<pi_result>(reterr); \
20
20
}
21
21
22
+ // Want all the needed casts be explicit, do not define conversion operators.
23
+ template <class To , class From > To cast (From value) {
24
+ // TODO: see if more sanity checks are possible.
25
+ static_assert (sizeof (From) == sizeof (To) && " cast failed size check" );
26
+ return (To)(value);
27
+ }
28
+
22
29
extern " C" {
23
30
24
31
// Convenience macro makes source code search easier
@@ -27,35 +34,35 @@ extern "C" {
27
34
// Example of a PI interface that does not map exactly to an OpenCL one.
28
35
pi_result OCL (piPlatformsGet)(pi_uint32 num_entries, pi_platform *platforms,
29
36
pi_uint32 *num_platforms) {
30
- cl_int result = clGetPlatformIDs (PI:: cast<cl_uint>(num_entries),
31
- PI:: cast<cl_platform_id *>(platforms),
32
- PI:: cast<cl_uint *>(num_platforms));
37
+ cl_int result = clGetPlatformIDs (cast<cl_uint>(num_entries),
38
+ cast<cl_platform_id *>(platforms),
39
+ cast<cl_uint *>(num_platforms));
33
40
34
41
// Absorb the CL_PLATFORM_NOT_FOUND_KHR and just return 0 in num_platforms
35
42
if (result == CL_PLATFORM_NOT_FOUND_KHR) {
36
43
assert (num_platforms != 0 );
37
44
*num_platforms = 0 ;
38
45
result = PI_SUCCESS;
39
46
}
40
- return PI::cast <pi_result>(result);
47
+ return static_cast <pi_result>(result);
41
48
}
42
49
43
50
// Example of a PI interface that does not map exactly to an OpenCL one.
44
51
pi_result OCL (piDevicesGet)(pi_platform platform, pi_device_type device_type,
45
52
pi_uint32 num_entries, pi_device *devices,
46
53
pi_uint32 *num_devices) {
47
54
cl_int result = clGetDeviceIDs (
48
- PI:: cast<cl_platform_id>(platform), PI:: cast<cl_device_type>(device_type),
49
- PI:: cast<cl_uint>(num_entries), PI:: cast<cl_device_id *>(devices),
50
- PI:: cast<cl_uint *>(num_devices));
55
+ cast<cl_platform_id>(platform), cast<cl_device_type>(device_type),
56
+ cast<cl_uint>(num_entries), cast<cl_device_id *>(devices),
57
+ cast<cl_uint *>(num_devices));
51
58
52
59
// Absorb the CL_DEVICE_NOT_FOUND and just return 0 in num_devices
53
60
if (result == CL_DEVICE_NOT_FOUND) {
54
61
assert (num_devices != 0 );
55
62
*num_devices = 0 ;
56
63
result = PI_SUCCESS;
57
64
}
58
- return PI:: cast<pi_result>(result);
65
+ return cast<pi_result>(result);
59
66
}
60
67
61
68
pi_result OCL (piextDeviceSelectBinary)(
@@ -82,69 +89,67 @@ pi_result OCL(piQueueCreate)(pi_context context, pi_device device,
82
89
83
90
cl_platform_id curPlatform;
84
91
cl_int ret_err =
85
- clGetDeviceInfo (PI:: cast<cl_device_id>(device), CL_DEVICE_PLATFORM,
92
+ clGetDeviceInfo (cast<cl_device_id>(device), CL_DEVICE_PLATFORM,
86
93
sizeof (cl_platform_id), &curPlatform, NULL );
87
94
88
- CHECK_ERR_SET_NULL_RET (ret_err,queue,ret_err);
95
+ CHECK_ERR_SET_NULL_RET (ret_err, queue, ret_err);
89
96
90
97
size_t platVerSize;
91
98
ret_err = clGetPlatformInfo (curPlatform, CL_PLATFORM_VERSION, 0 , NULL ,
92
99
&platVerSize);
93
100
94
- CHECK_ERR_SET_NULL_RET (ret_err,queue,ret_err);
101
+ CHECK_ERR_SET_NULL_RET (ret_err, queue, ret_err);
95
102
96
103
std::string platVer (platVerSize, ' \0 ' );
97
104
ret_err = clGetPlatformInfo (curPlatform, CL_PLATFORM_VERSION, platVerSize,
98
105
&platVer.front (), NULL );
99
106
100
- CHECK_ERR_SET_NULL_RET (ret_err,queue,ret_err);
107
+ CHECK_ERR_SET_NULL_RET (ret_err, queue, ret_err);
101
108
102
109
if (platVer.find (" OpenCL 1.0" ) != std::string::npos ||
103
110
platVer.find (" OpenCL 1.1" ) != std::string::npos ||
104
111
platVer.find (" OpenCL 1.2" ) != std::string::npos) {
105
- *queue = PI:: cast<pi_queue>(clCreateCommandQueue (
106
- PI:: cast<cl_context>(context), PI:: cast<cl_device_id>(device),
107
- PI:: cast<cl_command_queue_properties>(properties), &ret_err));
108
- return PI:: cast<pi_result>(ret_err);
112
+ *queue = cast<pi_queue>(clCreateCommandQueue (
113
+ cast<cl_context>(context), cast<cl_device_id>(device),
114
+ cast<cl_command_queue_properties>(properties), &ret_err));
115
+ return cast<pi_result>(ret_err);
109
116
}
110
117
111
118
cl_queue_properties CreationFlagProperties[] = {
112
- CL_QUEUE_PROPERTIES, PI::cast<cl_command_queue_properties>(properties),
113
- 0 };
114
- *queue = PI::cast<pi_queue>(clCreateCommandQueueWithProperties (
115
- PI::cast<cl_context>(context), PI::cast<cl_device_id>(device),
119
+ CL_QUEUE_PROPERTIES, cast<cl_command_queue_properties>(properties), 0 };
120
+ *queue = cast<pi_queue>(clCreateCommandQueueWithProperties (
121
+ cast<cl_context>(context), cast<cl_device_id>(device),
116
122
CreationFlagProperties, &ret_err));
117
- return PI:: cast<pi_result>(ret_err);
123
+ return cast<pi_result>(ret_err);
118
124
}
119
125
120
126
pi_result OCL (piProgramCreate)(pi_context context, const void *il,
121
127
size_t length, pi_program *res_program) {
122
128
123
129
size_t deviceCount;
124
130
125
- cl_int ret_err = clGetContextInfo (PI:: cast<cl_context>(context),
131
+ cl_int ret_err = clGetContextInfo (cast<cl_context>(context),
126
132
CL_CONTEXT_DEVICES, 0 , NULL , &deviceCount);
127
133
128
134
std::vector<cl_device_id> devicesInCtx (deviceCount);
129
-
130
-
135
+
131
136
if (ret_err != CL_SUCCESS || deviceCount < 1 ) {
132
137
if (res_program != nullptr )
133
138
*res_program = nullptr ;
134
- return PI:: cast<pi_result>(CL_INVALID_CONTEXT);
139
+ return cast<pi_result>(CL_INVALID_CONTEXT);
135
140
}
136
141
137
- ret_err = clGetContextInfo (PI:: cast<cl_context>(context), CL_CONTEXT_DEVICES,
142
+ ret_err = clGetContextInfo (cast<cl_context>(context), CL_CONTEXT_DEVICES,
138
143
deviceCount * sizeof (cl_device_id),
139
144
devicesInCtx.data (), NULL );
140
145
141
- CHECK_ERR_SET_NULL_RET (ret_err,res_program,CL_INVALID_CONTEXT);
146
+ CHECK_ERR_SET_NULL_RET (ret_err, res_program, CL_INVALID_CONTEXT);
142
147
143
148
cl_platform_id curPlatform;
144
149
ret_err = clGetDeviceInfo (devicesInCtx[0 ], CL_DEVICE_PLATFORM,
145
150
sizeof (cl_platform_id), &curPlatform, NULL );
146
151
147
- CHECK_ERR_SET_NULL_RET (ret_err,res_program,CL_INVALID_CONTEXT);
152
+ CHECK_ERR_SET_NULL_RET (ret_err, res_program, CL_INVALID_CONTEXT);
148
153
149
154
size_t devVerSize;
150
155
ret_err =
@@ -153,16 +158,16 @@ pi_result OCL(piProgramCreate)(pi_context context, const void *il,
153
158
ret_err = clGetPlatformInfo (curPlatform, CL_PLATFORM_VERSION, devVerSize,
154
159
&devVer.front (), NULL );
155
160
156
- CHECK_ERR_SET_NULL_RET (ret_err,res_program,CL_INVALID_CONTEXT);
161
+ CHECK_ERR_SET_NULL_RET (ret_err, res_program, CL_INVALID_CONTEXT);
157
162
158
163
pi_result err = PI_SUCCESS;
159
164
if (devVer.find (" OpenCL 1.0" ) == std::string::npos &&
160
165
devVer.find (" OpenCL 1.1" ) == std::string::npos &&
161
166
devVer.find (" OpenCL 1.2" ) == std::string::npos &&
162
167
devVer.find (" OpenCL 2.0" ) == std::string::npos) {
163
168
if (res_program != nullptr )
164
- *res_program = PI:: cast<pi_program>(clCreateProgramWithIL (
165
- PI:: cast<cl_context>(context), il, length, PI:: cast<cl_int *>(&err)));
169
+ *res_program = cast<pi_program>(clCreateProgramWithIL (
170
+ cast<cl_context>(context), il, length, cast<cl_int *>(&err)));
166
171
return err;
167
172
}
168
173
@@ -177,7 +182,7 @@ pi_result OCL(piProgramCreate)(pi_context context, const void *il,
177
182
extStr.find (" cl_khr_il_program" ) == std::string::npos) {
178
183
if (res_program != nullptr )
179
184
*res_program = nullptr ;
180
- return PI:: cast<pi_result>(CL_INVALID_CONTEXT);
185
+ return cast<pi_result>(CL_INVALID_CONTEXT);
181
186
}
182
187
183
188
using apiFuncT =
@@ -188,10 +193,10 @@ pi_result OCL(piProgramCreate)(pi_context context, const void *il,
188
193
189
194
assert (funcPtr != nullptr );
190
195
if (res_program != nullptr )
191
- *res_program = PI:: cast<pi_program>( funcPtr (
192
- PI:: cast<cl_context>(context), il, length, PI:: cast<cl_int *>(&err)));
196
+ *res_program = cast<pi_program>(
197
+ funcPtr ( cast<cl_context>(context), il, length, cast<cl_int *>(&err)));
193
198
else
194
- err = PI_INVALID_VALUE;
199
+ err = PI_INVALID_VALUE;
195
200
196
201
return err;
197
202
}
@@ -221,9 +226,9 @@ pi_result OCL(piSamplerCreate)(pi_context context,
221
226
}
222
227
223
228
// Always call OpenCL 1.0 API
224
- *result_sampler = PI:: cast<pi_sampler>( clCreateSampler (
225
- PI:: cast<cl_context>(context), normalizedCoords, addressingMode ,
226
- filterMode, PI:: cast<cl_int *>(&error_code)));
229
+ *result_sampler = cast<pi_sampler>(
230
+ clCreateSampler ( cast<cl_context>(context), normalizedCoords,
231
+ addressingMode, filterMode, cast<cl_int *>(&error_code)));
227
232
return error_code;
228
233
}
229
234
@@ -233,11 +238,11 @@ pi_result OCL(piextGetDeviceFunctionPointer)(pi_device device,
233
238
pi_uint64 *function_pointer_ret) {
234
239
pi_platform platform;
235
240
cl_int ret_err =
236
- clGetDeviceInfo (PI:: cast<cl_device_id>(device), PI_DEVICE_INFO_PLATFORM,
241
+ clGetDeviceInfo (cast<cl_device_id>(device), PI_DEVICE_INFO_PLATFORM,
237
242
sizeof (platform), &platform, nullptr );
238
243
239
244
if (ret_err != CL_SUCCESS) {
240
- return PI:: cast<pi_result>(ret_err);
245
+ return cast<pi_result>(ret_err);
241
246
}
242
247
243
248
using FuncT =
@@ -246,8 +251,7 @@ pi_result OCL(piextGetDeviceFunctionPointer)(pi_device device,
246
251
// TODO: add check that device supports corresponding extension
247
252
FuncT func_ptr =
248
253
reinterpret_cast <FuncT>(clGetExtensionFunctionAddressForPlatform (
249
- PI::cast<cl_platform_id>(platform),
250
- " clGetDeviceFunctionPointerINTEL" ));
254
+ cast<cl_platform_id>(platform), " clGetDeviceFunctionPointerINTEL" ));
251
255
// TODO: once we have check that device supports corresponding extension,
252
256
// we can insert an assertion that func_ptr is not nullptr. For now, let's
253
257
// just return an error if failed to query such function
@@ -261,15 +265,15 @@ pi_result OCL(piextGetDeviceFunctionPointer)(pi_device device,
261
265
return PI_INVALID_DEVICE;
262
266
}
263
267
264
- return PI:: cast<pi_result>(func_ptr (PI:: cast<cl_device_id>(device),
265
- PI:: cast<cl_program>(program), func_name,
266
- function_pointer_ret));
268
+ return cast<pi_result>(func_ptr (cast<cl_device_id>(device),
269
+ cast<cl_program>(program), func_name,
270
+ function_pointer_ret));
267
271
}
268
272
269
273
// TODO: Remove the 'OclPtr' extension used with the PI_APIs.
270
274
// Forward calls to OpenCL RT.
271
275
#define _PI_CL (pi_api, ocl_api ) \
272
- decltype (::pi_api) *pi_api##OclPtr = PI:: cast<decltype (&::pi_api)>(&ocl_api);
276
+ decltype (::pi_api) *pi_api##OclPtr = cast<decltype (&::pi_api)>(&ocl_api);
273
277
274
278
// Platform
275
279
_PI_CL (piPlatformsGet, OCL(piPlatformsGet))
0 commit comments