Struct hytra::TrAcc[][src]

pub struct TrAcc<T: Copy + Send, F: FnAcc<T>> { /* fields omitted */ }

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

impl<T: Debug + Copy + Send, F: Debug + FnAcc<T>> Debug for TrAcc<T, F>[src]

Auto Trait Implementations

impl<T, F> RefUnwindSafe for TrAcc<T, F> where
    F: RefUnwindSafe,
    T: RefUnwindSafe

impl<T, F> Send for TrAcc<T, F> where
    F: Send

impl<T, F> Sync for TrAcc<T, F> where
    F: Sync,
    T: Sync

impl<T, F> Unpin for TrAcc<T, F> where
    F: Unpin,
    T: Unpin

impl<T, F> UnwindSafe for TrAcc<T, F> where
    F: UnwindSafe,
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pointable for T

type Init = T

The type for initializers.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.