pyrucast 0.13.2

Finite-element library in Rust, inspired by cast3m, exposed to Python
Documentation
//! 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 rayon::prelude::*;

use crate::error::Result;
use std::sync::atomic::{AtomicU64, Ordering};

/// 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`, …).
///
/// ```
/// # use pyrucast::parallel;
/// // Splitting a tiny job costs more than it pays: below this grain, a
/// // parallel loop in fact runs sequentially.
/// assert_eq!(parallel::MIN_PARALLEL_LEN, 256);
/// // Applied through `.with_min_len(MIN_PARALLEL_LEN)` on an indexed iterator.
/// ```
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, …).
///
/// ```
/// # use pyrucast::parallel;
/// // Every slot is written **exactly once**: the result does not depend on
/// // the number of threads. It is the single primitive behind the
/// // element-wise field mathematics.
/// let mut v = vec![1.0, 4.0, 9.0];
/// parallel::map_inplace(&mut v, f64::sqrt);
/// assert_eq!(v, vec![1.0, 2.0, 3.0]);
/// ```
pub fn map_inplace(buf: &mut [f64], f: impl Fn(f64) -> f64 + Sync + Send) {
    buf.par_iter_mut()
        .with_min_len(MIN_PARALLEL_LEN)
        .for_each(|v| *v = f(*v));
}

/// 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.
///
/// ```
/// # use pyrucast::parallel;
/// // A single component of an interleaved buffer: here the second of three.
/// let mut v = vec![1.0, 10.0, 100.0, 2.0, 20.0, 200.0];
/// parallel::map_component_inplace(&mut v, 3, 1, |x| x * 2.0);
/// assert_eq!(v, vec![1.0, 20.0, 100.0, 2.0, 40.0, 200.0]);
/// ```
pub fn map_component_inplace(
    buf: &mut [f64],
    ncomp: usize,
    ci: usize,
    f: impl Fn(f64) -> f64 + Sync + Send,
) {
    debug_assert!(ncomp > 0 && ci < ncomp);
    buf.par_chunks_mut(ncomp)
        .with_min_len((MIN_PARALLEL_LEN / ncomp).max(1))
        .for_each(|row| row[ci] = f(row[ci]));
}

/// Accumulate `v` into the f64 atomic slot `a` (the value held as its bit
/// pattern). The colour-driven scatters — the global assembly
/// ([`crate::ops::matrix`]) 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.
#[inline]
pub(crate) fn add_atomic(a: &AtomicU64, v: f64) {
    let cur = f64::from_bits(a.load(Ordering::Relaxed));
    a.store((cur + v).to_bits(), Ordering::Relaxed);
}

/// 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.
///
/// ```
/// # use pyrucast::parallel;
/// // The accumulator of a coloured scatter, held in atomics. Within one
/// // colour, the cells touch **disjoint** slots: `add` does not
/// // court jamais.
/// let couleurs = vec![vec![0, 1]];
/// let mut v = vec![0.0; 2];
/// parallel::colored_scatter(&mut v, &couleurs, 1, || (), |cell, _s, out| {
///     out.add(cell, 1.0);
///     Ok(())
/// })?;
/// assert_eq!(v, vec![1.0, 1.0]);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub struct Scatter<'a> {
    values: &'a [AtomicU64],
}

impl Scatter<'_> {
    /// Accumulate `v` into slot `slot` of the shared accumulator.
    ///
    /// ```
    /// # use pyrucast::parallel;
    /// // The contributions **accumulate** in the targeted slot — from one colour
    /// // to the next, never within one. Three cells writing the same slot must
    /// // therefore be of three colours: that is exactly what the colouring
    /// // guarantees, and without which the additions would be lost.
    /// let couleurs = vec![vec![0], vec![1], vec![2]];
    /// let mut v = vec![0.0; 1];
    /// parallel::colored_scatter(&mut v, &couleurs, 1, || (), |_cell, _s, out| {
    ///     out.add(0, 2.0);
    ///     Ok(())
    /// })?;
    /// assert_eq!(v, vec![6.0]); // trois mailles × 2
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    #[inline]
    pub fn add(&self, slot: usize, v: f64) {
        add_atomic(&self.values[slot], v);
    }
}

/// 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::models::flux`]). Returns the
/// accumulator as plain `f64`.
///
/// ```
/// # use pyrucast::parallel;
/// // The cells are split into colours touching disjoint slots; every slot
/// // therefore adds its cells in a fixed order — increasing colour, then
/// // cell order — whatever the
/// // `RAYON_NUM_THREADS`.
/// let couleurs = vec![vec![0, 2], vec![1]];
/// let mut v = vec![0.0; 3];
/// parallel::colored_scatter(&mut v, &couleurs, 1, || 0usize, |cell, tampon, out| {
///     *tampon += 1; // one state per task, reused from one cell to the next
///     out.add(cell, (cell + 1) as f64);
///     Ok(())
/// })?;
/// assert_eq!(v, vec![1.0, 2.0, 3.0]);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn colored_scatter<S>(
    slots: &mut [f64],
    coloring: &[Vec<usize>],
    min_len: usize,
    init: impl Fn() -> S + Sync + Send,
    cell: impl Fn(usize, &mut S, &Scatter) -> Result<()> + Sync,
) -> Result<()> {
    // The accumulation itself needs shared atomic slots — that is what lets two
    // cells of one colour write side by side without a lock and without
    // `unsafe`. It is the one staging buffer this scatter cannot do without.
    //
    // The **result**, though, lands straight in the caller's own buffer: the
    // caller holds it under its write lock for the whole call, so there is no
    // reason to build a second `Vec<f64>` and copy it over.
    let values: Vec<AtomicU64> = (0..slots.len()).map(|_| AtomicU64::new(0)).collect();
    let out = Scatter { values: &values };
    for color in coloring {
        color
            .par_iter()
            .with_min_len(min_len)
            .try_for_each_init(&init, |scratch, &c| cell(c, scratch, &out))?;
    }
    for (slot, a) in slots.iter_mut().zip(values) {
        *slot = f64::from_bits(a.into_inner());
    }
    Ok(())
}