caelum0x fab9a4a45a feat: reach + performance + docs — Python analysis client, parallel eval, sensitivity, guide
Batch 7 of expansion-v2. Extends the product across surfaces and adds an
analysis primitive. Deterministic/offline/std-only (Rust) or graceful-absence
(Python).

python/lawsynth analysis.py (new): Python client for the engine's stability,
control, and domains analyses — shells out to the lawsynth CLI (--json) and
parses to typed dataclasses. No numerics re-implemented in Python; the Rust
engine is the single source of truth. Locates the binary like the benchmarks,
raises MissingBinaryError/CliError honestly, and is fully parse-testable
offline. 20 tests; full SDK suite 114 passed.

crates/lawsynth-features: opt-in std::thread-parallel feature-library
evaluation, bit-identical to serial for ALL thread counts (row-chunked via
thread::scope, ordered assembly, no parallel float reduction). Verified via
f64::to_bits across {1,2,3,4,7,8} threads x 10 library variants x uneven row
counts. Measured 3.6x at 8 threads; honestly sublinear (allocation-bound,
~few-thousand-row crossover). 28 tests + benchmark.

crates/lawsynth-sensitivity (new): forward sensitivity analysis. Integrates the
variational equations Ṡ_j = J_x·S_j + f_θj (J_x from the analytic Jacobian, f_θ
by symbolic differentiation) on the augmented system with fixed-step RK4.
Analytic check to <1e-6; finite-difference cross-check at 1e-6 for every
parameter on logistic + Lotka-Volterra. 22 tests.

docs/guide (new): verified end-to-end guide — getting-started, workflow
(discover→simplify→stability→control→export→validate→uncertainty), and a
determinism demo. All 17 documented commands run exit-0 via run_all.sh; honest
about real behaviors (already-minimal law simplifies to 0 nodes saved, CLI
bootstrap doesn't surface per-coef intervals, center points are inconclusive).
README links the guide.

specs/{parallel-evaluation,sensitivity-analysis}: boundary specs.
2026-08-18 16:14:46 +03:00

102 lines
3.7 KiB
Rust

//! Serial-vs-parallel wall-clock comparison for feature-library evaluation.
//!
//! Matches the crate's `harness = false` bench style: a plain `main` that times
//! a fixed workload with `std::time::Instant` and prints throughput. It builds a
//! large synthetic dataset and a degree-3 combined library, then evaluates it
//! serially and with 1/2/4/8 threads. Each parallel run is asserted bit-identical
//! to the serial baseline so the benchmark can never report a speedup for a
//! result that quietly diverged.
use lawsynth_core::Identifier;
use lawsynth_data::{Dataset, NumericColumn, TimeAxis};
use lawsynth_features::{FeatureLibrary, FeatureMatrix};
use std::{hint::black_box, time::Instant};
const ROWS: usize = 60_000;
const REPEATS: usize = 5;
fn ident(name: &str) -> Identifier {
Identifier::new(name).unwrap()
}
fn build_dataset() -> Dataset {
let time = (0..ROWS).map(|i| i as f64).collect::<Vec<_>>();
let x = (0..ROWS).map(|i| (i as f64) * 0.37 - 3.1).collect::<Vec<_>>();
let y = (0..ROWS).map(|i| (i as f64).sin() * 2.0 + 0.5).collect::<Vec<_>>();
let z = (0..ROWS).map(|i| ((i as f64) * 0.13).cos() - (i as f64) * 0.02).collect::<Vec<_>>();
Dataset::new(
TimeAxis::new(time).unwrap(),
[
NumericColumn::new(ident("x"), x),
NumericColumn::new(ident("y"), y),
NumericColumn::new(ident("z"), z),
],
)
.unwrap()
}
fn build_library() -> FeatureLibrary {
let vars = [ident("x"), ident("y"), ident("z")];
let mut library = FeatureLibrary::polynomial(vars.clone(), 3, true).unwrap();
library.extend(FeatureLibrary::trigonometric(vars.clone()).unwrap());
library.extend(FeatureLibrary::bounded_rational(vars).unwrap());
library
}
fn matrices_bit_identical(a: &FeatureMatrix, b: &FeatureMatrix) -> bool {
a.rows.len() == b.rows.len()
&& a.rows.iter().zip(&b.rows).all(|(ra, rb)| {
ra.len() == rb.len() && ra.iter().zip(rb).all(|(va, vb)| va.to_bits() == vb.to_bits())
})
}
/// Times `REPEATS` evaluations and returns the best (minimum) elapsed nanoseconds
/// to reduce noise from scheduler jitter.
fn best_nanos(mut run: impl FnMut() -> FeatureMatrix) -> u128 {
let mut best = u128::MAX;
for _ in 0..REPEATS {
let started = Instant::now();
let matrix = run();
let elapsed = started.elapsed().as_nanos();
black_box(&matrix);
best = best.min(elapsed);
}
best
}
fn main() {
let data = build_dataset();
let library = build_library();
let columns = library.terms().len();
let cells = (ROWS * columns) as f64;
println!(
"parallel_eval: {ROWS} rows x {columns} candidate terms ({} available cores)",
std::thread::available_parallelism().map(|n| n.get()).unwrap_or(0)
);
let serial_matrix = library.evaluate(&data).unwrap();
let serial_nanos = best_nanos(|| library.evaluate(&data).unwrap());
report("serial", serial_nanos, serial_nanos, cells);
for threads in [1usize, 2, 4, 8] {
let matrix = library.evaluate_parallel(&data, threads).unwrap();
assert!(
matrices_bit_identical(&serial_matrix, &matrix),
"parallel result with {threads} threads diverged from serial"
);
let nanos = best_nanos(|| library.evaluate_parallel(&data, threads).unwrap());
report(&format!("parallel/{threads}"), nanos, serial_nanos, cells);
}
}
fn report(label: &str, nanos: u128, serial_nanos: u128, cells: f64) {
let seconds = nanos as f64 / 1e9;
let throughput = cells / seconds / 1e6;
let speedup = serial_nanos as f64 / nanos as f64;
println!(
" {label:<12} {:>8.3} ms {throughput:>8.1} Mcell/s {speedup:>5.2}x vs serial",
seconds * 1e3
);
}