betaFeedback

WorksComputer Science

Vectorizing Recurrences over Semirings

Abstract

Recurrences are ubiquitous in signal and image processing, bioinformatics, numerical computing, combinatorial optimization, and many other domains. Because recurrences seem inherently sequential, they are often implemented serially and thus do not leverage vector instructions in modern hardware. This leaves much of the available, often highly parallel, hardware underutilized. In this paper, we present an algebra of recurrences over semirings and study algebraic identities that enable vectorization. Using our algebraic framework, we present a number of vectorizing transformations that yield efficient, vectorized implementations across various settings. These transformations naturally extend to 2D recurrences and mutual recurrences. We implement the recurrence algebra and the vectorizing transformations in a compiler called VecRec.VecRec compiles recurrence programs to efficient vectorized C++ implementations. The generated kernels can achieve up to an order-of-magnitude speedup over serial implementations across a wide variety of recurrences. A case study in bioinformatics shows that they are competitive with, and in many cases better than, existing libraries with manually-crafted vectorization.

CCS Concepts: • Do Not Use This Code → Generate the Correct Terms for Your Paper; Generate the Correct Terms for Your Paper; Generate the Correct Terms for Your Paper; Generate the Correct Terms for Your Paper.

Authors’ Contact Information: Yihong Zhang, [email protected], University of Washington, Seattle, USA; Bala Vinaithirthan, Stanford University, Stanford, USA; Karima Ma, Adobe, USA; Zachary Tatlock, [email protected], University of Washington, Seattle, USA; Maaz Bin Safeer Ahmad; Andrew Adams, Adobe, USA.

Introduction

A recurrence defines a sequence of values, where each value f(x)f(x) depends on previous values (e.g., f(x−1)f(x-1)). Recurrences are ubiquitous in many computational domains. The simplest form of a recurrence is a prefix sum of an input sequence gg:

f(x)=f(x−1)+g(x).f(x) = f(x-1) + g(x).

(Prefix Sum)

In signal processing, many classical filters can be expressed as recurrences. They are known as infinite-impulse response (IIR) filters. For example,

f(x)=1.8⋅f(x−1)−0.9⋅f(x−2)+g(x).f(x) = 1.8 \cdot f(x-1) - 0.9 \cdot f(x-2) + g(x).

(A Band-Pass Filter)

In computer graphics, an approximate distance transform is a recurrence with min and +:

f(x,y)=min⁡(f(x−1,y)+1,f(x,y−1)+1,f(x−1,y−1)+2,g(x,y)).f(x,y) = \min\left(f(x-1,y)+1, f(x,y-1)+1, f(x-1,y-1)+\sqrt{2}, g(x,y)\right).

(Distance Transform)

Sequence alignment algorithms in bioinformatics have a similar pattern, where ss is a score matrix and cgapc_{\mathrm{gap}} is a constant gap penalty:

f(x,y)=max⁡(f(x−1,y−1)+s(x,y),f(x−1,y)+cgap,f(x,y−1)+cgap).f(x,y) = \max\left(f(x-1,y-1)+s(x,y), f(x-1,y)+c_{\mathrm{gap}}, f(x,y-1)+c_{\mathrm{gap}}\right).

(Needleman–Wunsch)

Even dynamic programming problems on LeetCode often reduce to computing recurrences:

f1(x,y)=max⁡(f1(x,y−1),f2(x−1,y−1)−g(y)),f2(x,y)=max⁡(f1(x,y−1)+g(y),f2(x,y−1)).\begin{aligned} f_1(x,y) &= \max\left(f_1(x,y-1), f_2(x-1,y-1)-g(y)\right),\\ f_2(x,y) &= \max\left(f_1(x,y-1)+g(y), f_2(x,y-1)\right). \end{aligned}

(LeetCode 188. Buy/Sell Stock) These tasks from signal processing, graphics, bioinformatics, and LeetCode are all instances of the same problem: computing an FF that satisfies

F=A⊗F⊕GF = A \otimes F \oplus G

