@@ -19,7 +19,8 @@ constexpr static auto kUnsupported{"Unsupported"};
1919constexpr static auto kCudaVersionRegex {R"( CUDA Version:\s*([\d\.]+))" };
2020constexpr static auto kDriverVersionRegex {R"( Driver Version:\s*(\d+\.\d+))" };
2121constexpr static auto kGpuQueryCommand {
22- " nvidia-smi --query-gpu=index,memory.total,memory.free,name,compute_cap,uuid "
22+ " nvidia-smi "
23+ " --query-gpu=index,memory.total,memory.free,name,compute_cap,uuid "
2324 " --format=csv,noheader,nounits" };
2425constexpr static auto kGpuInfoRegex {
2526 R"( (\d+),\s*(\d+),\s*(\d+),\s*([^,]+),\s*([\d\.]+),\s*([^\n,]+))" };
@@ -100,53 +101,42 @@ inline bool IsNvidiaSmiAvailable() {
100101#endif
101102}
102103
103- inline std::string GetDriverVersion () {
104+ inline std::pair<std:: string, std::string> GetDriverAndCudaVersion () {
104105 if (!IsNvidiaSmiAvailable ()) {
105106 CTL_INF (" nvidia-smi is not available!" );
106- return " " ;
107+ return {} ;
107108 }
108109 try {
110+ std::string driver_version;
111+ std::string cuda_version;
109112 CommandExecutor cmd (" nvidia-smi" );
110113 auto output = cmd.execute ();
111114
112115 const std::regex driver_version_reg (kDriverVersionRegex );
113- std::smatch match ;
116+ std::smatch driver_match ;
114117
115- if (std::regex_search (output, match , driver_version_reg)) {
116- LOG_INFO << " Gpu Driver Version: " << match [1 ].str ();
117- return match [1 ].str ();
118+ if (std::regex_search (output, driver_match , driver_version_reg)) {
119+ LOG_INFO << " Gpu Driver Version: " << driver_match [1 ].str ();
120+ driver_version = driver_match [1 ].str ();
118121 } else {
119122 LOG_ERROR << " Gpu Driver not found!" ;
120- return " " ;
123+ return {} ;
121124 }
122- } catch (const std::exception& e) {
123- LOG_ERROR << " Error: " << e.what ();
124- return " " ;
125- }
126- }
127-
128- inline std::string GetCudaVersion () {
129- if (!IsNvidiaSmiAvailable ()) {
130- CTL_INF (" nvidia-smi is not available!" );
131- return " " ;
132- }
133- try {
134- CommandExecutor cmd (" nvidia-smi" );
135- auto output = cmd.execute ();
136125
137126 const std::regex cuda_version_reg (kCudaVersionRegex );
138- std::smatch match ;
127+ std::smatch cuda_match ;
139128
140- if (std::regex_search (output, match , cuda_version_reg)) {
141- LOG_INFO << " CUDA Version: " << match [1 ].str ();
142- return match [1 ].str ();
129+ if (std::regex_search (output, cuda_match , cuda_version_reg)) {
130+ LOG_INFO << " CUDA Version: " << cuda_match [1 ].str ();
131+ cuda_version = cuda_match [1 ].str ();
143132 } else {
144133 LOG_ERROR << " CUDA Version not found!" ;
145- return " " ;
134+ return {} ;
146135 }
136+ return std::pair (driver_version, cuda_version);
147137 } catch (const std::exception& e) {
148138 LOG_ERROR << " Error: " << e.what ();
149- return " " ;
139+ return {} ;
150140 }
151141}
152142
@@ -227,9 +217,9 @@ inline std::vector<GpuInfo> GetGpuInfoList() {
227217 if (!IsNvidiaSmiAvailable ())
228218 return gpuInfoList;
229219 try {
230- // TODO: improve by parsing both in one command execution
231- auto driver_version = GetDriverVersion ();
232- auto cuda_version = GetCudaVersion () ;
220+ auto [driver_version, cuda_version] = GetDriverAndCudaVersion ();
221+ if ( driver_version. empty () || cuda_version. empty ())
222+ return gpuInfoList ;
233223
234224 CommandExecutor cmd (kGpuQueryCommand );
235225 auto output = cmd.execute ();
@@ -249,7 +239,7 @@ inline std::vector<GpuInfo> GetGpuInfoList() {
249239 driver_version, // driver_version
250240 cuda_version, // cuda_driver_version
251241 match[5 ].str (), // compute_cap
252- match[6 ].str () // uuid
242+ match[6 ].str () // uuid
253243 };
254244 gpuInfoList.push_back (gpuInfo);
255245 search_start = match.suffix ().first ;
0 commit comments