Algorithm Search⌘K

Techniqueresearch

Memory partitioning for high-level synthesis

Also called array partitioning, cyclic and block memory partitioning.

Splits an array over several memory banks by a cyclic, block or linear mapping so that the accesses of one pipelined loop iteration hit different banks.

Memory partitioning splits one array of a high-level synthesis design over several memory banks, each a separate on-chip RAM with its own ports. A loop that is pipelined to start one iteration per cycle, or unrolled by loop unrolling, may read several elements of the array in the same cycle, but one RAM has only one or two ports. The partition picks a bank function, which sends element xx to bank B(x)B(x), and an offset function, which gives its address inside the bank, so that the accesses of one iteration fall in different banks. The two classic choices are block partitioning, which cuts the array into contiguous pieces, and cyclic partitioning, which deals the elements out to the banks in turn, B(x)=x mod NB(x) = x \bmod N. For accesses A[i+c1],…,A[i+cm]A[i + c_1], \ldots, A[i + c_m], the cyclic partition is free of conflicts in every iteration exactly when no more than pp offsets ckc_k share a remainder modulo NN, for banks with pp ports.

In plain words

An on-chip memory can hand out only one or two numbers per clock tick. A fast loop may want three numbers from the same list in every tick. If the whole list sits in one memory, the loop has to wait. So we cut the list into several smaller memories that work side by side. The trick is to cut so that the numbers one tick wants always come from different memories. Cutting the list into long pieces works badly when the loop reads neighbours, since neighbours sit in the same piece. Dealing the list out like cards, one item to each memory in turn, puts neighbours in different memories, and the loop never waits.

When it applies

  • The array lives on chip, in block RAMs or SRAM macros with one or two ports each.
  • A loop is pipelined with a target initiation interval, often 1, or unrolled, and its schedule needs more accesses to the array per cycle than the ports allow. The modulo scheduler then reports a resource bound on the initiation interval set by the memory ports.
  • The indices are affine functions of the loop variables, such as i+1i + 1 or 2i+j2i + j, so the bank of each access can be computed at compile time or by a small adder and a modulo. Such indices are what the polyhedral model describes.

The template

Take a one-dimensional array AA of size SS, banks with pp ports each, and a loop body with the references A[i+c1],…,A[i+cm]A[i + c_1], \ldots, A[i + c_m] in one iteration, with integer offsets ckc_k, after unrolling.

  1. CYCLIC-PARTITION(S, p, c_1, ..., c_m)
  2. 1N ← ⌈m / p⌉
  3. 2while some remainder ρ modulo N has more than p of the offsets c_k with c_k ≡ ρ (mod N) do N ← N + 1
  4. 3for each element x: B(x) ← x mod N and F(x) ← ⌊x / N⌋
  5. 4make N memories of ⌈S / N⌉ words each, and store A[x] at address F(x) of bank B(x)
  6. 5replace each reference A[i + c_k] by bank (i + c_k) mod N at address ⌊(i + c_k) / N⌋
  7. 6build a multiplexer from the N banks to each of the m references, selected by (i + c_k) mod N
  8. 7return N, B, F

The holes are the bank function of L3 (cyclic, block, block-cyclic or linear), the number of ports pp, and the routing of L6, which grows with N⋅mN \cdot m.

Why the test of L2 is exact. In iteration ii, reference kk goes to bank (i+ck) mod N(i + c_k) \bmod N. Two references kk and ll go to the same bank exactly when NN divides (i+ck)−(i+cl)=ck−cl(i + c_k) - (i + c_l) = c_k - c_l, which does not depend on ii. So the references that hit one bank in iteration ii are exactly those whose offsets share one remainder modulo NN, and they fit in one cycle exactly when there are at most pp of them. The same sets appear in every iteration, so the check over the remainders covers all iterations at once.

Why L2 stops. Once N>max⁡kck−min⁡kckN > \max_k c_k - \min_k c_k, all the differences ck−clc_k - c_l with ck≠clc_k \ne c_l lie strictly between −N-N and NN and are not multiples of NN, so distinct offsets have distinct remainders. Equal offsets read the same element, and a compiler merges them into one access. So the loop ends with N≤max⁡kck−min⁡kck+1N \le \max_k c_k - \min_k c_k + 1. It never ends below ⌈m/p⌉\lceil m / p \rceil, since mm accesses need at least that many banks of pp ports. Both bounds can be tight: offsets 0,1,…,m−10, 1, \ldots, m - 1 with p=1p = 1 need exactly mm banks.

Block partitioning. Here B(x)=⌊x/⌈S/N⌉⌋B(x) = \lfloor x / \lceil S / N \rceil \rfloor and F(x)=x mod ⌈S/N⌉F(x) = x \bmod \lceil S / N \rceil. Neighbouring elements share a bank, so it suits loops whose parallel accesses are far apart, such as two halves of an array processed at once.

Classic examples

Least banks for common access patterns. For the cyclic partition, the least NN found by L1 and L2:

offsets ckc_k one port two ports
0,1,20, 1, 2 3 2
−1,0,1-1, 0, 1 3 2
0,1,30, 1, 3 4 2
0,2,40, 2, 4 3 3
0,40, 4 3 1
0,1,2,30, 1, 2, 3 4 2
0,1,4,50, 1, 4, 5 6 2

