Algorithm Search⌘K

Algorithmcore

Deferred-merge embedding

Also called DME, deferred merge embedding.

Builds a zero-skew tree of least wire length for a given topology by computing merging segments bottom up, then embedding the tapping points top down.

Time
Θ(n) given_topology
Space
Θ(n) extra
Where
n = the number of clock sinks
Properties
deterministicexactrecursive
Origin
Ting Hai Chao, Yu Chin Hsu, Jan Ming Ho, Kenneth D. Boese, Andrew B. Kahng, 1992

Deferred-merge embedding (DME) is a clock routing algorithm: for a given binary topology over the clock sinks, it builds a wire tree in which every source-to-sink path has the same delay and the total wire length is the least possible. It does not fix the root of a subtree when the subtree is built. It keeps the whole set of points where the root could go, a segment called the merging segment, and picks a point from it only in a second, top-down pass. Under linear delay the result is proved optimal for the topology; under the Elmore delay it keeps zero skew but is a heuristic for wire length.

In plain words

Tsay's method joins two subtrees at one point on the wire between their roots. But a subtree often has many good roots, all with the same delay to its sinks, and the choice matters later.

DME first goes up the tree and only records where each root may go. For a single sink, that is the sink itself. For two joined subtrees, it is the set of points at the right distances from the two child sets, and on a Manhattan grid this set is always a straight piece of a diagonal line, or one point.

Then DME goes down. It puts the root anywhere on its segment, and puts each child at the point of its own segment nearest to its parent. Each wire is then as short as it can be, and all paths keep the same length.

The problem

The input is a set of nn sinks with points psp_s in the plane and a full binary tree (the topology) whose leaves are the sinks. Wires run horizontally and vertically, so distances are L1 distances dd. Under linear delay the delay of a path is its wire length. The output is a point pvp_v for each internal node and a wire length ec≥d(pc,pparent(c))e_c \ge d(p_c, p_{\text{parent}(c)}) for each non-root node cc (a longer wire is a detour), such that all root-to-sink paths have the same length and the total ∑cec\sum_c e_c is least. This is the zero-skew case of clock tree synthesis with the topology fixed.

The algorithm works in the rotated coordinates u=x+yu = x + y, v=x−yv = x - y, where the L1 distance becomes max⁡(∣Δu∣,∣Δv∣)\max(|\Delta u|, |\Delta v|). A segment of slope ±1\pm 1 in the plane is an axis-parallel segment in (u,v)(u, v). The tilted rectangular region TRR(S,e)\mathrm{TRR}(S, e), the set of points within distance ee of a region SS, is SS widened by ee on all four sides.

The algorithm

  1. DME(topology T, sinks, source q₀)
  2. // bottom-up phase: BUILD(v) sets ms(v), t(v) and the edge lengths below v
  3. BUILD(v)
  4. 1if v is a sink s then
  5. 2ms(v) ← {p_s}; t(v) ← 0; return
  6. 3BUILD(a); BUILD(b) // a, b are the children of v
  7. 4D ← d(ms(a), ms(b))
  8. 5if |t(a) − t(b)| ≤ D then e_a ← (D + t(b) − t(a)) / 2; e_b ← D − e_a
  9. 6else the slower child gets length 0 and the other gets |t(a) − t(b)|
  10. 7ms(v) ← TRR(ms(a), e_a) ∩ TRR(ms(b), e_b)
  11. 8t(v) ← t(a) + e_a
  12. // top-down phase: EMBED(v, q) places v at the point of ms(v) nearest q
  13. 9BUILD(root of T); EMBED(root of T, q₀)
  14. EMBED(v, q)
  15. 10p_v ← the point of ms(v) nearest to q
  16. 11for each child c of v do EMBED(c, p_v)

In line L6 the delays differ by more than the distance, so the faster child gets a detour wire, and ms(v)\mathrm{ms}(v) is part of the slower child's segment. In line L10 the nearest point is found by clamping each rotated coordinate of qq into the interval of ms(v)\mathrm{ms}(v); for the root, q0q_0 is the clock source, or any point if the source is free. Since the wire from the source to the root is not part of the tree, the choice of the root point does not change the total length.

