Fix random MPI crashes caused by array out-of-bounds access - #17
Merged
qianglbl merged 2 commits intoJul 6, 2026
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 = distparam0ininit_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 aserror-test.zip.Changes
1.
src/Contrl/Input.f90—obtype(0)when parsing commentsThe lattice input parser reads lines in a loop, incrementing index
ionly for data lines (non-comment). However, the-99end-of-lattice check onobtype(i)was outside theif(comst.ne."!")block, so when the first line is a comment,iis still 0 andobtype(0)is accessed.Fix: Move the
obtype(i).eq.-99check inside the data-reading branch.2.
src/Contrl/Output.f90—glbin(0)in 12 percentile search loopsThe 90th/95th/99th percentile emittance calculations use cumulative histograms. Twelve
do i = 1, nbinloops accessglbin(i-1), which givesglbin(0)wheni=1. The cumulative sum is built starting fromi=2, soglbin(0)is never initialized and is out of bounds.Fix: Start all 12 search loops at
i = 2instead ofi = 1. This is safe because the interpolation formula usesglbin(i-1)andglbin(i), andglbin(1)already holds the raw count for bin 1.3.
src/Func/Ptclmger.f90— zero-sized MPI buffer allocationsWhen a rank has no particles to exchange in a direction,
jleft/jright/jdown/jup(ornsmall, derived fromNptlocal/numbuf) can be 0. This produces zero-sized allocations fortemp1and theleft/right/up/downsend buffers, after whichMPI_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 indeposit_BeamBunchDefense-in-depth for the CIC charge deposition: skip any particle whose stencil (
ix..ix+1,jx..jx+1,kx..kx+1) falls outside the localrhogrid. Skipped particles are counted, reduced withMPI_ALLREDUCE, and reported once from rank 0 with aWARNING, so any charge loss is visible rather than causing a silent crash.Testing
error-test.zip): 10,000 particles through a 4-dipole chicane, extended diagnostics,mpirun -n 8.WARNING).-fcheck=all: no out-of-bounds reports.Files Changed
src/Contrl/Input.f90-99check inside the data branchsrc/Contrl/Output.f90do i = 1→do i = 2src/Func/Ptclmger.f90max(..., 1)src/Appl/BeamBunch.f90Acknowledgement
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/.