pyrucast 0.3.0

Finite-element library in Rust, inspired by cast3m, exposed to Python
Documentation
//! Global assembly by **scatter into a fixed CSR pattern**.
//!
//! The historical path turned every block into global `(row, col, value)`
//! triplets and rebuilt a CSR from them (a counting-sort with serial
//! histogram / scatter / dedup passes — the assembly bottleneck). This module
//! splits that into two phases.
//!
//! **Symbolic** ([`build_pattern`]): the global CSR sparsity (`row_offsets` +
//! `col_indices`) from the blocks' topology alone — no kernel eval. It is a
//! function of the model structure only, so it can be built once and reused
//! across assemblies (materials change, sparsity does not).
//!
//! **Numeric** ([`scatter_serial`]): evaluate each block's contribution and add
//! it into the CSR values at its precomputed slot.
//!
//! [`scatter_serial`] visits blocks — and, within a block, cells / COO entries —
//! in the same order the triplet stream had, accumulating each slot in that
//! order, so its result is **bit-for-bit** identical to the triplet path. The
//! parallel colour-driven scatter (the actual speed-up) builds on this same
//! pattern.

use crate::containers::matrix::{AssemblyPattern, BlockSlots, Matrix, NamedDof};
use crate::error::{PyrucastError, Result};
use crate::models::kernel;
use crate::ops::coloring;
use crate::parallel::*;
use nalgebra_sparse::CsrMatrix;
use std::collections::HashMap;
use std::sync::atomic::AtomicU64;