Example

Take four sinks s1=(1,3)s_1 = (1, 3), s2=(0,1)s_2 = (0, 1), s3=(2,0)s_3 = (2, 0) and s4=(3,2)s_4 = (3, 2) and the topology ((s1,s2),(s3,s4))((s_1, s_2), (s_3, s_4)).

node children DD eae_a, ebe_b merging segment tt
aa s1s_1, s2s_2 3 3/23/2, 3/23/2 from (0,5/2)(0, 5/2) to (1,3/2)(1, 3/2) 3/23/2
bb s3s_3, s4s_4 3 3/23/2, 3/23/2 from (2,3/2)(2, 3/2) to (3,1/2)(3, 1/2) 3/23/2
root aa, bb 1 1/21/2, 1/21/2 the point (3/2,3/2)(3/2, 3/2) 2

Every point of the segment of aa is at distance 3/23/2 from both s1s_1 and s2s_2; for example (0,5/2)(0, 5/2) is 1+1/21 + 1/2 from s1s_1 and 0+3/20 + 3/2 from s2s_2. The two segments come closest at (1,3/2)(1, 3/2) and (2,3/2)(2, 3/2), at distance 1, so the root segment is the single point between them. Top down, the root is at (3/2,3/2)(3/2, 3/2), aa goes to (1,3/2)(1, 3/2) and bb to (2,3/2)(2, 3/2). The wire lengths are 1/2+1/21/2 + 1/2 at the root and 3/23/2 to each sink, a total of 7, and every sink is at path length 2 from the root.

A merge that fixes each subtree root when it is built does worse here. With the taps at the ends (0,5/2)(0, 5/2) and (3,1/2)(3, 1/2) of the two segments, which also give delay 3/23/2, the top merge must span distance 5, and the total is 3+3+5=113 + 3 + 5 = 11. DME saves 4 units of wire by waiting.

Correctness

The proof is in proofs/correctness.md. It shows three facts. First, each merging segment is nonempty and is a segment of slope ±1\pm 1 or a point, because in each rotated coordinate the two widened intervals meet, and they touch at a single value in a coordinate where the gap equals DD. Second, the top-down pass is valid: a point of ms(v)\mathrm{ms}(v) lies within ece_c of ms(c)\mathrm{ms}(c), so the nearest point of ms(c)\mathrm{ms}(c) can be joined with a wire of length ece_c, and by induction all paths below vv have length t(v)t(v). Third, by induction on the height, every zero-skew tree on the same topology with root point pp and delay τ\tau has τ≥t(v)+d(p,ms(v))\tau \ge t(v) + d(p, \mathrm{ms}(v)) and a cost minus delay of at least the value of DME. At the root these give a cost of at least the DME cost, so the DME tree has the least wire length for the topology.

Proof: Deferred-merge embedding gives a zero-skew tree of least wire length for its topology under linear delayfull

Model. The sinks are points psp_s of the plane, with the L1 distance dd. A topology GG is a full binary tree whose leaves are the sinks. A zero-skew tree (ZST) on GG gives each internal node vv a point pvp_v and each non-root node cc an edge length ec≥d(pc,pparent(c))e_c \ge d(p_c, p_{\text{parent}(c)}); a length above the distance is a detour wire. Under linear delay the delay of a path is the sum of its edge lengths, and the tree has zero skew when all root-to-sink paths have the same length. The cost of the tree is ∑cec\sum_c e_c. For a subtree TvT_v and a ZST of it, its delay is the common length from pvp_v to its sinks. (If all root-to-sink lengths are equal, then for each vv all lengths from vv to its sinks are equal, since those paths share the part above vv.)

