-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlockfree.rs
46 lines (37 loc) · 1.16 KB
/
lockfree.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
pub mod lockfree
{
use crate::structure::AlignedVec;
use rayon::prelude::*;
pub fn run(n: usize, threads: usize) {
// Initialize the vector
let mut algn_buffer = AlignedVec::new(n, 64);
let chunk_size = n / threads;
let mut init = 0;
for i in 0..threads {
let buf = algn_buffer.as_mut_slice();
let start = i * chunk_size;
for j in start..(start + chunk_size) {
buf[j] = init;
}
init += 1;
}
// Print the initialized vector
println!("Vector after initialization:");
print_vec(&algn_buffer);
let algn_slice = &mut algn_buffer.as_mut_slice()[..];
algn_slice.par_chunks_mut(chunk_size).for_each(|chunk| {
for num in chunk.iter_mut() {
*num += 1;
}
});
// Print the modified vector
println!("Vector after modification:");
print_vec(&algn_buffer);
}
fn print_vec(vec: &AlignedVec<i32>) {
let nums: Vec<i32> = (0..vec.get().len())
.map(|i| vec.get()[i])
.collect();
println!("{:?}", nums);
}
}