Daily Perf Improver - Optimize dot product with Vector.Sum horizontal reduction #33
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 dot product SIMD horizontal reduction achieving 8.3-35.8% speedup for small to medium-sized vectors by replacing manual accumulation with
Vector.Sum()
, which uses hardware-specific horizontal add instructions.Performance Goal
Goal Selected: Optimize dot product accumulation (Phase 2, Priority: MEDIUM)
Rationale: The research plan from Discussion #11 identified "dot product accumulation" as a potential optimization target, suggesting tree-reduction strategies for better performance and numerical stability. Upon investigation, I found that the dot product was using manual element-by-element accumulation instead of the optimized
Vector.Sum()
method that's already used elsewhere in the codebase (Matrix.fs).Changes Made
Core Optimization
File Modified:
src/FsMath/SpanMath.fs
-dotUnchecked
function (lines 39-41)Original Implementation:
Optimized Implementation:
Approach
Vector.Sum()
is already used in Matrix.fs for similar purposesVector.Sum()
Performance Measurements
Test Environment
Results Summary
Detailed Benchmark Results
Before (Baseline):
After (Optimized):
Key Observations
Vector.Sum()
uses horizontal add instructions (e.g., VPHADDPS on AVX)Why This Works
The optimization leverages hardware-specific instructions:
Hardware-Optimized Instructions:
Instruction-Level Parallelism:
Vector.Sum()
can use tree-reduction internallyConsistency with Codebase:
Vector.Sum()
for similar operationsReplicating the Performance Measurements
To replicate these benchmarks:
Results are saved to
BenchmarkDotNet.Artifacts/results/
in multiple formats.Testing
✅ All 488 tests pass
✅ DotProduct benchmarks execute successfully
✅ No memory allocations changed
✅ Performance improves 8.3-35.8% for small-medium vectors
✅ Correctness verified across all test cases
Implementation Details
Optimization Techniques Applied
Vector.Sum()
intrinsicsimdWidth
additions to log₂(simdWidth) in hardwareCode Quality
Limitations and Future Work
While this optimization provides solid improvements, there are additional opportunities:
Next Steps
Based on the performance plan from Discussion #11, remaining Phase 2 work includes:
Related Issues/Discussions
Bash Commands Used
Web Searches Performed
None - this optimization was based on:
🤖 Generated with Claude Code