Rotation. Map (x,y)↦(u,v)=(x+y,x−y)(x, y) \mapsto (u, v) = (x + y, x - y). Then dd becomes the L-infinity distance max⁡(∣Δu∣,∣Δv∣)\max(|\Delta u|, |\Delta v|). A box is a product of two closed intervals in (u,v)(u, v). For a box SS and a point qq, d(q,S)=max⁡(gu,gv)d(q, S) = \max(g_u, g_v), where gug_u and gvg_v are the distances from the coordinates of qq to the intervals of SS. The TRR of SS with radius e≥0e \ge 0 is the box SS widened by ee on all sides, which is {q:d(q,S)≤e}\{q : d(q, S) \le e\}.

Lemma 1 (two facts on boxes). Let S,S1,S2S, S_1, S_2 be boxes, e≥0e \ge 0 and qq a point. (a) d(q,TRR(S,e))=max⁡(0,d(q,S)−e)d(q, \mathrm{TRR}(S, e)) = \max(0, d(q, S) - e). (b) If S1∩S2≠∅S_1 \cap S_2 \ne \emptyset, then d(q,S1∩S2)=max⁡(d(q,S1),d(q,S2))d(q, S_1 \cap S_2) = \max(d(q, S_1), d(q, S_2)).

Proof. (a) Widening an interval by ee lowers the gap from a number to it from gg to max⁡(0,g−e)\max(0, g - e), and max⁡(max⁡(0,gu−e),max⁡(0,gv−e))=max⁡(0,max⁡(gu,gv)−e)\max(\max(0, g_u - e), \max(0, g_v - e)) = \max(0, \max(g_u, g_v) - e). (b) For intervals I,JI, J that meet, I∩J=[max⁡(lows),min⁡(highs)]I \cap J = [\max(\text{lows}), \min(\text{highs})], so the gap from a number to I∩JI \cap J is the larger of its gaps to II and to JJ. Take the maximum over the two coordinates. □\square

The algorithm's values. For a sink, ms={ps}\mathrm{ms} = \{p_s\}, t∗=0t^* = 0, W∗=0W^* = 0. For an internal node vv with children a,ba, b, let D=d(ms(a),ms(b))D = d(\mathrm{ms}(a), \mathrm{ms}(b)). If ∣ta∗−tb∗∣≤D|t^*_a - t^*_b| \le D, then ea∗=(D+tb∗−ta∗)/2e^*_a = (D + t^*_b - t^*_a)/2 and eb∗=D−ea∗e^*_b = D - e^*_a; if ta∗>tb∗+Dt^*_a > t^*_b + D, then ea∗=0e^*_a = 0, eb∗=ta∗−tb∗e^*_b = t^*_a - t^*_b; the last case is the mirror. Then tv∗=ta∗+ea∗=tb∗+eb∗t^*_v = t^*_a + e^*_a = t^*_b + e^*_b, Wv∗=Wa∗+Wb∗+ea∗+eb∗W^*_v = W^*_a + W^*_b + e^*_a + e^*_b and ms(v)=TRR(ms(a),ea∗)∩TRR(ms(b),eb∗)\mathrm{ms}(v) = \mathrm{TRR}(\mathrm{ms}(a), e^*_a) \cap \mathrm{TRR}(\mathrm{ms}(b), e^*_b).

Lemma 2 (the regions are nonempty segments). Every ms(v)\mathrm{ms}(v) is a nonempty box with at least one side of length 0, that is, a segment of slope ±1\pm 1 in the plane or a point (a Manhattan arc).

Proof. By induction. A sink is a point. In the first case, ea∗,eb∗≥0e^*_a, e^*_b \ge 0 and ea∗+eb∗=De^*_a + e^*_b = D. In each coordinate the gap between the intervals of ms(a)\mathrm{ms}(a) and ms(b)\mathrm{ms}(b) is at most DD, so the widened intervals meet, and the intersection is nonempty. In a coordinate where the gap is exactly DD, the widened intervals touch at one value, so that side has length 0. If D=0D = 0, then ea∗=eb∗=0e^*_a = e^*_b = 0 and the region is ms(a)∩ms(b)⊆ms(a)\mathrm{ms}(a) \cap \mathrm{ms}(b) \subseteq \mathrm{ms}(a), which has a side of length 0 by induction. In the detour case, ms(v)=ms(a)∩TRR(ms(b),eb∗)⊆ms(a)\mathrm{ms}(v) = \mathrm{ms}(a) \cap \mathrm{TRR}(\mathrm{ms}(b), e^*_b) \subseteq \mathrm{ms}(a), which has a side of length 0 by induction, and it is nonempty because eb∗>De^*_b > D, so the point of ms(a)\mathrm{ms}(a) nearest to ms(b)\mathrm{ms}(b) lies in it. □\square

