Skip to content

Refactor: Vectorize cross-correlation in Scoring using Eigen (#8126) - #8849

Merged
timosachsenberg merged 2 commits into
OpenMS:developfrom
satyam102006:refactor/8126-vectorize-scoring
Mar 18, 2026
Merged

timosachsenberg merged 2 commits into
OpenMS:developfrom
satyam102006:refactor/8126-vectorize-scoring

Conversation

@satyam102006

@satyam102006 satyam102006 commented Mar 8, 2026 •

Copy link
Copy Markdown
Contributor

Description

Continuing the vectorization effort for issue #8126. This PR targets the heavily utilized cross-correlation functions in Scoring.cpp (calculateCrossCorrelation and calcxcorr_legacy_mquest_).

Replaced the manual nested for loops and std::accumulate calls with Eigen::Map, .array().square().sum(), and .dot(). This leverages Eigen's SIMD vectorization to compute the dot products, variances, and means across the array slices without manual iteration.

Benchmarking

I ran a standalone benchmark to test the speedup of the Eigen implementation against the existing manual loops. Testing was done on arrays of size 500 across 5,000 iterations.

Method Execution Time
Old Method (Manual Loop) 1399.37 ms
New Method (Eigen) 147.57 ms

Result: ~9.5x speedup.

Hardware: Mac Mini (Apple Silicon), compiled with g++ -O3.

Summary by CodeRabbit

  • Refactor

    • Internal scoring algorithm implementation refined for improved computational efficiency.
  • Tests

    • Expanded test coverage for scoring functions with comprehensive equivalence validation, mathematical property checks, and edge-case scenarios to ensure reliability.

@jpfeuffer

Copy link
Copy Markdown
Contributor

Hmm yeah I was expecting some error like this sooner or later.
Slight numeric differences might lead to completely different decisions in the code downstream, like declaring outliers.

Here we must validate that the new code is equivalent in all but the most extreme cases and then we must check the failing example to see which code handles which of the extreme cases better and decide what to keep.

I have the feeling that Eigen has thought a bit more about correct/safer/faster math than we did but maybe it's also "just" faster.

@timosachsenberg

Copy link
Copy Markdown
Contributor

I think I gave it a try before and ran into similar issues. You could try to update test files. If we are lucky they pass on all platforms (before they didn't. Likely because AVX was enabled on windows but not on linux). So worth a try.

@satyam102006

Copy link
Copy Markdown
Contributor Author

@jpfeuffer @timosachsenberg Thanks for the guidance You were spot on about the floating-point differences.

I investigated the 3 failing tests (21, 23, 27) and updated their baselines as Timo suggested. Here is what changed:

The Cause: Eigen's SIMD tree-reduction changes deep decimal rounding compared to the old sequential loops.

The Edge Case (Test 27): Eigen correctly evaluates an extreme outlier limit as NaN instead of defaulting to 0

@satyam102006

Copy link
Copy Markdown
Contributor Author

@timosachsenberg @jpfeuffer You both called it! The updated baselines pass locally (macOS) but fail on (tests 21 and 27) due to cross-platform SIMD differences.

I'm pulling the Linux FuzzyDiff logs now to check the exact numbers for these extreme cases. Once we see how the math diverges, we can decide if we just need to adjust the tolerances. I'll post the diffs shortly!

@jpfeuffer

jpfeuffer commented Mar 9, 2026 •

Copy link
Copy Markdown
Contributor

Note that they also fail on macos here 😄 no platform difference

@satyam102006

Copy link
Copy Markdown
Contributor Author

@jpfeuffer My local Mac is Apple Silicon, so the CI runners are naturally producing slightly different floating-point rounding.

I'll grab the FuzzyDiff logs from the CI right now to check the exact numbers so we can figure out the best fix

@jpfeuffer

Copy link
Copy Markdown
Contributor

they are silicon too

@satyam102006

Copy link
Copy Markdown
Contributor Author

@jpfeuffer Haha, there goes that theory! 😄 If the hardware is the same, it must be slight differences in compiler versions or optimization flags.

@satyam102006

Copy link
Copy Markdown
Contributor Author

@jpfeuffer @timosachsenberg While I track down these specific CI logs, my eyes could use a quick break from staring at floating-point diffs 😄

Are there any other open issues, or perhaps another small refactoring task you'd like me to take a look at in the meantime?

@timosachsenberg

Copy link
Copy Markdown
Contributor

I think you can also focus on your proposal. But if you find time: fixing these kind of things or coming up with a better test strategy is immensely valuable. :)

@jpfeuffer

jpfeuffer commented Mar 10, 2026 •

Copy link
Copy Markdown
Contributor

No I dont think so, I already listed some in the other issue. The switching to pixi PR recently might have become "free" #8823 but it is not easier than this here and you should quickly check with the previously responsible person.

satyam102006 and others added 2 commits March 18, 2026 16:30
…lity

Add comprehensive unit tests for the Eigen-vectorized scoring functions:
- Numerical equivalence tests comparing Eigen vs manual-loop reference
  implementations across small, medium, and large array sizes
- Mathematical property tests (autocorrelation max at lag 0, normalized
  autocorrelation unity, symmetry)
- Edge case tests (all zeros, constant values, single/two elements)
- Consistency test between legacy and normalized cross-correlation

Replace platform-specific baseline updates with FuzzyDiff whitelist for
potentialOutlier in TOPP tests 21, 23, 27. The outlier detection decision
is inherently fragile when cross-correlation scores are nearly equal, and
tiny FP rounding differences flip the min/max index comparison. All numeric
score differences are well within FuzzyDiff default tolerance (ratio 1.01,
absdiff 0.01).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@timosachsenberg
timosachsenberg force-pushed the refactor/8126-vectorize-scoring branch from ff24fef to c3df6d1 Compare March 18, 2026 16:22

@timosachsenberg timosachsenberg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpfeuffer I think this an be accepted

@timosachsenberg
timosachsenberg marked this pull request as ready for review March 18, 2026 17:50
@timosachsenberg
timosachsenberg merged commit 1ff4dd0 into OpenMS:develop Mar 18, 2026
39 checks passed
@coderabbitai

coderabbitai Bot commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8621f448-8da6-4d30-a7c8-8b7a26a69242

📥 Commits

Reviewing files that changed from the base of the PR and between 355bef5 and c3df6d1.

📒 Files selected for processing (3)
  • src/openswathalgo/source/OPENSWATHALGO/ALGO/Scoring.cpp
  • src/tests/class_tests/openswathalgo/Scoring_test.cpp
  • src/tests/topp/CMakeLists.txt

📝 Walkthrough

Walkthrough

Scoring.cpp replaces scalar cross-correlation loops with vectorized Eigen dot products using Eigen::Map, including per-iteration length checks and improved normalization. Scoring_test.cpp adds comprehensive test coverage with reference implementations for equivalence validation, mathematical properties, and edge cases. CMakeLists.txt updates test expectations with additional whitelisting and parameter adjustments.

Changes

Cohort / File(s) Summary
Cross-correlation Vectorization
src/openswathalgo/source/OPENSWATHALGO/ALGO/Scoring.cpp
Optimizes cross-correlation calculations by replacing element-wise scalar loops with Eigen-based vectorized dot products. Introduces per-iteration length checks, derives sub-vectors via Eigen::Map, recomputes means using Eigen operations, and adds safer normalization with zero-fallback guard on denominator.
Scoring Test Coverage
src/tests/class_tests/openswathalgo/Scoring_test.cpp
Adds reference implementations for cross-correlation validation (calculateCrossCorrelation_ref, calcxcorr_legacy_ref, generate_test_signal helpers) and introduces six new test cases covering equivalence checks, mathematical properties (autocorrelation, symmetry), edge cases (all-zeros, constants, single-element), and legacy vs normalized consistency.
TOPP Test Expectations
src/tests/topp/CMakeLists.txt
Updates OpenSwathWorkflow test whitelisting to include "potentialOutlier" entries alongside existing id= whitelists for tests 21, 23, and 27. Adjusts scoring parameter usage from using_ms1_mi to use_mi_score in tests 18, 19, and 20.

Possibly related PRs

Suggested reviewers

  • jpfeuffer

Poem

🐰 Cross-correlations hop with vectorized grace,
Eigen maps dance in computational space,
From scalar loops to dot-products bright,
Our tests validate each optimized flight,
Whisker-twitching speed and accuracy unite!

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

3 participants