RunnerWithPool

Struct RunnerWithPool 

Source
pub struct RunnerWithPool<P, R = FixedChunkRunner>{ /* private fields */ }
Expand description

Parallel runner with a given pool of type P and parallel executor of R.

A RunnerWithPool can always be created from owned pool implementing ParThreadPool, but also from

  • &pool in most cases,
  • &mut pool in others.

Note that default parallel runner; i.e., DefaultRunner is:

  • RunnerWithPool<StdDefaultPool> when โ€œstdโ€ feature is enabled,
  • RunnerWithPool<SequentialPool> when โ€œstdโ€ feature is disabled.

ยงExamples

use orx_parallel::*;

// parallel computation generic over parallel runner; and hence, the thread pool
fn run_with_runner<R: ParallelRunner>(runner: R, input: &[usize]) -> Vec<String> {
    input
        .par()
        .with_runner(runner)
        .flat_map(|x| [*x, 2 * x, x / 7])
        .map(|x| x.to_string())
        .collect()
}

let vec: Vec<_> = (0..42).collect();
let input = vec.as_slice();

// runs sequentially on the main thread
let runner = RunnerWithPool::from(SequentialPool);
let expected = run_with_runner(runner, input);

// uses native threads
let runner = RunnerWithPool::from(StdDefaultPool::default());
let result = run_with_runner(runner, input);
assert_eq!(&expected, &result);

// uses rayon-core ThreadPool with 8 threads
#[cfg(feature = "rayon-core")]
{
    let pool = rayon_core::ThreadPoolBuilder::new()
        .num_threads(8)
        .build()
        .unwrap();
    let result = run_with_runner(RunnerWithPool::from(&pool), input);
    assert_eq!(&expected, &result);
}

// uses scoped-pool Pool with 8 threads
#[cfg(feature = "scoped-pool")]
{
    let pool = scoped_pool::Pool::new(8);
    let result = run_with_runner(RunnerWithPool::from(&pool), input);
    assert_eq!(&expected, &result);
}

// uses scoped_threadpool Pool with 8 threads
#[cfg(feature = "scoped_threadpool")]
{
    let mut pool = scoped_threadpool::Pool::new(8);
    let result = run_with_runner(RunnerWithPool::from(&mut pool), input); // requires &mut pool
    assert_eq!(&expected, &result);
}

// uses yastl Pool wrapped as YastlPool with 8 threads
#[cfg(feature = "yastl")]
{
    let pool = YastlPool::new(8);
    let result = run_with_runner(RunnerWithPool::from(&pool), input);
    assert_eq!(&expected, &result);
}

// uses pond Pool wrapped as PondPool with 8 threads
#[cfg(feature = "pond")]
{
    let mut pool = PondPool::new_threads_unbounded(8);
    let result = run_with_runner(RunnerWithPool::from(&mut pool), input); // requires &mut pool
    assert_eq!(&expected, &result);
}

// uses poolite Pool with 8 threads
#[cfg(feature = "poolite")]
{
    let pool = poolite::Pool::with_builder(poolite::Builder::new().min(8).max(8)).unwrap();
    let result = run_with_runner(RunnerWithPool::from(&pool), input);
    assert_eq!(&expected, &result);
}

Implementationsยง

Sourceยง

impl<P, R> RunnerWithPool<P, R>

Source

pub fn into_inner_pool(self) -> P

Converts the runner into the wrapped underlying pool.

Note that a RunnerWithPool can always be created from owned pool, but also from

  • &pool in most cases,
  • &mut pool in others.

This function is only relevant when the runner is created from owned pool, in which case into_inner_pool can be used to get back ownership of the pool.

ยงExample

The following example demonstrates the use case for rayon-core thread pool; however, it holds for all thread pool implementations.

use orx_parallel::*;

let vec: Vec<_> = (0..42).collect();
let input = vec.as_slice();

