Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fan control wrappers #1

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions nvml-wrapper/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,65 @@ impl<'nvml> Device<'nvml> {
}
}

/**
Sets the intended operating speed of the specified fan as a percentage of the
maximum fan speed (100%).

You can determine valid fan indices using [`Self::num_fans()`].

WARNING: This function changes the fan control policy to manual. It means that YOU have to
monitor the temperature and adjust the fan speed accordingly. If you set the fan speed too
low you can burn your GPU! Use [`Self::set_default_fan_speed()`] to restore default control
policy.

# Errors

* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if this `Device` is invalid or `fan_idx` is invalid
* `NotSupported`, if this `Device` does not have a fan or is newer than Maxwell
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `Unknown`, on any unexpected error

# Device Support

Supports all cuda-capable discrete products with fans that are Maxwell or Newer.
*/
// Checked against local
// Tested (no-run)
#[doc(alias = "nvmlDeviceSetFanSpeed_v2")]
pub fn set_fan_speed(&self, fan_idx: u32, speed: u32) -> Result<(), NvmlError> {
let sym = nvml_sym(self.nvml.lib.nvmlDeviceSetFanSpeed_v2.as_ref())?;

unsafe { nvml_try(sym(self.device, fan_idx, speed)) }
}

/**
Sets the speed of the fan control policy to default.
Used to restore default control policy after calling [`Self::set_fan_speed()`].

You can determine valid fan indices using [`Self::num_fans()`].

# Errors

* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if this `Device` is invalid or `fan_idx` is invalid
* `NotSupported`, if this `Device` does not have a fan or is newer than Maxwell
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `Unknown`, on any unexpected error

# Device Support

Supports cuda-capable discrete products with fans.
*/
// Checked against local
// Tested (no-run)
#[doc(alias = "nvmlDeviceSetDefaultFanSpeed_v2")]
pub fn set_default_fan_speed(&self, fan_idx: u32) -> Result<(), NvmlError> {
let sym = nvml_sym(self.nvml.lib.nvmlDeviceSetDefaultFanSpeed_v2.as_ref())?;

unsafe { nvml_try(sym(self.device, fan_idx)) }
}

/**
Gets the current GPU operation mode and the pending one (that it will switch to
after a reboot).
Expand Down Expand Up @@ -5888,6 +5947,28 @@ mod test {
})
}

// This modifies device state, so we don't want to actually run the test
#[allow(dead_code)]
fn set_fan_speed() {
let nvml = nvml();
let mut device = device(&nvml);

device
.set_fan_speed(0, 50)
.expect("set fan with index 0 to speed of 50%")
}

// This modifies device state, so we don't want to actually run the test
#[allow(dead_code)]
fn set_default_fan_speed() {
let nvml = nvml();
let mut device = device(&nvml);

device
.set_default_fan_speed(0)
.expect("set fan with index 0 to default control policy")
}

// This modifies device state, so we don't want to actually run the test
#[allow(dead_code)]
fn set_accounting() {
Expand Down