Daily Perf Improver - Optimize QR decomposition with SIMD Householder transformations #71
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
This PR optimizes QR decomposition using Householder reflections, achieving 19-44% speedup for typical matrix sizes by replacing manual scalar operations with SIMD-accelerated dot products and row updates.
Performance Goal
Goal Selected: Optimize QR decomposition (Phase 3, Linear Algebra Optimizations)
Rationale: The research plan from Discussion #4 identified Phase 3 linear algebra optimizations as the next step after Phase 1 & 2 work. QR decomposition is fundamental to many operations (least squares, eigenvalues, matrix factorization) and the Householder implementation had clear opportunities for SIMD optimization in its inner loops.
Changes Made
Core Optimization
File Modified:
src/FsMath/Algebra/LinearAlgebra.fs-updateQandapplyHouseholderLeftfunctions (lines 361-419)Original updateQ Implementation:
Optimized updateQ Implementation:
applyHouseholderLeft Optimization
The
applyHouseholderLeftfunction was also optimized with cleaner code structure, though the column-wise strided access pattern limits SIMD gains here. The main improvements come from:Approach
Performance Measurements
Test Environment
Results Summary
Detailed Benchmark Results
Before (Baseline):
After (Optimized):
Key Observations
Why This Works
The optimization addresses key bottlenecks in the Householder transformations:
SIMD Dot Products:
SpanMath.dotUncheckedVectorized Row Updates:
Zero-Check Optimization:
Contiguous Memory Access:
Replicating the Performance Measurements
To replicate these benchmarks:
Results are saved to
BenchmarkDotNet.Artifacts/results/in multiple formats.Testing
✅ All 1381 tests pass (8 skipped)
✅ QR benchmarks execute successfully
✅ Memory allocations unchanged
✅ Performance improves 19-44% across all sizes
✅ Correctness verified across all test cases
✅ Build completes with only pre-existing warnings
Implementation Details
Optimization Techniques Applied
SpanMath.dotUncheckedfor hardware-accelerated dot productsNumerics.Vector<T>Code Quality
Limitations and Future Work
While this optimization provides solid improvements, there are additional opportunities:
Next Steps
Based on the performance plan from Discussion #4, remaining Phase 3 work includes:
Related Issues/Discussions
Bash Commands Used
Web Searches Performed
None - this optimization was based on:
🤖 Generated with Claude Code