Theorem 1 (the output is a ZST of cost W∗W^*). The top-down phase gives a ZST on GG with delay troot∗t^*_{\text{root}} and cost Wroot∗=∑cec∗W^*_{\text{root}} = \sum_c e^*_c.

Proof. The root gets a point of ms(root)\mathrm{ms}(\text{root}). If a node vv has a point q∈ms(v)q \in \mathrm{ms}(v), then q∈TRR(ms(c),ec∗)q \in \mathrm{TRR}(\mathrm{ms}(c), e^*_c) for each child cc, so d(q,ms(c))≤ec∗d(q, \mathrm{ms}(c)) \le e^*_c. The child gets the point of ms(c)\mathrm{ms}(c) nearest to qq (clamp each rotated coordinate into its interval), so d(pc,q)≤ec∗d(p_c, q) \le e^*_c, and the edge of length ec∗e^*_c is allowed. A sink gets its own point, since its region is that point. By induction on the height, the length from a point of ms(v)\mathrm{ms}(v) down to every sink of TvT_v is tv∗t^*_v, since ta∗+ea∗=tb∗+eb∗=tv∗t^*_a + e^*_a = t^*_b + e^*_b = t^*_v. The cost is the sum of the ec∗e^*_c, which is Wroot∗W^*_{\text{root}} by its definition. □\square

Lemma 3 (lower bounds). For every node vv and every ZST of TvT_v with root point pp, delay τ\tau and cost WW:

(i)  τ≥tv∗+d(p,ms(v)),(ii)  W−τ≥Wv∗−tv∗.\text{(i)}\ \ \tau \ge t^*_v + d(p, \mathrm{ms}(v)), \qquad \text{(ii)}\ \ W - \tau \ge W^*_v - t^*_v.

Proof. By induction on the height of vv. For a sink the only ZST is the sink itself, with p=psp = p_s, τ=0\tau = 0 and W=0W = 0, and both hold with equality. Let vv have children a,ba, b, and take a ZST of TvT_v with root pp. Its parts are ZSTs of TaT_a and TbT_b with roots pa,pbp_a, p_b, delays τa,τb\tau_a, \tau_b and costs Wa,WbW_a, W_b, and τ=τa+ea=τb+eb\tau = \tau_a + e_a = \tau_b + e_b with ea≥d(p,pa)e_a \ge d(p, p_a) and eb≥d(p,pb)e_b \ge d(p, p_b).

For (i): by induction and the triangle inequality for the distance to a set,

τ=τa+ea≥ta∗+d(pa,ms(a))+d(p,pa)≥ta∗+d(p,ms(a)),\tau = \tau_a + e_a \ge t^*_a + d(p_a, \mathrm{ms}(a)) + d(p, p_a) \ge t^*_a + d(p, \mathrm{ms}(a)),

and in the same way τ≥tb∗+d(p,ms(b))\tau \ge t^*_b + d(p, \mathrm{ms}(b)). Subtract tv∗=ta∗+ea∗=tb∗+eb∗t^*_v = t^*_a + e^*_a = t^*_b + e^*_b:

τ−tv∗≥M:=max⁡(d(p,ms(a))−ea∗, d(p,ms(b))−eb∗).\tau - t^*_v \ge M := \max\big(d(p, \mathrm{ms}(a)) - e^*_a,\ d(p, \mathrm{ms}(b)) - e^*_b\big).

