From 2eb95d1e693abc6d0915b26d59be693913d79063 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Fri, 14 Feb 2025 22:10:58 +0000 Subject: [PATCH 1/3] Add DeviceAttributes and device::attributes(). mapping nvmlDeviceAttributes_st and nvmlDeviceGetAttributes_v2 respectively. --- nvml-wrapper/src/device.rs | 32 +++++++++++++++++ nvml-wrapper/src/struct_wrappers/device.rs | 40 ++++++++++++++++++++++ nvml-wrapper/src/test_utils.rs | 1 + 3 files changed, 73 insertions(+) diff --git a/nvml-wrapper/src/device.rs b/nvml-wrapper/src/device.rs index 2e46a24..c7bb2cb 100644 --- a/nvml-wrapper/src/device.rs +++ b/nvml-wrapper/src/device.rs @@ -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 { + 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()`. @@ -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()) + } } diff --git a/nvml-wrapper/src/struct_wrappers/device.rs b/nvml-wrapper/src/struct_wrappers/device.rs index 213d16b..8d2d2d7 100644 --- a/nvml-wrapper/src/struct_wrappers/device.rs +++ b/nvml-wrapper/src/struct_wrappers/device.rs @@ -656,6 +656,46 @@ impl TryFrom 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 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 { diff --git a/nvml-wrapper/src/test_utils.rs b/nvml-wrapper/src/test_utils.rs index 9a6a010..e967e06 100644 --- a/nvml-wrapper/src/test_utils.rs +++ b/nvml-wrapper/src/test_utils.rs @@ -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 {} From 133764df26f6e2fa0d85bb6aeadedc8be9a629aa Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 18 Mar 2025 19:43:48 +0000 Subject: [PATCH 2/3] address clippy lifetime eliding --- nvml-wrapper/src/device.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nvml-wrapper/src/device.rs b/nvml-wrapper/src/device.rs index c7bb2cb..ab31fd2 100644 --- a/nvml-wrapper/src/device.rs +++ b/nvml-wrapper/src/device.rs @@ -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); From 0c08f825c424a565801a1310a1f7c9e6cd0c561d Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 18 Mar 2025 19:50:52 +0000 Subject: [PATCH 3/3] fix docs identation attempt and test-local feature had been removed --- nvml-wrapper/src/device.rs | 16 ++++++++-------- nvml-wrapper/src/event.rs | 1 - nvml-wrapper/src/test_utils.rs | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/nvml-wrapper/src/device.rs b/nvml-wrapper/src/device.rs index ab31fd2..7d984c9 100644 --- a/nvml-wrapper/src/device.rs +++ b/nvml-wrapper/src/device.rs @@ -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 @@ -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 @@ -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 @@ -1261,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 @@ -1924,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 @@ -2846,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 diff --git a/nvml-wrapper/src/event.rs b/nvml-wrapper/src/event.rs index a99d56e..0d5cd72 100644 --- a/nvml-wrapper/src/event.rs +++ b/nvml-wrapper/src/event.rs @@ -172,7 +172,6 @@ mod test { }) } - #[cfg(feature = "test-local")] #[test] fn wait() { use crate::error::NvmlError; diff --git a/nvml-wrapper/src/test_utils.rs b/nvml-wrapper/src/test_utils.rs index e967e06..72f1b8d 100644 --- a/nvml-wrapper/src/test_utils.rs +++ b/nvml-wrapper/src/test_utils.rs @@ -127,9 +127,9 @@ where T: Fn() -> Result, R: ShouldPrint, { - single(|| test()); + single(&test); - multi(reps, || test()); + multi(reps, test); } pub fn test_with_device(reps: usize, nvml: &Nvml, test: T)