#[cfg(feature = "rayon-core")]
{
    let pool = rayon_core::ThreadPoolBuilder::new()
        .num_threads(8)
        .build()
        .unwrap();

    // create runner owning the pool
    let mut runner = RunnerWithPool::from(pool);

    // use runner, and hence the pool, in parallel computations
    let sum = input.par().with_runner(&mut runner).sum();
    let max = input.par().with_runner(&mut runner).max();
    let txt: Vec<_> = input
        .par()
        .with_runner(&mut runner)
        .map(|x| x.to_string())
        .collect();

    // get back ownership of the pool
    let pool: rayon_core::ThreadPool = runner.into_inner_pool();
}
Source

pub fn with_executor<Q>(self) -> RunnerWithPool<P, Q>

Converts the runner into one using the ParallelExecutor Q rather than R.

Trait Implementationsยง

Sourceยง

impl<P, R> Default for RunnerWithPool<P, R>

Sourceยง

fn default() -> RunnerWithPool<P, R>

Returns the โ€œdefault valueโ€ for a type. Read more
Sourceยง

impl<P> From<P> for RunnerWithPool<P>
where P: ParThreadPool,

Sourceยง

fn from(pool: P) -> RunnerWithPool<P>

Converts to this type from the input type.
Sourceยง

impl<P, R> ParallelRunner for RunnerWithPool<P, R>

Sourceยง

type Executor = R

Parallel executor responsible for distribution of tasks to the threads.
Sourceยง

type ThreadPool = P

Thread pool responsible for providing threads to the parallel computation.
Sourceยง

fn thread_pool(&self) -> &<RunnerWithPool<P, R> as ParallelRunner>::ThreadPool

Reference to the underlying thread pool.
Sourceยง

fn thread_pool_mut( &mut self, ) -> &mut <RunnerWithPool<P, R> as ParallelRunner>::ThreadPool

Mutable reference to the underlying thread pool.
Sourceยง

fn new_executor( &self, kind: ComputationKind, params: Params, initial_input_len: Option<usize>, ) -> Self::Executor

Creates a new parallel executor for a parallel computation.
Sourceยง

fn run_all<I, F>( &mut self, params: Params, iter: I, kind: ComputationKind, thread_do: F, ) -> NumSpawned

Runs thread_do using threads provided by the thread pool.
Sourceยง

fn map_all<F, I, M, T>( &mut self, params: Params, iter: I, kind: ComputationKind, thread_map: M, ) -> (NumSpawned, Result<Vec<T>, <F as Fallibility>::Error>)
where F: Fallibility, I: ConcurrentIter, M: Fn(NumSpawned, &I, &<Self::Executor as ParallelExecutor>::SharedState, <Self::Executor as ParallelExecutor>::ThreadExecutor) -> Result<T, <F as Fallibility>::Error> + Sync, T: Send, <F as Fallibility>::Error: Send,

Runs thread_map using threads provided by the thread pool.
Sourceยง

fn map_infallible<I, M, T>( &mut self, params: Params, iter: I, kind: ComputationKind, thread_map: M, ) -> (NumSpawned, Result<Vec<T>, Never>)

Runs infallible thread_map using threads provided by the thread pool.
Sourceยง

fn max_num_threads_for_computation( &self, params: Params, iter_len: Option<usize>, ) -> NonZero<usize>

Returns the maximum number of threads that can be used for the computation defined by the params and input iter_len.

Auto Trait Implementationsยง

ยง

impl<P, R> Freeze for RunnerWithPool<P, R>
where P: Freeze,

ยง

impl<P, R> RefUnwindSafe for RunnerWithPool<P, R>

ยง

impl<P, R> Send for RunnerWithPool<P, R>
where P: Send, R: Send,

ยง

impl<P, R> Sync for RunnerWithPool<P, R>
where P: Sync,

ยง

impl<P, R> Unpin for RunnerWithPool<P, R>
where P: Unpin, R: Unpin,

ยง

impl<P, R> UnwindSafe for RunnerWithPool<P, R>
where P: UnwindSafe, R: 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> 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 = 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.