over a semiring τ\tau. In this definition, AA is a convolution kernel (a finite array of coefficients, or taps), GG is the input signal (a stream of values), FF is the output signal, and convolution ⊗\otimes computes a sliding weighted sum of the signal by the kernel.1 For example, by instantiating the recurrence over the arithmetic (semi)ring and letting AA be the kernel [0,1.8,−0.9][0, 1.8, -0.9], we obtain the band-pass filter above. Similarly, if we use the (min, +)-semiring and set the (2D) kernel to [∞112]\begin{bmatrix} \infty& 1 \\ 1 & \sqrt{2} \end{bmatrix}, we obtain the distance transform.

These recurrence relations are fundamental building blocks in their respective domains. Computing them efficiently is important, yet difficult. While modern vector instruction sets like AVX-512 allow computing 16 values at a time, these recurrences enforce dependencies on previous output values (e.g., f(x)f(x) depends on f(x−1)f(x-1)), forcing a serial implementation.

Previous work has studied different aspects of speeding up these recurrences, such as multithreading [4, 29], tiling [33], and data layout transformations [11]. This work takes a different approach: we vectorize recurrences through algebraic transformations. Unlike prior approaches that are tailored to specific application domains, our approach is general: we can vectorize a wide class of recurrences, including all those in our examples, with speedups of up to an order of magnitude.

Core to our approach are the algebraic structures of the recurrences F=A⊗F⊕GF = A \otimes F \oplus G. We define an algebra for recurrences parametrized by a semiring τ\tau. This algebra abstracts over the orders of recurrences,2 the numerical values of coefficients, and the concrete operators, retaining only their algebraic properties. We then present algebraic identities for recurrences over different semirings that enable vectorization for a wide range of real-world recurrences.

We instantiate the recurrence algebra and vectorizing transformations in a compiler dubbed VecRec. Given an input expression, VecRec exhaustively applies the transformation schemes to derive a set of vectorization-friendly alternative forms. It generates an efficient vectorized C++ kernel for each form and picks the best-performing kernel based on runtime. The generated kernels are significantly faster on a diverse set of recurrences and, in the bioinformatic domain, outperform state-of-the-art vectorized libraries.

Overview

To convey the vectorizing transformations this paper considers, we first (painfully) vectorize a recurrence by manual term manipulation. We then sketch our recurrence algebra and show how a handful of simple identities can transform the same example into the same vectorized form.

Vectorizing a second-order digital filter

Consider the following band-pass filter again:

f(x)=af(x−1)+bf(x−2)+g(x)a=1.8,b=−0.9.(1)f(x) = af(x-1) + bf(x-2) + g(x) \qquad a = 1.8, b = -0.9. \tag*{(1)}

As written, this recurrence is not directly vectorizable: the data dependence of f(x)f(x) on f(x−1)f(x-1) prevents computing consecutive outputs in a single SIMD vector.

To break this dependence, we expand f(x−1)f(x-1) and simplify:

f(x)=(a2+b)f(x−2)+abf(x−3)+g(x)+ag(x−1).(2)f(x) = (a^2 + b)f(x-2) + abf(x-3) + g(x) + ag(x-1). \tag*{(2)}

After this substitution, f(x)f(x) no longer depends on f(x−1)f(x-1), so we can compute two consecutive outputs in a single vector, as the following “vectorized” pseudocode3 shows:

f[x:x+2] = (a*a+b) * f[x-2:x] + a*b * f[x-3:x-1] + g[x:x+2] + a * g[x-1:x+1]

Repeating this process for f(x−2)f(x-2), f(x−3)f(x-3), …\ldots, f(x−n)f(x-n) lets us compute nn elements in parallel.

This approach, however, creates too much extra work. Each substitution lets us compute one more element in parallel, but it also incurs one fused multiply-add (fma) with a non-recursive gg term (e.g., g(x−1)g(x-1)). The original recurrence takes 2 fmas to compute one element, while the nn-times-substituted version takes n+2n+2 vectorized fmas to compute n+1n+1 elements in parallel, so we reduce the number of fmas used by (2/1)/((n+2)/(n+1))≤2(2/1)/((n+2)/(n+1)) \le2 times.

To do better, observe that by rearranging Equation (1), f(x−1)f(x-1) can be defined using f(x)f(x) and f(x−2)f(x-2):

f(x−1)=f(x)−bf(x−2)−g(x)a(3)f(x-1) = \frac{f(x)-bf(x-2)-g(x)}{a} \tag*{(3)}

