Skip to content

Commit bc01115

Browse files
authored
[SYCL] Fix platform selection in opencl-aot (#1510)
Before picking a matched platform name, ensure the device type also matches. This fixes a mismatch issue that was caused by an identical supported platform name for CPU and GPU. Only iterate through available platforms. When the error message is non-empty, always return CLErr, so that the message is shown. Signed-off-by: Dmitri Mokhov <[email protected]>
1 parent 99fa86f commit bc01115

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

opencl-aot/include/utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ enum Errors : int8_t {
3636
OPENCL_AOT_OPTIONS_COEXISTENCE_FAILURE,
3737
OPENCL_AOT_TARGET_CPU_ARCH_FAILURE,
3838
OPENCL_AOT_DEVICE_ID_IS_EMPTY,
39-
OPENCL_AOT_PROGRAM_IS_EMPTY
39+
OPENCL_AOT_PROGRAM_IS_EMPTY,
40+
OPENCL_AOT_PLATFORM_NOT_FOUND
4041
};
4142

4243
inline bool clFailed(cl_int ReturnCode) { return CL_SUCCESS != ReturnCode; }

opencl-aot/source/utils.cpp

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,24 @@ getOpenCLPlatform(DeviceType Type) {
178178
cl_int CLErr(CL_SUCCESS);
179179
std::string PlatformName;
180180

181-
const cl_uint MaxPlatformsCount = 10;
182-
std::array<cl_platform_id, MaxPlatformsCount> Platforms{};
183-
184181
cl_uint PlatformsCount = 0;
185-
CLErr =
186-
clGetPlatformIDs(MaxPlatformsCount, Platforms.data(), &PlatformsCount);
182+
CLErr = clGetPlatformIDs(0, nullptr, &PlatformsCount);
183+
if (clFailed(CLErr)) {
184+
return std::make_tuple(
185+
nullptr, "",
186+
formatCLError("Failed to retrieve OpenCL platform count", CLErr),
187+
CLErr);
188+
}
189+
190+
std::vector<cl_platform_id> Platforms(PlatformsCount);
191+
CLErr = clGetPlatformIDs(PlatformsCount, Platforms.data(), nullptr);
187192
if (clFailed(CLErr)) {
188193
return std::make_tuple(
189194
nullptr, "",
190195
formatCLError("Failed to retrieve OpenCL platform IDs", CLErr), CLErr);
191196
}
192197

198+
std::string ErrorMessage;
193199
for (const auto &Platform : Platforms) {
194200
size_t PlatformNameLength = 0;
195201
CLErr = clGetPlatformInfo(Platform, CL_PLATFORM_NAME, 0, nullptr,
@@ -221,24 +227,36 @@ getOpenCLPlatform(DeviceType Type) {
221227
std::find(SupportedPlatformNames.begin(), SupportedPlatformNames.end(),
222228
PlatformNameOnLoopIteration);
223229
if (Result != SupportedPlatformNames.end()) {
224-
PlatformId = Platform;
225-
PlatformName = PlatformNameOnLoopIteration;
226-
break;
230+
tie(std::ignore, ErrorMessage, CLErr) = getOpenCLDevice(Platform, Type);
231+
if (!clFailed(CLErr)) {
232+
PlatformId = Platform;
233+
PlatformName = PlatformNameOnLoopIteration;
234+
break;
235+
}
227236
}
228237
}
229238

230-
std::string ErrorMessage;
231-
if (PlatformId == nullptr) {
232-
ErrorMessage += "OpenCL platform ID is empty\n";
233-
}
234-
if (PlatformName.empty()) {
235-
ErrorMessage += "OpenCL platform name is empty\n";
239+
std::string SupportedPlatforms;
240+
for (const auto &Platform : DeviceTypesToSupportedPlatformNames[Type]) {
241+
SupportedPlatforms += " " + Platform + '\n';
236242
}
237-
if (!ErrorMessage.empty()) {
238-
ErrorMessage += "Failed to find any of these OpenCL platforms:\n";
239-
for (const auto &SupportedPlatformName :
240-
DeviceTypesToSupportedPlatformNames[Type]) {
241-
ErrorMessage += " " + SupportedPlatformName + '\n';
243+
if (clFailed(CLErr)) {
244+
std::map<DeviceType, std::string> DeviceTypeToDeviceTypeName{
245+
{cpu, "CPU"}, {gpu, "GPU"}, {fpga_fast_emu, "FPGA Fast Emu"}};
246+
ErrorMessage += "Failed to find OpenCL " +
247+
DeviceTypeToDeviceTypeName[Type] +
248+
" device in these OpenCL platforms:\n" + SupportedPlatforms;
249+
} else {
250+
if (PlatformId == nullptr) {
251+
ErrorMessage += "OpenCL platform ID is empty\n";
252+
}
253+
if (PlatformName.empty()) {
254+
ErrorMessage += "OpenCL platform name is empty\n";
255+
}
256+
if (!ErrorMessage.empty()) {
257+
ErrorMessage += "Failed to find any of these OpenCL platforms:\n" +
258+
SupportedPlatforms;
259+
CLErr = OPENCL_AOT_PLATFORM_NOT_FOUND;
242260
}
243261
}
244262

0 commit comments

Comments
 (0)