ENGINEERING NOTE

Dropping C HDF5, Fixed-Chunk Determinism, and Where the GPU Code Stands

Replacing libhdf5 and CMake with pure-Rust storage, keeping parallel reductions bitwise identical, and separating today's WGSL shaders from long-term Rust GPU plans.

Published 2026-11-28 4 min read 894 words All Engineering Notes

Scientific Rust tools often claim pure-Rust portability until you inspect how they read and write field data — at which point CMake, a C compiler, and a platform hdf5.dll or libhdf5.so quietly reappear. nyx spent its first phases linking hdf5-metno, forcing contributors and CI runners to wrestle with HDF5_DIR while producing results files whose layout fractured badly on 3D quadratic meshes (ADR 0107).

In that original layout (L1LegacyGzip3), saving a 14-mode 3D mechanical or electromagnetic run created $O(16 \times M)$ separate B-tree dataset headers compressed individually with Deflate(3). Files grew to 69–600 MB with only 1.07×–2.45× compression on cell_fields, reading one quantity buffered the whole archive into memory, and long parameter sweeps had no way to append points incrementally.

Zero-C hdf5-pure and a Packed 5D Layout

ADR 0107 replaced hdf5-metno across the workspace with crates/nyx-storage, built on pure-Rust hdf5-pure and pure-Rust SIMD zlib-rs (with fast-deflate deliberately disabled so C zlib-ng cannot sneak into the build graph). CI Gate EC-00 enforces zero C toolchain involvement directly by compiling with poisoned CC, CXX, and CMAKE variables.

Just swapping the reader was not enough; changing the on-disk layout solved the scaling bottleneck (FieldStorageLayout::L2ShuffleLzfPacked):

  • All $M$ modes of a physics study pack into one 5D dataset, /results/<physics>/cell_fields_packed, shaped [M, C, N_c, N_v, R] (R = 1 for real fields, R = 2 when any component is complex).
  • Each mode occupies one chunk [1, C, N_c, N_v, R] filtered with HDF5’s standard byte-shuffle (id = 2) followed by LZF (id = 32000), leaving every file readable directly by vanilla Python h5py without custom filter plugins.
  • Byte-shuffling IEEE-754 mantissa and exponent lanes ahead of LZF cut file size by 3.2× (reaching 16.3× compression on 3D Tet10 meshes) and accelerated writes 4.2× compared to Deflate(3).
  • Positional streaming (open_streaming plus ReadBudgetConfig) lets nyx slice a single 60 MB mode from a 600 MB 3D solve with ~57.2 MB peak RSS delta while asserting CHECKED integer bounds on corrupted headers before allocating memory.

One legacy promise was cut cleanly during that work: the old phdf5 MPI-IO parallel write flag had never been built or run (RESULTS_FORMAT_STUDY.md), so H5Mode::Parallel now stays an explicit zero-C stub returning an error in favour of atomic whole-file renames and in-place sweep checkpoints (SweepCheckpoint).

Fixed-Chunk Parallelism — and Where Determinism Stops

Reproducing regression baselines requires numerical answers not to wander when a run moves from 1 core to 8. Standard parallel .sum() folds over rayon iterators reassociate floating-point rounding differently whenever work-stealing schedules shift chunks between threads.

ADR 0011 solved thread-count drift by enforcing fixed-size chunking in femcore::par and femcore::linalg::par: parallel element assembly and vector reductions always partition into chunk sizes independent of thread count (NYX_THREADS), accumulate per-chunk partials, and fold chunks strictly in index order. Across electrostatics, adaptation, DGTD, FDTD, and post-processing, NYX_THREADS=1 and NYX_THREADS=8 match to zero ULPs.

Where determinism does not hold is documented just as explicitly:

  • Cross-machine BLAS/FMA differences: High-throughput CPU BLAS kernels vary FMA contraction and cache blocking by instruction set (--features portable-kernels trades some restart-multiply speed for cross-CPU parity under < 1e-13 differential gates).
  • Parallel 3D sparse direct factorisations (faer): Multi-threaded faer sparse LU/LDLT in 3D ems exhibits 3.59e-10 to 1.15e-7 relative eigenvalue drift between 1 and 8 threads due to internal task scheduling.

Where the GPU Code Stands Today

Recent additions (ADR 0146) added GPU execution to nyx, and it is important to be clear about what language actually runs on the hardware today.

Default builds (cargo build --release) remain 100% pure Rust on the CPU with zero GPU dependencies compiled into the workspace. Passing --features gpu enables a shared wgpu runtime across Vulkan, DirectX 12, and Metal targeting two time-domain electromagnetic solvers (Study::Fdtd and Study::Dgtd) plus mixed-precision AmgF32/SellCSigma linear operators (femcore::linalg::gpu). Those compute kernels are currently written as WGSL shaders executed through wgpu, not Rust code compiled directly to the GPU.1.To preserve repeatability across GPU runs, energy integrals and vector inner products avoid floating-point atomics and use two-level workgroup shared-memory binary reduction trees (@workgroup_size(64) and @workgroup_size(256)).

Several explicit guardrails govern when gpu falls back to CPU with a gpu_fallback_to_cpu warning:

  • Double precision hardware support: backend = "auto" picks f32 for large linear time-domain grids (>= 250,000 FDTD cells or >= 50,000 DGTD DOFs on discrete GPUs), selecting f64 only when nonlinear optics is enabled and the adapter exposes wgpu::Features::SHADER_F64. Requesting precision = "f64" on a card without SHADER_F64 drops cleanly back to CPU f64.
  • Advanced features still CPU-only: Multi-tier Local Time-Stepping (n_lts_tiers > 1) in Dgtd and dispersive Drude/Lorentz ADE poles currently execute on the CPU rather than maintaining diverging shader paths.

Because solvers program against femcore::linalg::device’s backend-agnostic Vector, DeviceLinearOperator, and DevicePreconditioner traits rather than raw buffer handles, wgpu/WGSL acts as a portable Tier-1 layer. Longer term, the roadmap plans to move compute kernels to Rust-native GPU toolchains (such as CubeCL or rust-gpu) once portable f64 support stabilises, alongside native CUDA/ROCm/cluster backends.