Skip to content

[SYCL] Fix platform selection in opencl-aot #1510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion opencl-aot/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ enum Errors : int8_t {
OPENCL_AOT_OPTIONS_COEXISTENCE_FAILURE,
OPENCL_AOT_TARGET_CPU_ARCH_FAILURE,
OPENCL_AOT_DEVICE_ID_IS_EMPTY,
OPENCL_AOT_PROGRAM_IS_EMPTY
OPENCL_AOT_PROGRAM_IS_EMPTY,
OPENCL_AOT_PLATFORM_NOT_FOUND
};

inline bool clFailed(cl_int ReturnCode) { return CL_SUCCESS != ReturnCode; }
Expand Down
56 changes: 37 additions & 19 deletions opencl-aot/source/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,24 @@ getOpenCLPlatform(DeviceType Type) {
cl_int CLErr(CL_SUCCESS);
std::string PlatformName;

const cl_uint MaxPlatformsCount = 10;
std::array<cl_platform_id, MaxPlatformsCount> Platforms{};

cl_uint PlatformsCount = 0;
CLErr =
clGetPlatformIDs(MaxPlatformsCount, Platforms.data(), &PlatformsCount);
CLErr = clGetPlatformIDs(0, nullptr, &PlatformsCount);
if (clFailed(CLErr)) {
return std::make_tuple(
nullptr, "",
formatCLError("Failed to retrieve OpenCL platform count", CLErr),
CLErr);
}

std::vector<cl_platform_id> Platforms(PlatformsCount);
CLErr = clGetPlatformIDs(PlatformsCount, Platforms.data(), nullptr);
if (clFailed(CLErr)) {
return std::make_tuple(
nullptr, "",
formatCLError("Failed to retrieve OpenCL platform IDs", CLErr), CLErr);
}

std::string ErrorMessage;
for (const auto &Platform : Platforms) {
size_t PlatformNameLength = 0;
CLErr = clGetPlatformInfo(Platform, CL_PLATFORM_NAME, 0, nullptr,
Expand Down Expand Up @@ -221,24 +227,36 @@ getOpenCLPlatform(DeviceType Type) {
std::find(SupportedPlatformNames.begin(), SupportedPlatformNames.end(),
PlatformNameOnLoopIteration);
if (Result != SupportedPlatformNames.end()) {
PlatformId = Platform;
PlatformName = PlatformNameOnLoopIteration;
break;
tie(std::ignore, ErrorMessage, CLErr) = getOpenCLDevice(Platform, Type);
if (!clFailed(CLErr)) {
PlatformId = Platform;
PlatformName = PlatformNameOnLoopIteration;
break;
}
}
}

std::string ErrorMessage;
if (PlatformId == nullptr) {
ErrorMessage += "OpenCL platform ID is empty\n";
}
if (PlatformName.empty()) {
ErrorMessage += "OpenCL platform name is empty\n";
std::string SupportedPlatforms;
for (const auto &Platform : DeviceTypesToSupportedPlatformNames[Type]) {
SupportedPlatforms += " " + Platform + '\n';
}
if (!ErrorMessage.empty()) {
ErrorMessage += "Failed to find any of these OpenCL platforms:\n";
for (const auto &SupportedPlatformName :
DeviceTypesToSupportedPlatformNames[Type]) {
ErrorMessage += " " + SupportedPlatformName + '\n';
if (clFailed(CLErr)) {
std::map<DeviceType, std::string> DeviceTypeToDeviceTypeName{
{cpu, "CPU"}, {gpu, "GPU"}, {fpga_fast_emu, "FPGA Fast Emu"}};
ErrorMessage += "Failed to find OpenCL " +
DeviceTypeToDeviceTypeName[Type] +
" device in these OpenCL platforms:\n" + SupportedPlatforms;
} else {
if (PlatformId == nullptr) {
ErrorMessage += "OpenCL platform ID is empty\n";
}
if (PlatformName.empty()) {
ErrorMessage += "OpenCL platform name is empty\n";
}
if (!ErrorMessage.empty()) {
ErrorMessage += "Failed to find any of these OpenCL platforms:\n" +
SupportedPlatforms;
CLErr = OPENCL_AOT_PLATFORM_NOT_FOUND;
}
}

Expand Down