Skip to content

Commit 844eccb

Browse files
committed
[SYCL] removal of dependency on pi.hpp file in opencl
plugin:pi_opencl.cpp - changed the dependency to pi.h, which contains declarations and no other defined macros or defintions. Signed-off-by: Garima Gupta <garima.gupta@intel.com>
1 parent 229246d commit 844eccb

File tree

5 files changed

+80
-71
lines changed

5 files changed

+80
-71
lines changed

sycl/include/CL/sycl/detail/pi.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ namespace RT = cl::sycl::detail::pi;
184184
// Want all the needed casts be explicit, do not define conversion operators.
185185
template <class To, class From> To pi::cast(From value) {
186186
// TODO: see if more sanity checks are possible.
187-
assert(sizeof(From) == sizeof(To) && "cast failed size check");
187+
PI_ASSERT(sizeof(From) == sizeof(To), "cast failed size check");
188188
return (To)(value);
189189
}
190190

sycl/plugins/opencl/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#TODO: Currently, the pi.hpp header is common between sycl and plugin library sources.
1111
#This can be changed by copying the pi.hpp file in the plugins project.
1212
add_library(pi_opencl SHARED
13-
"${sycl_inc_dir}/CL/sycl/detail/pi.hpp"
13+
"${sycl_inc_dir}/CL/sycl/detail/pi.h"
1414
"pi_opencl.cpp"
1515
)
1616

sycl/plugins/opencl/pi_opencl.cpp

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,26 @@
66
//
77
//===----------------------------------------------------------------------===//
88
#include "CL/opencl.h"
9-
#include <CL/sycl/detail/pi.hpp>
9+
#include <CL/sycl/detail/pi.h>
1010
#include <cassert>
1111
#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); \
2020
}
2121

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+
2229
extern "C" {
2330

2431
// Convenience macro makes source code search easier
@@ -27,35 +34,35 @@ extern "C" {
2734
// Example of a PI interface that does not map exactly to an OpenCL one.
2835
pi_result OCL(piPlatformsGet)(pi_uint32 num_entries, pi_platform *platforms,
2936
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));
3340

3441
// Absorb the CL_PLATFORM_NOT_FOUND_KHR and just return 0 in num_platforms
3542
if (result == CL_PLATFORM_NOT_FOUND_KHR) {
3643
assert(num_platforms != 0);
3744
*num_platforms = 0;
3845
result = PI_SUCCESS;
3946
}
40-
return PI::cast<pi_result>(result);
47+
return static_cast<pi_result>(result);
4148
}
4249

4350
// Example of a PI interface that does not map exactly to an OpenCL one.
4451
pi_result OCL(piDevicesGet)(pi_platform platform, pi_device_type device_type,
4552
pi_uint32 num_entries, pi_device *devices,
4653
pi_uint32 *num_devices) {
4754
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));
5158

5259
// Absorb the CL_DEVICE_NOT_FOUND and just return 0 in num_devices
5360
if (result == CL_DEVICE_NOT_FOUND) {
5461
assert(num_devices != 0);
5562
*num_devices = 0;
5663
result = PI_SUCCESS;
5764
}
58-
return PI::cast<pi_result>(result);
65+
return cast<pi_result>(result);
5966
}
6067

6168
pi_result OCL(piextDeviceSelectBinary)(
@@ -82,69 +89,67 @@ pi_result OCL(piQueueCreate)(pi_context context, pi_device device,
8289

8390
cl_platform_id curPlatform;
8491
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,
8693
sizeof(cl_platform_id), &curPlatform, NULL);
8794

88-
CHECK_ERR_SET_NULL_RET(ret_err,queue,ret_err);
95+
CHECK_ERR_SET_NULL_RET(ret_err, queue, ret_err);
8996

9097
size_t platVerSize;
9198
ret_err = clGetPlatformInfo(curPlatform, CL_PLATFORM_VERSION, 0, NULL,
9299
&platVerSize);
93100

94-
CHECK_ERR_SET_NULL_RET(ret_err,queue,ret_err);
101+
CHECK_ERR_SET_NULL_RET(ret_err, queue, ret_err);
95102

96103
std::string platVer(platVerSize, '\0');
97104
ret_err = clGetPlatformInfo(curPlatform, CL_PLATFORM_VERSION, platVerSize,
98105
&platVer.front(), NULL);
99106

100-
CHECK_ERR_SET_NULL_RET(ret_err,queue,ret_err);
107+
CHECK_ERR_SET_NULL_RET(ret_err, queue, ret_err);
101108

102109
if (platVer.find("OpenCL 1.0") != std::string::npos ||
103110
platVer.find("OpenCL 1.1") != std::string::npos ||
104111
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);
109116
}
110117

