Algorithmadvanced
Independent set matching
Also called cell matching detailed placement.
Picks a set of cells that share no nets and reassigns them to their positions by an optimal bipartite matching of wire length cost.
- Solves
- Detailed placement
- Time
- O(n (n D + k^2 D + k^3)) worst · average · best
- Space
- O(n + k^2) extra
- Where
- D = the largest number of pins on the nets of one cell; k = the size of each independent set; n = the number of cells
- Model
- Word RAM
- Properties
- deterministicnot exactiterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Tung Chieh Chen, Zhe Wei Jiang, Tien Chang Hsu, Hsin Chen Chen, Yao Wen Chang, 2008
Independent set matching is a move of detailed placement. It picks a set of cells of the same width, close to each other, such that no two of them are on a common net, and puts them back onto the same set of positions in the best order. Because the cells share no net, the wirelength of each cell depends only on its own position, so the change of the total HPWL is a sum of one term per cell and position. The best reassignment is then an assignment problem, solved exactly by the Hungarian algorithm in time for cells. The placement stays legal, and the HPWL never goes up. The choice of the sets is a greedy independent set in the graph of cells that share nets, so the method is a local search: it reaches a local minimum, not the best placement.
In plain words
Take a few parts of the same size that are close together, and that are not wired to each other. Lift them off the board, keeping their places empty. Now each part can go back into any of these places. Since the parts are not wired to each other, the cost of putting a part in a place does not depend on where the other parts go.
So write a table: one row per part, one column per place, and in each box the wire length of that part at that place. Choosing one box in each row and each column with the least sum is a classic problem with a fast exact method. Put the parts back that way. Then take the next group.
The problem
The input is a legal placement of a netlist in rows: cell has its left edge on a site, its row, and an integer width ; the fixed pins are pads. Every pin is at the center of its cell. Net has a weight , and
The output is a legal placement with a at most that of the input. One step takes a set of cells of the same width with no net that contains two of them, the set of their positions (slots) , where is at , and finds a permutation that minimizes when moves to . For a cell and a slot let
The algorithm
INDEPENDENT-SET-MATCHING(placement, k, window)- 1
repeat for the given number of passes - 2
for each cell c₀ in the order of the rows - 3
S ← [c₀]; for each cell c of width W_{c₀} in the window of c₀, nearest first: if no net has both c and a cell of S, append c to S; stop at k cells - 4
if |S| ≥ 2 - 5
s_a ← the position of S[a] for each a - 6
cost[a][b] ← cost(S[a], s_b) for all a, b - 7
σ ← the minimum cost assignment of cost, by the Hungarian algorithm - 8
move each S[a] to s_{σ(a)}
Line L3 is a greedy independent set in the graph whose vertices are the cells of width in the window and whose edges join two cells on a common net. The equal width makes any cell fit into any slot of the set. Line L6 computes costs; each one needs the nets of one cell. Line L7 is an exact solve of the assignment problem. Line L8 moves the cells. The identity is one of the permutations, so the step never makes larger.
Example
Three cells in a row. One row; cells , , of width 1 at , so their centers are at , with . Three pads on the same line: at , at , at . The nets , , all have weight 1, so no two of the cells share a net. The total is . The cost table, from the implementation with Fractions:
| cell | slot | slot | slot |
|---|---|---|---|
| (pad at 5) | |||
| (pad at 0) | |||
| (pad at 3) |
The Hungarian algorithm gives , , with the cost . The 6 permutations give the totals (the identity), , , , and , each a sum of three boxes with one in each row and column, and is the least. The HPWL drops from to . A swap of two cells could not get there in one move: it is a cycle of three.
Why the cells must share no net. Cells at and at share a net of weight 2; also has a net to a pad at , and a net to a pad at . The current is , and the swap gives 23. The cost table, computed as if the other cell stays put, is : in the swapped boxes each cell lands on the slot of the other and the shared net looks short. The table says the swap costs 13 and the identity 23, the wrong way round. So the matching would make the placement worse.
Correctness
The proof shows the following.
- Separable cost. If no net contains two cells of , then for every permutation the new wirelength is , where is the wirelength of the nets with no pin in . The reason is that a net with a pin on has all its other pins fixed during the step.
- Best permutation. So the minimum cost assignment of line L7 gives the least over all permutations of the set, and does not go up.
- Legal. The cells of have the same width and only trade their positions, so the set of occupied sites is the same, and the placement stays legal.
- Termination. Each pass does steps; with a bound on the passes it stops.
The step is exact for its set; the method is not exact for detailed placement, because the sets are chosen greedily and the result is a local minimum.
Proof: For cells that share no net, the wirelength is separable, so the assignment gives the best permutation, keeps the placement legal and never increases HPWLfull
Fix one step, with the set of line L3 and the slots of line L5, where is at before the step. For a permutation of , let be the wirelength when each is at and every other cell is where it was. The identity is .
Lemma 1 (the set is independent and of one width). Every cell of has the width , and no net contains two cells of .
Proof. Line L3 takes only cells of width . It appends a cell only if no net of is a net of a cell already in . So for two cells of , the later one shares no net with the earlier one.
Lemma 2 (separable cost). Let be the nets with a pin on a cell of , and . For every permutation ,
Proof. A net has no pin on a cell of , so no pin moves, and its term is the same for every ; together these terms give . By Lemma 1, each net has a pin on exactly one cell of , and its other pins are on cells outside or on pads, which do not move. So under the HPWL of equals its HPWL with at and all other cells where they were. Summing over the nets of gives , and each net of is counted once, for its one cell of .
Theorem 1 (best permutation, no increase). After line L8, the wirelength is , the wirelength before the step.
Proof. Line L6 fills . By Lemma 2, , and does not depend on . The Hungarian algorithm returns a permutation of least (see the Hungarian algorithm for its proof), so it returns a of least . The identity is a permutation, so the least value is at most .
Theorem 2 (legal). If the placement is legal before a step, it is legal after it.
Proof. By Lemma 1 all cells of have the same width . The slot is the left edge and row of before the step, so it is the interval of sites in a row, inside the row and free of every cell outside . After the step the cells of occupy the same intervals, one cell in each, because is a bijection. The cells outside do not move. So the set of occupied intervals is the same as before, and no two cells overlap.
Theorem 3 (termination and running time). With passes the algorithm stops after steps. One step costs time, where bounds the number of pins on the nets of one cell (so also the number of its nets).
Proof. The loops of lines L1 and L2 run and times. Line L3 looks at each cell of the window once, at most cells, and keeps the nets of in a hash set, so each test costs the number of nets of the cell, at most , in expected time per net. Line L6 computes values; each needs the HPWL of the nets of one cell, pin positions, so in total. The Hungarian algorithm with potentials runs in time, and line L8 in .
Complexity
- One step. Line L3 looks at the cells of the window, at most of them, and checks the at most nets of each against a hash set of the nets of : . Line L6 computes costs; each one recomputes the HPWL of the nets of one cell, pins, so . The Hungarian algorithm costs .
- One pass. steps, so . With a spatial index the window costs its own size instead of , and a pass is close to linear in for windows of fixed size.
- Choice of . The exact solve grows as and the quality gain flattens out, so placers use small sets, around ten cells or less.
- Space. The table and the placement .
- The implementation uses exact Fractions for HPWL, so the tests compare totals exactly.
Properties
What is proved. Each step gives the best positions for its set among the slots of the set, keeps the placement legal, and never increases . Nothing bounds the result against the best detailed placement.
What is measured. Placers that use independent set matching in detailed placement, such as NTUplace3, report the HPWL after detailed placement on the ISPD 2005 and 2006 contest benchmarks; the gain of each move is measured there, not proved. The tests here only check the exact claims on small random instances: the separable sum equals the HPWL computed directly, and the Hungarian result equals the best of all 120 permutations of 5 cells.
- Moves of many cells at once. A step can carry out any permutation of cells, a cycle of length included, which a sequence of improving swaps may not reach.
- Same width. Cells of different widths cannot trade slots in this form.
- Local. The window keeps the cells close, so the wirelength model of the other nets stays valid and the step stays cheap.
Variants and improvements
- Mixed widths. Allow cells of different widths by letting a slot be a gap of the right size, or by matching inside a row segment with a check of the total width.
- Other independent sets. Choose the set by a coloring of the shared-net graph (greedy coloring) and match each color class, so that a whole region is improved in parallel.
- Other objectives. Add timing or congestion terms to ; any cost that depends only on the cell and the slot keeps the step exact.
- Other detailed placement moves. Global swap, vertical swap, local reordering and single-segment clustering (FastDP) are the moves that ISM is usually combined with; see detailed placement.
When to use it
Use independent set matching in detailed placement after legalization, when many cells of the same width are in the same area, which is common for the most used standard cells. It is exact for its set and fast for small . It cannot change the order of cells of different widths and cannot fix overlap, so it needs a legal input and other moves next to it.
Pitfalls
- Shared nets. The cost is separable only if no net has two cells of the set. Without the check, the matching uses wrong costs and can make the placement worse, as the second example shows.
- Pin offsets. With pins at offsets inside the cell, cells of the same width can still have different pin offsets, which is fine, but flipped orientations change the offsets. Compute the cost with the orientation that the slot gives.
- Large . The solve and the cost evaluations grow fast: needs 100 cost evaluations, a swap of two cells needs 4.
- Ties. Many permutations can tie, for example when a cell has no nets. Keep the identity on a tie, or the cells move without gain.
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.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/independent-set-matching/. The authors are listed in prov/CJ581B.jsonl.
"""Independent set matching for detailed placement.
Cells 0 .. n-1 sit on sites: cell i has the integer left edge x[i], the row row[i] and the
integer width W[i]; rows have height h. A pin of a net is a cell index i < n, which stands for the
center of cell i, or an index n + j, which stands for the fixed pad pads[j] = (px, py).
Net k has the pin list nets[k] and the weight weights[k]. Wirelength is HPWL, computed exactly.
"""
from fractions import Fraction
from typing import Sequence
def pin_point(p, x, row, W, pads, h):
n = len(x)
if p < n:
return Fraction(x[p]) + Fraction(W[p], 2), Fraction(row[p] * h) + Fraction(h, 2)
px, py = pads[p - n]
return Fraction(px), Fraction(py)
def net_hpwl(pins, x, row, W, pads, h=1) -> Fraction:
pts = [pin_point(p, x, row, W, pads, h) for p in pins]
xs = [a for a, _ in pts]
ys = [b for _, b in pts]
return (max(xs) - min(xs)) + (max(ys) - min(ys))
def total_hpwl(x, row, W, nets, weights, pads, h=1) -> Fraction:
return sum(Fraction(weights[k]) * net_hpwl(nets[k], x, row, W, pads, h) for k in range(len(nets)))
def nets_of_cells(n: int, nets) -> list:
out = [[] for _ in range(n)]
for k, pins in enumerate(nets):
for p in set(pins):
if p < n:
out[p].append(k)
return out
def choose_set(seed, x, row, W, cell_nets, k, wx, wy) -> list:
"""Greedy independent set: the seed, then cells of the same width in the window, nearest first,
each taken only if it shares no net with the cells taken before."""
used = set(cell_nets[seed])
chosen = [seed]
cand = [c for c in range(len(x)) if c != seed and W[c] == W[seed]
and abs(x[c] - x[seed]) <= wx and abs(row[c] - row[seed]) <= wy]
cand.sort(key=lambda c: (abs(x[c] - x[seed]) + abs(row[c] - row[seed]), c))
for c in cand:
if len(chosen) == k:
break
if used.isdisjoint(cell_nets[c]):
chosen.append(c)
used.update(cell_nets[c])
return chosen
def cost_matrix(S, x, row, W, nets, weights, pads, cell_nets, h=1) -> list:
"""cost[a][b]: the weighted HPWL of the nets of cell S[a] with S[a] at the slot of S[b]
and every other cell where it is."""
slots = [(x[c], row[c]) for c in S]
cost = []
for c in S:
line = []
old = (x[c], row[c])
for sx, sr in slots:
x[c], row[c] = sx, sr
line.append(sum(Fraction(weights[k]) * net_hpwl(nets[k], x, row, W, pads, h) for k in cell_nets[c]))
x[c], row[c] = old
cost.append(line)
return cost
def hungarian(cost) -> tuple:
"""Minimum cost perfect assignment of a square matrix by shortest augmenting paths with
potentials, O(k^3). Returns (assign, value) with assign[a] = the column of row a."""
k = len(cost)
INF = None
u = [Fraction(0)] * (k + 1)
v = [Fraction(0)] * (k + 1)
p = [0] * (k + 1) # p[j]: the row matched to column j (1-based), 0 if none
way = [0] * (k + 1)
for i in range(1, k + 1):
p[0] = i
j0 = 0
minv = [INF] * (k + 1)
done = [False] * (k + 1)
while True:
done[j0] = True
i0 = p[j0]
delta, j1 = INF, 0
for j in range(1, k + 1):
if not done[j]:
cur = Fraction(cost[i0 - 1][j - 1]) - u[i0] - v[j]
if minv[j] is None or cur < minv[j]:
minv[j] = cur
way[j] = j0
if delta is None or minv[j] < delta:
delta, j1 = minv[j], j
for j in range(k + 1):
if done[j]:
u[p[j]] += delta
v[j] -= delta
else:
minv[j] -= delta
j0 = j1
if p[j0] == 0:
break
while j0:
j1 = way[j0]
p[j0] = p[j1]
j0 = j1
assign = [0] * k
for j in range(1, k + 1):
assign[p[j] - 1] = j - 1
return assign, sum(Fraction(cost[a][assign[a]]) for a in range(k))
def ism(x, row, W, nets, weights, pads, k=5, wx=4, wy=1, passes=1, h=1):
"""Return new (x, row) after the given number of passes of independent set matching.
The input lists are not changed."""
x, row = list(x), list(row)
n = len(x)
cell_nets = nets_of_cells(n, nets)
for _ in range(passes): # @line L1
for seed in sorted(range(n), key=lambda c: (row[c], x[c], c)): # @line L2
S = choose_set(seed, x, row, W, cell_nets, k, wx, wy) # @line L3
if len(S) < 2: # @line L4
continue
slots = [(x[c], row[c]) for c in S] # @line L5
cost = cost_matrix(S, x, row, W, nets, weights, pads, cell_nets, h) # @line L6
assign, _ = hungarian(cost) # @line L7
for a, c in enumerate(S): # @line L8
x[c], row[c] = slots[assign[a]]
return x, row
test_independent_set_matching.py 145 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/independent-set-matching/. The authors are listed in prov/CJ581B.jsonl.
import random
from fractions import Fraction
from itertools import permutations
from independent_set_matching import (
choose_set,
cost_matrix,
hungarian,
ism,
nets_of_cells,
total_hpwl,
)
def brute_assignment(cost):
k = len(cost)
return min(sum(cost[a][perm[a]] for a in range(k)) for perm in permutations(range(k)))
def random_instance(rng, rows=3, sites=12, n=14, m=16, q=4):
"""Cells of width 1 or 2 packed on random distinct sites, random nets, pads on the border."""
W = [rng.choice([1, 1, 2]) for _ in range(n)]
x, row = [0] * n, [0] * n
occupied = {r: set() for r in range(rows)}
for i in range(n):
while True:
r = rng.randrange(rows)
s = rng.randrange(sites - W[i] + 1)
span = set(range(s, s + W[i]))
if span.isdisjoint(occupied[r]):
occupied[r] |= span
x[i], row[i] = s, r
break
pads = [(rng.choice([0, sites]), rng.randint(0, rows)) for _ in range(q)]
nets, weights = [], []
for _ in range(m):
size = rng.randint(2, 4)
pins = rng.sample(range(n + q), size)
nets.append(pins)
weights.append(rng.randint(1, 3))
return x, row, W, nets, weights, pads
def legal(x, row, W, sites):
seen = set()
for i in range(len(x)):
for s in range(x[i], x[i] + W[i]):
if (row[i], s) in seen or not (0 <= s < sites):
return False
seen.add((row[i], s))
return True
def test_hungarian_against_all_permutations():
rng = random.Random(7)
for _ in range(300):
k = rng.randint(1, 6)
cost = [[Fraction(rng.randint(-20, 20), rng.randint(1, 4)) for _ in range(k)] for _ in range(k)]
assign, value = hungarian(cost)
assert sorted(assign) == list(range(k))
assert value == sum(cost[a][assign[a]] for a in range(k))
assert value == brute_assignment(cost)
def test_separable_cost_equals_direct_hpwl_and_matches_brute_force_on_5_cells():
rng = random.Random(11)
checked = 0
for _ in range(400):
x, row, W, nets, weights, pads = random_instance(rng)
n = len(x)
cell_nets = nets_of_cells(n, nets)
seed = rng.randrange(n)
S = choose_set(seed, x, row, W, cell_nets, 5, 12, 3)
# the chosen cells share no net and have the same width
for a in range(len(S)):
assert W[S[a]] == W[seed]
for b in range(a + 1, len(S)):
assert set(cell_nets[S[a]]).isdisjoint(cell_nets[S[b]])
if len(S) < 5:
continue
checked += 1
slots = [(x[c], row[c]) for c in S]
cost = cost_matrix(S, x, row, W, nets, weights, pads, cell_nets)
base = total_hpwl(x, row, W, nets, weights, pads)
rest = base - sum(cost[a][a] for a in range(5))
best_direct = None
for perm in permutations(range(5)):
xx, rr = list(x), list(row)
for a, c in enumerate(S):
xx[c], rr[c] = slots[perm[a]]
direct = total_hpwl(xx, rr, W, nets, weights, pads)
assert direct == rest + sum(cost[a][perm[a]] for a in range(5))
if best_direct is None or direct < best_direct:
best_direct = direct
_, value = hungarian(cost)
assert rest + value == best_direct
assert checked >= 30
def test_ism_pass_is_legal_and_never_increases_hpwl():
rng = random.Random(3)
for _ in range(25):
x, row, W, nets, weights, pads = random_instance(rng)
before = total_hpwl(x, row, W, nets, weights, pads)
nx, nr = ism(x, row, W, nets, weights, pads, k=5, wx=6, wy=2, passes=2)
after = total_hpwl(nx, nr, W, nets, weights, pads)
assert legal(nx, nr, W, 12)
assert sorted(zip(nr, nx, W)) == sorted(zip(row, x, W)) # the same slots, permuted
assert after <= before
def test_page_example():
# one row; cells a, b, c of width 1 at x = 0, 2, 4; pads P = (5, 1/2), Q = (0, 1/2), R = (3, 1/2)
x, row, W = [0, 2, 4], [0, 0, 0], [1, 1, 1]
pads = [(5, Fraction(1, 2)), (0, Fraction(1, 2)), (3, Fraction(1, 2))]
nets = [[0, 3], [1, 4], [2, 5]]
weights = [1, 1, 1]
assert total_hpwl(x, row, W, nets, weights, pads) == Fraction(17, 2)
cost = cost_matrix([0, 1, 2], x, row, W, nets, weights, pads, nets_of_cells(3, nets))
assert cost == [[Fraction(9, 2), Fraction(5, 2), Fraction(1, 2)],
[Fraction(1, 2), Fraction(5, 2), Fraction(9, 2)],
[Fraction(5, 2), Fraction(1, 2), Fraction(3, 2)]]
assign, value = hungarian(cost)
assert assign == [2, 0, 1] and value == Fraction(3, 2)
nx, _ = ism(x, row, W, nets, weights, pads, k=3, wx=4, wy=0)
assert nx == [4, 0, 2] and total_hpwl(nx, row, W, nets, weights, pads) == Fraction(3, 2)
def test_shared_net_breaks_the_separable_cost():
# a at x = 0 and b at x = 5 share a net of weight 2; a likes the left pad, b the right pad
x, row, W = [0, 5], [0, 0], [1, 1]
pads = [(-1, Fraction(1, 2)), (7, Fraction(1, 2))]
nets = [[0, 1], [0, 2], [1, 3]]
weights = [2, 1, 1]
cell_nets = nets_of_cells(2, nets)
cost = cost_matrix([0, 1], x, row, W, nets, weights, pads, cell_nets)
assign, _ = hungarian(cost)
assert assign == [1, 0] # the matrix prefers the swap
assert cost == [[Fraction(23, 2), Fraction(13, 2)], [Fraction(13, 2), Fraction(23, 2)]]
swapped = total_hpwl([5, 0], row, W, nets, weights, pads)
kept = total_hpwl(x, row, W, nets, weights, pads)
assert (swapped, kept) == (23, 13) # but the swap is worse
History
Placing objects on slots as an assignment goes back to the quadratic assignment model of Koopmans and Beckmann (1957), in which the cost of an object depends on where the others go. The restriction to cells that share no nets makes the assignment exact for the wirelength. In academic placers of the 2000s it became a standard move of detailed placement; NTUplace3 by Chen, Jiang, Hsu, Chen and Chang (2008) is a well known placer that uses it, next to cell swapping and reordering.
References
- Tung-Chieh Chen, Zhe-Wei Jiang, Tien-Chang Hsu, Hsin-Chen Chen and Yao-Wen Chang, NTUplace3, an analytical placer for large-scale mixed-size designs with preplaced blocks and density constraints, IEEE Transactions on Computer-Aided Design of Integrated Circuits and Systems 27(7), 1228 to 1240, 2008, the detailed placement stage, with the matching of independent cells (general locator)
- Peter Spindler, Ulf Schlichtmann and Frank M. Johannes, Abacus, fast legalization of standard cell circuits with minimal movement, Proceedings of the International Symposium on Physical Design (ISPD), 47 to 53, 2008, the legal placements that detailed placement starts from (general locator)
- Andrew B. Kahng, Jens Lienig, Igor L. Markov and Jin Hu, VLSI Physical Design, From Graph Partitioning to Timing Closure, second edition, Springer, 2022, Chapter 4, Global and Detailed Placement (detailed placement by local moves; general locator)
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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag CJ581B Edit this page History Report an error Markdown