First, M≥0M \ge 0. In the detour case ea∗=0e^*_a = 0, so the first term is d(p,ms(a))≥0d(p, \mathrm{ms}(a)) \ge 0, and the mirror case is the same. In the other case, if both terms were negative, then D≤d(p,ms(a))+d(p,ms(b))<ea∗+eb∗=DD \le d(p, \mathrm{ms}(a)) + d(p, \mathrm{ms}(b)) < e^*_a + e^*_b = D, which is false. So M=max⁡(0,M)M = \max(0, M), and by Lemma 1 (a) and (b) this is max⁡(d(p,TRR(ms(a),ea∗)),d(p,TRR(ms(b),eb∗)))=d(p,ms(v))\max(d(p, \mathrm{TRR}(\mathrm{ms}(a), e^*_a)), d(p, \mathrm{TRR}(\mathrm{ms}(b), e^*_b))) = d(p, \mathrm{ms}(v)), using Lemma 2 for the nonempty intersection.

For (ii): since ea=τ−τae_a = \tau - \tau_a and eb=τ−τbe_b = \tau - \tau_b,

W−τ=Wa+Wb+ea+eb−τ=(Wa−τa)+(Wb−τb)+τ.W - \tau = W_a + W_b + e_a + e_b - \tau = (W_a - \tau_a) + (W_b - \tau_b) + \tau.

The same computation for the algorithm's values gives Wv∗−tv∗=(Wa∗−ta∗)+(Wb∗−tb∗)+tv∗W^*_v - t^*_v = (W^*_a - t^*_a) + (W^*_b - t^*_b) + t^*_v. The first two terms compare by induction, and τ≥tv∗\tau \ge t^*_v by (i). □\square

Theorem 2 (least wire length). Every ZST on the topology GG costs at least Wroot∗W^*_{\text{root}}. So the tree of Theorem 1 has the least cost among all zero-skew trees on GG under linear delay.

Proof. By Lemma 3 at the root, W=(W−τ)+τ≥(Wroot∗−troot∗)+troot∗+d(p,ms(root))≥Wroot∗W = (W - \tau) + \tau \ge (W^*_{\text{root}} - t^*_{\text{root}}) + t^*_{\text{root}} + d(p, \mathrm{ms}(\text{root})) \ge W^*_{\text{root}}. □\square

The proof also shows that a root point outside ms(root)\mathrm{ms}(\text{root}) costs at least its distance to the region in extra wire, which is why the region is the set of best root points.

Theorem 3 (time). For nn sinks and a given topology the algorithm runs in O(n)O(n) time.

Proof. There are n−1n - 1 internal nodes. At each, the bottom-up phase computes one distance between boxes, two widened boxes and one intersection, and the top-down phase does one clamp, each in O(1)O(1) arithmetic operations. □\square

Complexity

With the topology given, each of the n−1n - 1 internal nodes needs one distance between two rectangles, two widenings and one intersection bottom up, and one clamp top down, each with O(1)O(1) arithmetic operations. The time is Θ(n)\Theta(n) and the extra space is Θ(n)\Theta(n) for the segments. Building the topology is a separate step; the greedy and partitioning methods used in practice take O(nlog⁡n)O(n \log n) time or more.

Properties

  • Exact zero skew under linear delay, for every topology.
  • Least total wire length among all zero-skew trees on the topology under linear delay (proved). It is not the least over all topologies; the topology is an input.
  • Each merging segment is a Manhattan arc, so it is stored as four numbers.
  • When the source is not fixed, every point of the root segment gives the same total wire length; the root segment is the set of best root points.
  • Under the Elmore delay the same two passes work with the balance formula of Tsay's method in place of line L5, and the segments are still Manhattan arcs. The tree has exact zero Elmore skew, but the wire length is not proved least, because the split of each wire then depends on its length.

