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>
impl<T: Send, F: Fn(usize) -> T + Sync> UseVec<T, F>
Sourcepub fn new(init: F) -> Self
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);Sourcepub fn into_vec(self) -> Vec<T>
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>
impl<T: Send, F: Fn(usize) -> T + Sync> Use for UseVec<T, F>
Source§unsafe fn init_get(&self, thread_idx: usize) -> &mut Self::Item
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 moreSource§impl<T: Send, F: Fn(usize) -> T + Sync> Use for &mut UseVec<T, F>
impl<T: Send, F: Fn(usize) -> T + Sync> Use for &mut UseVec<T, F>
Source§unsafe fn init_get(&self, thread_idx: usize) -> &mut Self::Item
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 moreAuto 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>where
ConcurrentOrderedBag<T>: Sync,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more