orx-parallel 4.0.0

Performant parallel computations with an expressive iterator API.
Documentation
use crate::runner::par_runner::ParRunner;
use crate::runner::runner_variants::fixed_chunk::{heuristic, state::State};
use crate::{parameters::Params, pools::ThreadPool};

pub struct FixedChunkRunner<P: ThreadPool> {
    pool: P,
}

unsafe impl<P: ThreadPool> Sync for FixedChunkRunner<P> {}

impl<P: ThreadPool> FixedChunkRunner<P> {
    pub fn new(pool: P) -> Self {
        Self { pool }
    }
}

impl<P: ThreadPool> ParRunner for FixedChunkRunner<P> {
    type Pool = P;

    type State = State;

    type ChunkState = ();

    fn pool(&self) -> &Self::Pool {
        &self.pool
    }

    fn pool_mut(&mut self) -> &mut Self::Pool {
        &mut self.pool
    }

    fn do_spawn_new(spawned: usize, state: &Self::State) -> Option<usize> {
        (spawned < state.max_num_threads).then_some(spawned)
    }

    fn new_state(
        &mut self,
        params: Params,
        max_num_threads: usize,
        size_hint: (usize, Option<usize>),
    ) -> Self::State {
        let chunk_size =
            heuristic::compute_chunk_size(params.chunk_size, size_hint, max_num_threads);
        State {
            max_num_threads,
            size_hint,
            chunk_size,
        }
    }

    fn configure_for_serialized_input(
        _state: &mut Self::State,
        _size_hint: (usize, Option<usize>),
    ) {
    }

    #[inline(always)]
    fn begin_thread(_: &Self::State, _: usize) {}

    #[inline(always)]
    fn next_chunk_size(state: &Self::State, _: (usize, Option<usize>)) -> usize {
        state.chunk_size
    }

    #[inline(always)]
    fn begin_chunk(_: usize, _: usize) -> Self::ChunkState {}

    #[inline(always)]
    fn complete_chunk(_: &Self::State, _: Self::ChunkState) {}

    #[inline(always)]
    fn complete_thread(_: &Self::State, _: usize) {}

    #[inline(always)]
    fn complete_computation(_: Self::State) {}
}