Variants and improvements

  • Elmore delay. Chao et al. gave the version for the Elmore delay of the RC interconnect tree, which is the one used in practice. It keeps zero skew and in their measurements uses less wire than Tsay's merge on the same topology.
  • Topology generation. DME needs a topology. A top-down split of the sinks, as in the method of means and medians, or a bottom-up greedy matching of the nearest merging segments both work. Edahiro's clustering method builds the topology and the segments together.
  • Bounded skew. If a skew bound B>0B > 0 is allowed, the merging regions become polygons rather than segments, and the extra freedom saves wire. Cong, Kahng, Koh and Tsao gave this bounded-skew form.
  • Fixed source. When the source position is given, line L9 starts the top-down pass from it, and the root goes to the point of its segment nearest to the source.

When to use it

Use it to route a zero-skew clock tree on a placement when the topology is known or built by a separate heuristic, and wire length matters. It replaces Tsay's merge with the same skew and less wire. For a regular array of sinks an H-tree needs no computation. For the plain least-wire tree with no skew constraint, use a rectilinear Steiner tree method instead.

Pitfalls

  • The topology decides most of the wire length. DME is optimal for the topology it is given, not over all topologies, and a poor topology forces long wires or detours.
  • In line L6 the detour wire has length ∣t(a)−t(b)∣|t(a) - t(b)|, which is longer than the distance. The embedding must route it as a real detour; a straight wire of length DD leaves a skew.
  • The rotation halves coordinates when it is undone, so integer sinks give half-integer points. Use exact arithmetic or keep the rotated coordinates.
  • A choice of the tap that is only nearest in the plane, and not in the merging segment, breaks the bound d(pc,pv)≤ecd(p_c, p_v) \le e_c. Clamp in the rotated coordinates.
  • Zero skew under linear delay is not zero skew under the Elmore delay, since loads differ between branches. Use the Elmore form for real wires.

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.

deferred_merge_embedding.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/deferred-merge-embedding/. The authors are listed in prov/RDEGA7.jsonl.
"""Deferred-merge embedding (DME) for a zero-skew tree under linear delay.

The delay of a path is its wire length. Sinks are points (x, y). A topology
is a sink index or a pair of topologies. In the rotated coordinates
u = x + y, v = x - y the L1 distance is the L-infinity distance, so a merging
segment (a Manhattan arc) is an axis-parallel segment and a tilted
rectangular region (TRR) is an axis-parallel rectangle. A region is stored
as (ulo, uhi, vlo, vhi). All arithmetic is exact with Fractions.
"""

from fractions import Fraction


class Node:
    def __init__(self, kids=(), sink=None):
        self.kids = list(kids)  # child Nodes
        self.sink = sink
        self.ms = None  # merging region in (u, v)
        self.t = Fraction(0)  # delay from any point of ms to every sink below
        self.edge = Fraction(0)  # wire length from the parent to this node
        self.pt = None  # embedded point (x, y)


def rotate(p):
    return (Fraction(p[0]) + p[1], Fraction(p[0]) - p[1])


def unrotate(q):
    return ((q[0] + q[1]) / 2, (q[0] - q[1]) / 2)


def gap(a_lo, a_hi, b_lo, b_hi):
    return max(Fraction(0), b_lo - a_hi, a_lo - b_hi)


def dist(r1, r2):
    """L-infinity distance between two rectangles (L1 in the plane)."""
    return max(gap(r1[0], r1[1], r2[0], r2[1]), gap(r1[2], r1[3], r2[2], r2[3]))


def trr(r, e):
    return (r[0] - e, r[1] + e, r[2] - e, r[3] + e)


def meet(r1, r2):
    out = (max(r1[0], r2[0]), min(r1[1], r2[1]), max(r1[2], r2[2]), min(r1[3], r2[3]))
    assert out[0] <= out[1] and out[2] <= out[3]
    return out


def clamp(q, r):
    return (min(max(q[0], r[0]), r[1]), min(max(q[1], r[2]), r[3]))


def build(sinks, topo):
    if isinstance(topo, tuple):
        return Node([build(sinks, topo[0]), build(sinks, topo[1])])
    return Node(sink=topo)