For 0,2,40, 2, 4 with two ports, N=2N = 2 puts all three offsets in bank 0, so N=3N = 3 is the least, above the bound ⌈3/2⌉=2\lceil 3/2 \rceil = 2. For 0,1,4,50, 1, 4, 5 with one port, N=4N = 4 and N=5N = 5 both put two offsets together (0≡40 \equiv 4 and 0≡50 \equiv 5), so 6 banks are needed.

Block against cyclic. A 12-element array is read by the loop i=0,…,9i = 0, \ldots, 9 with the references A[i],A[i+1],A[i+2]A[i], A[i + 1], A[i + 2], and each bank has one port. With 3 blocks of 4 elements, iteration 0 reads elements 0, 1 and 2, all in bank 0, so it takes 3 cycles. Over the 10 iterations, the block partition takes 3+3+2+2+3+3+2+2+3+3=263 + 3 + 2 + 2 + 3 + 3 + 2 + 2 + 3 + 3 = 26 cycles. The cyclic partition into 3 banks reads the three elements from three different banks in every iteration, and takes 10 cycles, one per iteration.

A two-dimensional stencil. A 5-point stencil reads A[i][j]A[i][j], A[i±1][j]A[i \pm 1][j] and A[i][j±1]A[i][j \pm 1]. Cyclic partitioning along one dimension leaves two of the five references in the same bank for any number of banks, since A[i][j−1]A[i][j - 1], A[i][j]A[i][j] and A[i][j+1]A[i][j + 1] differ in jj only and the two references A[i±1][j]A[i \pm 1][j] have the same jj as A[i][j]A[i][j]. The linear bank function B(x,y)=(x+2y) mod 5B(x, y) = (x + 2y) \bmod 5 maps the five offsets (0,0),(1,0),(−1,0),(0,1),(0,−1)(0, 0), (1, 0), (-1, 0), (0, 1), (0, -1) to 0,1,4,2,30, 1, 4, 2, 3, all different, so 5 single-port banks suffice, and fewer is impossible with 5 accesses. Wang, Li and Cong (2014) search such linear functions for general patterns.

Variations

  • Block-cyclic. Deal out blocks of kk elements in turn, B(x)=⌊x/k⌋ mod NB(x) = \lfloor x / k \rfloor \bmod N. It covers loops that read groups of kk neighbours.
  • Linear partitioning for several dimensions. Use B(x)=(α⋅x) mod NB(\mathbf{x}) = (\boldsymbol\alpha \cdot \mathbf{x}) \bmod N with an integer vector α\boldsymbol\alpha. The test of L2 becomes a test on the values α⋅ck\boldsymbol\alpha \cdot \mathbf{c}_k modulo NN.
  • Scheduling with the partition. Cong, Jiang, Liu and Zou (2009, 2011) let the accesses of one iteration spread over the IIII cycles of the initiation interval, so a bank may serve p⋅IIp \cdot II of them, and choose the schedule and the partition together.
  • Padding. When the offset function needs a division by an awkward NN, pad each row or round NN up to a power of two, so that BB and FF are bit fields. This costs memory and may raise NN.
  • Duplication. A read-only array can be copied into several memories, one per reference, at the cost of memory and of keeping the copies equal when written.
  • Reshaping. Packing several elements into one wide word lets one port read them all, which helps when the accesses are neighbours.

Pitfalls

  • Non-affine or data-dependent indices. For A[f(i)]A[f(i)] with an unknown ff, no static bank function avoids conflicts, and the tool must keep a conservative schedule or add arbitration.
  • The routing cost. Each of the mm references needs a multiplexer over the NN banks, so a large NN adds wide multiplexers that may lengthen the clock period, see resource binding for the same trade-off with units.
  • Division and modulo hardware. With a general NN, the address ⌊(i+ck)/N⌋\lfloor (i + c_k) / N \rfloor needs a divider unless it is computed by counters that step with ii.
  • Size not divisible by NN. The last bank is shorter, or every bank gets ⌈S/N⌉\lceil S / N \rceil words and some stay unused.
  • Accesses across loops. A partition chosen for one loop may cause conflicts in another loop that reads the same array in a different pattern.

Exercises

  1. Find the least number of single-port banks for the cyclic partition of the offsets 0,3,6,90, 3, 6, 9, and explain why N=3N = 3 fails.
  2. Prove that for offsets 0,1,…,m−10, 1, \ldots, m - 1 and banks with pp ports, the least cyclic NN is ⌈m/p⌉\lceil m / p \rceil.
  3. For the 5-point stencil, find every β∈{0,1,2,3,4}\beta \in \{0, 1, 2, 3, 4\} for which B(x,y)=(x+βy) mod 5B(x, y) = (x + \beta y) \bmod 5 sends the five offsets to five different banks.
  4. A loop reads A[2i]A[2i] and A[2i+1]A[2i + 1] in each iteration. Compare the block partition into 2 banks with the cyclic one, and say which needs a wider multiplexer.

References

  1. Jason Cong, Wei Jiang, Bin Liu and Yi Zou, Automatic memory partitioning and scheduling for throughput and power optimization, ACM Transactions on Design Automation of Electronic Systems 16(2), 2011, whole paper (cyclic partitioning of arrays for loop pipelining, with the accesses scheduled over the initiation interval; first presented at ICCAD 2009)
  2. Yuxin Wang, Peng Li and Jason Cong, Theory and algorithm for generalized memory partitioning in high-level synthesis, Proceedings of the ACM/SIGDA International Symposium on Field-Programmable Gate Arrays, 2014, whole paper (linear bank functions for multidimensional arrays and the search for the fewest banks)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • three classic examples