fix: reset ap/bp/cp at phi==0 (thread-order-dependent stale-buffer bug)#4
Open
gcmullen wants to merge 1 commit into
Open
fix: reset ap/bp/cp at phi==0 (thread-order-dependent stale-buffer bug)#4gcmullen wants to merge 1 commit into
gcmullen wants to merge 1 commit into
Conversation
The phi sweep skips the spindle rotation at phi==0 via `if(phi != 0.0)`, but ap/bp/cp are declared outside the pixel loop (OpenMP firstprivate), so at the phi==0 sample (default phi0=0 with phisteps>1) they still hold the previous iteration's rotated values instead of the unrotated a0/b0/c0. The result is thread-order-dependent: running the same config with different OMP_NUM_THREADS gives per-pixel differences up to ~28% (a physics result must not depend on thread count), and a ~14% total-intensity shift vs the intended phi==0 = a0 orientation. Keep the fast phi==0 path (skip the rotation) and add an `else` that resets ap/bp/cp to a0/b0/c0 — mirroring the mosaic reset just below. Bit-exact with rotating at phi==0 (rotate_axis(phi=0) is identity) and unchanged for all phi!=0.
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.
Summary
At the phi==0 sample the spindle rotation is skipped (
if(phi != 0.0)), butap/bp/cp are declared outside the pixel loop (OpenMP firstprivate), so they
still hold the previous pixel's rotated orientation instead of the unrotated
a0/b0/c0. Intensity is inflated whenever phi0==0 (the default) with phisteps>1.
Why it's a bug, not just a divergence
The stale buffer makes the result depend on thread count — which a physics
result must not. Same config, varying OMP_NUM_THREADS:
Aggregates wash out (~corr 1.0), which is why it hid for years. Independently
confirmed:
-phi 1e-9(phi never exactly 0) matches to float-eps at all phisteps.Fix
Keep the fast phi==0 path (skip the rotation) and add an
elseresettingap/bp/cp = a0/b0/c0 — mirroring the mosaic reset just below. rotate_axis(phi=0)
is the identity, so this is bit-exact with unconditionally rotating and
unchanged for all phi!=0.
Scope
nanoBragg.c, +10 lines, one hunk. No API/CLI change.
Proposal for your review — happy to switch to remove-guard form (byte-identical).