Read GPU Information from Console C++

629 views Asked by At

I want to create my own Overclocking Monitor for which I need to read information like the current voltage, clockspeeds and others.

In C++ I can easily get the Information from Nvidia-smi with typing for example:

console("nvidia-smi -q -i voltage");

Which then displays me:

==============NVSMI LOG==============

Timestamp                                 : Tue Dec 13 17:55:54 2022
Driver Version                            : 526.47
CUDA Version                              : 12.0

Attached GPUs                             : 1
GPU 00000000:01:00.0
    Voltage
        Graphics                          : 806.250 mV

From that I need only the voltage number, in this case "806.25".

I´ve investigated a bit into <cctype> which was something I´ve read about, but I´m not making any progress.

So how can I import only that number into my c++ Program? I´d just guess that the process will be the same for the other commands.

1

There are 1 answers

1
Masquerading On

I don't currently have an Nvidia GPU to test this (stuck with Intel integrated graphics), so I can't import cuda.h but feel free to test this and let me know if it works or not.

#include <iostream>
#include <chrono>
#include <cuda.h>

int main() {
  // Get the current timestamp
  auto current_time = std::chrono::system_clock::now();

  // Get the current driver version
  int driver_version;
  cudaDriverGetVersion(&driver_version);

  // Get the current CUDA version
  int cuda_version;
  cudaRuntimeGetVersion(&cuda_version);

  // Get the name of the attached GPU
  cudaDeviceProp device_properties;
  cudaGetDeviceProperties(&device_properties, 0);
  std::string gpu_name = device_properties.name;

  // Get the current voltage
  int power_usage;
  cudaDeviceGetPowerUsage(&power_usage, 0);
  int voltage = power_usage / current;

  // Output the overclocking data
  std::cout << "Timestamp: " << current_time << std::endl;
  std::cout << "Driver version: " << driver_version << std::endl;
  std::cout << "CUDA version: " << cuda_version << std::endl;
  std::cout << "Attached GPU: " << gpu_name << std::endl;
  std::cout << "Voltage: " << voltage << std::endl;

  return 0;
}

If it works then your voltage can be accessed from int voltage.