/// Build the global CSR sparsity [`AssemblyPattern`] for `k` from its blocks'
/// topology alone — no kernel evaluation. Each block contributes its global
/// `(row, col)` entries (a computed block via [`kernel::element_block_pattern`],
/// a literal block via its stored COO indices); columns are then sorted and
/// deduplicated per row.
pub fn build_pattern(k: &Matrix) -> Result<AssemblyPattern> {
    let row_dofs = k.row_dofs()?;
    let col_dofs = k.col_dofs()?;
    let row_map: HashMap<NamedDof, usize> = row_dofs
        .iter()
        .cloned()
        .enumerate()
        .map(|(i, d)| (d, i))
        .collect();
    let col_map: HashMap<NamedDof, usize> = col_dofs
        .iter()
        .cloned()
        .enumerate()
        .map(|(i, d)| (d, i))
        .collect();

    let nrows = row_dofs.len();
    // Per row, the columns it touches (with duplicates; deduped below).
    let mut row_cols: Vec<Vec<usize>> = vec![Vec::new(); nrows];
    // Per block (in `subs` order), its global entries as `(r, c)` in the exact
    // order the numeric scatter emits them — kept so the second pass can map
    // each to its CSR slot once, and cache the result on the pattern.
    enum BlockEntries {
        /// One `(r, c)` list per cell (computed block).
        Computed(Vec<Vec<(usize, usize)>>),
        /// One `(r, c)` per COO entry (literal block).
        Literal(Vec<(usize, usize)>),
    }
    let mut block_entries: Vec<BlockEntries> = Vec::new();
    for blk_h in k {
        let blk = blk_h.read();
        let trow: Vec<usize> = blk.row_dofs().iter().map(|d| row_map[d]).collect();
        let tcol: Vec<usize> = blk.col_dofs().iter().map(|d| col_map[d]).collect();
        match blk.recipe() {
            Some(recipe) => {
                // A non-empty `col_fespaces` marks an inter-mesh block: rows and
                // columns are walked on two facing connectivities instead of one.
                let (_, _, per_cell) = match recipe.col_fespaces.first() {
                    Some(col_fe) => kernel::coupling_block_pattern(
                        &recipe.fespaces[0],
                        col_fe,
                        blk.row_support(),
                        blk.col_support(),
                        blk.dual_vars().len(),
                        blk.primal_vars().len(),
                        blk.ordering(),
                    )?,
                    None => kernel::element_block_pattern(
                        &recipe.fespaces[0],
                        blk.row_support(),
                        blk.col_support(),
                        blk.dual_vars().len(),
                        blk.primal_vars().len(),
                        blk.ordering(),
                    )?,
                };
                let cells: Vec<Vec<(usize, usize)>> = per_cell
                    .iter()
                    .map(|cell| {
                        cell.iter()
                            .map(|&(ri, ci)| {
                                let (gr, gc) = (trow[ri], tcol[ci]);
                                row_cols[gr].push(gc);
                                (gr, gc)
                            })
                            .collect()
                    })
                    .collect();
                block_entries.push(BlockEntries::Computed(cells));
            }
            None => {
                let (lr, lc, _) = blk.local_coo_arrays();
                let entries: Vec<(usize, usize)> = (0..lr.len())
                    .map(|k| {
                        let (gr, gc) = (trow[lr[k]], tcol[lc[k]]);
                        row_cols[gr].push(gc);
                        (gr, gc)
                    })
                    .collect();
                block_entries.push(BlockEntries::Literal(entries));
            }
        }
    }

    // Sort + dedup each row's columns independently → parallel across rows.
    row_cols
        .par_iter_mut()
        .with_min_len(MIN_PARALLEL_LEN)
        .for_each(|cols| {
            cols.sort_unstable();
            cols.dedup();
        });
    // Concatenate into the CSR arrays (serial: O(nnz) appends + prefix sum).
    let mut row_offsets = vec![0usize; nrows + 1];
    let mut col_indices: Vec<usize> = Vec::new();
    for r in 0..nrows {
        col_indices.extend_from_slice(&row_cols[r]);
        row_offsets[r + 1] = col_indices.len();
    }

    let mut pattern = AssemblyPattern {
        row_dofs,
        col_dofs,
        row_offsets,
        col_indices,
        block_slots: Vec::new(),
    };

    // Second pass: map every block entry to its CSR value slot once, so the
    // numeric scatter reads slots directly instead of binary-searching per
    // entry on every assembly. Parallel across blocks (independent output).
    // No `with_min_len` here: an item is a whole block (all its cells, all
    // their entries), so blocks are few and each is heavy — the grain policy
    // counts leaf items and would serialise this loop outright.
    pattern.block_slots = block_entries
        .into_par_iter()
        .map(|be| match be {
            BlockEntries::Computed(cells) => BlockSlots::Computed(
                cells
                    .into_iter()
                    .map(|cell| cell.iter().map(|&(r, c)| pattern.slot(r, c)).collect())
                    .collect(),
            ),
            BlockEntries::Literal(entries) => {
                BlockSlots::Literal(entries.iter().map(|&(r, c)| pattern.slot(r, c)).collect())
            }
        })
        .collect();

    Ok(pattern)
}

