OClCollMeshEngn: use helper fns for parsing version numbers

This commit is contained in:
2025-11-12 20:43:48 -04:00
parent df58f324a9
commit 5031b22a31
@@ -4,6 +4,7 @@
#include <cstring> #include <cstring>
#include <vector> #include <vector>
#include <string> #include <string>
#include <string_view>
#include <boost/system/error_code.hpp> #include <boost/system/error_code.hpp>
#include <asynchronousContinuation.h> #include <asynchronousContinuation.h>
#include <callback.h> #include <callback.h>
@@ -20,6 +21,66 @@
namespace smo { namespace smo {
namespace stim_buff { namespace stim_buff {
/* @brief Helper function to parse OpenCL version string.
* Expected format: "OpenCL <major>.<minor> <vendor info>"
* @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<int, int> 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( OpenClCollatingAndMeshingEngine::OpenClCollatingAndMeshingEngine(
PcloudStimulusBuffer& parent_) PcloudStimulusBuffer& parent_)
: parent(parent_), : parent(parent_),
@@ -83,28 +144,8 @@ bool OpenClCollatingAndMeshingEngine::setup()
sizeof(platformVersion), platformVersion, nullptr); sizeof(platformVersion), platformVersion, nullptr);
if (err == CL_SUCCESS) if (err == CL_SUCCESS)
{ {
// Platform version string format: "OpenCL <major>.<minor> <vendor info>" if (!validateOpenClVersion(platformVersion, "platform", 1, 2)) {
// Extract version number return false;
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;
}
} }
} }
@@ -114,27 +155,8 @@ bool OpenClCollatingAndMeshingEngine::setup()
sizeof(deviceVersion), deviceVersion, nullptr); sizeof(deviceVersion), deviceVersion, nullptr);
if (err == CL_SUCCESS) if (err == CL_SUCCESS)
{ {
// Device version string format: "OpenCL <major>.<minor> <vendor info>" if (!validateOpenClVersion(deviceVersion, "device", 1, 2)) {
std::string versionStr(deviceVersion); return false;
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;
}
} }
} }