pub fn run_pool_parallel_vec_with_initializations_mutable<Key, Data, CommonData, InitData, F, FInit, const D: usize>(
iter: impl Iterator<Item = Key> + Send,
common_data: &CommonData,
closure: &F,
closure_init: FInit,
number_of_thread: usize,
capacity: usize,
l: &LatticeCyclic<D>,
default_data: &Data
) -> Result<Vec<Data>, ThreadAnyError>where
CommonData: Sync,
Key: Eq + Send + Clone + Sync + LatticeElementToIndex<D>,
Data: Send + Clone,
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_vec
but with initiation.
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
use lattice_qcd_rs::lattice::{LatticeCyclic, LatticeElementToIndex, LatticePoint};
use lattice_qcd_rs::thread::run_pool_parallel_vec_with_initializations_mutable;
let l = LatticeCyclic::<4>::new(1_f64, 25)?;
let iter = l.get_points();
let c = 5_usize;
// we could have put 4 inside the closure but this demonstrate how to use common data
let result = run_pool_parallel_vec_with_initializations_mutable(
iter,
&c,
&|has_greeted: &mut bool, i: &LatticePoint<4>, c: &usize| {
if !*has_greeted {
*has_greeted = true;
println!("Hello from the thread");
}
i[0] * c
},
|| false,
4,
100000,
&l,
&0,
)?;
will print “Hello from the thread” four times.
Another useful application is to use an rng
extern crate rand;
extern crate rand_distr;
extern crate nalgebra;
use lattice_qcd_rs::field::Su3Adjoint;
use lattice_qcd_rs::lattice::LatticeCyclic;
use lattice_qcd_rs::thread::run_pool_parallel_vec_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_vec_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(),
&l,
&nalgebra::Matrix3::<nalgebra::Complex<f64>>::zeros(),
)?;