8000 reassociate the affine-gap recurrence in ksw2_ll for arm64 by nh13 · Pull Request #6 · nh13/minibwa · GitHub
[go: up one dir, main page]

Skip to content

reassociate the affine-gap recurrence in ksw2_ll for arm64 - #6

Draft
nh13 wants to merge 1 commit into
masterfrom
perf/ll-sse-affine-reassoc-arm
Draft

nh13 wants to merge 1 commit into
masterfrom
perf/ll-sse-affine-reassoc-arm

Conversation

@nh13
@nh13 nh13 commented Jun 25, 2026 •
Copy link
Copy Markdown
Owner

Recreated from lh3/minibwa#37, which Heng closed unmerged on 2026-06-22.
Heng's closing rationale: "Simplicity over performance. This PR changes the logic despite identical results. It is not worth the minor performance gain."
Recreated as a draft in this fork (nh13/minibwa) for continued development and review.
Note: this touches the ksw2_ll SW kernel; per repo policy, any further iteration on the kernel must be validated with a differential bench before merge.


ksw_ll_u8_core/ksw_ll_i16_core (the striped kernel used for paired-end mate rescue) update each cell with E'(i+1,j)=max(E'-r, H'-q) and F'(i,j+1)=max(F'-r, H'-q), where H'=max(M,E',F') and q=o+e>=e=r. Since o>=0, the E' term of H'-q is dominated by E'-r and the F' term by F'-r, so E' only needs max(M,F)-q and F' only needs max(M,E)-q. Reading those in place of the reduced H'(i,j) is score- and CIGAR-exact, but drops the gap updates' dependency on the final three-way max and shortens the loop-carried F chain.

This wins on arm64 (NEON) and loses on x86 (the extra max per cell outweighs the shorter chain), so it sits under #if defined(__ARM_NEON) and x86 keeps the original recurrence. Both the 8- and 16-bit cores are converted.

Checked with a standalone harness that builds the old and new ksw_ll_{u8,i16}_core side by side and compares them per pair: 120000 pairs identical over qlen 64..250, 8- and 16-bit; minibwa map -x sr on HG002 WGS PE gives byte-identical SAM. Kernel throughput (candidate/baseline): Graviton4 1.13-1.17x, Apple NEON ~1.1-1.2x; x86, gated off, measured 0.90-0.92x on Sapphire Rapids and 0.95-0.98x on Zen3. End-to-end single-thread wall is ~2% on WGS short-read PE, where mate rescue is ~1/5 of the work.

In the striped inner loop E'/F' read max(M,F)/max(M,E) rather than the reduced
H'(i,j). As the gap open o>=0 the term dropped from each is always dominated, so
the recurrence is unchanged (score- and CIGAR-exact). This breaks the e/f
updates' dependency on the fully reduced H'(i,j) and shortens the loop-carried F
chain, speeding mate rescue by ~1.15x on arm64. x86 keeps the original
recurrence, which is faster there.
nh13 pushed a commit that referenced this pull request Jul 19, 2026
1. In bwa-mem, mapq_se is not changed if it is higher than mapq_pe. This is
   supposed to take care of the case where one read is highly unique and the
   mate falls in TRs. However, there is another case: read1 mapped at NM=1 and
   read2 at NM=0, versus, read1 at NM=0 and read2 at NM=1. The pair should get
   mapq 0. With this PR, mapq_se is only allowed to be higher if the read has
   only one hit. This may be relaxed.

2. Even if the unpaired alignment is preferred, its mapq should be adjusted
   against the best pair. Bwa and older minibwa does not have this mechanism.

3. Add a heuristic learned from dragmap: somewhat cap mapq_pe by considering
   the second best hit of each end. Effectively, we penalize repetitive reads
   even if there is only one good paired alignment, because the second best
   pair is more likely to be missing. Minibwa already implemented two other
   heuristics but the new heuristic still plays a role in rare cases.

This PR addresses z20 and z21 in #6
@nh13 nh13 added the feature review vehicle for a manifest feature; never a merge vehicle label Aug 3, 2026
@nh13
nh13 commented Sep 4, 2026
Copy link
Copy Markdown
Owner Author

@coderabbitai pause

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature review vehicle for a manifest feature; never a merge vehicle

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0