Struct ppl::thread_pool::ThreadPool
source · pub struct ThreadPool { /* private fields */ }Expand description
Struct representing a thread pool.
Implementations§
source§impl ThreadPool
impl ThreadPool
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new thread pool.
If the environment variable PPL_MAX_CORES is set, the capacity of the thread
pool is set to that value. Otherwise, the number of logical threads available
on the host machine is used instead.
Examples
use ppl::thread_pool::ThreadPool;
let mut pool = ThreadPool::new();sourcepub fn with_capacity(num_threads: usize) -> Self
pub fn with_capacity(num_threads: usize) -> Self
Create a new thread pool with num_threads threads.
Examples
use ppl::thread_pool::ThreadPool;
let mut pool = ThreadPool::with_capacity(8);sourcepub fn execute<F>(&self, task: F)where
F: FnOnce() + Send + 'static,
pub fn execute<F>(&self, task: F)where F: FnOnce() + Send + 'static,
Execute a function task on a thread in the thread pool.
This method is non-blocking, so the developer must call wait to wait for the task to finish.
sourcepub fn par_for<F>(&mut self, range: Range<usize>, chunk_size: usize, f: F)where
F: FnMut(usize) + Send + Copy,
pub fn par_for<F>(&mut self, range: Range<usize>, chunk_size: usize, f: F)where F: FnMut(usize) + Send + Copy,
Given a function f, a range of indices range, and a chunk size chunk_size,
it distributes works of size chunk_size to the threads in the pool.
The function f is applied to each element in the range.
The range is split in chunks of size chunk_size and each chunk is assigned to a thread.
sourcepub fn par_for_each<Iter, F>(&mut self, iter: Iter, f: F)where
F: FnOnce(Iter::Item) + Send + Copy,
<Iter as IntoIterator>::Item: Send,
Iter: IntoIterator,
pub fn par_for_each<Iter, F>(&mut self, iter: Iter, f: F)where F: FnOnce(Iter::Item) + Send + Copy, <Iter as IntoIterator>::Item: Send, Iter: IntoIterator,
Applies in parallel the function f on a iterable object iter.
Examples
Increment of 1 all the elements in a vector concurrently:
use ppl::thread_pool::ThreadPool;
let mut pool = ThreadPool::new();
let mut vec = vec![0; 100];
pool.par_for_each(&mut vec, |el: &mut i32| *el = *el + 1);sourcepub fn par_map<Iter, F, R>(
&mut self,
iter: Iter,
f: F
) -> impl Iterator<Item = R>where
F: FnOnce(Iter::Item) -> R + Send + Copy,
<Iter as IntoIterator>::Item: Send,
R: Send + 'static,
Iter: IntoIterator,
pub fn par_map<Iter, F, R>( &mut self, iter: Iter, f: F ) -> impl Iterator<Item = R>where F: FnOnce(Iter::Item) -> R + Send + Copy, <Iter as IntoIterator>::Item: Send, R: Send + 'static, Iter: IntoIterator,
Applies in parallel the function f on a iterable object iter,
producing a new iterator with the results.
Examples
Produce a vec of String from the elements of a vector vec concurrently:
use ppl::thread_pool::ThreadPool;
let mut pool = ThreadPool::new();
let mut vec = vec![0i32; 100];
let res: Vec<String> = pool.par_map(&mut vec, |el| -> String {
String::from("Hello from: ".to_string() + &el.to_string())
}).collect();sourcepub fn par_map_reduce<Iter, F, K, V, R, Reduce>(
&mut self,
iter: Iter,
f: F,
reduce: Reduce
) -> impl Iterator<Item = (K, R)>where
F: FnOnce(Iter::Item) -> (K, V) + Send + Copy,
<Iter as IntoIterator>::Item: Send,
K: Send + Ord + 'static,
V: Send + 'static,
R: Send + 'static,
Reduce: FnOnce(K, Vec<V>) -> (K, R) + Send + Copy,
Iter: IntoIterator,
pub fn par_map_reduce<Iter, F, K, V, R, Reduce>( &mut self, iter: Iter, f: F, reduce: Reduce ) -> impl Iterator<Item = (K, R)>where F: FnOnce(Iter::Item) -> (K, V) + Send + Copy, <Iter as IntoIterator>::Item: Send, K: Send + Ord + 'static, V: Send + 'static, R: Send + 'static, Reduce: FnOnce(K, Vec<V>) -> (K, R) + Send + Copy, Iter: IntoIterator,
Parallel Map Reduce.
Applies in parallel the function f on a iterable object iter,
producing a new object with the results.
The function f must return a tuple of two elements, the first one
is the key and the second one is the value.
The results are grouped by key and reduced by the function reduce.
The function reduce must take two arguments, the first one is the
key and the second one is a vector of values.
The function reduce must return a tuple of two elements, the first one
is the key and the second one is the value.
This method return an iterator of tuples of two elements, the first one
is the key and the second one is the value.
Examples
use ppl::thread_pool::ThreadPool;
let mut pool = ThreadPool::with_capacity(8);
let mut vec = Vec::new();
for i in 0..100 {
vec.push(i);
}
let res: Vec<(i32, i32)> = pool.par_map_reduce(&mut vec, |el| -> (i32, i32) {
(*el % 10, *el)
}, |k, v| -> (i32, i32) {
(k, v.iter().sum())
}).collect();
assert_eq!(res.len(), 10);sourcepub fn par_reduce<Iter, K, V, R, F>(
&mut self,
iter: Iter,
f: F
) -> impl Iterator<Item = (K, R)>where
<Iter as IntoIterator>::Item: Send,
K: Send + Ord + 'static,
V: Send + 'static,
R: Send + 'static,
F: FnOnce(K, Vec<V>) -> (K, R) + Send + Copy,
Iter: IntoIterator<Item = (K, V)>,
pub fn par_reduce<Iter, K, V, R, F>( &mut self, iter: Iter, f: F ) -> impl Iterator<Item = (K, R)>where <Iter as IntoIterator>::Item: Send, K: Send + Ord + 'static, V: Send + 'static, R: Send + 'static, F: FnOnce(K, Vec<V>) -> (K, R) + Send + Copy, Iter: IntoIterator<Item = (K, V)>,
Reduces in parallel the elements of an iterator iter by the function f.
The function f must take two arguments, the first one is the
key and the second one is a vector of values.
The function f must return a tuple of two elements, the first one
is the key and the second one is the value.
This method take in input an iterator, it groups the elements by key and then
reduces them by the function f.
This method return an iterator of tuples of two elements, the first one
is the key and the second one is the value obtained by the function f.
Examples
use ppl::thread_pool::ThreadPool;
let mut pool = ThreadPool::new();
let mut vec = Vec::new();
for i in 0..100 {
vec.push((i % 10, i));
}
let res: Vec<(i32, i32)> = pool.par_reduce(vec, |k, v| -> (i32, i32) {
(k, v.iter().sum())
}).collect();
assert_eq!(res.len(), 10);sourcepub fn scope<'pool, 'scope, F, R>(&'pool mut self, f: F) -> Rwhere
F: FnOnce(&Scope<'pool, 'scope>) -> R,
pub fn scope<'pool, 'scope, F, R>(&'pool mut self, f: F) -> Rwhere F: FnOnce(&Scope<'pool, 'scope>) -> R,
Create a new scope to execute jobs on other threads.
The function passed to this method will be provided with a Scope object,
which can be used to spawn new jobs through the Scope::execute method.
The scope will block the current thread until all jobs spawned from this scope
have completed.
Examples
use ppl::thread_pool::ThreadPool;
let mut pool = ThreadPool::new();
let mut vec = vec![0; 100];
pool.scope(|scope| {
for el in &mut vec {
scope.execute(move || {
*el += 1;
});
}
});
assert_eq!(vec.iter().sum::<i32>(), 100);