Skip to content

Commit

Permalink
Only search once in JSON
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Jerphanion <[email protected]>

Co-authored-by: Klaim <[email protected]>
  • Loading branch information
jjerphan and Klaim committed Jan 6, 2025
1 parent 4b79b1e commit 068e74d
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions libmamba/src/core/virtual_packages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,28 @@ namespace mamba
std::ifstream f = open_ifstream(cuda_version_file);
nlohmann::json j;
f >> j;
if (j.contains("cuda") && j["cuda"].contains("version"))
if (auto it_cuda = j.find("cuda"); it_cuda != j.end())
{
cuda_version = j["cuda"]["version"];
LOG_DEBUG << "CUDA version found: " << cuda_version;

// Extract major, minor and patch version number from the version string
// and return only major.minor to match the cuda package version return
// by `nvidia-smi --query -u -x`
std::regex re("([0-9]+)\\.([0-9]+)\\.([0-9]+)");
std::smatch m;
if (std::regex_search(cuda_version, m, re) && m.size() >= 3)
auto cuda_val = *it_cuda;
if (auto it_cuda_version = cuda_val.find("version");
it_cuda_version != cuda_val.end())
{
std::ssub_match major = m[1];
std::ssub_match minor = m[2];
cuda_version = major.str() + "." + minor.str();
LOG_DEBUG << "CUDA version returned: " << cuda_version;
return cuda_version;
cuda_version = it_cuda_version->get<std::string>();
LOG_DEBUG << "CUDA version found: " << cuda_version;

// Extract major, minor and patch version number from the version string
// and return only major.minor to match the cuda package version return
// by `nvidia-smi --query -u -x`
std::regex re("([0-9]+)\\.([0-9]+)\\.([0-9]+)");
std::smatch m;
if (std::regex_search(cuda_version, m, re) && m.size() >= 3)
{
std::ssub_match major = m[1];
std::ssub_match minor = m[2];
cuda_version = major.str() + "." + minor.str();
LOG_DEBUG << "CUDA version returned: " << cuda_version;
return cuda_version;
}
}
LOG_WARNING << "Could not extract CUDA version from: " << cuda_version;
}
Expand Down

0 comments on commit 068e74d

Please sign in to comment.