Skip to content

Inline common distance routines - #1197

Merged
drroe merged 1 commit into
Amber-MD:masterfrom
whahnsr:optimize-dist-routines
Oct 14, 2026
Merged

drroe merged 1 commit into
Amber-MD:masterfrom
whahnsr:optimize-dist-routines

Conversation

@whahnsr

@whahnsr whahnsr commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Inline the common pointer-based distance routines in DistRoutines.h:

  • DIST2()
  • DIST2_ImageOrtho()
  • DIST2_NoImage()

For orthogonal imaging, cache the three box lengths and use std::fabs() for coordinate differences.

This follows the suggestion in #1196 to refactor the common distance routines so Actions beyond Action_Radial can benefit from the same optimization.

Performance

Tested with Action_MinMaxDist using the 11,670-atom orthogonal trajectory from Test_AutoImage.

The benchmark evaluates approximately 170 million pair distances over 5 frames.

Five serial runs with OMP_NUM_THREADS=1:

Baseline user times:
1.26, 1.24, 1.25, 1.25, 1.24 s

Baseline mean:
1.248 s

Optimized user times:
0.92, 0.92, 0.93, 0.92, 0.92 s

Optimized mean:
0.922 s

CPU time reduction:
26.1%

Speedup:
approximately 1.35x

Before the change, perf showed:

  • DIST2_ImageOrtho(): 56.71%
  • DIST2(): 18.07%

After inlining, those routines no longer appear as separate hot symbols; their work is folded into the calling Action_MinMaxDist routine.

Validation

The MinMaxDist benchmark output was byte-for-byte identical between baseline and optimized builds.

Regression tests:

  • Test_MinMaxDist: 6/6 comparisons passed
  • Test_Distance: 5/5 comparisons passed
  • Test_Radial: 17/17 comparisons passed

git diff --check reports no whitespace errors.

@drroe

drroe commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

That's funny, I actually have a branch I'm working on that implements something similar. I'll just merge this for now in the meantime.

@drroe
drroe merged commit 85b0ee9 into Amber-MD:master Oct 14, 2026
12 of 13 checks passed
@drroe

drroe commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

@whahnsr If you would like me to add your name and affiliation to the cpptraj README, let me know.

@whahnsr

whahnsr commented Oct 22, 2026 via email

Copy link
Copy Markdown
Contributor Author

@drroe drroe mentioned this pull request Oct 23, 2026
@drroe

drroe commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

I've added some details to the README, see #1203

Let me know if you'd like anything changed. Thanks again for your contributions!

-Dan

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