@@ -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