/// Assemble `k` into a CSR by scattering each block's contribution into
/// `pattern`'s value slots, **serially in block order** (and, within a block,
/// in cell / COO order). Because every slot accumulates in the same order the
/// triplet stream had, the result is bit-for-bit identical to the triplet path.
/// This is the reference numeric phase; the colour-parallel scatter reuses the
/// same pattern and slots.
pub fn scatter_serial(k: &Matrix, pattern: &AssemblyPattern) -> Result<CsrMatrix<f64>> {
    let nrows = pattern.row_dofs.len();
    let ncols = pattern.col_dofs.len();

    let mut values = vec![0.0f64; pattern.nnz()];
    for (bi, blk_h) in k.into_iter().enumerate() {
        let blk = blk_h.read();
        match blk.recipe() {
            Some(recipe) => {
                // Read the sub-model once (not per cell) so the element kernel
                // stays lock-free while it runs in parallel over cells.
                let sm = recipe.submodel.read();
                let phys = sm.as_kind();
                // Per-cell triplets so the value stream lines up cell-for-cell
                // with the precomputed slots (same `(li,di,lj,pj)` order).
                let (_, _, per_cell) = if recipe.col_fespaces.is_empty() {
                    kernel::element_block_triplets_per_cell(
                        &recipe.fespaces,
                        blk.row_support(),
                        blk.col_support(),
                        blk.dual_vars().len(),
                        blk.primal_vars().len(),
                        blk.ordering(),
                        recipe.material.as_ref(),
                        recipe.state.as_ref(),
                        |geoms, m, state, ke| phys.matrix_element(recipe.kind, geoms, m, state, ke),
                    )?
                } else {
                    kernel::coupling_block_triplets_per_cell(
                        &recipe.fespaces,
                        &recipe.col_fespaces,
                        blk.row_support(),
                        blk.col_support(),
                        blk.dual_vars().len(),
                        blk.primal_vars().len(),
                        blk.ordering(),
                        recipe.material.as_ref(),
                        |row_geoms, col_geoms, m, ke| {
                            phys.coupling_element(recipe.kind, row_geoms, col_geoms, m, ke)
                        },
                    )?
                };
                let slots = match &pattern.block_slots[bi] {
                    BlockSlots::Computed(s) => s,
                    BlockSlots::Literal(_) => {
                        return Err(PyrucastError::Message(
                            "scatter_serial: pattern/block kind mismatch (computed block)".into(),
                        ))
                    }
                };
                let factor = blk.factor();
                for (cell, cell_slots) in per_cell.iter().zip(slots) {
                    for (&(_, _, v), &slot) in cell.iter().zip(cell_slots) {
                        values[slot] += v * factor;
                    }
                }
            }
            None => {
                let (_, _, lv) = blk.local_coo_arrays();
                let factor = blk.factor();
                let slots = match &pattern.block_slots[bi] {
                    BlockSlots::Literal(s) => s,
                    BlockSlots::Computed(_) => {
                        return Err(PyrucastError::Message(
                            "scatter_serial: pattern/block kind mismatch (literal block)".into(),
                        ))
                    }
                };
                for (&slot, &v) in slots.iter().zip(lv) {
                    values[slot] += v * factor;
                }
            }
        }
    }

    CsrMatrix::try_from_csr_data(
        nrows,
        ncols,
        pattern.row_offsets.clone(),
        pattern.col_indices.clone(),
        values,
    )
    .map_err(|e| PyrucastError::Message(format!("scatter_serial: invalid CSR: {e}")))
}