def bottom_up(node, sinks):
    """Phase 1: merging regions and edge lengths, from the leaves up."""
    if node.sink is not None:  # @line L1
        u, v = rotate(sinks[node.sink])
        node.ms, node.t = (u, u, v, v), Fraction(0)  # @line L2
        return
    a, b = node.kids
    bottom_up(a, sinks)  # @line L3
    bottom_up(b, sinks)
    d = dist(a.ms, b.ms)  # @line L4
    if abs(a.t - b.t) <= d:  # @line L5
        a.edge = (d + b.t - a.t) / 2
        b.edge = d - a.edge
    elif a.t > b.t:  # @line L6
        a.edge, b.edge = Fraction(0), a.t - b.t
    else:
        a.edge, b.edge = b.t - a.t, Fraction(0)
    node.ms = meet(trr(a.ms, a.edge), trr(b.ms, b.edge))  # @line L7
    node.t = a.t + a.edge  # @line L8


def top_down(node, parent_q):
    """Phase 2: embed each node at the point of its region nearest the parent."""
    q = clamp(parent_q, node.ms)  # @line L10
    node.pt = unrotate(q)
    for k in node.kids:  # @line L11
        top_down(k, q)


def dme(sinks, topo, source=None):
    """Return the root of the embedded zero-skew tree. The root goes to the
    point of its region nearest to source (or its lowest corner)."""
    root = build(sinks, topo)
    bottom_up(root, sinks)
    start = rotate(source) if source is not None else (root.ms[0], root.ms[2])
    top_down(root, start)  # @line L9
    return root


def wirelength(node):
    return sum(k.edge + wirelength(k) for k in node.kids)


def l1(p, q):
    return abs(p[0] - q[0]) + abs(p[1] - q[1])
test_deferred_merge_embedding.py 116 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/deferred-merge-embedding/. The authors are listed in prov/RDEGA7.jsonl.

import random
from fractions import Fraction
from itertools import product

from deferred_merge_embedding import dme, l1, wirelength


def check_tree(root, sinks):
    """Exact zero skew, edges at least the distance, sinks at their points."""
    delays = {}

    def walk(v, acc):
        if v.sink is not None:
            assert v.pt == (sinks[v.sink][0], sinks[v.sink][1])
            delays[v.sink] = acc
        for k in v.kids:
            assert k.edge >= l1(v.pt, k.pt)
            walk(k, acc + k.edge)

    walk(root, Fraction(0))
    assert set(delays) == set(range(len(sinks)))
    assert set(delays.values()) == {root.t}


def random_topology(rng, items):
    if len(items) == 1:
        return items[0]
    k = rng.randint(1, len(items) - 1)
    return (random_topology(rng, items[:k]), random_topology(rng, items[k:]))


def internal_nodes(topo, out):
    if isinstance(topo, tuple):
        out.append(topo)
        internal_nodes(topo[0], out)
        internal_nodes(topo[1], out)
    return out


def brute_cost(sinks, topo, step):
    """Least wire of a zero-skew tree on the topology over all placements of
    the internal nodes on a grid of the bounding box. For fixed points the
    least cost sets each delay t(v) = max over children of t(c) + d(v, c),
    since the cost 2 t(root) + sum of the other internal t(v) grows with every
    t(v). Clamping the points into the box never makes a distance longer."""
    xs = [p[0] for p in sinks]
    ys = [p[1] for p in sinks]
    gx = [Fraction(min(xs)) + step * i for i in range(int((max(xs) - min(xs)) / step) + 1)]
    gy = [Fraction(min(ys)) + step * i for i in range(int((max(ys) - min(ys)) / step) + 1)]
    grid = [(x, y) for x in gx for y in gy]
    nodes = internal_nodes(topo, [])
    best = None
    for pick in product(grid, repeat=len(nodes)):
        pos = {id(t): p for t, p in zip(nodes, pick)}

        def solve(t):
            if not isinstance(t, tuple):
                return sinks[t], Fraction(0), Fraction(0)
            p = pos[id(t)]
            (pa, ta, ca), (pb, tb, cb) = solve(t[0]), solve(t[1])
            tv = max(ta + l1(p, pa), tb + l1(p, pb))
            return p, tv, ca + cb + (tv - ta) + (tv - tb)

        cost = solve(topo)[2]
        best = cost if best is None else min(best, cost)
    return best


