Skip to content

Commit 6fef594

Browse files
committed
Add helper for parsing MMD version string
1 parent 569cc16 commit 6fef594

File tree

1 file changed

+55
-5
lines changed

1 file changed

+55
-5
lines changed

src/acl_hal_mmd.cpp

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,39 @@ static int debug_verbosity = 0;
417417
// ************************** Helper functions ******************************
418418
// **************************************************************************
419419

420+
// Version string format should be MAJOR.MINOR(.PATCH)
421+
// To compare the MMD/HAL versions we will only compare the last two digits
422+
// of the MAJOR field together with the MINOR field, and ignore the PATCH field
423+
// if that exists. This function truncates the version string to exactly that
424+
// and convert it to double to be compared. Returns -1 if input is invalid.
425+
double l_parse_mmd_version_str(std::string version_str) {
426+
size_t start_idx, length;
427+
428+
// Find the '.' between the MAJOR and MINOR field
429+
std::string::size_type i = version_str.find('.');
430+
if (i == std::string::npos || i < 2) {
431+
return -1;
432+
} else {
433+
start_idx = i - 2;
434+
}
435+
// Check if there is a '.' for PATCH field
436+
std::string::size_type j = version_str.find('.', i + 1);
437+
length = (j == std::string::npos ? version_str.length() : j) - start_idx;
438+
439+
// Get the part of the MMD version string that will be used to compare
440+
// for compatibility with the runtime HAL
441+
std::string version_substr = version_str.substr(start_idx, length);
442+
double mmd_version_num = 0;
443+
try {
444+
mmd_version_num = std::stod(version_substr);
445+
} catch (const std::exception &) {
446+
// Just return error and let the caller handle failure
447+
return -1;
448+
}
449+
450+
return mmd_version_num;
451+
}
452+
420453
// MMD dynamic load helpers
421454
#ifdef _WIN32
422455
char *acl_strtok(char *str, const char *delim, char **saveptr) {
@@ -963,22 +996,40 @@ void l_get_physical_devices(acl_mmd_dispatch_t *mmd_dispatch,
963996
mmd_dispatch->aocl_mmd_get_offline_info(AOCL_MMD_VERSION, sizeof(buf), buf,
964997
NULL);
965998
buf[sizeof(buf) - 1] = 0;
966-
mmd_dispatch->mmd_version = atof(buf);
999+
mmd_dispatch->mmd_version = l_parse_mmd_version_str(std::string(buf));
1000+
if (mmd_dispatch->mmd_version < 0) {
1001+
printf(" Invalid MMD version: %s\n", buf);
1002+
printf("Contact the board package support vendor for resolution.\n");
1003+
fflush(stdout);
1004+
assert(0);
1005+
}
9671006
min_MMD_version =
9681007
(!MMDVERSION_LESSTHAN(min_MMD_version, mmd_dispatch->mmd_version))
9691008
? mmd_dispatch->mmd_version
9701009
: min_MMD_version;
9711010
ACL_HAL_DEBUG_MSG_VERBOSE(1, "HAL : Getting info version: %s\n", buf);
9721011

973-
if (MMDVERSION_LESSTHAN(atof(AOCL_MMD_VERSION_STRING),
1012+
static double hal_version = 0;
1013+
if (hal_version == 0) { // Just parse once at start-up
1014+
hal_version = l_parse_mmd_version_str(AOCL_MMD_VERSION_STRING);
1015+
if (hal_version < 0) { // This should theoretically never happen
1016+
printf(" Invalid runtime version: %s\n", AOCL_MMD_VERSION_STRING);
1017+
fflush(stdout);
1018+
assert(0);
1019+
}
1020+
}
1021+
if (MMDVERSION_LESSTHAN(hal_version,
9741022
mmd_dispatch->mmd_version) || // MMD newer than HAL
9751023
MMDVERSION_LESSTHAN(mmd_dispatch->mmd_version,
9761024
14.0)) // Before this wasn't forward compatible
9771025
{
9781026
printf(" Runtime version: %s\n", AOCL_MMD_VERSION_STRING);
9791027
printf(" MMD version: %s\n", buf);
1028+
printf("MMD version is newer than the runtime version! Use the runtime "
1029+
"with version greater or equal to the MMD version, or contact the "
1030+
"board support package vendors if mismatch is unexpected.\n");
9801031
fflush(stdout);
981-
assert(0 && "MMD version mismatch");
1032+
assert(0);
9821033
}
9831034

9841035
// Disable yield as initialization
@@ -2682,8 +2733,7 @@ int acl_hal_mmd_support_buffer_location(
26822733
for (const auto &device : devices) {
26832734
unsigned int physical_device_id = device->def.physical_device_id;
26842735
if (MMDVERSION_LESSTHAN(
2685-
device_info[physical_device_id].mmd_dispatch->mmd_version,
2686-
2024.2)) {
2736+
device_info[physical_device_id].mmd_dispatch->mmd_version, 24.2)) {
26872737
bl_supported = 0;
26882738
}
26892739
}

0 commit comments

Comments
 (0)