Algorithm Search⌘K

Algorithmcore

Van Ginneken's buffer insertion

Also called van Ginneken algorithm, van Ginneken buffering.

Places buffers on a routing tree to maximise slack at the source by dynamic programming over non-dominated pairs of load and required time, in O(n^2) time.

Time
O(b^2 n^2) b_typesO(n^2) one_buffer_type
Space
O(n^2) extra
Where
b = the number of buffer types; n = the number of nodes of the tree, sinks and candidate positions included
Properties
deterministicexactiterative
Origin
Lukas P. P. P. van Ginneken, 1990

Van Ginneken's algorithm chooses where to put buffers on a fixed routing tree of a net so that the slack at the source is as large as possible, with the Elmore delay on an RC interconnect tree as the delay model. It is a dynamic program on the tree: each node keeps a list of the pairs (load capacitance, required time) that some buffer choice below it can give, and drops every pair that another pair beats on both counts. Lukas van Ginneken published it in 1990. For one buffer type it is exact and takes O(n2)O(n^2) time, where nn counts the sinks and the candidate positions.

In plain words

A long wire is slow, because its resistance must charge its own capacitance and everything behind it. A buffer cuts the wire in two: the gate before it sees only the small input of the buffer, and the buffer drives the rest. But each buffer adds its own delay, so the places must be chosen well.

Work from the sinks back to the source. At each point, keep a short list of options. Each option says how heavy the load is when you look down from this point, and how late the signal may arrive here so that every sink below is still on time. If one option is worse than another in both ways, throw it away. Moving up a wire, joining two branches and adding a buffer are simple rules on these lists. At the source, pick the best option.

The problem

The input is a rooted tree. The root 00 is the source, driven by a gate with resistance RdR_d and intrinsic delay DdD_d. Every other node vv has a wire to its parent with resistance Rv≥0R_v \ge 0 and capacitance Wv≥0W_v \ge 0, in the pi model, with half of WvW_v at each end. The leaves are the sinks: sink ss has load csc_s and required time qsq_s. A set of candidate nodes may take a buffer of type bb, with input capacitance CbC_b, output resistance RbR_b and intrinsic delay DbD_b. A buffer at vv drives the subtree of vv, and its input is a load at vv for the stage above. The output is a placement of buffers that maximizes the slack at the source,

slack=min⁡s (qs−as),\text{slack} = \min_{s} \,(q_s - a_s),

where the arrival time asa_s is the sum of the Elmore delays and the intrinsic delays of the stages on the path to ss, with the input of the driver at time 0.

A candidate at a node vv is a pair (c,q)(c, q): the load seen from vv into its subtree, and the latest arrival time at vv that meets all sinks below. The pair (c,q)(c, q) dominates (c′,q′)(c', q') when c≤c′c \le c' and q≥q′q \ge q', the Pareto dominance order with one quantity to make small and one to make large.

The algorithm

  1. VAN-GINNEKEN(T)
  2. 1for each node v of T in post-order do
  3. 2if v is a sink then L(v) ← [(c_v, q_v)]
  4. 3else
  5. 4L(v) ← nothing
  6. 5for each child u of v do
  7. 6M ← [(c + W_u, q − R_u (W_u / 2 + c)) for (c, q) in L(u)]; PRUNE(M)
  8. 7if L(v) is nothing then L(v) ← M else L(v) ← JOIN(L(v), M)
  9. 8if v is a candidate node then
  10. 9for each buffer type b do add (C_b, max over (c, q) in L(v) of q − D_b − R_b c) to L(v)
  11. 10PRUNE(L(v))
  12. 11return the largest q − D_d − R_d c over (c, q) in L(0)
  13. JOIN(A, B) ▷ A and B sorted by load, without dominated pairs
  14. J1 i ← 1; j ← 1; O ← empty list
  15. J2 while i ≤ |A| and j ≤ |B| do
  16. J3 append (A[i].c + B[j].c, min(A[i].q, B[j].q)) to O
  17. J4 if A[i].q < B[j].q then i ← i + 1
  18. J5 else if B[j].q < A[i].q then j ← j + 1
  19. J6 else i ← i + 1; j ← j + 1
  20. J7 PRUNE(O); return O

PRUNE sorts by load when needed and keeps a pair only if its required time is larger than that of every pair with a load no larger. After PRUNE the loads and the required times both increase strictly along the list. Line L6 is the wire rule, line L9 the buffer rule and JOIN the rule at a branch. Each pair also keeps a record of the buffers that make it, so the placement is read back from the best pair at the end.

