1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! A crate with helper functions that execute given closures in parallel on all available processors.
use thread;
/// Runs a given closure in parallel on all available CPUs.
///
/// The given closure can return a value. All return values of all closure executions are
/// aggregated with a given aggregation closure. The method returns a tuple with
/// the aggregated result and the number of executions (=number of CPUs).
///
/// # Arguments
///
/// * `body` - closure that should be run in parallel on all available processors.
/// * `initial` - initial value used for aggregating the results of all closure executions.
/// * `agg` - closure used to aggregate method results.
///
/// # Examples
///
/// ```
/// use mth_calc::run_on_all_cpus;
/// use rand::prelude::*;
///
/// let result = mth_calc::run_on_all_cpus::<f32>(|| {
/// // Create a thread-local random generator
/// let mut rng = rand::rng();
///
/// // Calculate sum over ten random numbers
/// let mut total = 0f32;
/// for _ in 0..10 {
/// total += rng.random::<f32>();
/// }
///
/// total
/// },
/// 0f32, // initial value for aggreation is zero
/// |x, y| { x + y }); // aggregation function = sum
///
/// let avg = result.0 / (result.1 * 10) as f32;
/// # if avg < 0f32 || avg >= 1f32 {
/// # panic!();
/// # }
/// println!("The average random number was {}", avg);
/// ```