Skip to content

Commit

Permalink
feat(wrapper): provide abstraction over vGPU type information
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Heiss <[email protected]>
  • Loading branch information
christoph-heiss committed Nov 6, 2024
1 parent 9403545 commit 54b1bc8
Show file tree
Hide file tree
Showing 3 changed files with 402 additions and 0 deletions.
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
1 change: 1 addition & 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
Loading

0 comments on commit 54b1bc8

Please sign in to comment.