From 5031b22a310928a5141941d03a340e0a3567a5ad Mon Sep 17 00:00:00 2001 From: Hayodea Hekol Date: Wed, 12 Nov 2025 20:43:48 -0400 Subject: [PATCH] OClCollMeshEngn: use helper fns for parsing version numbers --- .../openClCollatingAndMeshingEngine.cpp | 108 +++++++++++------- 1 file changed, 65 insertions(+), 43 deletions(-) diff --git a/stimBuffApis/livoxGen1/openClCollatingAndMeshingEngine.cpp b/stimBuffApis/livoxGen1/openClCollatingAndMeshingEngine.cpp index 4e09ff2..0698eb5 100644 --- a/stimBuffApis/livoxGen1/openClCollatingAndMeshingEngine.cpp +++ b/stimBuffApis/livoxGen1/openClCollatingAndMeshingEngine.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -20,6 +21,66 @@ namespace smo { namespace stim_buff { +/* @brief Helper function to parse OpenCL version string. + * Expected format: "OpenCL . " + * @param versionStr The OpenCL version string to parse. + * @return A pair of (major, minor) version numbers. + * If parsing fails, returns (-1, -1). + */ +static std::pair parseOpenClVersion(const std::string& versionStr) +{ + size_t spacePos = versionStr.find(' '); + if (spacePos == std::string::npos) { return {-1, -1}; } + + std::string versionNum = versionStr.substr(spacePos + 1); + size_t dotPos = versionNum.find('.'); + if (dotPos == std::string::npos) { return {-1, -1}; } + + try { + int major = std::stoi(versionNum.substr(0, dotPos)); + int minor = std::stoi(versionNum.substr(dotPos + 1)); + return {major, minor}; + } catch (const std::exception&) { + return {-1, -1}; + } +} + +/* + * @brief Validates OpenCL version string and checks if it meets minimum requirement. + * @param versionStr The OpenCL version string to validate. + * @param versionType Description of version type (e.g., "platform", "device") for error messages. + * @param minMajor Minimum major version required. + * @param minMinor Minimum minor version required (for the given major version). + * @return true if version is valid and meets minimum requirement, false otherwise. + */ +static bool validateOpenClVersion( + std::string_view versionStr, std::string_view versionType, + int minMajor, int minMinor) +{ + auto [major, minor] = parseOpenClVersion(std::string(versionStr)); + + // Early return if version couldn't be parsed + if (major == -1 && minor == -1) + { + std::cerr << __func__ << ": failed to parse OpenCL " << versionType + << " version: " << versionStr << std::endl; + return false; + } + + // Require minimum version + if (major < minMajor || (major == minMajor && minor < minMinor)) + { + std::cerr << __func__ << ": OpenCL " << versionType << " version " + << major << "." << minor << " found, but " << minMajor << "." + << minMinor << " or higher is required" << std::endl; + return false; + } + + std::cout << __func__ << ": OpenCL " << versionType << " version: " + << versionStr << std::endl; + return true; +} + OpenClCollatingAndMeshingEngine::OpenClCollatingAndMeshingEngine( PcloudStimulusBuffer& parent_) : parent(parent_), @@ -83,28 +144,8 @@ bool OpenClCollatingAndMeshingEngine::setup() sizeof(platformVersion), platformVersion, nullptr); if (err == CL_SUCCESS) { - // Platform version string format: "OpenCL . " - // Extract version number - std::string versionStr(platformVersion); - size_t spacePos = versionStr.find(' '); - if (spacePos != std::string::npos) - { - std::string versionNum = versionStr.substr(spacePos + 1); - size_t dotPos = versionNum.find('.'); - if (dotPos != std::string::npos) - { - int major = std::stoi(versionNum.substr(0, dotPos)); - int minor = std::stoi(versionNum.substr(dotPos + 1)); - if (major < 1 || (major == 1 && minor < 2)) - { - std::cerr << __func__ << ": OpenCL version " << major - << "." << minor << " found, but 1.2 or higher is required" - << std::endl; - return false; - } - std::cout << __func__ << ": OpenCL platform version: " - << platformVersion << std::endl; - } + if (!validateOpenClVersion(platformVersion, "platform", 1, 2)) { + return false; } } @@ -114,27 +155,8 @@ bool OpenClCollatingAndMeshingEngine::setup() sizeof(deviceVersion), deviceVersion, nullptr); if (err == CL_SUCCESS) { - // Device version string format: "OpenCL . " - std::string versionStr(deviceVersion); - size_t spacePos = versionStr.find(' '); - if (spacePos != std::string::npos) - { - std::string versionNum = versionStr.substr(spacePos + 1); - size_t dotPos = versionNum.find('.'); - if (dotPos != std::string::npos) - { - int major = std::stoi(versionNum.substr(0, dotPos)); - int minor = std::stoi(versionNum.substr(dotPos + 1)); - if (major < 1 || (major == 1 && minor < 2)) - { - std::cerr << __func__ << ": OpenCL device version " << major - << "." << minor << " found, but 1.2 or higher is required" - << std::endl; - return false; - } - std::cout << __func__ << ": OpenCL device version: " - << deviceVersion << std::endl; - } + if (!validateOpenClVersion(deviceVersion, "device", 1, 2)) { + return false; } }