From eef48c36012180d20001def874864607e15968b8 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Mon, 3 Mar 2025 23:16:09 +0100 Subject: Avoid temporary file for listing GPUs. --- src/lib/grok/util.cc | 34 +++++++++++++++++----------------- src/lib/grok/util.h | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) (limited to 'src/lib') diff --git a/src/lib/grok/util.cc b/src/lib/grok/util.cc index e9b535df8..8a6d2c4f9 100644 --- a/src/lib/grok/util.cc +++ b/src/lib/grok/util.cc @@ -20,6 +20,8 @@ #include "util.h" +#include +#include using std::string; @@ -27,24 +29,22 @@ using std::vector; vector -get_gpu_names(boost::filesystem::path binary, boost::filesystem::path filename) +get_gpu_names(boost::filesystem::path binary) { - // Execute the GPU listing program and redirect its output to a file - if (std::system((binary.string() + " > " + filename.string()).c_str()) < 0) { - return {}; - } - - std::vector gpu_names; - std::ifstream file(filename.c_str()); - if (file.is_open()) - { - std::string line; - while (std::getline(file, line)) - gpu_names.push_back(line); - file.close(); - } - - return gpu_names; + namespace bp = boost::process; + + bp::ipstream stream; + bp::child child(binary, bp::std_out > stream); + + string line; + vector gpu_names; + while (child.running() && std::getline(stream, line) && !line.empty()) { + gpu_names.push_back(line); + } + + child.wait(); + + return gpu_names; } diff --git a/src/lib/grok/util.h b/src/lib/grok/util.h index 069a6cc08..9996fa0e9 100644 --- a/src/lib/grok/util.h +++ b/src/lib/grok/util.h @@ -24,4 +24,4 @@ #include -extern std::vector get_gpu_names(boost::filesystem::path binary, boost::filesystem::path filename); +extern std::vector get_gpu_names(boost::filesystem::path binary); -- cgit v1.2.3