rust gpu temperature in rust

To read the GPU temperature in Rust, you would most likely need to interface with the appropriate hardware monitoring library that provides access to GPU sensor data. One popular library that you can consider using is nvidia-smi for Nvidia GPUs.

You can use the std::process::Command module in Rust to run the nvidia-smi command and read the output. Here's an example code snippet that demonstrates how you can retrieve the GPU temperature of an Nvidia GPU in Rust:

main.rs
use std::process::Command;

fn main() {
    let output = Command::new("nvidia-smi")
        .arg("--query-gpu=temperature.gpu")
        .arg("--format=csv,noheader")
        .output()
        .expect("Failed to execute nvidia-smi command");

    if output.status.success() {
        if let Ok(output_string) = String::from_utf8(output.stdout) {
            let temperature: i32 = output_string.trim().parse().expect("Failed to parse temperature");
            println!("GPU Temperature: {}°C", temperature);
        } else {
            eprintln!("Error parsing nvidia-smi output");
        }
    } else {
        eprintln!("nvidia-smi command failed");
    }
}
662 chars
21 lines

In this code:

  1. We use Command::new("nvidia-smi") to run the nvidia-smi command.
  2. We pass the appropriate arguments to nvidia-smi to query for the GPU temperature in Celsius.
  3. We capture the output of the command and parse the GPU temperature from the output.
  4. Finally, we print the GPU temperature in Celsius.

Remember to add error handling and customize the code further based on your requirements and the specifics of the hardware you are working with.

related categories

gistlibby LogSnag