An optimized C++23 implementation of the Striped Smith-Waterman algorithm proposed by Michael Farrar, transitioning from a basic reference to hardware-accelerated SIMD (SSE/AVX) and RISC-V(RVV).
The Smith-Waterman algorithm is the gold standard for local sequence alignment in bioinformatics. However, its classic dynamic programming formulation has a quadratic time complexity of
The recurrence relation dictates that each cell in the scoring matrix
Because of these tight data dependencies (
To bypass these dependency bottlenecks, Michael Farrar (2007) proposed the Striped Smith-Waterman method. Instead of processing the matrix parallel to the query or diagonally, the query sequence is divided into parallel segments (stripes) equal to the SIMD vector width.
This approach:
- Increases instruction-level parallelism.
- Minimizes expensive vector-shift operations.
- Introduces a Lazy F-loop to handle and propagate delayed vertical gap corrections (
$F_{i,j}$ ) across SIMD boundary segments.
This repository is structured to show a clean evolutionary path from absolute logical correctness to bare-metal hardware optimization:
-
Golden Reference Smith Waterman: A C++20/23 implementation of classic Smith-Waterman, used as a validation reference in unit tests. -
RISC-V RVV Acceleration: Translation of the Strip-Smith-Waterman algorithm’s logic to the RISC-V SIMD model using vector extensions. Vector intrinsics for different LMULs are derived through template specialization at compile time. This template specialization is found in the filervv-traits.hpp. Performance measurements will be conducted on the Banana BPI F3 SBC, which features eight SpaceMIT K1 cores with a 256-bit VLEN. -
SIMD Acceleration(Work in Progress): The next phase involves mapping the validated emulated logic directly to hardware vector intrinsics (SSE4.1, AVX2, and AVX-512) to achieve massive performance speedups.
Evaluated natively on real silicon (Banana Pi BPI-F3, SpaceMit K1 octacore, RISC-V RVV 1.0 at LMUL = 1) comparing the scalar baseline against the vectorized Farrar Striped implementation:
| Query Size | Total Cells | Scalar Time (MCUPS) | MCUPS LMUL=1 (Speedup) | MCUPS LMUL=2 (Speedup) | MCUPS LMUL=4 (Speedup) | MCUPS LMUL=8 (Speedup) |
|---|---|---|---|---|---|---|
💡 Key Takeaways:
- Scalar Baseline Consistency: The classic scalar implementation exhibits near-constant execution throughput (
$\approx 64.5 \text{ MCUPS}$ ), confirming a stable hardware testbench and clock frequency.- Optimal Working Set (
$Q = 2000$ ): Peak efficiency reaches$643.61$ MCUPS ($9.20\times$ speedup) for LMUL=4. At this query length, vector striping aligns perfectly with the L1 Data Cache capacity and register file layout.- L1 Cache Eviction for (
$Q > 2000$ ): As the size of the query profile exceeds the capacity of the L1 cache, the phenomenon of cache eviction becomes more evident.
Evaluated natively on SpaceMit K1 RVV 1.0 at LMUL = 4 evaluating the asymptotic algorithmic scaling ($O(N^2)$) from small sequences up to
Execution comparison on SpaceMIT K1 (RISC-V Vector 1.0) measuring CPU execution time, throughput (MCUPS), and vector speedup across various sequence matrix dimensions.
| Query Size | Profile | Total Cells | Classic SW Time | Classic SW (MCUPS) | SSW RVV Time | SSW RVV (MCUPS) | Speedup |
|---|---|---|---|---|---|---|---|
| 65.22 | 77.62 | 1.19× | |||||
| 64.02 | 105.49 | 1.65× | |||||
| 68.49 | 358.63 | 5.24× | |||||
| 67.86 | 451.78 | 6.66× | |||||
| 68.33 | 525.83 | 7.70× | |||||
| 67.58 | 484.78 | 7.17× | |||||
| 68.50 | 569.47 | 8.31× | |||||
| 68.70 | 593.61 | 8.64× | |||||
| 68.76 | 452.84 | 6.59× |
Peak Acceleration: Achieved 8.64× Speedup (593.61 MCUPS) on
$20,000 \times 20,000$ alignment matrices.
Architectural Summary:
- Scalar Baseline Stability: Classic SW exhibits flat performance at
$\sim 68.5\text{ MCUPS}$ regardless of input length due to execution pipeline latency bound loops.- Vector Vectorization Gain: RVV Striped SW rapidly scales throughput beyond
$500\text{ MCUPS}$ once vector register saturation improves instruction density ($|Q| \ge 2,000$ ).- Memory Hierarchy Impact: At extreme lengths (
$50,000\times 50,000$ ,$2.5\times 10^9$ cells), throughput lowers to$452.84\text{ MCUPS}$ due to memory subsystem limits and L2 cache capacity pressure.
Experimental profiling on the SpaceMIT K1 processor reveals a strict structural trade-off between instruction efficiency and vector register availability across varying query lengths (
-
Optimal Operating Point (LMUL = 4):
-
Throughput: Reaches a peak throughput of 630.74 MCUPS (
$|Q|=2000$ ) and maintains an average throughput of 558.2 MCUPS, representing a 1.81× speedup over theLMUL=1baseline. -
Instruction Density: Evaluates up to 1.69 cells per instruction issued (
cells_per_inst), reducing total dynamic instruction count by up to 74% compared toLMUL=1.
-
Throughput: Reaches a peak throughput of 630.74 MCUPS (
-
Register Spilling & Collapse at LMUL = 8:
- Register Pressure: Aggregating vector registers into groups of 8 reduces the architectural register file to only 4 logical vector registers.
-
Cache Load Surge: For
$|Q|=10,000$ , memory read requests (L1 Data Loads) drop from 690.1M (LMUL=1) down to 464.3M (LMUL=4). However, underLMUL=8, register spilling forces stack spills, causing L1 Data Loads to surge by +29.7% up to 602.1M. -
Execution Pipeline Saturation: Pipeline stall analysis confirms that
LMUL=8induces severe execution latency stalls, elevating Backend Stalls to >91.4% and degrading the hardware IPC down to 0.124.
To compile and run the current emulated golden reference:
# Compile using C++23
cmake -B build -DCMAKE_C_COMPILER=riscv64-linux-gnu-gcc -DCMAKE_CXX_COMPILER=riscv64-linux-gnu-g++ -DCMAKE_BUILD_TYPE=Release
cmake --build build
# Run the alignment tests
build/unit_test1
build/unit_test2An example of alignment
ALIGNMENT
A G C - T G A C - A T C G A T A C G A G C T G G C T A - G A C A T T C A C G A T A C G
| | | | | | | | | | | | | | | | | | | | | | | | | |
A G C T T G - C G A - - G - T A C - A - - - G G C T A - G - - A T T - A C - A - A - G
- Farrar's Striped Algorithm: Farrar, M. (2007). Striped Smith–Waterman database searching instruments. Bioinformatics, 23(2), 156-161. DOI: 10.1093/bioinformatics/btl582
- Original Smith-Waterman: Smith, T. F., & Waterman, M. S. (1981). Identification of common molecular subsequences. Journal of Molecular Biology, 147(1), 195-197. DOI: 10.1016/0022-2836(81)90087-5