Example

The tree has the source 00, a path 0−1−20 - 1 - 2, and two branches at node 22: one to sink 33, and one through node 44 to sink 55. The wires into 1,2,3,4,51, 2, 3, 4, 5 have resistances 2,1,1,2,22, 1, 1, 2, 2 and every wire has capacitance 22. The sinks have load 1, with q3=40q_3 = 40 and q5=60q_5 = 60. Nodes 1, 2 and 4 are candidates for one buffer type with Cb=1C_b = 1, Rb=1R_b = 1, Db=2D_b = 2. The driver has Rd=3R_d = 3 and Dd=0D_d = 0. Pairs are written (load, required time).

step list
sink 5 (1, 60)
up the wire into 5 (3, 56)
buffer at 4 (1, 51), (3, 56)
up the wire into 4 (3, 47), (5, 48)
sink 3, up the wire into 3 (3, 38)
join at 2 (6, 38)
buffer at 2 (1, 30), (6, 38)
up the wire into 2 (3, 28), (8, 31)
buffer at 1 (1, 23), (3, 28), (8, 31)
up the wire into 1 (3, 19), (5, 20)

The join at node 2 makes only one pair. The branch of sink 3 has the smaller required time, 38, so the pointer on that side leaves its list after one step, and the branch of node 4 gives its lightest pair, (3, 47), which is enough to meet 38. The last wire turns (8, 31) into (10, 13), which (5, 20) dominates, so PRUNE drops it. At the source the slacks are 19−3⋅3=1019 - 3 \cdot 3 = 10 and 20−3⋅5=520 - 3 \cdot 5 = 5. The best slack is 10, with buffers at 1, 2 and 4. Without buffers the slack is −29-29. The eight placements of buffers on the three candidates give slacks −29,4,3,−17,8,5,8,10-29, 4, 3, -17, 8, 5, 8, 10, so 10 is the optimum.

Correctness

The proof is in proofs/correctness.md. Its key fact is that the part of the tree above a node sees the subtree below only through the pair (load, required time) of that subtree: the slack of the whole net is a nonincreasing function of the load and a nondecreasing function of the required time, because every Elmore delay outside the subtree is a linear function of the load with a nonnegative coefficient. So a dominated pair can never be part of a better solution, and the three rules (wire, buffer, join) map dominating pairs to dominating pairs. The join needs care: for any two pairs of the two branches, the two pointer loop reaches a pair of indices that is no heavier and has the same or a larger minimum required time.

Proof: Van Ginneken's algorithm returns the largest slack over all buffer placements, in O(n^2) time for one buffer typefull

We use the model of the page. The tree has the source 00 and nodes 1,…,n−11, \ldots, n - 1. The wire into node vv has resistance Rv≥0R_v \ge 0 and capacitance Wv≥0W_v \ge 0, in the pi model: half of WvW_v at each end. A sink ss is a leaf with load cs≥0c_s \ge 0 and required time qsq_s. A buffer of type bb has input capacitance Cb≥0C_b \ge 0, resistance Rb≥0R_b \ge 0 and intrinsic delay DbD_b. The driver at the source has resistance Rd≥0R_d \ge 0 and intrinsic delay DdD_d. A placement PP maps some candidate nodes to buffer types. A buffer at vv drives the subtree of vv, and its input is a load at vv for the stage above. The arrival time of each node is given by the Elmore delay of its stage, and the slack of PP is min⁡s(qs−as)\min_s (q_s - a_s).

Elmore delay in a stage. Let a stage have driver resistance R0R_0 at its root dd, and let KdK_d be the whole capacitance of the stage. For a wire ee of the stage, let BeB_e be the capacitance of the stage strictly below ee. The resistor of ee sees the half We/2W_e / 2 at its lower end and BeB_e below it, so by the downstream form of the Elmore delay the delay from the driver to a node uu of the stage is

R0Kd+∑e on the path from d to uRe(We2+Be).R_0 K_d + \sum_{e \text{ on the path from } d \text{ to } u} R_e \Big( \frac{W_e}{2} + B_e \Big).

The pair of a partial solution. Fix a node vv and a placement PvP_v of the candidate nodes in the subtree TvT_v of vv. Cut the tree at vv and look into TvT_v from vv, without the wire above vv.

  • The load C(Pv)C(P_v) is the capacitance that the stage containing the point vv sees in TvT_v: CbC_b if PvP_v puts a buffer of type bb at vv, else the total capacitance of the stage below vv (wire capacitances, sink loads and buffer input capacitances, down to the first buffers).
  • The required time q(Pv)=min⁡s∈Tv(qs−δs)q(P_v) = \min_{s \in T_v} (q_s - \delta_s), where δs\delta_s is the arrival time at ss minus the arrival time at vv.