111118
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),
116122
CreationFlagProperties, &ret_err));
117-
return PI::cast<pi_result>(ret_err);
123+
return cast<pi_result>(ret_err);
118124
}
119125

120126
pi_result OCL(piProgramCreate)(pi_context context, const void *il,
121127
size_t length, pi_program *res_program) {
122128

123129
size_t deviceCount;
124130

125-
cl_int ret_err = clGetContextInfo(PI::cast<cl_context>(context),
131+
cl_int ret_err = clGetContextInfo(cast<cl_context>(context),
126132
CL_CONTEXT_DEVICES, 0, NULL, &deviceCount);
127133

128134
std::vector<cl_device_id> devicesInCtx(deviceCount);
129-
130-
135+
131136
if (ret_err != CL_SUCCESS || deviceCount < 1) {
132137
if (res_program != nullptr)
133138
*res_program = nullptr;
134-
return PI::cast<pi_result>(CL_INVALID_CONTEXT);
139+
return cast<pi_result>(CL_INVALID_CONTEXT);
135140
}
136141

137-
ret_err = clGetContextInfo(PI::cast<cl_context>(context), CL_CONTEXT_DEVICES,
142+
ret_err = clGetContextInfo(cast<cl_context>(context), CL_CONTEXT_DEVICES,
138143
deviceCount * sizeof(cl_device_id),
139144
devicesInCtx.data(), NULL);
140145

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);
142147

143148
cl_platform_id curPlatform;
144149
ret_err = clGetDeviceInfo(devicesInCtx[0], CL_DEVICE_PLATFORM,
145150
sizeof(cl_platform_id), &curPlatform, NULL);
146151

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);
148153

149154
size_t devVerSize;
150155
ret_err =
@@ -153,16 +158,16 @@ pi_result OCL(piProgramCreate)(pi_context context, const void *il,
153158
ret_err = clGetPlatformInfo(curPlatform, CL_PLATFORM_VERSION, devVerSize,
154159
&devVer.front(), NULL);
155160

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);
157162

158163
pi_result err = PI_SUCCESS;
159164
if (devVer.find("OpenCL 1.0") == std::string::npos &&
160165
devVer.find("OpenCL 1.1") == std::string::npos &&
161166
devVer.find("OpenCL 1.2") == std::string::npos &&
162167
devVer.find("OpenCL 2.0") == std::string::npos) {
163168
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)));
166171
return err;
167172
}
168173

@@ -177,7 +182,7 @@ pi_result OCL(piProgramCreate)(pi_context context, const void *il,
177182
extStr.find("cl_khr_il_program") == std::string::npos) {
178183
if (res_program != nullptr)
179184
*res_program = nullptr;
180-
return PI::cast<pi_result>(CL_INVALID_CONTEXT);
185+
return cast<pi_result>(CL_INVALID_CONTEXT);
181186
}
182187

183188
using apiFuncT =
@@ -188,10 +193,10 @@ pi_result OCL(piProgramCreate)(pi_context context, const void *il,
188193

189194
assert(funcPtr != nullptr);
190195
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)));
193198
else
194-
err = PI_INVALID_VALUE;
199+
err = PI_INVALID_VALUE;
195200

196201
return err;
197202
}
@@ -221,9 +226,9 @@ pi_result OCL(piSamplerCreate)(pi_context context,
221226
}
222227

223228
// 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)));
227232
return error_code;
228233
}
229234

@@ -233,11 +238,11 @@ pi_result OCL(piextGetDeviceFunctionPointer)(pi_device device,
233238
pi_uint64 *function_pointer_ret) {
234239
pi_platform platform;
235240
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,
237242
sizeof(platform), &platform, nullptr);
238243

239244
if (ret_err != CL_SUCCESS) {
240-
return PI::cast<pi_result>(ret_err);
245+
return cast<pi_result>(ret_err);
241246
}
242247

