diff options
| author | Carl Hetherington <cth@carlh.net> | 2025-03-03 23:16:09 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2025-03-08 00:12:07 +0100 |
| commit | eef48c36012180d20001def874864607e15968b8 (patch) | |
| tree | 59c45a1579dc5dbf2ddd03f7a363df2f5e30647f | |
| parent | 2b3a495f31d3f9de57e6bbe2605843e6f1efbe3a (diff) | |
Avoid temporary file for listing GPUs.
| -rw-r--r-- | src/lib/grok/util.cc | 34 | ||||
| -rw-r--r-- | src/lib/grok/util.h | 2 | ||||
| -rw-r--r-- | src/wx/grok/gpu_config_panel.h | 3 | ||||
| -rw-r--r-- | test/grok_util_test.cc | 4 |
4 files changed, 21 insertions, 22 deletions
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 <boost/process.hpp> +#include <future> using std::string; @@ -27,24 +29,22 @@ using std::vector; vector<string> -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<std::string> 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<string> 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 <vector> -extern std::vector<std::string> get_gpu_names(boost::filesystem::path binary, boost::filesystem::path filename); +extern std::vector<std::string> get_gpu_names(boost::filesystem::path binary); diff --git a/src/wx/grok/gpu_config_panel.h b/src/wx/grok/gpu_config_panel.h index 6b8a4f150..b61bccbde 100644 --- a/src/wx/grok/gpu_config_panel.h +++ b/src/wx/grok/gpu_config_panel.h @@ -45,9 +45,8 @@ public: { auto grok = Config::instance()->grok(); auto lister_binary = grok.binary_location / "gpu_lister"; - auto lister_file = grok.binary_location / "gpus.txt"; if (boost::filesystem::exists(lister_binary)) { - auto gpu_names = get_gpu_names(lister_binary, lister_file); + auto gpu_names = get_gpu_names(lister_binary); _combo_box->Clear(); for (auto const& name: gpu_names) { diff --git a/test/grok_util_test.cc b/test/grok_util_test.cc index 355e67219..8fba372a9 100644 --- a/test/grok_util_test.cc +++ b/test/grok_util_test.cc @@ -19,7 +19,7 @@ */ -#include "lib/gpu_util.h" +#include "lib/grok/util.h" #include "test.h" #include <boost/test/unit_test.hpp> @@ -27,7 +27,7 @@ #ifdef DCPOMATIC_GROK BOOST_AUTO_TEST_CASE(get_gpu_names_test) { - auto names = get_gpu_names("test/gpu_lister", "build/test/gpus.txt"); + auto names = get_gpu_names("test/gpu_lister"); BOOST_REQUIRE_EQUAL(names.size(), 3U); BOOST_CHECK_EQUAL(names[0], "Foo bar baz"); BOOST_CHECK_EQUAL(names[1], "Spondoolix Mega Kompute 2000"); |