Lemma 1 (δs\delta_s depends only on PvP_v). For each sink ss of TvT_v, δs\delta_s is a function of PvP_v and of the data inside TvT_v.

Proof. If PvP_v has no buffer at vv, vv lies inside a stage with root dd above it. By the formula above, the arrival time at ss (if ss is in the same stage) minus that at vv is ∑Re(We/2+Be)\sum R_e (W_e/2 + B_e) over the wires ee from vv to ss, and each BeB_e is a capacitance inside TvT_v. If ss lies in a later stage, the path from vv to ss passes through buffers of PvP_v in TvT_v, and each stage on the way is inside TvT_v, so its delay is also fixed by PvP_v. If PvP_v has a buffer at vv, the same holds with the stage of that buffer, plus its intrinsic delay DbD_b. □\square

Lemma 2 (the outside sees only the pair). Fix a placement QQ outside TvT_v. Then the slack of Pv∪QP_v \cup Q is min⁡(ϕQ(C(Pv)),  q(Pv)−αQ−βQC(Pv))\min(\phi_Q(C(P_v)),\; q(P_v) - \alpha_Q - \beta_Q C(P_v)), where ϕQ\phi_Q is a nonincreasing function and αQ\alpha_Q and βQ≥0\beta_Q \ge 0 are numbers, all fixed by QQ. So the slack is nondecreasing in q(Pv)q(P_v) and nonincreasing in C(Pv)C(P_v).

Proof. Let dd be the root of the stage that contains the wire above vv (or the point vv). The subtree TvT_v changes the outside only through the capacitance it adds at vv, which is C(Pv)C(P_v). In the Elmore formula of that stage, the term C(Pv)C(P_v) appears in KdK_d and in BeB_e for each wire ee on the path from dd to vv. So the arrival time of each stage node uu outside TvT_v is au0+βuC(Pv)a_u^0 + \beta_u C(P_v), where βu≥0\beta_u \ge 0 is R0R_0 plus the resistance of the common part of the paths from dd to uu and to vv. Stages that are not below dd do not depend on C(Pv)C(P_v), and a stage that hangs below a node uu of the stage of dd shifts by βuC(Pv)\beta_u C(P_v) as a whole. So every outside sink ss has slack qs−as0−βsC(Pv)q_s - a_s^0 - \beta_s C(P_v) with βs≥0\beta_s \ge 0, and its minimum is a function ϕ(C(Pv))\phi(C(P_v)) that is nonincreasing. The arrival time at vv is αQ+βQC(Pv)\alpha_Q + \beta_Q C(P_v) with βQ=βv\beta_Q = \beta_v, and by Lemma 1 the sinks of TvT_v give the term q(Pv)−αQ−βQC(Pv)q(P_v) - \alpha_Q - \beta_Q C(P_v). □\square

Definition. A pair (c,q)(c, q) dominates (c′,q′)(c', q') when c≤c′c \le c' and q≥q′q \ge q'. By Lemma 2, if the pair of PvP_v dominates the pair of Pv′P'_v, then for every outside QQ the slack of Pv∪QP_v \cup Q is at least that of Pv′∪QP'_v \cup Q.

Lemma 3 (the three steps). Let PvP_v have pair (c,q)(c, q).

  1. Wire. Seen from the top end uu of the wire into vv, the branch has load c+Wvc + W_v and required time q−Rv(Wv/2+c)q - R_v (W_v / 2 + c).
  2. Buffer. If PvP_v has no buffer at vv, adding a buffer of type bb at vv gives the pair (Cb,q−Db−Rbc)(C_b, q - D_b - R_b c).
  3. Join. If the branches of two children of vv, seen from vv, have pairs (c1,q1)(c_1, q_1) and (c2,q2)(c_2, q_2), the union has the pair (c1+c2,min⁡(q1,q2))(c_1 + c_2, \min(q_1, q_2)).

Proof. 1: the stage that contains the wire sees the wire capacitance and all of cc below the top end. The arrival time at vv minus that at uu is Rv(Wv/2+B)R_v (W_v / 2 + B) with B=cB = c, the capacitance strictly below the wire. So every δs\delta_s grows by Rv(Wv/2+c)R_v (W_v/2 + c). 2: the buffer input is a load CbC_b at vv. The buffer adds Db+RbcD_b + R_b c between its input and the point vv of its own stage, since its stage has capacitance cc. 3: loads in parallel add, and qq is a minimum over the sinks of both branches. □\square