def test_zero_skew_exact(rng=random.Random(45)):
    for _ in range(200):
        n = rng.randint(1, 9)
        sinks = [(rng.randint(0, 20), rng.randint(0, 20)) for _ in range(n)]
        order = list(range(n))
        rng.shuffle(order)
        topo = random_topology(rng, order)
        root = dme(sinks, topo)
        check_tree(root, sinks)
        src = (rng.randint(0, 20), rng.randint(0, 20))
        root2 = dme(sinks, topo, source=src)
        check_tree(root2, sinks)
        assert wirelength(root2) == wirelength(root)


def test_least_wire_against_grid(rng=random.Random(2718)):
    equal = 0
    cases = 0
    for _ in range(24):
        n = rng.choice([3, 3, 4])
        sinks = [(rng.randint(0, 2), rng.randint(0, 2)) for _ in range(n)]
        topo = ((0, 1), 2) if n == 3 else rng.choice([((0, 1), (2, 3)), (((0, 1), 2), 3)])
        root = dme(sinks, topo)
        check_tree(root, sinks)
        step = Fraction(1, 4) if n == 3 else Fraction(1, 2)
        grid_best = brute_cost(sinks, topo, step)
        assert wirelength(root) <= grid_best
        cases += 1
        equal += wirelength(root) == grid_best
    # DME is never worse than a grid placement, and on these small boxes the
    # best grid placement is as good as DME, so the two values are equal
    assert equal == cases


def test_example_of_the_page():
    sinks = [(1, 3), (0, 1), (2, 0), (3, 2)]
    root = dme(sinks, ((0, 1), (2, 3)))
    check_tree(root, sinks)
    left, right = root.kids
    h = Fraction(1, 2)
    assert (left.t, right.t, root.t) == (3 * h, 3 * h, 2)
    assert root.pt == (3 * h, 3 * h) and left.pt == (1, 3 * h) and right.pt == (2, 3 * h)
    assert (left.edge, right.edge) == (h, h)
    assert wirelength(root) == 7
    assert brute_cost(sinks, ((0, 1), (2, 3)), h) == 7

History

Before 1992, clock routers such as the method of means and medians of Jackson, Srinivasan and Kuh (1990) balanced path lengths only roughly, and Tsay's exact zero-skew method (1991) fixed each tapping point as soon as it joined two subtrees. In 1992 Chao, Hsu, Ho, Boese and Kahng showed that the choice can be deferred to a top-down pass, proved that this gives the least wire length for the topology under linear delay, and gave the Elmore form. Masato Edahiro described the same idea independently, and his 1993 paper uses it inside a greedy clustering that also builds the topology.

References

  1. Ting-Hai Chao, Yu-Chin Hsu, Jan-Ming Ho, Kenneth D. Boese and Andrew B. Kahng, Zero skew clock routing with minimum wirelength, IEEE Transactions on Circuits and Systems II 39(11), 799 to 814, 1992, the merging segments as Manhattan arcs, the bottom-up and top-down phases, and the proof of least wire length for a fixed topology under linear delay
  2. Kenneth D. Boese and Andrew B. Kahng, Zero-skew clock routing trees with minimum wirelength, Proceedings of the IEEE International ASIC Conference 1992, the conference presentation of the method
  3. Andrew B. Kahng, Jens Lienig, Igor L. Markov and Jin Hu, VLSI Physical Design, From Graph Partitioning to Timing Closure, Springer, 2011, Chapter 7, Specialized Routing, Section 7.5.1, Constructing Trees with Zero Global Skew (deferred-merge embedding)
  4. Masato Edahiro, A clustering-based optimization algorithm in zero-skew routings, Proceedings of the 30th Design Automation Conference, 612 to 616, 1993, the greedy clustering that builds the topology on which the merging segments are computed

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/RDEGA7/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