pub fn run_pool_parallel_with_initializations_mutable<Key, Data, CommonData, InitData, F, FInit>(
    iter: impl Iterator<Item = Key> + Send,
    common_data: &CommonData,
    closure: &F,
    closure_init: FInit,
    number_of_thread: usize,
    capacity: usize
) -> Result<HashMap<Key, Data>, ThreadAnyError>where
    CommonData: Sync,
    Key: Eq + Hash + Send + Clone + Sync,
    Data: Send,
    F: Sync + Clone + Fn(&mut InitData, &Key, &CommonData) -> Data,
    FInit: Send + Clone + FnOnce() -> InitData,
Expand description

run jobs in parallel. Similar to run_pool_parallel but with initiation.

see run_pool_parallel. Moreover let some data to be initialize per thread. closure_init is run once per thread and store inside a mutable data which closure can modify.

Errors

Returns ThreadAnyError::ThreadNumberIncorrect is the number of threads is 0. Returns ThreadAnyError::Panic if a thread panicked. Contains the panic message.

Examples

Let us create some value but we will greet the user from the threads


let iter = 0_u128..100000_u128;
let c = 5_u128;
// we could have put 4 inside the closure but this demonstrate how to use common data
let result = run_pool_parallel_with_initializations_mutable(
    iter,
    &c,
    &|has_greeted: &mut bool, i, c| {
        if !*has_greeted {
            *has_greeted = true;
            println!("Hello from the thread");
        }
        i * i * c
    },
    || false,
    4,
    100000,
)?;

will print “Hello from the thread” four times.

Another useful application is to use an rng

extern crate rand;
extern crate rand_distr;
use lattice_qcd_rs::field::Su3Adjoint;
use lattice_qcd_rs::lattice::LatticeCyclic;
use lattice_qcd_rs::thread::run_pool_parallel_with_initializations_mutable;

let l = LatticeCyclic::<4>::new(1_f64, 4)?;
let distribution = rand::distributions::Uniform::from(-1_f64..1_f64);
let result = run_pool_parallel_with_initializations_mutable(
    l.get_links(),
    &distribution,
    &|rng, _, d| Su3Adjoint::random(rng, d).to_su3(),
    rand::thread_rng,
    4,
    l.number_of_canonical_links_space(),
)?;