@@ -178,18 +178,24 @@ getOpenCLPlatform(DeviceType Type) {
178
178
cl_int CLErr (CL_SUCCESS);
179
179
std::string PlatformName;
180
180
181
- const cl_uint MaxPlatformsCount = 10 ;
182
- std::array<cl_platform_id, MaxPlatformsCount> Platforms{};
183
-
184
181
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 );
187
192
if (clFailed (CLErr)) {
188
193
return std::make_tuple (
189
194
nullptr , " " ,
190
195
formatCLError (" Failed to retrieve OpenCL platform IDs" , CLErr), CLErr);
191
196
}
192
197
198
+ std::string ErrorMessage;
193
199
for (const auto &Platform : Platforms) {
194
200
size_t PlatformNameLength = 0 ;
195
201
CLErr = clGetPlatformInfo (Platform, CL_PLATFORM_NAME, 0 , nullptr ,
@@ -221,24 +227,36 @@ getOpenCLPlatform(DeviceType Type) {
221
227
std::find (SupportedPlatformNames.begin (), SupportedPlatformNames.end (),
222
228
PlatformNameOnLoopIteration);
223
229
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
+ }
227
236
}
228
237
}
229
238
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 ' ;
236
242
}
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;
242
260
}
243
261
}
244
262
0 commit comments