diff --git a/opencl-aot/include/utils.h b/opencl-aot/include/utils.h index a57fefa1e170f..b8c84ea45edfb 100644 --- a/opencl-aot/include/utils.h +++ b/opencl-aot/include/utils.h @@ -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; } diff --git a/opencl-aot/source/utils.cpp b/opencl-aot/source/utils.cpp index 68c70ba6f223e..fb1fdd2bde70c 100644 --- a/opencl-aot/source/utils.cpp +++ b/opencl-aot/source/utils.cpp @@ -178,18 +178,24 @@ getOpenCLPlatform(DeviceType Type) { cl_int CLErr(CL_SUCCESS); std::string PlatformName; - const cl_uint MaxPlatformsCount = 10; - std::array 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 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, @@ -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 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; } }