pub struct RunnerWithPool<P, R = FixedChunkRunner>where
P: ParThreadPool,
R: ParallelExecutor,{ /* 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
&poolin most cases,&mut poolin 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>where
P: ParThreadPool,
R: ParallelExecutor,
impl<P, R> RunnerWithPool<P, R>where
P: ParThreadPool,
R: ParallelExecutor,
Sourcepub fn into_inner_pool(self) -> P
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
&poolin most cases,&mut poolin 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();
}Sourcepub fn with_executor<Q>(self) -> RunnerWithPool<P, Q>where
Q: ParallelExecutor,
pub fn with_executor<Q>(self) -> RunnerWithPool<P, Q>where
Q: ParallelExecutor,
Converts the runner into one using the ParallelExecutor Q rather than R.
Trait Implementationsยง
Sourceยงimpl<P, R> Default for RunnerWithPool<P, R>
impl<P, R> Default for RunnerWithPool<P, R>
Sourceยงfn default() -> RunnerWithPool<P, R>
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,
impl<P> From<P> for RunnerWithPool<P>where
P: ParThreadPool,
Sourceยงfn from(pool: P) -> RunnerWithPool<P>
fn from(pool: P) -> RunnerWithPool<P>
Converts to this type from the input type.
Sourceยงimpl<P, R> ParallelRunner for RunnerWithPool<P, R>where
P: ParThreadPool,
R: ParallelExecutor,
impl<P, R> ParallelRunner for RunnerWithPool<P, R>where
P: ParThreadPool,
R: ParallelExecutor,
Sourceยงtype ThreadPool = P
type ThreadPool = P
Thread pool responsible for providing threads to the parallel computation.
Sourceยงfn thread_pool(&self) -> &<RunnerWithPool<P, R> as ParallelRunner>::ThreadPool
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
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
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,
) -> NumSpawnedwhere
I: ConcurrentIter,
F: Fn(NumSpawned, &I, &<Self::Executor as ParallelExecutor>::SharedState, <Self::Executor as ParallelExecutor>::ThreadExecutor) + Sync,
fn run_all<I, F>(
&mut self,
params: Params,
iter: I,
kind: ComputationKind,
thread_do: F,
) -> NumSpawnedwhere
I: ConcurrentIter,
F: Fn(NumSpawned, &I, &<Self::Executor as ParallelExecutor>::SharedState, <Self::Executor as ParallelExecutor>::ThreadExecutor) + Sync,
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,
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>)where
I: ConcurrentIter,
M: Fn(NumSpawned, &I, &<Self::Executor as ParallelExecutor>::SharedState, <Self::Executor as ParallelExecutor>::ThreadExecutor) -> Result<T, Never> + Sync,
T: Send,
fn map_infallible<I, M, T>(
&mut self,
params: Params,
iter: I,
kind: ComputationKind,
thread_map: M,
) -> (NumSpawned, Result<Vec<T>, Never>)where
I: ConcurrentIter,
M: Fn(NumSpawned, &I, &<Self::Executor as ParallelExecutor>::SharedState, <Self::Executor as ParallelExecutor>::ThreadExecutor) -> Result<T, Never> + Sync,
T: Send,
Runs infallible
thread_map using threads provided by the thread pool.Auto Trait Implementationsยง
impl<P, R> Freeze for RunnerWithPool<P, R>where
P: Freeze,
impl<P, R> RefUnwindSafe for RunnerWithPool<P, R>where
P: RefUnwindSafe,
R: RefUnwindSafe,
impl<P, R> Send for RunnerWithPool<P, R>
impl<P, R> Sync for RunnerWithPool<P, R>where
P: Sync,
impl<P, R> Unpin for RunnerWithPool<P, R>
impl<P, R> UnwindSafe for RunnerWithPool<P, R>where
P: UnwindSafe,
R: UnwindSafe,
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