TrAcc

Struct TrAcc 

Source
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)));

Implementations§

Source§

impl<T: Copy + Send, F: Sync + FnAcc<T>> TrAcc<T, F>

Source

pub fn new(acc_fn: F, identity: T) -> Self

Create a a TrAcc.

Source

pub fn acc(&self, x: T)

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.

Source

pub fn get(&self) -> T

Return the current state.

This function has Acquire semantic w.r.t. the accumulator.

Trait Implementations§

Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, F> !Freeze for TrAcc<T, F>

§

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

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.