/// Assemble `k` into a CSR by scattering each block's contribution into
/// `pattern`'s value slots **in parallel**, colour by colour. A computed
/// block's element matrices are evaluated in parallel
/// ([`kernel::element_block_triplets_per_cell`]); then, for each colour of the
/// block's cell colouring (cached on its FE subspace), that colour's cells —
/// which touch pairwise-disjoint DOFs — scatter concurrently into disjoint
/// slots. Literal blocks scatter serially. The colouring is deterministic, so
/// the assembled values are reproducible regardless of thread count (though the
/// per-slot summation order differs from the serial path, hence not bit-for-bit
/// with it).
pub fn scatter_parallel(k: &Matrix, pattern: &AssemblyPattern) -> Result<CsrMatrix<f64>> {
    let nrows = pattern.row_dofs.len();
    let ncols = pattern.col_dofs.len();

    // f64 values held as bits so the colour-parallel scatter can write them
    // through shared references (see `add_atomic`).
    let values: Vec<AtomicU64> = (0..pattern.nnz()).map(|_| AtomicU64::new(0)).collect();

    for (bi, blk_h) in k.into_iter().enumerate() {
        let blk = blk_h.read();
        match blk.recipe() {
            Some(recipe) => {
                let sm = recipe.submodel.read();
                let phys = sm.as_kind();
                // Element matrices, evaluated in parallel, one triplet list per
                // cell (grouping needed for the colour-driven scatter).
                let (_, _, per_cell) = if recipe.col_fespaces.is_empty() {
                    kernel::element_block_triplets_per_cell(
                        &recipe.fespaces,
                        blk.row_support(),
                        blk.col_support(),
                        blk.dual_vars().len(),
                        blk.primal_vars().len(),
                        blk.ordering(),
                        recipe.material.as_ref(),
                        recipe.state.as_ref(),
                        |geoms, m, state, ke| phys.matrix_element(recipe.kind, geoms, m, state, ke),
                    )?
                } else {
                    kernel::coupling_block_triplets_per_cell(
                        &recipe.fespaces,
                        &recipe.col_fespaces,
                        blk.row_support(),
                        blk.col_support(),
                        blk.dual_vars().len(),
                        blk.primal_vars().len(),
                        blk.ordering(),
                        recipe.material.as_ref(),
                        |row_geoms, col_geoms, m, ke| {
                            phys.coupling_element(recipe.kind, row_geoms, col_geoms, m, ke)
                        },
                    )?
                };
                let slots = match &pattern.block_slots[bi] {
                    BlockSlots::Computed(s) => s,
                    BlockSlots::Literal(_) => {
                        return Err(PyrucastError::Message(
                            "scatter_parallel: pattern/block kind mismatch (computed block)".into(),
                        ))
                    }
                };

                let factor = blk.factor();
                if recipe.col_fespaces.is_empty() {
                    // Cell colouring (cached on the primary FE subspace): two cells
                    // sharing a node conflict, so one colour's cells touch disjoint DOFs.
                    let fe = recipe.fespaces[0].read();
                    let submesh = fe.submesh();
                    let submesh_g = submesh.read();
                    let conn = submesh_g.connectivity();
                    let n_cells = fe.cell_count()?;
                    let keys_per_cell = conn.len().checked_div(n_cells).unwrap_or(0);
                    let coloring =
                        fe.coloring(|| coloring::greedy_color(n_cells, keys_per_cell, conn));

                    // Scatter colour by colour: within a colour, cells write disjoint
                    // slots ⇒ the parallel atomic stores never race. Slots are
                    // precomputed (indexed cell-for-cell, entry-for-entry).
                    for color in coloring {
                        color
                            .par_iter()
                            .with_min_len(MIN_PARALLEL_LEN)
                            .for_each(|&cell| {
                                for (&(_, _, v), &slot) in per_cell[cell].iter().zip(&slots[cell]) {
                                    add_atomic(&values[slot], v * factor);
                                }
                            });
                    }
                } else {
                    // An inter-mesh block writes rows on one mesh and columns on
                    // another: a colouring of *one* connectivity no longer proves
                    // the slots disjoint. Its element matrices are still evaluated
                    // in parallel above; only the scatter is serial, as for a
                    // literal block. An interface carries a boundary mesh's worth
                    // of cells, so this costs nothing worth a two-sided colouring.
                    for (cell_trips, cell_slots) in per_cell.iter().zip(slots) {
                        for (&(_, _, v), &slot) in cell_trips.iter().zip(cell_slots) {
                            add_atomic(&values[slot], v * factor);
                        }
                    }
                }
            }
            None => {
                let (_, _, lv) = blk.local_coo_arrays();
                let factor = blk.factor();
                let slots = match &pattern.block_slots[bi] {
                    BlockSlots::Literal(s) => s,
                    BlockSlots::Computed(_) => {
                        return Err(PyrucastError::Message(
                            "scatter_parallel: pattern/block kind mismatch (literal block)".into(),
                        ))
                    }
                };
                for (&slot, &v) in slots.iter().zip(lv) {
                    add_atomic(&values[slot], v * factor);
                }
            }
        }
    }

    let vals: Vec<f64> = values
        .into_iter()
        .map(|a| f64::from_bits(a.into_inner()))
        .collect();
    CsrMatrix::try_from_csr_data(
        nrows,
        ncols,
        pattern.row_offsets.clone(),
        pattern.col_indices.clone(),
        vals,
    )
    .map_err(|e| PyrucastError::Message(format!("scatter_parallel: invalid CSR: {e}")))
}