Skip to content
Merged
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
52 changes: 42 additions & 10 deletions nvml-wrapper/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ pub struct Device<'nvml> {
nvml: &'nvml Nvml,
}

unsafe impl<'nvml> Send for Device<'nvml> {}
unsafe impl<'nvml> Sync for Device<'nvml> {}
unsafe impl Send for Device<'_> {}
unsafe impl Sync for Device<'_> {}

assert_impl_all!(Device: Send, Sync);

Expand Down Expand Up @@ -155,10 +155,10 @@ impl<'nvml> Device<'nvml> {

* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if this `Device` is invalid or the apiType is invalid (may occur if
the C lib changes dramatically?)
* the C lib changes dramatically?)
* `NotSupported`, if this query is not supported by this `Device` or this `Device`
does not support the feature that is being queried (e.g. enabling/disabling auto
boosted clocks is not supported by this `Device`).
* does not support the feature that is being queried (e.g. enabling/disabling auto
* boosted clocks is not supported by this `Device`).
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `UnexpectedVariant`, for which you can read the docs for
* `Unknown`, on any unexpected error
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<'nvml> Device<'nvml> {

* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if this `Device` is invalid or the clockType is invalid (may occur
if the C lib changes dramatically?)
* if the C lib changes dramatically?)
* `NotSupported`, if this `Device` does not support this feature
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `Unknown`, on any unexpected error
Expand Down Expand Up @@ -432,7 +432,7 @@ impl<'nvml> Device<'nvml> {
* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if this `Device` is invalid or `clock_type` is invalid (shouldn't occur?)
* `NotSupported`, if this `Device` or the `clock_type` on this `Device`
does not support this feature
* does not support this feature
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `Unknown`, on any unexpected error

Expand Down Expand Up @@ -926,6 +926,31 @@ impl<'nvml> Device<'nvml> {
}
}

/**
Gets GPU device hardware attributes

DeviceAttributes represents compute capabilities, Streaming MultiProcessor
capacity, slices allocated to a given GPU, decoding/encoding supported,
available memory for these GPU operations

# Errors
* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if this `Device` is invalid
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `Unknown`, on any unexpected error
*/
#[doc(alias = "nvmlDeviceGetAttributes_v2")]
pub fn attributes(&self) -> Result<DeviceAttributes, NvmlError> {
let sym = nvml_sym(self.nvml.lib.nvmlDeviceGetAttributes_v2.as_ref())?;

unsafe {
let mut attrs: nvmlDeviceAttributes_t = mem::zeroed();
nvml_try(sym(self.device, &mut attrs))?;

Ok(attrs.into())
}
}

/**
Gets the default applications clock that this `Device` boots with or defaults to after
`reset_applications_clocks()`.
Expand Down Expand Up @@ -1236,7 +1261,7 @@ impl<'nvml> Device<'nvml> {
* `Uninitialized`, if the library has not been successfully initialized
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `UnexpectedVariant`, if an enum variant not defined in this wrapper gets
returned in a field of an `EncoderSessionInfo` struct
* returned in a field of an `EncoderSessionInfo` struct
* `Unknown`, on any unexpected error

# Device Support
Expand Down Expand Up @@ -1899,7 +1924,7 @@ impl<'nvml> Device<'nvml> {
* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if `error_type`, `counter_type`, or `location` is invalid (shouldn't occur?)
* `NotSupported`, if this `Device` does not support ECC error reporting for the specified
memory
* memory
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `Unknown`, on any unexpected error

Expand Down Expand Up @@ -2821,7 +2846,7 @@ impl<'nvml> Device<'nvml> {

* `Uninitialized`, if the library has not been successfully initialized
* `IncorrectBits`, if NVML returns any bits that do not correspond to flags in
`ThrottleReasons`
* `ThrottleReasons`
* `NotSupported`, if this `Device` does not support this feature
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `Unknown`, on any unexpected error
Expand Down Expand Up @@ -6075,4 +6100,11 @@ mod test {
let nvml = nvml();
test_with_device(3, &nvml, |device| device.is_drain_enabled(None))
}

#[cfg(target_os = "linux")]
#[test]
fn device_attributes() {
let nvml = nvml();
test_with_device(3, &nvml, |device| device.attributes())
}
}
1 change: 0 additions & 1 deletion nvml-wrapper/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ mod test {
})
}

#[cfg(feature = "test-local")]
#[test]
fn wait() {
use crate::error::NvmlError;
Expand Down
40 changes: 40 additions & 0 deletions nvml-wrapper/src/struct_wrappers/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,46 @@ impl TryFrom<nvmlFBCSessionInfo_t> for FbcSessionInfo {
}
}

/// Hardware level attributes from a GPU device
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DeviceAttributes {
/// Streaming MultiProcessor Count
pub multiprocessor_count: u32,
/// Shared Copy Engine Count
pub shared_copy_engine_count: u32,
/// Shared Decoder Count
pub shared_decoder_count: u32,
/// Shared Encoder Count
pub shared_encoder_count: u32,
/// Shared JPEG Count
pub shared_jpeg_count: u32,
/// Shared OFA Count
pub shared_ofa_count: u32,
/// GPU instance slice Count
pub gpu_instance_slice_count: u32,
/// Compute Instance slice count
pub compute_instance_slice_count: u32,
/// Device memory size in MB
pub memory_size_mb: u64,
}

impl From<nvmlDeviceAttributes_t> for DeviceAttributes {
fn from(struct_: nvmlDeviceAttributes_t) -> Self {
Self {
multiprocessor_count: struct_.multiprocessorCount,
shared_copy_engine_count: struct_.sharedCopyEngineCount,
shared_decoder_count: struct_.sharedDecoderCount,
shared_encoder_count: struct_.sharedEncoderCount,
shared_jpeg_count: struct_.sharedJpegCount,
shared_ofa_count: struct_.sharedOfaCount,
gpu_instance_slice_count: struct_.gpuInstanceSliceCount,
compute_instance_slice_count: struct_.computeInstanceSliceCount,
memory_size_mb: struct_.memorySizeMB,
}
}
}

#[cfg(test)]
#[allow(unused_variables, unused_imports)]
mod tests {
Expand Down
5 changes: 3 additions & 2 deletions nvml-wrapper/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl ShouldPrint for BusType {}
impl ShouldPrint for PowerSource {}
impl ShouldPrint for DeviceArchitecture {}
impl ShouldPrint for PcieLinkMaxSpeed {}
impl ShouldPrint for DeviceAttributes {}

#[cfg(target_os = "windows")]
impl ShouldPrint for DriverModelState {}
Expand All @@ -126,9 +127,9 @@ where
T: Fn() -> Result<R, NvmlError>,
R: ShouldPrint,
{
single(|| test());
single(&test);

multi(reps, || test());
multi(reps, test);
}

pub fn test_with_device<T, R>(reps: usize, nvml: &Nvml, test: T)
Expand Down