-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for atomic parallel modification
- Loading branch information
1 parent
21051f2
commit 900ce9d
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use image_texel::texels::{AtomicBuffer, U32}; | ||
use std::{mem, thread}; | ||
|
||
#[test] | ||
fn mapping_atomics_parallel() { | ||
const LEN: usize = 128; | ||
let buffer = AtomicBuffer::new(LEN * mem::size_of::<u32>()); | ||
// And receive all the results in this shared copy of our buffer. | ||
let output_tap = buffer.clone(); | ||
|
||
const SPLIT_MAX: usize = 1 << 6; | ||
// Proxy for whether we run with optimization. Makes execution time bearable. | ||
#[cfg(debug_assertions)] | ||
const REPEAT: usize = 1 << 6; | ||
#[cfg(not(debug_assertions))] | ||
const REPEAT: usize = 1 << 12; | ||
|
||
for split in 0..SPLIT_MAX { | ||
// We want the modifying loops to overlap as much as possible for the strongest test, so | ||
// ensure they do not run early. | ||
let barrier = &std::sync::Barrier::new(2); | ||
|
||
// Concurrently and repeatedly increment non-overlapping parts of the image. | ||
thread::scope(|join| { | ||
let img_a = buffer.clone(); | ||
let img_b = buffer.clone(); | ||
|
||
join.spawn(move || { | ||
let _ = barrier.wait(); | ||
for _ in 0..REPEAT { | ||
img_a.map_within(..split, 0, |n: u32| n + 1, U32, U32); | ||
} | ||
}); | ||
|
||
join.spawn(move || { | ||
let _ = barrier.wait(); | ||
for _ in 0..REPEAT { | ||
img_b.map_within(split.., split, |n: u32| n + 1, U32, U32); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
// Each individual `u32` has been incremented precisely as often as each other. Since the | ||
// individual transforms are synchronized with thread-scope and within they do not overlap with | ||
// each other, we must expect that the values have each been touched precisely how we intended | ||
// them to. | ||
let expected = (SPLIT_MAX * REPEAT) as u32; | ||
assert_eq!( | ||
output_tap.to_owned().as_texels(U32)[..LEN].to_vec(), | ||
(0..LEN as u32).map(|_| expected).collect::<Vec<_>>() | ||
); | ||
} |