Skip to main content

UseVec

Struct UseVec 

Source
pub struct UseVec<T: Send, F: Fn(usize) -> T + Sync> { /* private fields */ }
Expand description

Owned worker-local mutable state.

UseVec stores one value per worker thread and lets parallel operations mutate those values independently. It is typically used with Par::use_vec.

§Examples

use orx_parallel::*;

let n = 10_000usize;
let mut partial_sums = UseVec::new(|_| 0usize);

(0..n)
    .into_par()
    .use_vec(&mut partial_sums)
    .for_each(|thread_sum, x| *thread_sum += x);

let total: usize = partial_sums.into_vec().into_iter().sum();
assert_eq!(total, (n - 1) * n / 2);

Implementations§

Source§

impl<T: Send, F: Fn(usize) -> T + Sync> UseVec<T, F>

Source

pub fn new(init: F) -> Self

Creates a UseVec with per-thread initialization logic.

The init function receives the worker thread_idx and is invoked on first access to create that thread’s local value.

§Example
use orx_parallel::*;

let mut calls = UseVec::new(|_| 0usize);

let values: Vec<_> = (0..128usize)
    .into_par()
    .use_vec(&mut calls)
    .map(|count, x| {
        *count += 1;
        x * 2
    })
    .collect();

assert_eq!(values.len(), 128);
assert_eq!(calls.into_vec().into_iter().sum::<usize>(), 128);
Source

pub fn into_vec(self) -> Vec<T>

Consumes the UseVec and returns the per-thread values as a Vec.

This is typically used after a parallel computation to aggregate thread-local state.

§Example
use orx_parallel::*;

let mut partial_sums = UseVec::new(|_| 0usize);

(0..100usize)
    .into_par()
    .num_threads(4)
    .use_vec(&mut partial_sums)
    .for_each(|thread_sum, x| *thread_sum += x);

let partials = partial_sums.into_vec();
assert_eq!(partials.into_iter().sum::<usize>(), 4950);

Trait Implementations§

Source§

impl<T: Send, F: Fn(usize) -> T + Sync> Use for UseVec<T, F>

Source§

type Item = T

Type of the worker-local mutable value stored for each thread.
Source§

unsafe fn init_get(&self, thread_idx: usize) -> &mut Self::Item

Returns the mutable worker-local value for thread_idx, creating it if needed. Read more
Source§

fn get(&mut self, thread_idx: usize) -> &mut Self::Item

Returns the already-initialized mutable worker-local value for thread_idx. Read more
Source§

fn max_threads(&self) -> Option<usize>

Returns an upper bound on the number of worker threads that may use this value. Read more
Source§

impl<T: Send, F: Fn(usize) -> T + Sync> Use for &mut UseVec<T, F>

Source§

type Item = T

Type of the worker-local mutable value stored for each thread.
Source§

unsafe fn init_get(&self, thread_idx: usize) -> &mut Self::Item

Returns the mutable worker-local value for thread_idx, creating it if needed. Read more
Source§

fn get(&mut self, thread_idx: usize) -> &mut Self::Item

Returns the already-initialized mutable worker-local value for thread_idx. Read more
Source§

fn max_threads(&self) -> Option<usize>

Returns an upper bound on the number of worker threads that may use this value. Read more

Auto Trait Implementations§

§

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

§

impl<T, F> !RefUnwindSafe for UseVec<T, F>

§

impl<T, F> Send for UseVec<T, F>

§

impl<T, F> Sync for UseVec<T, F>

§

impl<T, F> Unpin for UseVec<T, F>

§

impl<T, F> UnsafeUnpin for UseVec<T, F>

§

impl<T, F> UnwindSafe for UseVec<T, F>

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> SoM<T> for T

Source§

fn get_ref(&self) -> &T

Returns a reference to self.
Source§

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

Returns a mutable reference to self.
Source§

impl<T> SoR<T> for T

Source§

fn get_ref(&self) -> &T

Returns a reference to self.
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.