Using this to eliminate f(x−3)f(x-3) in Equation (2), we obtain a definition of f(x)f(x) in terms of f(x−2)f(x-2) and f(x−4)f(x-4),

f(x)=(a2+b)f(x−2)+ab(f(x−2)−bf(x−4)−g(x−2)a)+g(x)+ag(x−1)(4)f(x) = (a^2+b)f(x-2) + ab\left(\frac{f(x-2)-bf(x-4)-g(x-2)}{a}\right) + g(x) + ag(x-1) \tag*{(4)}
=a′f(x−2)+b′f(x−4)+g′(x)(5)= a'f(x-2) + b'f(x-4) + g'(x) \tag*{(5)}
wherea′=a2+2b,b′=−b2,g′(x)=g(x)+ag(x−1)−bg(x−2).(6)\text{where}\quad a'=a^2+2b,\quad b'=-b^2,\quad g'(x)=g(x)+ag(x-1)-bg(x-2). \tag*{(6)}

Computing the auxiliary g′g' costs 2 additional fmas. We can apply the trick again to f(x)=a′f(x−2)+b′f(x−4)+g′(x)f(x)=a'f(x-2)+b'f(x-4)+g'(x): expand f(x−2)f(x-2), define f(x−6)f(x-6) in terms of f(x−4)f(x-4) and f(x−8)f(x-8), and substitute. We now define f(x)f(x) in terms of f(x−4)f(x-4) and f(x−8)f(x-8), moving the self-dependency from 2 to 4 elements away. In effect, this procedure doubles the distances between a recurrence’s coefficients. We call it dilation.

By repeatedly applying dilation, we obtain a tower of definitions:

g1(x)=a0g(x−1)+b0g(x−2)+g(x),…gn(x)=an−1gn−1(x−2n−1)+bn−1gn−1(x−2n)+gn−1(x),f(x)=anf(x−2n)+bnf(x−2n+1)+gn(x).\begin{aligned} g_1(x) &= a_0g(x-1)+b_0g(x-2)+g(x),\\ &\ldots\\ g_n(x) &= a_{n-1}g_{n-1}(x-2^{n-1})+b_{n-1}g_{n-1}(x-2^n)+g_{n-1}(x),\\ f(x) &= a_nf(x-2^n)+b_nf(x-2^{n+1})+g_n(x). \end{aligned}

Each layer of the tower doubles the distance between coefficients. Moreover, since the definition of each gig_i does not depend on gig_i itself, we can compute the elements of gig_i in parallel, for any i=1,…,ni=1,\ldots,n. In total, this tower of definitions requires 2⋅(n+1)2\cdot(n+1) vectorized fmas, but it can compute 2n2^n elements at once. In other words, nn steps of dilation reduce the number of fmas by

2/12⋅(n+1)/2n×=2nn+1×.\frac{2/1}{2\cdot(n+1)/2^n}\times=\frac{2^n}{n+1}\times.

This trick is known as scattered-lookahead [25] in digital circuit design but is largely overlooked in software, where it can yield significant speedups, as we will show: on an AMD Zen 5 CPU, dilating the above filter four times achieves a 4.86× speedup.

In this work, we systematically study such vectorization techniques. Dilation for arithmetic recurrences is just one of many transformations that enable vectorization; Figure 1 organizes the full set used in this paper by the algebraic property each exploits.

Vectorization techniques proposed in this paper

Figure 1. Vectorization techniques proposed in this paper. Fast associative scan and path-based decomposition apply to any recurrence, while dilation requires a time-invariant kernel over a ring or an idempotent semiring. VecRec use all applicable transformations and use profiling to find the fastest kernel.

The recurrence algebra

We propose an algebra of linear, fixed-order recurrences, inspired by the Z-transform [24]. Central to our recurrence algebra are two types of values, kernels and signals, and two types of operations, convolution (⊗\otimes) and (pointwise) addition (⊕\oplus).

Under this algebra, a recurrence FF is defined as a signal that satisfies F=A⊗F⊕GF=A\otimes F\oplus G. Here, GG and FF are the input and output signals, and AA is a convolution kernel. That is, FF is defined as the convolution of itself with a kernel AA, plus an external input GG. To avoid self-dependency (i.e., f(x)f(x) depending on f(x)f(x)), we require the kernel AA to be causal: it is zero at the origin, i.e., A[0]=0τA[0]=0_\tau.

