From 204cef2569adb72b935302b7338ea16bfd2343e1 Mon Sep 17 00:00:00 2001 From: Denis Date: Sat, 9 May 2026 22:31:37 +0100 Subject: [PATCH] Fix OpenCL device enumeration error handling --- profanity.cpp | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/profanity.cpp b/profanity.cpp index a5b0e1d..bd1e611 100644 --- a/profanity.cpp +++ b/profanity.cpp @@ -33,24 +33,46 @@ std::string readFile(const char * const szFilename) return contents.str(); } -std::vector getAllDevices(cl_device_type deviceType = CL_DEVICE_TYPE_GPU) -{ +std::vector getAllDevices(cl_device_type deviceType = CL_DEVICE_TYPE_GPU) { std::vector vDevices; cl_uint platformIdCount = 0; - clGetPlatformIDs (0, NULL, &platformIdCount); + cl_int err = clGetPlatformIDs(0, NULL, &platformIdCount); + if (err != CL_SUCCESS || platformIdCount == 0) { + std::cerr << "warning: no OpenCL platforms found, err = " << err << std::endl; + return vDevices; + } + + std::vector platformIds(platformIdCount); + err = clGetPlatformIDs(platformIdCount, platformIds.data(), NULL); + if (err != CL_SUCCESS) { + std::cerr << "warning: failed to enumerate OpenCL platforms, err = " << err << std::endl; + return vDevices; + } - std::vector platformIds (platformIdCount); - clGetPlatformIDs (platformIdCount, platformIds.data (), NULL); + for (auto it = platformIds.cbegin(); it != platformIds.cend(); ++it) { + cl_uint countDevice = 0; - for( auto it = platformIds.cbegin(); it != platformIds.cend(); ++it ) { - cl_uint countDevice; - clGetDeviceIDs(*it, deviceType, 0, NULL, &countDevice); + err = clGetDeviceIDs(*it, deviceType, 0, NULL, &countDevice); + if (err != CL_SUCCESS || countDevice == 0) { + char platformName[256] = {0}; + clGetPlatformInfo(*it, CL_PLATFORM_NAME, sizeof(platformName), platformName, NULL); + std::cerr << "warning: skipping OpenCL platform without usable GPU devices: " + << platformName << ", err = " << err << std::endl; + continue; + } std::vector deviceIds(countDevice); - clGetDeviceIDs(*it, deviceType, countDevice, deviceIds.data(), &countDevice); + err = clGetDeviceIDs(*it, deviceType, countDevice, deviceIds.data(), &countDevice); + if (err != CL_SUCCESS) { + char platformName[256] = {0}; + clGetPlatformInfo(*it, CL_PLATFORM_NAME, sizeof(platformName), platformName, NULL); + std::cerr << "warning: failed to get GPU devices from platform: " + << platformName << ", err = " << err << std::endl; + continue; + } - std::copy( deviceIds.begin(), deviceIds.end(), std::back_inserter(vDevices) ); + std::copy(deviceIds.begin(), deviceIds.end(), std::back_inserter(vDevices)); } return vDevices;