Repository navigation
[Scripting] Add map-reduce parallel reductions over leaf cells - #287
BenWibking wants to merge 2 commits into
Conversation
25deb01 to
b3bdacf
Compare
b3bdacf to
4b8fd0a
Compare
4b8fd0a to
7a5bab6
Compare
…sis tools (#289) Small changes to existing code that the analysis library (next PR) needs. No new feature yet. - **Cache budget in `core`.** The cache-budget default and `AMREXPLORER_CACHE_SIZE_MB` parsing move from `src/qt/CacheConfig.hpp` to `include/amrexplorer/core/CacheConfig.hpp`, so code without Qt can share them. `src/qt/CacheConfig.hpp` now uses the core version. - **Derived-field policy.** `PlotfileDataset` takes a `DerivedFieldPolicy`: - The viewer keeps `Skip`. - An unattended analysis passes `Strict`, so a definition that doesn't resolve fails at open instead of silently dropping the field. - **`derivedFieldProgram(field)`.** Exposes a derived field's compiled program, so a caller can bound in advance which blocks a read will pin. - **`FabValues` spans.** `FabValues::singleValues()` and `doubleValues()` expose the payload in its stored precision, for callers copying many values without dispatching on precision per value. ## Tests - New `tests/integration/test_derived_field_policy.cpp`: - `Skip` installs what resolves and reports the rest. - `Strict` fails the open, naming the broken definition. - `derivedFieldProgram` returns null for a stored field and the right inputs for a derived one, and a derived block evaluates its program. - `test_single_precision_plotfile.cpp` checks both span accessors for float and double FABs. - The headless ctest suite passes (63 tests). ## Stack Part of a stack replacing #276. Review and merge in order; each PR shows only its own layer. 1. #289 Prepare PlotfileDataset and the cache budget for analysis ← this PR 2. #285 Exact leaf-cell plans and chunks (C++) 3. #286 The amrexplorer Python package (bindings) 4. #287 Parallel reductions and batch helpers 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
7a5bab6 to
7fd2d90
Compare
PR 287 review draft (head 7fd2d90) — Request changesReview bodyThe scheduling and reduction logic looks right. Ordered combining can't
The tests here are pure Python, so they also depend on the CI job requested Inline commentspython/amrexplorer/parallel.py:13This bound doesn't hold for python/amrexplorer/parallel.py:190Combined with a full-size cache per worker handle, docs/python-analysis.md:90
docs/python-analysis.md:89Please describe docs/python-analysis.md:92The MPI hint leaves out where docs/python-analysis.md:97This section needs an example. It names |
A Qt-free `src/analysis` library with an exact planner and reader for a plotfile's **leaf cells**, the cells no finer level covers. This is the core algorithm the Python package builds on. - **Plans.** `planLeafChunks` partitions the leaf cells of a region into one item per grid, in a fixed order: levels coarsest first, grids in native FAB order. - Covered coarse cells never appear, and every leaf cell appears exactly once. - Regions select cells by center, `lower <= center < upper`, so adjacent regions split the cells between them. - Planning reads metadata only. - **Precision.** Field values keep the widest precision any level stores unless the plan asks for float32 or float64. Derived fields are evaluated per block and follow the plan's dtype. - **Cache budget.** Each item's worst-case cache use is checked against the budget at planning, and again when reading. The error names the item. - **Chunks.** `readLeafChunk` copies an item's values out of the cache: a chunk holds no cache pins and outlives the dataset. Geometry arrays (indices, centers, widths, volumes) are computed on first use. - **Errors.** Unknown fields, unsupported geometry or centering, invalid regions and items planned on another dataset are `AnalysisError`. ## Review notes `src/analysis/LeafChunks.cpp` is the file to read closely. The covered-cell computation, region-to-index-box conversion and the pinned-bytes estimate are where the guarantees come from. ## Tests `tests/integration/test_leaf_chunks.cpp` runs on a two-level 2-D fixture whose fine grid straddles a coarse grid boundary. It checks: - exact partition, stored values, and the leaf cells tiling the domain - the half-open region rule, and an empty plan outside the domain - unknown, duplicate and non-finite requests are refused, as is non-Cartesian geometry - float32 narrowing and derived fields - reading items repeatedly and in any order - no pins after a read, and chunks outliving the dataset - the planning-time and read-time budget checks - cancellation, and items bound to their dataset The headless ctest suite passes (64 tests). ## Stack Part of a stack replacing #276. Review and merge in order; each PR shows only its own layer. 1. #289 Prepare PlotfileDataset and the cache budget for analysis 2. #285 Exact leaf-cell plans and chunks (C++) ← this PR 3. #286 The amrexplorer Python package (bindings) 4. #287 Parallel reductions and batch helpers 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
7fd2d90 to
378d4d7
Compare
7870ec6 to
1d5474d
Compare
amrexplorer.parallel.map_reduce reads and maps leaf-chunk plan items across thread, process, or MPI-partitioned workers: - Each worker opens its own dataset handle and plans the same request, checking that the plan matches. - At most max_in_flight items are in progress at once. - Partials are combined in plan order by default, so results are bit-identical to a serial run. amrexplorer.sequence finds a run's plotfiles in step order, opens them one at a time, and skips outputs already written. tools/analysis_validation/benchmark_parallel.py measures the executors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Handles that map_reduce opens itself now drop their unpinned blocks after every item: worker threads, worker processes, and the handle opened for a path. Each item is read once, so those caches were never hit and only held up to cache_bytes per worker until the call ended. A handle the caller passes in, and the shared handle, keep their caches. Exposes Dataset.clear_unpinned_cache() for this. The default worker count is now AMREXPLORER_WORKERS, or else the CPUs the process may use, at most 8; workers= overrides both. The guide's parallel section now lists every option with its default and memory cost, and gives complete processes and MPI scripts. The MPI text no longer claims rank results combine to the serial result: partition() is round-robin, so the result is reproducible for a fixed rank count but may differ from serial in the last bits. The batch section gains a complete loop. A new test runs each of these examples as a script. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `amrexplorer` Python package: nanobind bindings over the leaf-cell planner and reader from the previous PR, plus a small pure-Python wrapper and build plumbing. Most of this PR is mechanical glue. The Python tests are the clearest statement of the API. - **`open_plotfile(path, derived=..., cache_bytes=...)`** returns a `Dataset` usable as a context manager. Derived-field definitions use `Strict`, so a bad one fails at open. - **Plans and chunks.** `plan_leaf_chunks` returns a sequence of items, and `read_leaf_chunk` returns a chunk. - A chunk is a mapping of read-only NumPy arrays that keep their memory alive, so they outlive the chunk and the dataset. - Geometry (`x`, `dx`, `i`, `cell_volume`, ...) is computed lazily. - `leaf_cells()` plans and reads in one loop. - **`CancelToken`** stops a read in progress. Analysis, cache-budget, read, closed-dataset and cancellation errors map to Python exceptions. - **Build.** - `-DAMREXPLORER_ENABLE_PYTHON=ON` builds the module into `<build>/python`. - `pip install .` builds the module alone through scikit-build-core (`pyproject.toml`). - ctest runs the pytest suite as `python_analysis` when pytest is installed. - **Docs.** `docs/python-analysis.md` (installing and leaf cells) and `docs/building.md`. ## Review notes - `python/src/module.cpp` (~540 lines) is type conversion: dicts and boxes, NumPy views kept alive by capsules, GIL release around reads, and closed-dataset checks. - `python/amrexplorer/_dataset.py` is property accessors and the Mapping/Sequence protocols over the native objects. - Both are worth skimming, not reading line by line. ## Tests - `tests/python/test_leaf_chunks.py` (25 tests) checks every contract against an independent NumPy reconstruction of 1-D, 2-D and 3-D fixture plotfiles. It includes a region over a 2^64-cell domain, a regression test for an overflow found on real data. - `tests/python/plotfile_writer.py` writes AMReX-layout plotfiles (per-level precision, stored ghost cells, version-1 and version-2 FAB headers) for these and later tests. - The headless ctest suite passes (65 tests, `python_analysis` among them). ## Stack Part of a stack replacing #276. Review and merge in order; each PR shows only its own layer. 1. #289 Prepare PlotfileDataset and the cache budget for analysis 2. #285 Exact leaf-cell plans and chunks (C++) 3. #286 The amrexplorer Python package (bindings) ← this PR 4. #287 Parallel reductions and batch helpers 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
716d2e7 to
fb2be18
Compare
|
Blocking:
Non-blocking, worth raising with Ben: |
Pure-Python layers on the package from the previous PR.
amrexplorer.parallel.map_reducereads and maps leaf-chunk plan items on thread, process or MPI-partitioned workers.ownership="shared"keeps one handle for every thread instead.max_in_flightitems are in progress at once, which bounds memory.ordered=Falsecombines partials as they arrive.amrexplorer.sequencefinds a run's plotfiles in step order, opens them one at a time, and skips outputs already written.tools/analysis_validation/benchmark_parallel.pytimes the executors and ownership models on a real plotfile. With worker-local handles, 8 threads ran 2.5x faster than serial on a 92-million-cell plotfile. A shared handle is slower than serial, becausePlotfileDatasetserializes block reads.Tests
test_parallel.py:test_batch.py: cancellation of reads and iteration, progress callbacks, and sequence discovery.Stack
Part of a stack replacing #276. Review and merge in order; each PR shows only its own layer.
🤖 Generated with Claude Code