Struct hytra::TrAcc [−][src]
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)));
Implementations
impl<T: Copy + Send, F: Sync + FnAcc<T>> TrAcc<T, F>
[src]
pub fn new(acc_fn: F, identity: T) -> Self
[src]
Create a a TrAcc
.
pub fn acc(&self, x: T)
[src]
Accumulate x
. If state
is the current state, the new state is fn_acc(state, x)
.
This function has Release
semantic w.r.t. the accumulator.
pub fn get(&self) -> T
[src]
Return the current state.
This function has Acquire
semantic w.r.t. the accumulator.
Trait Implementations
Auto Trait Implementations
impl<T, F> RefUnwindSafe for TrAcc<T, F> where
F: RefUnwindSafe,
T: RefUnwindSafe,
F: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, F> Send for TrAcc<T, F> where
F: Send,
F: Send,
impl<T, F> Sync for TrAcc<T, F> where
F: Sync,
T: Sync,
F: Sync,
T: Sync,
impl<T, F> Unpin for TrAcc<T, F> where
F: Unpin,
T: Unpin,
F: Unpin,
T: Unpin,
impl<T, F> UnwindSafe for TrAcc<T, F> where
F: UnwindSafe,
T: UnwindSafe,
F: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Pointable for T
pub const ALIGN: usize
type Init = T
The type for initializers.
pub unsafe fn init(init: <T as Pointable>::Init) -> usize
pub unsafe fn deref<'a>(ptr: usize) -> &'a T
pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T
pub unsafe fn drop(ptr: usize)
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,