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 DeviceAttributes and device::attributes(). #71

Open
wants to merge 1 commit 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
32 changes: 32 additions & 0 deletions nvml-wrapper/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
nvml: &'nvml Nvml,
}

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

Check warning on line 76 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

the following explicit lifetimes could be elided: 'nvml
unsafe impl<'nvml> Sync for Device<'nvml> {}

Check warning on line 77 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

the following explicit lifetimes could be elided: 'nvml

assert_impl_all!(Device: Send, Sync);

Expand Down Expand Up @@ -155,10 +155,10 @@

* `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?)

Check warning on line 158 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `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

Check warning on line 160 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
boosted clocks is not supported by this `Device`).

Check warning on line 161 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `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 @@

* `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?)

Check warning on line 195 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `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 @@
* `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

Check warning on line 435 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `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 @@
}
}

/**
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 @@
* `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

Check warning on line 1264 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `Unknown`, on any unexpected error

# Device Support
Expand Down Expand Up @@ -1899,7 +1924,7 @@
* `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

Check warning on line 1927 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `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 @@

* `Uninitialized`, if the library has not been successfully initialized
* `IncorrectBits`, if NVML returns any bits that do not correspond to flags in
`ThrottleReasons`

Check warning on line 2849 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `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 @@
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())
}
}
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
1 change: 1 addition & 0 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 Down
Loading