Skip to content

Optimize two-mask radial calculation for orthogonal cells - #1196

Merged
drroe merged 2 commits into
Amber-MD:masterfrom
whahnsr:optimize-radial-two-mask
Oct 13, 2026
Merged

drroe merged 2 commits into
Amber-MD:masterfrom
whahnsr:optimize-radial-two-mask

Conversation

@whahnsr

@whahnsr whahnsr commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Optimize the two-mask radial distribution calculation for orthogonal
cells.

Profiling a two-mask radial calculation showed essentially all runtime
concentrated in Action_Radial::calcRDF_twoMask() and the distance
routines it calls.

For orthogonal cells, this change caches the box lengths once per frame
and performs the orthogonal minimum-image distance calculation directly
in the inner pair loop. Other imaging modes continue to use the existing
DIST2() path.

Using std::fabs() for the coordinate differences also allows the
compiler to generate branchless absolute-value operations in this hot
loop.

Performance

Test case:

  • test/Test_AutoImage/walp_2.strip.dcd
  • 11,670 atoms
  • 71 frames
  • two masks of 5,520 atoms each
  • radial DOPC2 :39-78 :79-118 out ... 0.25 15.0
  • Serial GNU build with -O3

CPU user time:

  • Before: 17.10 s
  • After: 10.30 s

This is approximately a 39.8% reduction in CPU time, or a 1.66x speedup.

Validation

  • All 17 comparisons in test/Test_Radial pass.
  • RDF output from the benchmark is byte-for-byte identical to the output
    produced by the original code.
  • The modified Action_Radial.cpp was compiled successfully with
    -fopenmp to verify the OpenMP code path.
  • git diff --check reports no whitespace errors.

Non-orthogonal and no-image calculations retain the existing DIST2()
path.

@drroe

drroe commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Thanks for these! I'm surprised the overhead from the function call/branching is so high. Probably should look into refactoring the routines in DistRoutines.h so that more Actions etc can benefit from this kind of speedup.

@drroe

drroe commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

FYI I've merged #1195 into this branch.

@drroe
drroe merged commit 5387608 into Amber-MD:master Oct 13, 2026
12 of 13 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