Lemma 4 (pruning). The procedure PRUNE, on a list sorted by load, returns a list with strictly increasing loads and strictly increasing required times, in which every input pair is dominated by an output pair.

Proof. An input pair is skipped only when its qq is at most that of the last kept pair, whose load is no larger, so it is dominated. A kept pair with the same load as the last kept one replaces it and has a larger qq, so it dominates it. By induction the kept list has strictly increasing loads, and each kept pair has a larger qq than the one before it. □\square

Lemma 5 (the join keeps a dominating pair). Let AA and BB be lists with strictly increasing loads and required times. For every ii and jj, the two pointer join outputs a pair that dominates (Ai.c+Bj.c,min⁡(Ai.q,Bj.q))(A_i.c + B_j.c, \min(A_i.q, B_j.q)).

Proof. Let m=min⁡(Ai.q,Bj.q)m = \min(A_i.q, B_j.q). Let i0i_0 be the first index with Ai0.q≥mA_{i_0}.q \ge m and j0j_0 the first with Bj0.q≥mB_{j_0}.q \ge m. Then i0≤ii_0 \le i and j0≤jj_0 \le j, so Ai0.c+Bj0.c≤Ai.c+Bj.cA_{i_0}.c + B_{j_0}.c \le A_i.c + B_j.c. We show that the pointers (x,y)(x, y) reach a pair with x≤i0x \le i_0, y≤j0y \le j_0 and min⁡(Ax.q,By.q)≥m\min(A_x.q, B_y.q) \ge m. At the start (x,y)=(0,0)(x, y) = (0, 0). Suppose x≤i0x \le i_0, y≤j0y \le j_0 and min⁡(Ax.q,By.q)<m\min(A_x.q, B_y.q) < m. If Ax.q<By.qA_x.q < B_y.q, then Ax.q<mA_x.q < m, so x<i0x < i_0, and the step moves to (x+1,y)(x + 1, y), which stays in the bounds. The case By.q<Ax.qB_y.q < A_x.q is the mirror. If Ax.q=By.q<mA_x.q = B_y.q < m, then x<i0x < i_0 and y<j0y < j_0, and the step moves both. Each step increases x+yx + y by at least 1 and stays inside the lists, so after at most i0+j0i_0 + j_0 steps the pointers hold a pair whose minimum is at least mm and whose load is at most Ai0.c+Bj0.cA_{i_0}.c + B_{j_0}.c. That pair is output, and PRUNE keeps a pair that dominates it by Lemma 4. □\square

Theorem 1 (correctness). For every node vv, every pair in the list L(v)L(v) is the pair of some placement of TvT_v, stored in its record, and for every placement PvP_v of TvT_v some pair in L(v)L(v) dominates the pair of PvP_v. So the value returned is the largest slack over all placements, and the returned placement reaches it.

