Skip to content

Commit 7ea1bc4

Browse files
authored
adding device::cpu_affinity_within_scope. (#79)
Adds a new cpu affinity function which can narrow down the affinity to a specific context, NUMA or sockets.
1 parent c42a7a7 commit 7ea1bc4

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

nvml-wrapper/src/device.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4090,6 +4090,58 @@ impl<'nvml> Device<'nvml> {
40904090
unsafe { nvml_try(sym(self.device)) }
40914091
}
40924092

4093+
/**
4094+
Gets a vector of bitmasks with the ideal CPU affinity for this `Device` within the specified `scope`,
4095+
the latter being NUMA node or processor socket (`NVML_AFFINITY_SCOPE_NODE` and `NVML_AFFINITY_SCOPE_SOCKET`).
4096+
4097+
Beyond this, the outcome and meaning are similar to `cpu_affinity`
4098+
4099+
# Errors
4100+
4101+
* `Uninitialized`, if the library has not been successfully initialized
4102+
* `InvalidArg`, if this `Device` is invalid
4103+
* `InsufficientSize`, if the passed-in `size` is 0 (must be > 0)
4104+
* `NotSupported`, if this `Device` does not support this feature
4105+
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
4106+
* `Unknown`, on any unexpected error
4107+
4108+
# Device Support
4109+
4110+
Supports Kepler or newer fully supported devices.
4111+
4112+
# Platform Support
4113+
4114+
Only supports Linux.
4115+
4116+
*/
4117+
#[cfg(target_os = "linux")]
4118+
#[doc(alias = "nvmlDeviceGetCpuAffinityWithinScope")]
4119+
pub fn cpu_affinity_within_scope(
4120+
&self,
4121+
size: usize,
4122+
scope: nvmlAffinityScope_t,
4123+
) -> Result<Vec<c_ulong>, NvmlError> {
4124+
let sym = nvml_sym(self.nvml.lib.nvmlDeviceGetCpuAffinityWithinScope.as_ref())?;
4125+
4126+
unsafe {
4127+
if size == 0 {
4128+
// Return an error containing the minimum size that can be passed.
4129+
return Err(NvmlError::InsufficientSize(Some(1)));
4130+
}
4131+
4132+
let mut affinities: Vec<c_ulong> = vec![mem::zeroed(); size];
4133+
4134+
nvml_try(sym(
4135+
self.device,
4136+
size as c_uint,
4137+
affinities.as_mut_ptr(),
4138+
scope,
4139+
))?;
4140+
4141+
Ok(affinities)
4142+
}
4143+
}
4144+
40934145
/**
40944146
Try to set the default state of auto boosted clocks on this `Device`.
40954147
@@ -5550,6 +5602,13 @@ mod test {
55505602
test_with_device(3, &nvml, |device| device.cpu_affinity(64))
55515603
}
55525604

5605+
#[cfg(target_os = "linux")]
5606+
#[test]
5607+
fn cpu_affinity_within_scope() {
5608+
let nvml = nvml();
5609+
test_with_device(3, &nvml, |device| device.cpu_affinity_within_scope(64, 0))
5610+
}
5611+
55535612
#[test]
55545613
fn current_pcie_link_gen() {
55555614
let nvml = nvml();

0 commit comments

Comments
 (0)