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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//! Shared-memory parallelism helpers (rayon).
//!
//! pyrucast parallelises its heavy compute loops — per-element assembly,
//! per-Gauss-point behaviour integration, element-wise field maths, VTK export
//! — across CPU cores. This module is the single import point for the rayon
//! parallel-iterator prelude, plus the project-wide grain-size policy.
//!
//! # Where parallelism lives
//!
//! It is deliberately **lifted above** the per-physics kernels: the drivers in
//! [`crate::models::kernel`] and the field/container layers call rayon here, so
//! a physics author writes only sequential point-/cell-local maths and never
//! touches a parallel iterator. See the book chapter *« Parallélisme »*.
//!
//! # Determinism
//!
//! Every parallel region either writes each output slot **exactly once**
//! (indexed / `par_chunks_mut` writes), is an **associative reduction over the
//! same values in the same grouping**, or is a **colour-driven scatter**
//! (the `add_atomic` primitive): cells are partitioned into colours that touch disjoint
//! slots, so each slot accumulates its cells in a fixed order — increasing
//! colour, then cell order within the colour — regardless of
//! `RAYON_NUM_THREADS`. The first two are bit-for-bit identical to a sequential
//! run; the colour-driven scatter is reproducible for any thread count but,
//! summed in colour order rather than cell order, is not bit-for-bit with a
//! naive sequential sum (it backs the global assembly and the nodal scatters —
//! the `Bᵀ` divergence, the internal forces and the distributed flux load). The
//! linear solver is the sole non-deterministic exception — see
//! [`crate::ops::solver`].
//!
//! # Grain size
//!
//! Splitting a tiny workload across threads costs more than it saves. Parallel
//! loops use [`MIN_PARALLEL_LEN`] via `with_min_len` / `with_max_len` so small
//! problems (a handful of cells) run effectively sequentially, with no thread
//! hand-off overhead.
pub use *;
use crateResult;
use ;
/// Minimum number of leaf items (values, cells, …) a rayon job should cover
/// before it is worth splitting further. Tuned so that single small elements
/// (e.g. one SEG2) stay on one thread, while real meshes fan out.
///
/// Apply with `.with_min_len(MIN_PARALLEL_LEN)` on an indexed parallel
/// iterator (`par_iter`, `par_chunks_mut`, …).
pub const MIN_PARALLEL_LEN: usize = 256;
/// Apply `f` to every element of `buf` **in place**, in parallel, honouring the
/// grain-size policy. Each slot is written exactly once ⇒ result is independent
/// of the thread count. The single primitive behind element-wise field maths
/// (`map_all`, the scalar `+ − × ÷` operators, …).
/// Apply `f` in place to component `ci` (stride `ncomp`, `ci < ncomp`) of a
/// flat component-major buffer, in parallel. Each touched slot is written once.
/// Accumulate `v` into the f64 atomic slot `a` (the value held as its bit
/// pattern). The colour-driven scatters — the global assembly
/// ([`crate::ops::assemble`]) and the nodal scatters ([`colored_scatter`], behind
/// the `Bᵀ` divergence and the distributed flux load) — guarantee that within one
/// colour no two parallel cells touch the same slot, so this load-then-store
/// never races; colours run in sequence behind a rayon barrier, so cross-colour
/// accumulation is ordered. `Relaxed` therefore suffices — on x86 the store is a
/// plain `mov`, so a colour-disjoint scatter costs the same as a non-atomic one.
pub
/// Handle onto a colour-scatter's flat f64 accumulator (held as atomics), handed
/// to the per-cell closure of [`colored_scatter`]. Within one colour the cells
/// touch pairwise-disjoint slots, so [`Scatter::add`] never races.
/// Scatter per-cell contributions into a flat accumulator of `n_slots`, in
/// parallel, **colour by colour**. `coloring` partitions the cells so that one
/// colour's cells touch pairwise-disjoint slots; that colour's cells then run
/// concurrently (grain `min_len`) without racing, the colours running in
/// sequence. `init` builds a **per-thread** scratch value reused across a
/// thread's cells (e.g. a local element buffer — pass `|| ()` when none is
/// needed); `cell(c, scratch, out)` computes cell `c`'s contributions and pushes
/// each into `out` via [`Scatter::add`].
///
/// Determinism: each slot accumulates its cells in a fixed order — increasing
/// colour, then cell order within the colour — independent of the thread count,
/// so the result is reproducible for any `RAYON_NUM_THREADS` (see the module
/// *Determinism* note). It is the shared mechanism behind the `Bᵀ` divergence
/// ([`crate::models::kernel::scatter_to_nodes`], behind the `Bᵀ` divergence,
/// the internal forces and the distributed flux load
/// [`crate::ops::assemble::flux`](fn@crate::ops::assemble::flux)). Returns the
/// accumulator as plain `f64`.