243248
using FuncT =
@@ -246,8 +251,7 @@ pi_result OCL(piextGetDeviceFunctionPointer)(pi_device device,
246251
// TODO: add check that device supports corresponding extension
247252
FuncT func_ptr =
248253
reinterpret_cast<FuncT>(clGetExtensionFunctionAddressForPlatform(
249-
PI::cast<cl_platform_id>(platform),
250-
"clGetDeviceFunctionPointerINTEL"));
254+
cast<cl_platform_id>(platform), "clGetDeviceFunctionPointerINTEL"));
251255
// TODO: once we have check that device supports corresponding extension,
252256
// we can insert an assertion that func_ptr is not nullptr. For now, let's
253257
// just return an error if failed to query such function
@@ -261,15 +265,15 @@ pi_result OCL(piextGetDeviceFunctionPointer)(pi_device device,
261265
return PI_INVALID_DEVICE;
262266
}
263267

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));
267271
}
268272

269273
// TODO: Remove the 'OclPtr' extension used with the PI_APIs.
270274
// Forward calls to OpenCL RT.
271275
#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);
273277

274278
// Platform
275279
_PI_CL(piPlatformsGet, OCL(piPlatformsGet))

sycl/source/detail/pi.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ std::string platformInfoToString(pi_platform_info info) {
3838
// Check for manually selected BE at run-time.
3939
bool useBackend(Backend TheBackend) {
4040
static const char *GetEnv = std::getenv("SYCL_BE");
41-
static const Backend Use = std::map<std::string, Backend>{
42-
{"PI_OPENCL", SYCL_BE_PI_OPENCL}, {"PI_OTHER", SYCL_BE_PI_OTHER}
43-
// Any other value would yield PI_OPENCL (current default)
44-
}[GetEnv ? GetEnv : "PI_OPENCL"];
41+
// Current default backend as SYCL_BE_PI_OPENCL
42+
// Valid values of GetEnv are "PI_OPENCL" and "PI_OTHER"
43+
std::string StringGetEnv = (GetEnv ? GetEnv : "PI_OPENCL");
44+
static const Backend Use =
45+
(StringGetEnv == "PI_OTHER" ? SYCL_BE_PI_OTHER : SYCL_BE_PI_OPENCL);
4546
return TheBackend == Use;
4647
}
4748

@@ -53,17 +54,19 @@ bool useBackend(Backend TheBackend) {
5354
// Find the plugin at the appropriate location and return the location in
5455
// PluginPath
5556
// TODO: Change the function appropriately when there are multiple plugins.
56-
bool findPlugin(std::string &PluginPath) {
57+
std::string findPlugin() {
5758
// TODO: Based on final design discussions, change the location where the
5859
// plugin must be searched; how to identify the plugins etc. Currently the
59-
// search is done for libpi_opencl.so file in LD_LIBRARY_PATH env only.
60-
PluginPath = PLUGIN_NAME;
61-
return true;
60+
// search is done for libpi_opencl.so/pi_opencl.dll file in LD_LIBRARY_PATH
61+
// env only.
62+
return PLUGIN_NAME;
6263
}
6364

6465
// Load the Plugin by calling the OS dependent library loading call.
6566
// Return the handle to the Library.
66-
void *loadPlugin(const std::string &PluginPath) { return loadOsLibrary(PluginPath); }
67+
void *loadPlugin(const std::string &PluginPath) {
68+
return loadOsLibrary(PluginPath);
69+
}
6770

6871
// Binds all the PI Interface APIs to Plugin Library Function Addresses.
6972
// TODO: Remove the 'OclPtr' extension to PI_API.
@@ -100,17 +103,20 @@ void initialize() {
100103
die("Unknown SYCL_BE");
101104
}
102105

103-
std::string PluginPath;
104-
if (!findPlugin(PluginPath))
106+
std::string PluginPath = findPlugin();
107+
if (PluginPath.empty())
105108
die("Plugin Not Found.");
106109

107110
void *Library = loadPlugin(PluginPath);
108111
if (!Library) {
109-
die("Failed to load plugin. Check if plugin is present.");
112+
std::string Message =
113+
"Check if plugin is present. Failed to load plugin: " + PluginPath;
114+
die(Message.c_str());
110115
}
111116

112117
if (!bindPlugin(Library)) {
113-
die("Failed to bind PI APIs to the plugin.");
118+
std::string Message = "Failed to bind PI APIs to the plugin: " + PluginPath;
119+
die(Message.c_str());
114120
}
115121

116122
Initialized = true;

sycl/source/detail/windows_pi.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <string>
44

55
void *loadOsLibrary(const std::string &PluginPath) {
6-
// TODO: Check if the option RTLD_NOW is correct.
76
return (void *)LoadLibraryA(PluginPath.c_str());
87
}
98

0 commit comments

Comments
 (0)