The recurrence algebra is parametrized by the semiring it operates over. For instance, we can also interpret F=A⊗F⊕GF=A\otimes F\oplus G over the tropical semiring (R∪{∞},min⁡,+,∞,0)(\mathbb{R}\cup\{\infty\},\min,+,\infty,0), where ⊕\oplus is minimum and ⊗\otimes is addition. Tropical recurrences capture DP-style optimization problems such as the distance transform in image processing and sequence alignment in bioinformatics.

Identities of the recurrence algebra

The identities of the recurrence algebra express the vectorizing transformations in § 2.1 cleanly. For instance, arithmetic is not just a semiring but a ring, so it has an additive inverse (subtraction), denoted as ⊖\ominus. This allows us to perform the following derivation.

Given a time-invariant arithmetic kernel A=[0,a1,a2,…]A=[0,a_1,a_2,\ldots], let us define EE and OO as the even and odd terms of AA, i.e., E=[0,0,a2,0,a4,…]E=[0,0,a_2,0,a_4,\ldots] and O=[0,a1,0,a3,…]O=[0,a_1,0,a_3,\ldots], such that A=E⊕OA=E\oplus O. We have

References

  1. [1]Mahmoud Abo Khamis, Hung Q. Ngo, Reinhard Pichler, Dan Suciu, and Yisu Remy Wang. 2024. Convergence of datalog over (Pre-) Semirings. J. ACM 71, 2, Article 8 (April 2024), 55 pages. doi:10.1145/3643027DOI
  2. [2]Guy E. Blelloch. 1990. Prefix Sums and Their Applications. Technical Report CMU-CS-90-190. School of Computer Science, Carnegie Mellon University.
  3. [3]Mihai Budiu, Tej Chajed, Frank McSherry, Leonid Ryzhyk, and Val Tannen. 2024. DBSP: Incremental Computation on Streams and Its Applications to Databases. SIGMOD Rec. 53, 1 (May 2024), 87–95. doi:10.1145/3665252.3665271DOI
  4. [4]Gaurav Chaurasia, Jonathan Ragan-Kelley, Sylvain Paris, George Drettakis, and Fredo Durand. 2015. Compiling High Performance Recursive Filters. In Proceedings of the 7th Conference on High-Performance Graphics (Los Angeles, California). 85–94. doi:10.1145/2790060.2790063
  5. [5]Clang Developers. [n. d.]. Clang Language Extensions. https://clang.llvm.org/docs/LanguageExtensions.html. Clang 23.0.0git documentation.
  6. [6]Jeff Daly. 2016. Parasail: SIMD C library for global, semi-global, and local pairwise sequence alignments. BMC Bioinformatics 17, 1 (2016), 81. doi:10.1186/s12859-016-0930-z
  7. [7]Margaret O. Dayhoff, Robert M. Schwartz, and Bruce C. Orcutt. 1978. A model of evolutionary change in proteins. In Atlas of Protein Sequence and Structure, Margaret O. Dayhoff (Ed.). Vol. 5. National Biomedical Research Foundation, Washington, D.C., 345–352.
  8. [8]R. Deriche. 1990. Fast algorithms for low-level vision. IEEE Transactions on Pattern Analysis and Machine Intelligence 12, 1 (1990), 78–87. doi:10.1109/34.41386
  9. [9]Alexander Dobin, C. Alexe A. Davis, Felix Schlesinger, Jorg Drenkow, Chris Zaleski, Sonali Jha, Philippe Batut, Mark J. P. Chaisson, and Thomas R. Gingeras. 2013. STAR: ultrafast universal RNA-seq aligner. Bioinformatics 29 1 (2013), 15–21. https://api.semanticscholar.org/CorpusID:268128107DOI
  10. [10]Conal Elliott. 2019. Generalized Convolution and Efficient Language Recognition. CoRR abs/1903.10677 (2019). arXiv:1903.10677 http://arxiv.org/abs/1903.10677
  11. [11]Michael Farrar. 2007. Striped Smith–Waterman speeds database searches six times over other SIMD implementations. Bioinformatics 23, 2 (01 2007), 156–161. arXiv:https://academic.oup.com/bioinformatics/article-pdf/23/2/156/49820591/bioinformatics_23_2_156.pdf doi:10.1093/bioinformatics/btl582DOI
  12. [12]R.B. Feinberg. 1990. Vectorizing IIR filters. In International Conference on Acoustics, Speech, and Signal Processing. 1045–1048 vol.2. doi:10.1109/ICASSP.1990.116087
  13. [13]Osamu Gotoh. 1982. An improved algorithm for matching biological sequences. Journal of Molecular Biology 162, 3 (1982), 705–708. doi:10.1016/0022-2836(82)90398-9DOI
  14. [14]Todd J. Green, Grigoris Karvounarakis, and Val Tannen. 2007. Provenance semirings. In Proceedings of the Twenty-Sixth ACM SIGMOD-SIGACT-SIGART Symposium on Principles of Database Systems (Beijing, China) (PODS ’07). Association for Computing Machinery, New York, NY, USA, 31–40. doi:10.1145/1265530.1265535DOI
  15. [15]Steven Henikoff and Jorja G. Henikoff. 1992. Amino acid substitution matrices from protein blocks. Proceedings of the National Academy of Sciences 89, 22 (1992), 10915–10919. doi:10.1073/pnas.89.22.10915DOI
  16. [16]Kazutaka Katoh and Hiroyuki Toh. 2008. Recent developments in the MAFFT multiple sequence alignment program. Briefings in Bioinformatics 9, 4 (2008), 286–298. doi:10.1093/bib/bbn013DOI
  17. [17]Peter M. Kogge and Harold S. Stone. 1973. A Parallel Algorithm for the Efficient Solution of a General Class of Recurrence Equations. IEEE Trans. Comput. 22, 8 (Aug. 1973), 786–793. doi:10.1109/TC.1973.5009159DOI
  18. [18]Ben Langmead and Steven L. Salzberg. 2012. Fast gapped-read alignment with Bowtie 2. Nature methods 9 (2012), 357–359. https://api.semanticscholar.org/CorpusID:205420704DOI
  19. [19]Heng Li. 2013. Aligning sequence reads, clone sequences and assembly contigs with BWA-MEM. arXiv: Genomics (2013). https://api.semanticscholar.org/CorpusID:14669139arxiv.org/abs/1303.3997
  20. [20]Heng Li. 2018. Minimap2: pairwise alignment of nucleotide sequences. Bioinformatics 34, 18 (09 2018), 3094–3100. arXiv:https://academic.oup.com/bioinformatics/article-pdf/34/18/3094/48919122/bioinformatics_34_18_3094.pdf doi:10.1093/bioinformatics/bty191DOI
  21. [21]Santiago Marco-Sola, Juan Carlos Moure, Miquel Moreto, and Antonio Espinosa. 2021. Fast gap-affine pairwise alignment using the wavefront algorithm. Bioinformatics 37, 4 (05 2021), 456–463. arXiv:https://academic.oup.com/bioinformatics/article-pdf/37/4/456/50359789/btaa777.pdf doi:10.1093/bioinformatics/btaa777
  22. [22]Diego Nehab, André Maximo, Rodolfo S. Lima, and Hugues Hoppe. 2011. GPU-efficient recursive filtering and summed-area tables. ACM Trans. Graph. 30, 6 (Dec. 2011), 1–12. doi:10.1145/2070781.2024210DOI
  23. [23]Marcelo A. Nobrega and Len A. Pennacchio. 2004. Comparative genomics analyses as a tool for biological discovery. The Journal of Physiology 554, 1 (2004), 31–39. doi:10.1113/jphysiol.2003.050948DOI
  24. [24]A.V. Oppenheim, R.W. Schafer, and J.R. Buck. 1999. Discrete-time Signal Processing. Prentice Hall. https://books.google.com/books?id=Bv1SAAAAMAAJ
  25. [25]K.K. Parhi and D.G. Messerschmitt. 1989. Pipeline interleaving and parallelism in recursive digital filters. I. Pipelining using scattered look-ahead and decomposition. IEEE Transactions on Acoustics, Speech, and Signal Processing 37, 7 (1989), 1099–1117. doi:10.1109/29.32286
  26. [26]René Rahn, Stefan Budach, Pablo Costanza, Marcel Ehrhardt, Jonathan Hancox, and Knut Reinert. 2018. Generic accelerated sequence alignment in SeqAn using vectorization and multi-threading. Bioinformatics 34, 20 (2018), 3437–3445. doi:10.1093/bioinformatics/bty380DOI
  27. [27]J. Robelly, G. Cichon, H. Seidel, and G. Fettweis. 2004. Implementation of recursive digital filters into vector SIMD DSP architectures. In 2004 IEEE International Conference on Acoustics, Speech, and Signal Processing, Vol. 5. V–165. doi:10.1109/ICASSP.2004.1327073DOI
  28. [28]Torbjørn Rognes. 2011. Faster Smith-Waterman database searches with inter-sequence SIMD parallelisation. BMC Bioinformatics 12 (2011), 221 – 221. https://api.semanticscholar.org/CorpusID:1713355DOI
  29. [29]Guodong Rong and Tiow-Seng Tan. 2006. Jump flooding in GPU with applications to Voronoi diagram and distance transform. In Proceedings of the 2006 Symposium on Interactive 3D Graphics and Games (Redwood City, California) (I3D ’06). Association for Computing Machinery, New York, NY, USA, 109–116. doi:10.1145/1111411.1111431DOI
  30. [30]Harold S. Stone. 1973. An Efficient Parallel Algorithm for the Solution of a Tridiagonal Linear System of Equations. J. ACM 20, 1 (Jan. 1973), 27–38. doi:10.1145/321738.321741DOI
  31. [31]Shiv Sundram, Muhammad Usman Tariq, and Fredrik Kjolstad. 2024. Compiling Recurrences over Dense and Sparse Arrays. Proc. ACM Program. Lang. 8, OOPSLA1, Article 103 (April 2024), 26 pages. doi:10.1145/3649820DOI
  32. [32]Hajime Suzuki and Masahiro Kasahara. 2018. Introducing difference recurrence relations for faster semi-global alignment of long sequences. BMC Bioinformatics 19 (2018). https://api.semanticscholar.org/CorpusID:3395471DOI
  33. [33]Muhammad Usman Tariq, Shiv Sundram, and Fredrik Kjolstad. 2025. REPTILE: Performant Tiling of Recurrences. Proc. ACM Program. Lang. 9, OOPSLA2, Article 296 (Oct. 2025), 27 pages. doi:10.1145/3763074DOI
  34. [34]B. P. Welford. 1962. Note on a Method for Calculating Corrected Sums of Squares and Products. Technometrics 4 (1962), 419–420. https://api.semanticscholar.org/CorpusID:120126049
  35. [35]Andrzej Wozniak. 1997. Using video-oriented instructions to speed up sequence comparison. Computer applications in the biosciences : CABIOS 13 2 (1997), 145–50. https://api.semanticscholar.org/CorpusID:18620993pubmed.ncbi.nlm.nih.gov/9146961
  36. [36]Yihong Zhang, Derek Gerstmann, Andrew Adams, and Maaz Bin Safeer Ahmad. 2026. Pushing Tensor Accelerators beyond MatMul in a User-Schedulable Language. In 2026 IEEE/ACM International Symposium on Code Generation and Optimization (CGO). 242–254. doi:10.1109/CGO68049.2026.11395214DOI
  37. [37]Yihong Zhang, Bala Vinayuthan, Karim Ma, Zachary Mathod, Maaz Bin Safeer Ahmad, and Andrew Adams. 2026. VecRec: A vectorizing compiler for recurrence kernels. doi:10.5281/zenodo.23330549DOI
  38. [38]Martin Šošić and Mile Šikić. 2017. Edlib: a C/C++ library for fast, exact sequence alignment using edit distance. Bioinformatics 33, 9 (05 2017), 1394–1395. arXiv:https://academic.oup.com/bioinformatics/article-pdf/33/9/1394/49038512/bioinformatics_33_9_1394.pdf doi:10.1093/bioinformatics/btw753DOI

Paper details

vlnc:W_9jtcaeu4
Version
v1 · latest
Published
Fresh — added to the Hub within the last 30 days
Added
License
CC BY 4.0
Usage

views · 10

downloads · 1

A view counts once per browser per day, a download each time the PDF is requested.

  • Computer Science

Not recorded for this paper

View statement
Looking for related papers…
Contents