pub struct Bitmap { /* private fields */ }
Expand description
A generic bitmap, understood by hwloc.
The Bitmap
represents a set of objects, typically OS processors – which may actually be
hardware threads (represented by CpuSet
, which is a type alias for Bitmap
– or memory
nodes (represented by NodeSet
, which is also a typedef for Bitmap
).
Both CpuSet
and NodeSet
are always indexed by OS physical number.
A Bitmap
may be of infinite size.
Implementations§
Source§impl Bitmap
impl Bitmap
Sourcepub fn new() -> Bitmap
pub fn new() -> Bitmap
Creates an empty Bitmap
.
Examples:
use hwloc::Bitmap;
let bitmap = Bitmap::new();
assert_eq!("", format!("{}", bitmap));
assert_eq!(true, bitmap.is_empty());
Sourcepub fn full() -> Bitmap
pub fn full() -> Bitmap
Creates a full Bitmap
.
Examples:
use hwloc::Bitmap;
let bitmap = Bitmap::full();
assert_eq!("0-", format!("{}", bitmap));
assert_eq!(false, bitmap.is_empty());
Sourcepub fn from(id: u32) -> Bitmap
pub fn from(id: u32) -> Bitmap
Creates a new HwlocBitmap (either CpuSet or NodeSet) and sets one index right away.
Examples:
use hwloc::Bitmap;
let bitmap = Bitmap::from(1);
assert_eq!("1", format!("{}", bitmap));
assert_eq!(false, bitmap.is_empty());
Sourcepub fn from_range(begin: u32, end: i32) -> Bitmap
pub fn from_range(begin: u32, end: i32) -> Bitmap
Creates a new Bitmap
with the given range.
Examples:
use hwloc::Bitmap;
let bitmap = Bitmap::from_range(0, 5);
assert_eq!("0-5", format!("{}", bitmap));
Sourcepub fn from_raw(bitmap: *mut IntHwlocBitmap, manage: bool) -> Bitmap
pub fn from_raw(bitmap: *mut IntHwlocBitmap, manage: bool) -> Bitmap
Wraps the given hwloc bitmap pointer into its Bitmap
representation.
This function is not meant to be used directly, it rather serves as the conversion factory when dealing with hwloc-internal structures.
Sourcepub fn as_ptr(&self) -> *const IntHwlocBitmap
pub fn as_ptr(&self) -> *const IntHwlocBitmap
Returns the containted hwloc bitmap pointer for interaction with hwloc.
Sourcepub fn set(&mut self, id: u32)
pub fn set(&mut self, id: u32)
Set index id
in this Bitmap
.
Examples:
use hwloc::Bitmap;
let mut bitmap = Bitmap::new();
bitmap.set(4);
assert_eq!("4", format!("{}", bitmap));
Sourcepub fn set_range(&mut self, begin: u32, end: i32)
pub fn set_range(&mut self, begin: u32, end: i32)
Add indexes from begin
to end
in this Bitmap
.
If end is -1, the range is infinite.
Examples:
use hwloc::Bitmap;
let mut bitmap = Bitmap::new();
bitmap.set_range(3, 5);
assert_eq!("3-5", format!("{}", bitmap));
bitmap.set_range(2, -1);
assert_eq!("2-", format!("{}", bitmap));
Sourcepub fn unset(&mut self, id: u32)
pub fn unset(&mut self, id: u32)
Remove index id
from the Bitmap
.
Examples:
use hwloc::Bitmap;
let mut bitmap = Bitmap::from_range(1,3);
bitmap.unset(1);
assert_eq!("2-3", format!("{}", bitmap));
Sourcepub fn unset_range(&mut self, begin: u32, end: i32)
pub fn unset_range(&mut self, begin: u32, end: i32)
Remove indexes from begin
to end
in this Bitmap
.
If end is -1, the range is infinite.
Examples:
use hwloc::Bitmap;
let mut bitmap = Bitmap::from_range(1,5);
bitmap.unset_range(4,6);
assert_eq!("1-3", format!("{}", bitmap));
bitmap.unset_range(2,-1);
assert_eq!("1", format!("{}", bitmap));
Sourcepub fn weight(&self) -> i32
pub fn weight(&self) -> i32
The number of indexes that are in the bitmap.
Examples:
use hwloc::Bitmap;
let mut bitmap = Bitmap::from_range(1,5);
assert_eq!(5, bitmap.weight());
bitmap.unset(3);
assert_eq!(4, bitmap.weight());
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the Bitmap
.
Examples:
use hwloc::Bitmap;
let mut bitmap = Bitmap::from_range(1,5);
assert_eq!(5, bitmap.weight());
assert_eq!(false, bitmap.is_empty());
bitmap.clear();
assert_eq!(0, bitmap.weight());
assert_eq!(true, bitmap.is_empty());
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks if this Bitmap
has indexes set.
Examples:
use hwloc::Bitmap;
let mut bitmap = Bitmap::new();
assert_eq!(true, bitmap.is_empty());
bitmap.set(3);
assert_eq!(false, bitmap.is_empty());
bitmap.clear();
assert_eq!(true, bitmap.is_empty());
Sourcepub fn is_set(&self, id: u32) -> bool
pub fn is_set(&self, id: u32) -> bool
Check if the field with the given id is set.
Examples:
use hwloc::Bitmap;
let mut bitmap = Bitmap::new();
assert_eq!(false, bitmap.is_set(2));
bitmap.set(2);
assert_eq!(true, bitmap.is_set(2));
Sourcepub fn singlify(&mut self)
pub fn singlify(&mut self)
Keep a single index among those set in the bitmap.
May be useful before binding so that the process does not have a chance of migrating between multiple logical CPUs in the original mask.
Examples:
use hwloc::Bitmap;
let mut bitmap = Bitmap::new();
bitmap.set_range(0, 127);
assert_eq!(128, bitmap.weight());
bitmap.invert();
assert_eq!(-1, bitmap.weight());
bitmap.singlify();
assert_eq!(1, bitmap.weight());
assert_eq!(128, bitmap.first());
assert_eq!(128, bitmap.last());
Examples found in repository?
19fn main() {
20 let mut topo = Topology::new();
21
22 // Grab last core and exctract its CpuSet
23 let mut cpuset = last_core(&mut topo).cpuset().unwrap();
24
25 // Get only one logical processor (in case the core is SMT/hyper-threaded).
26 cpuset.singlify();
27
28 // Print the current cpu binding before explicit setting
29 println!("Cpu Binding before explicit bind: {:?}",
30 topo.get_cpubind(CPUBIND_PROCESS));
31 println!("Cpu Location before explicit bind: {:?}",
32 topo.get_cpu_location(CPUBIND_PROCESS));
33
34 // Try to bind all threads of the current (possibly multithreaded) process.
35 match topo.set_cpubind(cpuset, CPUBIND_PROCESS) {
36 Ok(_) => println!("Correctly bound to last core"),
37 Err(e) => println!("Failed to bind: {:?}", e),
38 }
39
40 // Print the current cpu binding after explicit setting
41 println!("Cpu Binding after explicit bind: {:?}",
42 topo.get_cpubind(CPUBIND_PROCESS));
43 println!("Cpu Location after explicit bind: {:?}",
44 topo.get_cpu_location(CPUBIND_PROCESS));
45}
More examples
12fn main() {
13 let mut topo = Topology::new();
14
15 // load the current pid through libc
16 let pid = get_pid();
17
18 println!("Binding Process with PID {:?}", pid);
19
20 // Grab last core and exctract its CpuSet
21 let mut cpuset = last_core(&mut topo).cpuset().unwrap();
22
23 // Get only one logical processor (in case the core is SMT/hyper-threaded).
24 cpuset.singlify();
25
26 println!("Before Bind: {:?}",
27 topo.get_cpubind_for_process(pid, CPUBIND_PROCESS)
28 .unwrap());
29
30 // Last CPU Location for this PID (not implemented on all systems)
31 if let Some(l) = topo.get_cpu_location_for_process(pid, CPUBIND_PROCESS) {
32 println!("Last Known CPU Location: {:?}", l);
33 }
34
35 // Bind to one core.
36 topo.set_cpubind_for_process(pid, cpuset, CPUBIND_PROCESS)
37 .unwrap();
38
39 println!("After Bind: {:?}",
40 topo.get_cpubind_for_process(pid, CPUBIND_PROCESS)
41 .unwrap());
42
43 // Last CPU Location for this PID (not implemented on all systems)
44 if let Some(l) = topo.get_cpu_location_for_process(pid, CPUBIND_PROCESS) {
45 println!("Last Known CPU Location: {:?}", l);
46 }
47}
21fn main() {
22 let topo = Arc::new(Mutex::new(Topology::new()));
23
24 // Grab the number of cores in a block so that the lock is removed once
25 // the end of the block is reached.
26 let num_cores = {
27 let topo_rc = topo.clone();
28 let topo_locked = topo_rc.lock().unwrap();
29 (*topo_locked)
30 .objects_with_type(&ObjectType::Core)
31 .unwrap()
32 .len()
33 };
34 println!("Found {} cores.", num_cores);
35
36 // Spawn one thread for each and pass the topology down into scope.
37 let handles: Vec<_> = (0..num_cores)
38 .map(|i| {
39 let child_topo = topo.clone();
40 thread::spawn(move || {
41 // Get the current thread id and lock the topology to use.
42 let tid = get_thread_id();
43 let mut locked_topo = child_topo.lock().unwrap();
44
45 // Thread binding before explicit set.
46 let before = locked_topo.get_cpubind_for_thread(tid, CPUBIND_THREAD);
47
48 // load the cpuset for the given core index.
49 let mut bind_to = cpuset_for_core(&*locked_topo, i);
50
51 // Get only one logical processor (in case the core is SMT/hyper-threaded).
52 bind_to.singlify();
53
54 // Set the binding.
55 locked_topo
56 .set_cpubind_for_thread(tid, bind_to, CPUBIND_THREAD)
57 .unwrap();
58
59 // Thread binding after explicit set.
60 let after = locked_topo.get_cpubind_for_thread(tid, CPUBIND_THREAD);
61 println!("Thread {}: Before {:?}, After {:?}", i, before, after);
62 })
63 })
64 .collect();
65
66 // Wait for all threads to complete before ending the program.
67 for h in handles {
68 h.join().unwrap();
69 }
70}
Sourcepub fn invert(&mut self)
pub fn invert(&mut self)
Inverts the current Bitmap
.
Examples:
use hwloc::Bitmap;
let mut bitmap = Bitmap::new();
bitmap.set(3);
assert_eq!("3", format!("{}", bitmap));
assert_eq!("0-2,4-", format!("{}", !bitmap));
Sourcepub fn first(&self) -> i32
pub fn first(&self) -> i32
Compute the first index (least significant bit) in this Bitmap
.
Returns -1 if no index is set.
Examples:
use hwloc::Bitmap;
let bitmap = Bitmap::from_range(4,10);
assert_eq!(4, bitmap.first());