Skip to content

Refactor: Vectorize dotprodScoring in StatsHelpers using Eigen (Closes #8126) - #8848

Merged
jpfeuffer merged 1 commit into
OpenMS:developfrom
satyam102006:refactor/8126-vectorize-math
Mar 7, 2026
Merged

jpfeuffer merged 1 commit into
OpenMS:developfrom
satyam102006:refactor/8126-vectorize-math

Conversation

@satyam102006

@satyam102006 satyam102006 commented Mar 7, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR addresses issue #8126 by vectorizing and centralizing math operations, starting with the heavily utilized dotprodScoring and manhattanScoring functions in StatsHelpers.cpp.

Replaced the manual for loop iterations and std::accumulate with Eigen::Map, cwiseSqrt(), and Eigen::VectorXd::dot(). This avoids memory copying while leveraging Eigen's underlying SIMD vectorization for the square roots, summations, and dot products.

Benchmarking

I ran a standalone benchmark to test the speedup of the Eigen implementation against the existing manual loop. Testing was done on arrays of size 1,000 across 1,000,000 iterations (simulating heavy OpenSwath usage).

Method Execution Time
Old Method (Manual Loop) 472.506 ms
New Method (Eigen .dot) 90.222 ms

Result: ~5.2x speedup.

If this architectural approach looks good to the maintainers, I can follow up with further PRs to address the redundant sums and norms scattered across Scoring.cpp and LinearRegression.cpp.

Summary by CodeRabbit

  • Refactor
    • Optimized performance of scoring algorithms through vectorized operations.

@satyam102006

Copy link
Copy Markdown
Contributor Author

@jpfeuffer can you check ?

@jpfeuffer

Copy link
Copy Markdown
Contributor

This is awesome and exactly what I was looking for!

@jpfeuffer
jpfeuffer marked this pull request as ready for review March 7, 2026 18:24
@coderabbitai

coderabbitai Bot commented Mar 7, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Vectorized mathematical operations in a statistics helper file by replacing element-wise square root loops with Eigen-based vectorized calculations. Added Eigen/Dense header support and optimized accumulation operations using Eigen maps for improved computational efficiency.

Changes

Cohort / File(s) Summary
Vectorization Optimization
src/openswathalgo/source/OPENSWATHALGO/ALGO/StatsHelpers.cpp
Replaced manual element-wise sqrt loops in dotprodScoring and manhattanScoring with vectorized Eigen operations. Added Eigen/Dense inclusion and refactored manual accumulation to use Eigen map sums for improved performance.

Poem

🐰 The humble loop, once element by element slow,
Now dances with Eigen's vectorized flow,
Where cwiseSqrt springs forth with algorithmic grace,
And maps sum faster through computational space!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: vectorizing dotprodScoring using Eigen. It is specific, clear, and directly reflects the primary modifications in the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (2)
src/openswathalgo/source/OPENSWATHALGO/ALGO/StatsHelpers.cpp (2)

41-53: Variable naming: prefer snake_case for local variables.

The coding guidelines specify that variable names must use snake_case. Consider renaming mapExp → map_exp and mapTheor → map_theor.

The vectorization approach using Eigen::Map with cwiseSqrt() is correct and efficient—it modifies the vector storage in-place without copying.

🔧 Suggested naming fix
-    Eigen::Map<Eigen::VectorXd> mapExp(intExp.data(), intExp.size());
-    Eigen::Map<Eigen::VectorXd> mapTheor(theorint.data(), theorint.size());
+    Eigen::Map<Eigen::VectorXd> map_exp(intExp.data(), intExp.size());
+    Eigen::Map<Eigen::VectorXd> map_theor(theorint.data(), theorint.size());

-    mapExp = mapExp.cwiseSqrt();
-    mapTheor = mapTheor.cwiseSqrt();
+    map_exp = map_exp.cwiseSqrt();
+    map_theor = map_theor.cwiseSqrt();

As per coding guidelines: "Variable names must use snake_case".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/openswathalgo/source/OPENSWATHALGO/ALGO/StatsHelpers.cpp` around lines 41
- 53, Rename the local Eigen::Map variables to follow snake_case: change mapExp
to map_exp and mapTheor to map_theor in the StatsHelpers.cpp snippet (update the
declarations Eigen::Map<Eigen::VectorXd> map_exp(...) and map_theor(...), and
their subsequent cwiseSqrt() assignments) so all references (assignments and any
future uses) use the new snake_case names consistently.

15-16: Remove #include <Eigen/Dense> and rely on #include <Eigen/Core> (line 15) only.

Eigen/Core already provides all the functionality used in this file: Eigen::Map, VectorXd, cwiseSqrt(), sum(), norm(), cwiseAbs(), and dot(). The Eigen/Dense header additionally pulls in decomposition modules (LU, Cholesky, QR, SVD, Geometry, Eigenvalues) which are not used here, increasing compile time unnecessarily.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/openswathalgo/source/OPENSWATHALGO/ALGO/StatsHelpers.cpp` around lines 15
- 16, Remove the unnecessary `#include` <Eigen/Dense> and rely solely on `#include`
<Eigen/Core> (already present) because this translation unit only uses
Eigen::Map, Eigen::VectorXd and element-wise/aggregate ops such as cwiseSqrt(),
sum(), norm(), cwiseAbs(), and dot(); delete the Eigen/Dense include to avoid
pulling in unused decomposition modules and reduce compile time.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/openswathalgo/source/OPENSWATHALGO/ALGO/StatsHelpers.cpp`:
- Around line 41-53: Rename the local Eigen::Map variables to follow snake_case:
change mapExp to map_exp and mapTheor to map_theor in the StatsHelpers.cpp
snippet (update the declarations Eigen::Map<Eigen::VectorXd> map_exp(...) and
map_theor(...), and their subsequent cwiseSqrt() assignments) so all references
(assignments and any future uses) use the new snake_case names consistently.
- Around line 15-16: Remove the unnecessary `#include` <Eigen/Dense> and rely
solely on `#include` <Eigen/Core> (already present) because this translation unit
only uses Eigen::Map, Eigen::VectorXd and element-wise/aggregate ops such as
cwiseSqrt(), sum(), norm(), cwiseAbs(), and dot(); delete the Eigen/Dense
include to avoid pulling in unused decomposition modules and reduce compile
time.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 703432b0-994d-4fe1-904c-1a12f645d079

📥 Commits

Reviewing files that changed from the base of the PR and between 3efb237 and a145e39.

📒 Files selected for processing (1)
  • src/openswathalgo/source/OPENSWATHALGO/ALGO/StatsHelpers.cpp

@jpfeuffer
jpfeuffer merged commit 8317056 into OpenMS:develop Mar 7, 2026
37 of 38 checks passed
@cbielow

cbielow commented Mar 8, 2026

Copy link
Copy Markdown
Contributor

What hardware did you run the benchmarks on?

@satyam102006

Copy link
Copy Markdown
Contributor Author

Hi @cbielow,

I ran the standalone benchmarks locally on my Mac Mini (m4) with [16GB] of RAM.

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