FrankenNumPy

Docs/Numerics

Threading and Concurrency Model

FrankenNumPy parallelizes per operation rather than globally: an op runs rayon-parallel when measurement showed a win at that size and dtype, and serial otherwise (see §Limitations for the policy). The choice merits precise description. Two integration test files exercise the model: crates/fnp-ufunc/tests/concurrency_safety.rs (verifies UFuncArray Send/Sync and parallel reduce/elementwise variants) and crates/fnp-conformance/tests/concurrency_safety.rs (verifies fnp-conformance's 4+ static Mutex/OnceLock combinations are thread-safe + deadlock-free).

Send / Sync for the core types. UFuncArray owns its Vec<f64> and is Send + Sync; you can move an array between threads or share it behind an Arc for concurrent reads. MaskedArray and StringArray follow the same pattern. Datetime/timedelta arrays are UFuncArray instances and inherit the same Send + Sync.

UFuncArrayView is Send + Sync only when read-only. A view's backing store is Arc<RwLock<Vec<f64>>>; concurrent readers can hold a shared RwLockReadGuard. A writer acquires the exclusive lock for the duration of its mutation.

Generator and BitGenerator are effectively single-thread. Their inner state is plain integers (u128 for PCG, the MT19937 state array, etc.), so the auto-derived Send and Sync impls apply, but every method that draws a value takes &mut self. Two threads cannot draw from the same generator without external locking, and you would not want them to: the draw order would become non-deterministic and the bit-exact-vs-NumPy contract would break. The right pattern for parallel RNG work is SeedSequence::spawn(n), which hands each worker an independent child stream that remains individually reproducible. NumPy documents the same pattern.

fnp-runtime's EvidenceLedger has no interior locking. It is a plain Vec<DecisionEvent> (plus an optional capacity and a dropped-event counter) behind &mut self mutators. Multi-threaded appenders must wrap the ledger in their own Mutex / RwLock. The crate does not impose a synchronization model; that choice is left to the embedder.

No global mutable state in numeric ops. The one exception is fnp-ufunc's thread-local FloatErrorState, which is, as the name implies, per-thread. Configuring errstate(divide=Raise) on one thread does not affect another thread.

Parallelism is gated per operation. Measured large-array paths use Rayon and targeted SIMD where their size and operation gates show a benefit; bandwidth-bound or small paths remain serial. The policy is encoded in BinaryOp::is_parallel_worth, UnaryOp::is_parallel_worth, and family-specific thresholds. Three conformance bins calibrate those choices: run_parallel_calibration_matrix (sweeps workload sizes), run_parallel_speedup_verdict (per-op verdict), and run_parallel_thread_recommendations (per-machine thread-count suggestion) — all under crates/fnp-conformance/src/bin/.

Async story is observability-only. When the optional asupersync feature is enabled in fnp-runtime, it powers RaptorQ encoding, telemetry channels, and cancellation-safe oracle capture; it does not schedule numerical kernels.


This section in the README on GitHub