Proof. By induction from the leaves up. A sink has only the empty placement, with pair (cs,qs)(c_s, q_s). Let vv be internal and take a placement PvP_v. Its restriction to each child subtree TuT_u has a pair dominated by some pair pup_u of L(u)L(u), by induction. The wire map of Lemma 3 is monotone: if (c,q)(c, q) dominates (c′,q′)(c', q') then (c+W,q−R(W/2+c))(c + W, q - R(W/2 + c)) dominates (c′+W,q′−R(W/2+c′))(c' + W, q' - R(W/2 + c')), since R≥0R \ge 0. So after the wire, some pair of the moved list dominates the branch of PvP_v, and PRUNE keeps a dominating pair (Lemma 4). The join map is monotone in both arguments in the same sense, so by Lemma 5 the joined list holds a pair that dominates the pair of PvP_v without its buffer at vv. If PvP_v has a buffer of type bb at vv, its pair is (Cb,q′−Db−Rbc′)(C_b, q' - D_b - R_b c') for the unbuffered pair (c′,q′)(c', q'), and the candidate for bb uses the best value of q−Db−Rbcq - D_b - R_b c over the list, which is at least q′−Db−Rbc′q' - D_b - R_b c' for a pair that dominates (c′,q′)(c', q'). Each record holds the placement whose pair it is, by Lemma 3. At the source the slack is q−Dd−Rdcq - D_d - R_d c by Lemma 2 with an empty outside, which is nonincreasing in cc and increasing in qq, so the best pair of L(0)L(0) gives the best slack. □\square

Lemma 6 (list size). With one buffer type, ∣L(v)∣≤sv+pv|L(v)| \le s_v + p_v, where svs_v is the number of sinks in TvT_v and pvp_v the number of candidate nodes in TvT_v. With kk types, ∣L(v)∣≤sv+kpv|L(v)| \le s_v + k p_v.

Proof. A sink has one pair. The wire map and PRUNE do not make a list longer. The join of lists of lengths aa and bb makes at most a+b−1a + b - 1 steps, since each step moves at least one pointer and the loop stops when a pointer leaves its list, so it outputs at most a+b−1a + b - 1 pairs. A buffer step adds kk pairs. Summing over the children gives the bound. □\square

Theorem 2 (time). With one buffer type the algorithm takes O(n2)O(n^2) time and O(n2)O(n^2) space for the records in the worst case.

Proof. At node vv, the wire map, PRUNE, the buffer step (one pass for the best value and one merge) and the join each take time linear in the lengths of the lists they read, which are at most sv+pv≤ns_v + p_v \le n by Lemma 6. Each node does a constant number of these steps per child, so the total is ∑vO(n)=O(n2)\sum_v O(n) = O(n^2). Each step makes O(1)O(1) new records per pair, which bounds the record space by the same sum. □\square

Complexity

With one buffer type, the list at a node has at most as many pairs as there are sinks and candidate nodes below it. A sink gives one pair, a buffer adds one pair, a wire never adds a pair, and the join of lists of lengths k1k_1 and k2k_2 makes at most k1+k2−1k_1 + k_2 - 1 pairs, since each step of the loop moves a pointer. Every rule takes time linear in the lengths of the lists it reads, so the work at a node is O(n)O(n) and the total is O(n2)O(n^2) in the worst case. The records of the pairs take O(n2)O(n^2) space in the worst case. With bb buffer types the list can grow by bb pairs at each candidate, and the bound of Lillis, Cheng and Lin is O(b2n2)O(b^2 n^2).

Properties

  • Exact for the given tree, the given candidate positions and the Elmore delay with the pi model of wires. It does not move the wires or add new candidate positions.
  • Deterministic. The best slack does not depend on the order of the children at a node, but the placement that reaches it can, when two placements tie.
  • The pruning stays valid for other delay models in which the rest of the net sees a subtree only through its load and its required time, and in which a heavier load never helps. Lillis, Cheng and Lin use such a generalized model. The table based nonlinear delay model does not fit, because a stage delay there also depends on the input slew.
  • The value at the source is the slack of the net with the arrival time of the driver input at 0. For a net inside a circuit, the required times of the sinks come from static timing analysis.

Variants and improvements

  • Buffer library and power. Lillis, Cheng and Lin allow bb buffer types, wire sizing, and a power term as a third value in each candidate, so that the lists hold the triples that no other triple beats on all three counts.
  • Faster lists. Shi and Li keep the candidates in balanced search trees and add the effect of a wire lazily, which gives O(nlog⁡n)O(n \log n) for a 2-pin net and O(nlog⁡2n)O(n \log^2 n) for a net with many sinks, for one buffer type. Li and Shi lowered the library bound from O(b2n2)O(b^2 n^2) to O(bn2)O(b n^2).
  • Load limits. A limit on the load that one stage may drive is a simple extra rule: drop every pair whose load is above the limit before a buffer or the driver must drive it. The dominance argument still holds, since the rule removes only heavy pairs.

When to use it

Use it to buffer one net whose route is fixed, with a small buffer library and a few hundred candidate positions, for example after routing a long net or during timing closure. It is exact for the model, so it is also the reference that faster heuristics are compared with. For a clock net, where the aim is equal delays rather than the best slack, the zero-skew methods of clock tree synthesis are the right tool. For very large nets with many sinks, the O(nlog⁡2n)O(n \log^2 n) variant of Shi and Li is faster.

Pitfalls

  • After a wire, a pair can become dominated, since the wire takes more time from a pair with a heavier load. PRUNE must run after the wire rule, not only after a join.
  • The buffer rule takes the largest value of q−Db−Rbcq - D_b - R_b c over the whole list. The pair with the largest required time is not always the best, because a heavy load costs time through the buffer resistance.
  • The join loop must advance the pointer of the smaller required time. Advancing the other pointer loses pairs that can be optimal.
  • The result is optimal only for the candidate positions given. A long wire with no candidate node inside it cannot get a buffer, so split long wires into short segments first.
  • The Elmore delay is an upper bound on the 50 percent delay of an RC tree, not the delay itself. The slack is exact for the model, not for the circuit.
  • Floating point ties make the dominance test unstable. Two pairs that differ by rounding both survive, and the lists grow. Exact or carefully rounded arithmetic avoids it.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

van_ginneken_buffer_insertion.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/van-ginneken-buffer-insertion/. The authors are listed in prov/98230Y.jsonl.
"""Van Ginneken's buffer insertion on an RC interconnect tree with Elmore delay.

The tree has nodes 0 to n - 1. Node 0 is the source, driven by a gate with
resistance driver_r and intrinsic delay driver_d. Every other node v has a
parent parent[v] and a wire to it with resistance wire_r[v] and capacitance
wire_c[v] (pi model: half of the capacitance at each end). The leaves are the
sinks: sinks[v] = (load capacitance, required time). A buffer of type b is a
triple (input capacitance, output resistance, intrinsic delay) and may go at
any node in positions. A buffer at v drives the subtree of v.

All arithmetic is exact when the inputs are Fractions or integers.
"""

import heapq
from fractions import Fraction


def prune(cands):
    """Drop dominated pairs from a list sorted by load. A pair (c, q, s) is
    dominated when another pair has a load no larger and a required time no
    smaller. The result has strictly increasing load and required time."""
    out = []
    for c, q, s in cands:
        if out and q <= out[-1][1]:
            continue
        if out and c == out[-1][0]:
            out[-1] = (c, q, s)
        else:
            out.append((c, q, s))
    return out


def add_wire(cands, r, c):
    """Move every pair up through a wire of resistance r and capacitance c."""
    moved = [(load + c, q - r * (Fraction(c) / 2 + load), s) for load, q, s in cands]
    return prune(moved)


def add_buffers(cands, buffers, v):
    """Add, for each buffer type, the pair of a buffer at node v."""
    new = []
    for b, (cb, rb, db) in enumerate(buffers):
        best = None
        for load, q, s in cands:
            t = q - db - rb * load
            if best is None or t > best[0]:
                best = (t, s)
        new.append((cb, best[0], ("buf", v, b, best[1])))
    key = lambda p: (p[0], -p[1])
    return prune(list(heapq.merge(cands, sorted(new, key=key), key=key)))  # @line L10


def merge(a, b):
    """Join the lists of two branches that meet at one node (two pointers)."""
    out, i, j = [], 0, 0
    while i < len(a) and j < len(b):
        ca, qa, sa = a[i]
        cb, qb, sb = b[j]
        out.append((ca + cb, min(qa, qb), ("join", sa, sb)))
        if qa < qb:
            i += 1
        elif qb < qa:
            j += 1
        else:
            i += 1
            j += 1
    return prune(out)


def placement(s, out):
    """Collect the buffers of a solution record into the dict out."""
    stack = [s]
    while stack:
        s = stack.pop()
        if s is None:
            continue
        if s[0] == "buf":
            out[s[1]] = s[2]
            stack.append(s[3])
        else:
            stack.append(s[1])
            stack.append(s[2])
    return out


def van_ginneken(parent, wire_r, wire_c, sinks, positions, buffers, driver_r, driver_d):
    """Return (best slack at the source, {node: buffer type}) and the size of
    the largest candidate list seen."""
    n = len(parent)
    children = [[] for _ in range(n)]
    for v in range(1, n):
        children[parent[v]].append(v)
    order, stack = [], [0]
    while stack:
        v = stack.pop()
        order.append(v)
        stack.extend(children[v])
    lists = [None] * n
    largest = 0
    for v in reversed(order):  # @line L1
        if not children[v]:
            if v not in sinks:
                raise ValueError("every leaf must be a sink")
            cap, req = sinks[v]
            cur = [(cap, req, None)]  # @line L2
        else:  # @line L3
            if v in sinks:
                raise ValueError("a sink must be a leaf")
            cur = None  # @line L4
            for u in children[v]:  # @line L5
                up = add_wire(lists[u], wire_r[u], wire_c[u])  # @line L6
                lists[u] = None
                cur = up if cur is None else merge(cur, up)  # @line L7
            if v in positions and v != 0:  # @line L8
                cur = add_buffers(cur, buffers, v)  # @line L9
        largest = max(largest, len(cur))
        lists[v] = cur
    best = None
    for load, q, s in lists[0]:  # @line L11
        slack = q - driver_d - driver_r * load
        if best is None or slack > best[0]:
            best = (slack, s)
    return best[0], placement(best[1], {}), largest
test_van_ginneken_buffer_insertion.py 146 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/van-ginneken-buffer-insertion/. The authors are listed in prov/98230Y.jsonl.

import random
from fractions import Fraction
from itertools import product

from van_ginneken_buffer_insertion import merge, prune, van_ginneken


def slack_of(parent, wire_r, wire_c, sinks, place, buffers, driver_r, driver_d):
    """Slack of one buffer placement, from the Elmore sum over R_ik C_k of
    every stage, with no use of the candidate lists."""
    n = len(parent)
    children = [[] for _ in range(n)]
    for v in range(1, n):
        children[parent[v]].append(v)
    arrival_in = {0: Fraction(0)}
    worst = None
    todo = [0]
    while todo:
        d = todo.pop()
        if d == 0:
            rd, dd = driver_r, driver_d
        else:
            _, rd, dd = buffers[place[d]]
        nodes, stack = [d], [d]
        while stack:
            u = stack.pop()
            if u != d and (u in place or u in sinks):
                continue
            for w in children[u]:
                nodes.append(w)
                stack.append(w)
        cap = {}
        for u in nodes:
            c = Fraction(0) if u == d else Fraction(wire_c[u], 2)
            if u != d and u in place:
                c += buffers[place[u]][0]
            elif u in sinks:
                c += sinks[u][0]
            else:
                c += sum(Fraction(wire_c[w], 2) for w in children[u])
            cap[u] = c

        def path(u):
            p = []
            while u != d:
                p.append(u)
                u = parent[u]
            return set(p)

        paths = {u: path(u) for u in nodes}
        for i in nodes:
            if i == d or not (i in place or i in sinks):
                continue
            t = Fraction(0)
            for k in nodes:
                common = paths[i] & paths[k]
                t += (rd + sum(wire_r[e] for e in common)) * cap[k]
            at = arrival_in[d] + dd + t
            if i in place:
                arrival_in[i] = at
                todo.append(i)
            else:
                s = sinks[i][1] - at
                worst = s if worst is None else min(worst, s)
    return worst


def random_net(rng, m):
    parent = [-1] + [rng.randrange(v) for v in range(1, m)]
    # split some edges with a node of degree two, as a buffer site on the wire
    for v in range(1, m):
        if rng.random() < 0.4:
            parent.append(parent[v])
            parent[v] = len(parent) - 1
    n = len(parent)
    has_child = [False] * n
    for v in range(1, n):
        has_child[parent[v]] = True
    wire_r = [0] + [rng.randint(0, 4) for _ in range(1, n)]
    wire_c = [0] + [rng.randint(0, 4) for _ in range(1, n)]
    sinks = {v: (Fraction(rng.randint(1, 3)), Fraction(rng.randint(10, 80))) for v in range(1, n) if not has_child[v]}
    positions = [v for v in range(1, n) if has_child[v] and rng.random() < 0.8]
    return parent, wire_r, wire_c, sinks, positions


def brute(parent, wire_r, wire_c, sinks, positions, buffers, driver_r, driver_d):
    best = None
    for pick in product(range(len(buffers) + 1), repeat=len(positions)):
        place = {v: b - 1 for v, b in zip(positions, pick) if b > 0}
        s = slack_of(parent, wire_r, wire_c, sinks, place, buffers, driver_r, driver_d)
        best = s if best is None else max(best, s)
    return best


def test_against_enumeration(rng=random.Random(98230)):
    done = 0
    while done < 300:
        m = rng.randint(2, 8)
        parent, wire_r, wire_c, sinks, positions = random_net(rng, m)
        if len(positions) > 6:
            continue
        nb = rng.choice([1, 1, 2])
        buffers = [(Fraction(rng.randint(1, 2)), Fraction(rng.randint(1, 3)), Fraction(rng.randint(0, 4))) for _ in range(nb)]
        driver_r, driver_d = Fraction(rng.randint(1, 5)), Fraction(rng.randint(0, 2))
        args = (parent, wire_r, wire_c, sinks, set(positions), buffers, driver_r, driver_d)
        slack, place, largest = van_ginneken(*args)
        assert slack == brute(parent, wire_r, wire_c, sinks, positions, buffers, driver_r, driver_d)
        assert set(place) <= set(positions)
        assert slack_of(parent, wire_r, wire_c, sinks, place, buffers, driver_r, driver_d) == slack
        if nb == 1:
            assert largest <= len(sinks) + len(positions)
        done += 1


def test_no_positions_gives_plain_elmore(rng=random.Random(7)):
    for _ in range(40):
        parent, wire_r, wire_c, sinks, _ = random_net(rng, rng.randint(2, 9))
        buffers = [(Fraction(1), Fraction(1), Fraction(1))]
        slack, place, _ = van_ginneken(parent, wire_r, wire_c, sinks, set(), buffers, Fraction(2), Fraction(0))
        assert place == {}
        assert slack == slack_of(parent, wire_r, wire_c, sinks, {}, buffers, Fraction(2), Fraction(0))


def test_prune_and_merge():
    assert prune([(1, 5, None), (2, 5, None), (2, 7, None), (3, 6, None), (4, 9, None)]) == [(1, 5, None), (2, 7, None), (4, 9, None)]
    a = [(1, 3, "a1"), (2, 6, "a2")]
    b = [(1, 4, "b1"), (3, 8, "b2")]
    got = [(c, q) for c, q, _ in merge(a, b)]
    assert got == [(2, 3), (3, 4), (5, 6)]


def test_example_of_the_page():
    # source 0 - 1 - 2, and 2 branches to sink 3 and, through node 4, to sink 5
    parent = [-1, 0, 1, 2, 2, 4]
    wire_r = [0, 2, 1, 1, 2, 2]
    wire_c = [0, 2, 2, 2, 2, 2]
    sinks = {3: (Fraction(1), Fraction(40)), 5: (Fraction(1), Fraction(60))}
    buffers = [(Fraction(1), Fraction(1), Fraction(2))]
    slack, place, _ = van_ginneken(parent, wire_r, wire_c, sinks, {1, 2, 4}, buffers, Fraction(3), Fraction(0))
    assert slack == 10 and place == {1: 0, 2: 0, 4: 0}
    assert slack == brute(parent, wire_r, wire_c, sinks, [1, 2, 4], buffers, Fraction(3), Fraction(0))
    assert slack_of(parent, wire_r, wire_c, sinks, {}, buffers, Fraction(3), Fraction(0)) == -29
    assert slack == slack_of(parent, wire_r, wire_c, sinks, place, buffers, Fraction(3), Fraction(0))

History

Van Ginneken presented the algorithm at ISCAS 1990, for one buffer type, a fixed routing tree and the Elmore delay. Lillis, Cheng and Lin extended it to a buffer library, wire sizing and power at ICCAD 1995 and in the IEEE Journal of Solid-State Circuits in 1996. Shi and Li gave the O(nlog⁡n)O(n \log n) and O(nlog⁡2n)O(n \log^2 n) versions at DAC 2003 and in IEEE Transactions on Computer-Aided Design in 2005, and Li and Shi gave the O(bn2)O(b n^2) library bound in 2006. The method is the basis of the buffering step of physical synthesis in the textbook of Kahng, Lienig, Markov and Hu.

References

  1. Lukas P. P. P. van Ginneken, Buffer placement in distributed RC-tree networks for minimal Elmore delay, Proceedings of the IEEE International Symposium on Circuits and Systems (ISCAS) 1990, 865 to 868, the bottom-up lists of load and required time, the pruning of dominated pairs, and the quadratic bound for one buffer type
  2. John Lillis, Chung-Kuan Cheng and Ting-Ting Y. Lin, Optimal wire sizing and buffer insertion for low power and a generalized delay model, IEEE Journal of Solid-State Circuits 31(3), 437 to 447, 1996, the extension to a library of buffer types, wire sizing and a power term
  3. Weiping Shi and Zhuo Li, A fast algorithm for optimal buffer insertion, IEEE Transactions on Computer-Aided Design 24(6), 879 to 891, 2005, the O(n log n) algorithm for 2-pin nets and the O(n log^2 n) algorithm for multi-pin nets, with balanced search trees for the candidate lists
  4. Zhuo Li and Weiping Shi, An O(bn^2) time algorithm for optimal buffer insertion with b buffer types, IEEE Transactions on Computer-Aided Design 25(3), 484 to 489, 2006, the improvement from O(b^2 n^2) to O(b n^2) for a library of b buffer types
  5. Andrew B. Kahng, Jens Lienig, Igor L. Markov and Jin Hu, VLSI Physical Design, From Graph Partitioning to Timing Closure, Springer, 2011, Chapter 8, Timing Closure, Section 8.5.2, Buffering
  6. Charles J. Alpert, Dinesh P. Mehta and Sachin S. Sapatnekar (editors), Handbook of Algorithms for Physical Design Automation, CRC Press, 2008, Chapter 26, Buffer Insertion Basics, by Jiang Hu, Zhuo Li and Shiyan Hu

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 proofH2 full proof
  • correctness: no review record reviews/98230Y/proof-correctness.yaml
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example