Skip to content

Fix random MPI crashes caused by array out-of-bounds access - #17

Merged
qianglbl merged 2 commits into
impact-lbl:masterfrom
ChristopherMayes:fix-out-of-bound-access
Jul 6, 2026
Merged

Fix random MPI crashes caused by array out-of-bounds access#17
qianglbl merged 2 commits into
impact-lbl:masterfrom
ChristopherMayes:fix-out-of-bound-access

Conversation

@ChristopherMayes

@ChristopherMayes ChristopherMayes commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

Fix remaining MPI array out-of-bounds crashes

Summary

Hardens the MPI build against intermittent segmentation faults (e.g. mpirun -n 8 ImpactZexe-mpi) caused by array out-of-bounds access. The crashes are non-deterministic because they depend on the runtime particle distribution across ranks.

Master already fixes the main offender via V2.7.6 (distparam = distparam0 in init_CompDom), which eliminates the uninitialized initial computational domain. This PR fixes three further, independent out-of-bounds bugs that V2.7.6 does not touch, plus a defensive guard in charge deposition. All are exposed by compiling with -fcheck=all. A reproducer is attached as error-test.zip.

Changes

1. src/Contrl/Input.f90obtype(0) when parsing comments

The lattice input parser reads lines in a loop, incrementing index i only for data lines (non-comment). However, the -99 end-of-lattice check on obtype(i) was outside the if(comst.ne."!") block, so when the first line is a comment, i is still 0 and obtype(0) is accessed.

Fix: Move the obtype(i).eq.-99 check inside the data-reading branch.

2. src/Contrl/Output.f90glbin(0) in 12 percentile search loops

The 90th/95th/99th percentile emittance calculations use cumulative histograms. Twelve do i = 1, nbin loops access glbin(i-1), which gives glbin(0) when i=1. The cumulative sum is built starting from i=2, so glbin(0) is never initialized and is out of bounds.

Fix: Start all 12 search loops at i = 2 instead of i = 1. This is safe because the interpolation formula uses glbin(i-1) and glbin(i), and glbin(1) already holds the raw count for bin 1.

3. src/Func/Ptclmger.f90 — zero-sized MPI buffer allocations

When a rank has no particles to exchange in a direction, jleft/jright/jdown/jup (or nsmall, derived from Nptlocal/numbuf) can be 0. This produces zero-sized allocations for temp1 and the left/right/up/down send buffers, after which MPI_RECV(temp1(1,1), ...) / MPI_SEND(left(1,1), ...) indexes element (1,1) out of bounds.

Fix: Allocate with max(..., 1) for the six affected buffers.

4. src/Appl/BeamBunch.f90 — defensive bounds check in deposit_BeamBunch

Defense-in-depth for the CIC charge deposition: skip any particle whose stencil (ix..ix+1, jx..jx+1, kx..kx+1) falls outside the local rho grid. Skipped particles are counted, reduced with MPI_ALLREDUCE, and reported once from rank 0 with a WARNING, so any charge loss is visible rather than causing a silent crash.

The out-of-bounds deposition that originally crashed the reproducer is fixed at the source by V2.7.6 (uninitialized initial domain). With master, this guard is not triggered by the reproducer; it remains as cheap insurance against any future decomposition producing out-of-bounds indices.

Testing

  • Reproducer (error-test.zip): 10,000 particles through a 4-dipole chicane, extended diagnostics, mpirun -n 8.
  • On top of master (V2.7.6): 5/5 runs complete cleanly with zero particles dropped (no WARNING).
  • Debug build with -fcheck=all: no out-of-bounds reports.
  • Regression: Example1, Example2, and Example3 still run correctly.

Files Changed

File Change
src/Contrl/Input.f90 Move -99 check inside the data branch
src/Contrl/Output.f90 12 percentile loops: do i = 1do i = 2
src/Func/Ptclmger.f90 6 MPI buffer allocations guarded with max(..., 1)
src/Appl/BeamBunch.f90 Defensive deposition bounds check + skipped-particle reporting

Acknowledgement

Bug diagnosis and fixes developed with AI assistance (GitHub Copilot, Claude Opus 4.8). The original problem shows up in https://christophermayes.github.io/lume-impact/examples/z/elements/csr-zeuthen/.

@qianglbl
qianglbl merged commit 478d49d into impact-lbl:master Jul 6, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants