Skip to content

add AVX2/AVX-512 to ksw_extd2 with runtime dispatch - #20

Open
nh13 wants to merge 1 commit into
masterfrom
extd2-avx512-dispatch
Open

add AVX2/AVX-512 to ksw_extd2 with runtime dispatch#20
nh13 wants to merge 1 commit into
masterfrom
extd2-avx512-dispatch

Conversation

@nh13

@nh13 nh13 commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

ksw_extd2 (dual-gap banded extension) is minibwa's main base-alignment cost for long reads (~50–66% of HiFi/ONT CPU) and was 128-bit only. This adds AVX2 and AVX-512BW implementations of the gap-fill path, selected at runtime via __builtin_cpu_supports so a single binary auto-uses the widest available ISA and falls back to SSE4.2/NEON otherwise. Only the banded approx+cigar path is widened; every other flag combination calls the original kernel.

Output is bit-identical to the SSE4.2 build on all four HG002 zenodo sets vs GRCh38, across sse/avx2/avx512 (~4.4M alignment records, 0 diffs).

End-to-end on a c7i (Sapphire Rapids), user CPU vs SSE4.2:

set        readlen mean/median   avx2    avx512
WGS-1M     151 / 151             1.00x   1.00x
HiC-1M     151 / 151             1.00x   1.00x
SBX-HG002  241 / 227             1.00x   1.00x
HiFi-10k   18881 / 18047         1.15x   1.19x
ONT-10k    19810 / 18180         1.11x   1.14x

The win needs wide-band DP — long, divergent reads where the gap-fill bands are wide; it is absent on short/mid reads and on low-divergence data of any length (confirmed by simulation: flat with small indels at every length, ~1.09x only at 16 kb with large indels). MINIBWA_EXTD2_ISA=sse|avx2|avx512 forces a tier for testing.

@nh13
nh13 force-pushed the extd2-avx512-dispatch branch 3 times, most recently from 5ee0228 to cd10f54 Compare June 19, 2026 05:55
ksw_extd2 (dual-gap banded extension) is the main base-alignment cost for
long reads (~50-66% of HiFi/ONT CPU) and was 128-bit only. This adds AVX2
and AVX-512BW versions of the gap-fill path, selected at runtime via
__builtin_cpu_supports (one binary; falls back to SSE4.2/NEON). Only the
banded approx+cigar path is widened; other flags call the original kernel.

Output is bit-identical to the SSE4.2 build on all four HG002 zenodo sets
vs GRCh38, across sse/avx2/avx512 (~4.4M alignment records, 0 diffs).

End-to-end on a c7i (user CPU, vs SSE4.2):
  set        readlen mean/median   avx2   avx512
  WGS-1M     151 / 151             1.00x  1.00x
  HiC-1M     151 / 151             1.00x  1.00x
  SBX-HG002  241 / 227             1.00x  1.00x
  HiFi-10k   18881 / 18047         1.15x  1.19x
  ONT-10k    19810 / 18180         1.11x  1.14x

The win needs wide-band DP -- long, divergent reads where gap-fill bands
are wide; it is absent on short/mid reads and on low-divergence data of any
length (confirmed by simulation: flat with small indels at every length,
~1.09x only at 16 kb with large indels). MINIBWA_EXTD2_ISA=sse|avx2|avx512
forces a tier for testing.
@nh13
nh13 force-pushed the extd2-avx512-dispatch branch from cd10f54 to fde95fe Compare June 21, 2026 08:41
@lh3

lh3 commented Jun 22, 2026

Copy link
Copy Markdown
Owner

I like the way AVX2/AVX512 is implemented but this will considerably increase the burden of maintenance in the long term. Given that the short-read performance remains the same and minimap2 still dominates long-read alignment, I will not merge now. I will keep the PR open until there is a real need.

@lh3 lh3 added the enhancement New feature or request label Jun 22, 2026
@nh13

nh13 commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator Author

I agree there's a tension between maintenance burden and trying to make minibwa as fast as one can, supporting both long and short reads, as well as generally trying to favor simplicity. Notheless, the small speed improvements compound, especially if we do them earlier in development, as more executions of minibwa will benefit. I am hopefull that coding agents can help with the maintenance, is that something you'd be open to?

@nh13

nh13 commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator Author

Independently reproduced on a fresh c7i.4xlarge (Xeon 8488C), this time as wall-clock of the mapping phase rather than user CPU, five reps per tier interleaved with order reversal, -t 16, SAM to /dev/null:

             avx2    avx512   rep spread
  HiFi-10k   1.123x  1.164x   0.28-1.43%
  ONT-10k    1.109x  1.134x
  WGS-1M     1.000x  1.000x   0.14-0.25%

Same shape as the user-CPU numbers above, slightly lower as expected for a wall-clock metric that includes the unwidened parts of the run. SAM output byte-identical across all three tiers on HiFi and WGS, re-confirming the bit-identity check. MINIBWA_EXTD2_ISA made this easy — one binary, three tiers, so the comparison carries no build or link differences.

Two notes for whoever picks this up:

The short-read rows were measured with -x sr, which #63 shows is currently the same preset as -x adap. The numbers are correct as measured; the label is what is wrong. If #63 lands and sr becomes genuinely non-adaptive, those rows want re-taking.

#64 speeds up the 128-bit ksw_extd2/ksw_extz2 by 5-11% depending on read length. This PR's dispatcher calls the reference kernel for every flag combination except banded approx+cigar, so the two compose, but the ratios above are against an unimproved baseline and would shrink if #64 lands first.

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants