Struct ppl::thread_pool::ThreadPool

source ·
pub struct ThreadPool { /* private fields */ }
Expand description

Struct representing a thread pool.

Implementations§

source§

impl ThreadPool

source

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();
source

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

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.

source

pub fn is_empty(&self) -> bool

Check if there are jobs in the thread pool.

source

pub fn wait(&self)

Block until all current jobs in the thread pool are finished.

source

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.

source

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

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();
source

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

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

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

Trait Implementations§

source§

impl Clone for ThreadPool

source§

fn clone(&self) -> Self

Create a new thread pool from an existing one, using the same number of threads.

1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Default for ThreadPool

source§

fn default() -> Self

Create a new thread pool with all the availables threads.

Examples
use ppl::thread_pool::ThreadPool;
     
let mut pool = ThreadPool::default();
source§

impl Drop for ThreadPool

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> DynClone for Twhere T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.