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

Provide idiomatic abstraction for vGPU type information #68

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
44 changes: 44 additions & 0 deletions nvml-wrapper/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::ffi::bindings::*;
use crate::struct_wrappers::device::*;
use crate::structs::device::*;

use crate::vgpu::VgpuType;

#[cfg(target_os = "linux")]
use std::convert::TryInto;
#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -5058,6 +5060,48 @@ impl<'nvml> Device<'nvml> {
pub fn link_wrapper_for(&self, link: u32) -> NvLink {
NvLink { device: self, link }
}

// vGPU

/// Obtain a list of vGPU type (profiles) supported by the device, if any.
pub fn vgpu_supported_types(&self) -> Result<Vec<VgpuType>, NvmlError> {
let sym = nvml_sym(self.nvml.lib.nvmlDeviceGetSupportedVgpus.as_ref())?;
let mut ids = vec![];

unsafe {
let mut count: c_uint = mem::zeroed();

match nvml_try(sym(self.device, &mut count, ids.as_mut_ptr())) {
Ok(()) | Err(NvmlError::InsufficientSize(_)) => {}
Err(err) => return Err(err),
}

ids.resize(count as usize, 0);
nvml_try(sym(self.device, &mut count, ids.as_mut_ptr()))?;
}

Ok(ids.into_iter().map(|id| VgpuType::new(self, id)).collect())
}

/// Obtain a list of vGPU type (profiles) creatable on the device, if any.
pub fn vgpu_creatable_types(&self) -> Result<Vec<VgpuType>, NvmlError> {
let sym = nvml_sym(self.nvml.lib.nvmlDeviceGetCreatableVgpus.as_ref())?;
let mut ids = vec![];

unsafe {
let mut count: c_uint = mem::zeroed();

match nvml_try(sym(self.device, &mut count, ids.as_mut_ptr())) {
Ok(()) | Err(NvmlError::InsufficientSize(_)) => {}
Err(err) => return Err(err),
}

ids.resize(count as usize, 0);
nvml_try(sym(self.device, &mut count, ids.as_mut_ptr()))?;
}

Ok(ids.into_iter().map(|id| VgpuType::new(self, id)).collect())
}
}

#[cfg(test)]
Expand Down
6 changes: 6 additions & 0 deletions nvml-wrapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub mod structs;
#[cfg(test)]
mod test_utils;
pub mod unit;
pub mod vgpu;

// Re-exports for convenience
pub use crate::device::Device;
Expand Down Expand Up @@ -297,6 +298,11 @@ impl Nvml {
NvmlBuilder::default()
}

/// Get the underlying `NvmlLib` instance.
pub fn lib(&self) -> &NvmlLib {
&self.lib
}

/**
Use this to shutdown NVML and release allocated resources if you care about handling
potential errors (*the `Drop` implementation ignores errors!*).
Expand Down
Loading