pub struct TrAcc<T: Copy + Send, F: FnAcc<T>> { /* private fields */ }Expand description
The threaded accumulator allows to accumulate data in a single state from multiple threads without contention, which allows performance to scale well with the number of thread/processors.
The accumulation function must be associative an commutative, and the identity element must be
the neutral element w.r.t. the accumulation function.
The accumulated state can be any Copy + Send state, and the implementation uses atomic
instructions if supported by the architecture for the size of T (in which case this
datastructure is lock-free), and mutexes otherwise.
This optimizes for accumulation speed, at the expense of increased memory usage (the state is replicated once for each thread) and cost of reading the accumulated state (which has to walk over the states of each thread).
use hytra::TrAcc;
let acc: TrAcc<i64, _> = TrAcc::new(|a, b| a*b, 1);
let acc_ref = &acc;
crossbeam_utils::thread::scope(|s| {
for j in 1..=2 {
s.spawn(move |_| {
for i in 1..=3 {
acc_ref.acc(i*j);
}
});
}
})
.unwrap();
assert_eq!(acc.get(), (1*2*3)*((2*1)*(2*2)*(2*3)));