Algorithm Search⌘K

Algorithmresearch

Electrostatics-based placement

Also called ePlace.

Lu and coauthors' placement that models cells as charges, computes the density penalty as electric potential by FFT and minimizes with Nesterov's method.

Solves
Placement
Time
O(k (P + n + M^2 log M)) worst · averageO(P + n + M^2 log M) best
Space
O(P + n + M^2) extra
Where
M = the number of bins on each side of the grid; P = the number of pins; k = the number of iterations; n = the number of movable cells
Properties
deterministicnot exactiterative
Origin
Jingwei Lu, Pengwen Chen, Chin Chih Chang, Lu Sha, Dennis Jen Hsin Huang, Chin Chi Teng, Chung Kuan Cheng, 2014

Electrostatics-based placement is a global placer for the placement problem in chip design. It treats each cell as a positive electric charge equal to its area. Crowded regions then have a high electric potential, and the electric field pushes the cells out of them. The density penalty is the potential energy of the system, found by solving Poisson's equation on a grid of bins with cosine and sine transforms, computed by the fast Fourier transform. The placer minimizes a smooth wirelength plus λ\lambda times this energy by Nesterov's method with a diagonal preconditioner, and raises λ\lambda until the overflow is small. ePlace (Lu and coauthors, 2014 and 2015) introduced the method, and RePlAce extended it. It is an analytical placement method: a heuristic with a smooth objective, whose output is still to be legalized.

In plain words

Think of every part of the circuit as a small charged ball. Balls with the same charge push each other away, and the push is strongest where many balls are packed together. The wires act like rubber bands that pull connected balls together.

The placer lets both forces act, a little at a time, and makes the push stronger step by step. In the end the balls are spread fairly evenly over the board, and the bands still keep connected balls near each other. A short cleanup step then puts each ball on an exact spot.

The problem

The input is a netlist with nn movable cells of widths WiW_i and heights HiH_i, fixed pads, nets ee with weights wew_e, and PP pins, with every pin at the center of its cell. The region is the square [0,MB]×[0,MB][0, MB] \times [0, MB], cut into M×MM \times M bins of side BB. The output is a position for each cell in the region, with a small wirelength and a small overflow

τ=∑bins bmax⁡(0,ρb−1)B2∑iWiHi,\tau = \frac{\sum_{\text{bins } b} \max(0, \rho_b - 1) B^2}{\sum_i W_i H_i},

where ρb\rho_b is the cell area inside bin bb divided by B2B^2. Overlap is allowed in the output; the legalizer removes it.

Electrostatics. Let ρ\rho be the density on the grid, and t=x/Bt = x / B the coordinate in bin units. The potential ψ\psi solves

−∇2ψ=ρ−ρˉ,∂ψ∂n=0 on the border,∫ψ=0,-\nabla^2 \psi = \rho - \bar\rho, \qquad \frac{\partial \psi}{\partial \mathbf{n}} = 0 \text{ on the border}, \qquad \int \psi = 0,

where ρˉ\bar\rho is the mean density, which must be removed for a solution to exist with this border condition. The field is ξ=−∇ψ\xi = -\nabla \psi. With ωu=πu/M\omega_u = \pi u / M and the cosine coefficients auva_{uv} of ρ\rho (a 2D discrete cosine transform, with a00=ρˉa_{00} = \bar\rho),

ψ=∑(u,v)≠(0,0)auvωu2+ωv2cos⁡(ωutx)cos⁡(ωvty),ξx=1B∑(u,v)≠(0,0)auv ωuωu2+ωv2sin⁡(ωutx)cos⁡(ωvty),\psi = \sum_{(u, v) \ne (0, 0)} \frac{a_{uv}}{\omega_u^2 + \omega_v^2} \cos(\omega_u t_x) \cos(\omega_v t_y), \qquad \xi_x = \frac1B \sum_{(u, v) \ne (0, 0)} \frac{a_{uv} \, \omega_u}{\omega_u^2 + \omega_v^2} \sin(\omega_u t_x) \cos(\omega_v t_y),

and ξy\xi_y in the same way. Every term has a zero normal derivative on the border, because sin⁡(ωut)=0\sin(\omega_u t) = 0 at t=0t = 0 and t=Mt = M.

Objective. With the log-sum-exp wirelength WγW_\gamma and the energy N=12∑iqiψ(xi,yi)N = \frac12 \sum_i q_i \psi(x_i, y_i) with qi=WiHiq_i = W_i H_i, minimize f=Wγ+λNf = W_\gamma + \lambda N. ePlace takes the gradient of NN for cell ii as −qiξ(xi,yi)-q_i \xi(x_i, y_i), the force on a charge in a field, and ignores the change of the field caused by the cell itself.

The algorithm

  1. EPLACE(cells, nets, pads, M × M bins, γ, target τ*)
  2. 1put the cells near the center of the region (v ← u ← start)
  3. 2λ ← Σ|∇W_γ| / Σ q_i |ξ_i| at the start; a ← 1
  4. 3repeat
  5. 4ρ ← bin densities of the cells at v
  6. 5a_uv ← DCT of ρ; ψ, ξ ← potential and field from a_uv / (ω_u² + ω_v²)
  7. 6g_i ← (∇_i W_γ(v) − λ q_i ξ(v_i)) / (|E_i| + λ q_i) ▷ preconditioned gradient
  8. 7α ← |v − v_prev| / |g − g_prev| ▷ inverse Lipschitz estimate
  9. 8u_new ← clamp(v − α g) to the region
  10. 9a_new ← (1 + √(4a² + 1)) / 2; v ← clamp(u_new + (a − 1)/a_new · (u_new − u)); u ← u_new; a ← a_new
  11. 10λ ← 1.1 λ
  12. 11until τ(v) ≤ τ*
  13. 12return u

Line L5 is the electrostatic step. The forward transform is a DCT, and the potential and the two field components are inverse transforms of the same coefficients with cosines and sines. With the FFT each costs O(M2log⁡M)O(M^2 \log M). In line L6, ∣Ei∣|E_i| is the number of nets of cell ii; ePlace divides by this approximate diagonal of the Hessian so that cells of very different sizes move at similar speeds. Line L7 is the step of ePlace: the inverse of a local estimate of the Lipschitz constant of the gradient, from the last two points. Line L9 is Nesterov's update: uu is the main sequence and vv the look-ahead point. ePlace changes λ\lambda by a factor near 1 that depends on the change of HPWL in the last step; the implementation here uses the fixed factor 1.1.

Example

Potential of one full bin. A grid of 2×22 \times 2 bins of side 1 has all cell area in bin (0,0)(0, 0): ρ=2\rho = 2 there and 0 in the other three bins, so ρˉ=12\bar\rho = \frac12. Then ω1=π/2\omega_1 = \pi / 2, and the coefficients are a00=12a_{00} = \frac12, a10=a01=22a_{10} = a_{01} = \frac{\sqrt2}{2} and a11=1a_{11} = 1. At the bin centers:

bin ρ\rho ψ\psi ξx\xi_x ξy\xi_y
(0,0)(0, 0) 2 5π2≈0.5066\frac{5}{\pi^2} \approx 0.5066 32π≈0.4775\frac{3}{2\pi} \approx 0.4775 32π\frac{3}{2\pi}
(1,0)(1, 0) 0 −1π2≈−0.1013-\frac{1}{\pi^2} \approx -0.1013 32π\frac{3}{2\pi} 12π≈0.1592\frac{1}{2\pi} \approx 0.1592
(0,1)(0, 1) 0 −1π2-\frac{1}{\pi^2} 12π\frac{1}{2\pi} 32π\frac{3}{2\pi}
(1,1)(1, 1) 0 −3π2≈−0.3040-\frac{3}{\pi^2} \approx -0.3040 12π\frac{1}{2\pi} 12π\frac{1}{2\pi}

For example ψ(0,0)=a10ω12⋅22+a01ω12⋅22+a112ω12⋅12=2π2+2π2+1π2\psi(0, 0) = \frac{a_{10}}{\omega_1^2} \cdot \frac{\sqrt2}{2} + \frac{a_{01}}{\omega_1^2} \cdot \frac{\sqrt2}{2} + \frac{a_{11}}{2 \omega_1^2} \cdot \frac12 = \frac{2}{\pi^2} + \frac{2}{\pi^2} + \frac{1}{\pi^2}. The potential is highest in the full bin and lowest in the far corner, and the field points from the full bin toward the empty ones in every bin. The four potentials sum to 0, and the energy is N=12⋅2⋅5π2=5π2N = \frac12 \cdot 2 \cdot \frac{5}{\pi^2} = \frac{5}{\pi^2}. The tests check these values against the direct sums.

A run. On 5 random netlists of 16 unit cells in an 8×88 \times 8 region with unit bins (seed 14, all cells started within 0.1 of the center), the overflow starts at 0.75 and falls below the target 0.1 in 16 to 22 iterations. With 32 cells it starts at 0.875 and needs 25 to 95 iterations.

Correctness

The electrostatic part is exact: the coefficients auva_{uv} reproduce ρ\rho at the bin centers, the series ψ\psi solves Poisson's equation with the border condition for the cosine interpolant of ρ−ρˉ\rho - \bar\rho, and ξ\xi is −∇ψ-\nabla \psi. See the proof. The tests check the transform against the direct formula, the Poisson equation by finite differences at the bin centers, the field by finite differences of ψ\psi, and the gradient of WγW_\gamma by finite differences.

The optimization has no such guarantee. Nesterov's method reaches error O(1/k2)O(1/k^2) for a convex function with a Lipschitz gradient and a fixed step, but NN is not convex, λ\lambda changes at every step, and the step of line L7 is an estimate. So the placer is a heuristic: it stops when the overflow is small, not at a proved optimum.

Proof: The cosine coefficients reproduce the density, the cosine series solves Poisson's equation with a zero normal derivative, and the field is minus its gradientfull

Let M≥1M \ge 1, let ρ\rho be an M×MM \times M array, and write ωu=πu/M\omega_u = \pi u / M, Cu(t)=cos⁡(ωut)C_u(t) = \cos(\omega_u t) and Su(t)=sin⁡(ωut)S_u(t) = \sin(\omega_u t). Bin xx has its center at t=x+12t = x + \frac12. Line L5 computes

auv=cucv∑x,yρxy Cu(x+12) Cv(y+12),c0=1M,  cu=2M  (u≥1).a_{uv} = c_u c_v \sum_{x, y} \rho_{xy} \, C_u(x + \tfrac12) \, C_v(y + \tfrac12), \qquad c_0 = \tfrac1M, \; c_u = \tfrac2M \; (u \ge 1).

Lemma 1 (orthogonality at the bin centers). For 0≤u,u′≤M−10 \le u, u' \le M - 1,

∑x=0M−1Cu(x+12) Cu′(x+12)={Mu=u′=0,M/2u=u′≥1,0u≠u′.\sum_{x=0}^{M-1} C_u(x + \tfrac12) \, C_{u'}(x + \tfrac12) = \begin{cases} M & u = u' = 0, \\ M / 2 & u = u' \ge 1, \\ 0 & u \ne u'. \end{cases}

Proof. Write θk=πk/(2M)\theta_k = \pi k / (2M) and sk=∑x=0M−1cos⁡((2x+1)θk)s_k = \sum_{x=0}^{M-1} \cos\bigl((2x + 1) \theta_k\bigr). Then s0=Ms_0 = M. For 0<∣k∣<2M0 < |k| < 2M, sks_k is the real part of ∑xei(2x+1)θk=eiθk1−eiπk1−e2iθk\sum_x e^{i (2x+1) \theta_k} = e^{i \theta_k} \frac{1 - e^{i \pi k}}{1 - e^{2 i \theta_k}}, where the denominator is not 0. If kk is even, eiπk=1e^{i \pi k} = 1 and the sum is 0. If kk is odd, the sum is 2eiθk1−e2iθk=2e−iθk−eiθk=isin⁡θk\frac{2 e^{i \theta_k}}{1 - e^{2 i \theta_k}} = \frac{2}{e^{-i \theta_k} - e^{i \theta_k}} = \frac{i}{\sin \theta_k}, which has real part 0. So sk=0s_k = 0 for 0<∣k∣<2M0 < |k| < 2M. By the product formula, the sum of the lemma is 12(su+u′+su−u′)\frac12 (s_{u + u'} + s_{u - u'}). Both indices lie in (−2M,2M)(-2M, 2M). If u=u′=0u = u' = 0 both are 0, giving MM. If u=u′≥1u = u' \ge 1, the first is 2u≠02u \ne 0 and the second 0, giving M/2M / 2. If u≠u′u \ne u', both are nonzero, giving 0. □\square

Theorem 1 (the transform is exact). For every bin, ρxy=∑u,vauvCu(x+12)Cv(y+12)\rho_{xy} = \sum_{u, v} a_{uv} C_u(x + \frac12) C_v(y + \frac12), and a00a_{00} is the mean ρˉ\bar\rho of ρ\rho.

Proof. The MM vectors (Cu(x+12))x(C_u(x + \frac12))_{x} are pairwise orthogonal and nonzero by Lemma 1, so they are a basis of RM\mathbb{R}^M, and the products of two of them are a basis of the M×MM \times M arrays. Write ρ=∑buvCuCv\rho = \sum b_{uv} C_u C_v in this basis. Taking the inner product with CuCvC_u C_v and using Lemma 1 twice gives ∑x,yρxyCuCv=buv⋅1cu⋅1cv\sum_{x, y} \rho_{xy} C_u C_v = b_{uv} \cdot \frac{1}{c_u} \cdot \frac{1}{c_v}, so buv=auvb_{uv} = a_{uv}. For u=v=0u = v = 0, a00=1M2∑x,yρxy=ρˉa_{00} = \frac{1}{M^2} \sum_{x, y} \rho_{xy} = \bar\rho. ■\blacksquare

Theorem 2 (Poisson's equation and the field). Let ρ~(tx,ty)=∑u,vauvCu(tx)Cv(ty)\tilde\rho(t_x, t_y) = \sum_{u, v} a_{uv} C_u(t_x) C_v(t_y), the cosine interpolant of ρ\rho; by Theorem 1 it equals ρ\rho at the bin centers. The function ψ=∑(u,v)≠(0,0)auvωu2+ωv2CuCv\psi = \sum_{(u, v) \ne (0, 0)} \frac{a_{uv}}{\omega_u^2 + \omega_v^2} C_u C_v satisfies −∇2ψ=ρ~−ρˉ-\nabla^2 \psi = \tilde\rho - \bar\rho on [0,M]2[0, M]^2, its normal derivative is 0 on the border, and its integral over the square is 0. The field of line L5 is ξ=−∇ψ\xi = -\nabla \psi in length units.

Proof. For (u,v)≠(0,0)(u, v) \ne (0, 0), ωu2+ωv2>0\omega_u^2 + \omega_v^2 > 0, so every term is defined. ∇2(Cu(tx)Cv(ty))=−(ωu2+ωv2)CuCv\nabla^2 (C_u(t_x) C_v(t_y)) = -(\omega_u^2 + \omega_v^2) C_u C_v, so −∇2ψ=∑(u,v)≠(0,0)auvCuCv=ρ~−a00=ρ~−ρˉ-\nabla^2 \psi = \sum_{(u, v) \ne (0, 0)} a_{uv} C_u C_v = \tilde\rho - a_{00} = \tilde\rho - \bar\rho by Theorem 1. The derivative of Cu(tx)C_u(t_x) is −ωuSu(tx)-\omega_u S_u(t_x), which is 0 at tx=0t_x = 0 and at tx=Mt_x = M, because ωuM=πu\omega_u M = \pi u. So ∂ψ/∂tx=0\partial \psi / \partial t_x = 0 on the vertical sides, and in the same way ∂ψ/∂ty=0\partial \psi / \partial t_y = 0 on the horizontal sides. Each term with (u,v)≠(0,0)(u, v) \ne (0, 0) has one factor CuC_u with u≥1u \ge 1 or CvC_v with v≥1v \ge 1, whose integral over [0,M][0, M] is sin⁡(πu)/ωu=0\sin(\pi u) / \omega_u = 0, so ∫ψ=0\int \psi = 0. Finally −∂ψ/∂tx=∑auvωuωu2+ωv2SuCv-\partial \psi / \partial t_x = \sum \frac{a_{uv} \omega_u}{\omega_u^2 + \omega_v^2} S_u C_v, and tx=x/Bt_x = x / B gives the factor 1B\frac1B in ξx=−∂ψ/∂x\xi_x = -\partial \psi / \partial x; the same holds for ξy\xi_y. ■\blacksquare

A uniform ρ\rho has auv=0a_{uv} = 0 for (u,v)≠(0,0)(u, v) \ne (0, 0) by Theorem 1 and Lemma 1, so ψ=0\psi = 0 and ξ=0\xi = 0: a spread placement feels no density force.

Theorem 3 (termination and running time). The loop of lines L3 to L11 runs at most the given number kk of times. One iteration costs O(P+n+M2log⁡M)O(P + n + M^2 \log M) time with FFT-based transforms, and O(P+n+M3)O(P + n + M^3) with the separable direct sums of the implementation, if no cell overlaps more than a constant number of bins.

Proof. The implementation stops at the iteration limit even if τ\tau stays above τ∗\tau^*. The value and gradient of WγW_\gamma visit each pin a constant number of times, O(P)O(P). Line L4 clears M2M^2 bins and adds each cell to O(1)O(1) bins. A separable direct transform does MM sums of length MM for each of M2M^2 outputs, twice, so O(M3)O(M^3); a DCT or DST of length MM by the FFT costs O(Mlog⁡M)O(M \log M), and 2M2M of them cost O(M2log⁡M)O(M^2 \log M). Lines L6 to L10 do O(1)O(1) work per cell, and τ\tau costs O(M2)O(M^2). ■\blacksquare

Complexity

  • Wirelength. The value and the gradient of WγW_\gamma cost O(P)O(P).
  • Density. With no cell larger than a few bins, line L4 costs O(n+M2)O(n + M^2).
  • Transforms. One DCT and three inverse transforms by the FFT, O(M2log⁡M)O(M^2 \log M). The implementation here uses separable direct sums, O(M3)O(M^3), which is simpler and fast enough for small grids.
  • Field at the cells and the update. O(n)O(n).

So kk iterations cost O(k(P+n+M2log⁡M))O(k (P + n + M^2 \log M)) time and O(P+n+M2)O(P + n + M^2) space. With about one bin per cell, M2≈nM^2 \approx n, and the transforms cost O(nlog⁡n)O(n \log n) per iteration.

Properties

What is proved.

  • The coefficients auva_{uv} give back ρ\rho exactly at the bin centers, and a00a_{00} is the mean density.
  • The series ψ\psi solves −∇2ψ=ρ−ρˉ-\nabla^2 \psi = \rho - \bar\rho for the cosine interpolant of ρ\rho, with a zero normal derivative on the border, and ξ=−∇ψ\xi = -\nabla \psi. A uniform density gives ψ=0\psi = 0 and no field.
  • The method stops after the iteration limit, and each iteration costs the time above.

What is measured. The numbers in the example come from the implementation on toy inputs, not from benchmarks. ePlace was measured on the ISPD 2005 and 2006 benchmarks against the placers of its time, and RePlAce on the same suites and on routability benchmarks; both report smaller HPWL than the earlier analytical placers at the same target density. See those papers for the numbers. None of those results has a proof.

Variants and improvements

  • Weighted average wirelength. ePlace uses the weighted average model instead of LSE (see the log-sum-exp wirelength page). It is closer to HPWL for the same γ\gamma.
  • Smoothing γ\gamma with the overflow. ePlace ties γ\gamma to the overflow, large at the start and small at the end, so the wirelength gets sharper as the cells spread.
  • Mixed size. ePlace-MS places macros and standard cells together, with the macros as large charges, and then fixes the macros before the cells are finished.
  • RePlAce. Adds a local density per bin, so the penalty adapts to the region, and routability-driven inflation of cells in congested regions.
  • GPU placers. DREAMPlace runs the same model as a deep learning toolkit on a GPU, with the transforms and gradients as batched operations.
  • Other density penalties. Earlier analytical placers, such as NTUplace3, use a bell-shaped smoothing of the bin density instead of a potential. Force-directed placement in Kraftwerk also uses a potential from Poisson's equation, but with a quadratic wirelength and a linear solve.

When to use it

Use an electrostatics-based placer for large standard cell and mixed-size designs when you want the best wirelength from global placement and can afford a few hundred gradient iterations. The density penalty is global, so it spreads cells smoothly over the whole region, and the cost per iteration is near linear. For a quick placement, a quadratic placer such as SimPL is simpler and often faster. The output must still go through legalization and detailed placement.

Pitfalls

  • Forgetting to remove the mean. With zero normal derivative on the border, Poisson's equation has a solution only if the source has zero mean. Drop the term (0,0)(0, 0).
  • The wrong transform. The border condition calls for a cosine series. A plain FFT of ρ\rho assumes a periodic region, where a cell at the right edge pushes cells at the left edge.
  • A grid too coarse. If a bin is much larger than a cell, the density inside a bin is flat and cells in the same bin feel no force from each other.
  • The step without a bound. The estimate of line L7 can be very large when the gradient barely changes; ePlace backtracks if the estimate is not consistent. The implementation here caps α\alpha at one bin.
  • Exponent overflow. Use the shift by the maximum in WγW_\gamma, as on the log-sum-exp page.

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.

electrostatics_based_placement.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/electrostatics-based-placement/. The authors are listed in prov/KESX1E.jsonl.

"""Electrostatics-based global placement in the style of ePlace, on a small grid.

Pins 0 .. n-1 are the movable cells and pins n, n+1, ... are the fixed pads.
A cell position is its center, and every pin sits at the center of its cell.
The region is [0, M B] x [0, M B], cut into M x M bins of side B.
The cosine transforms are direct separable sums in O(M^3) time; ePlace computes
the same transforms by the FFT in O(M^2 log M) time.
The wirelength model is log-sum-exp (ePlace itself uses the weighted average model).
"""

import math


# ---------- wirelength ----------

def lse_wirelength(x, y, nets, weights, pads, gamma):
    """Return (W, gx, gy): the total LSE wirelength and its gradient over the cells."""
    n = len(x)
    gx, gy = [0.0] * n, [0.0] * n
    total = 0.0
    for e, w in zip(nets, weights):
        for coord, grad, k in ((x, gx, 0), (y, gy, 1)):
            vals = [coord[p] if p < n else pads[p - n][k] for p in e]
            hi, lo = max(vals), min(vals)
            ea = [math.exp((v - hi) / gamma) for v in vals]
            eb = [math.exp((lo - v) / gamma) for v in vals]
            sa, sb = sum(ea), sum(eb)
            total += w * (hi + gamma * math.log(sa) - lo + gamma * math.log(sb))
            for p, a, b in zip(e, ea, eb):
                if p < n:
                    grad[p] += w * (a / sa - b / sb)
    return total, gx, gy


def hpwl(x, y, nets, weights, pads):
    n = len(x)
    total = 0.0
    for e, w in zip(nets, weights):
        xs = [x[p] if p < n else pads[p - n][0] for p in e]
        ys = [y[p] if p < n else pads[p - n][1] for p in e]
        total += w * (max(xs) - min(xs) + max(ys) - min(ys))
    return total


# ---------- density and the Poisson equation ----------

def bin_density(x, y, W, H, M, B):
    """rho[u][v] = cell area inside bin (u, v), divided by the bin area B^2."""
    rho = [[0.0] * M for _ in range(M)]
    for i in range(len(x)):
        x0, x1 = x[i] - W[i] / 2, x[i] + W[i] / 2
        y0, y1 = y[i] - H[i] / 2, y[i] + H[i] / 2
        for u in range(max(0, int(x0 // B)), min(M, int(x1 // B) + 1)):
            ox = min(x1, (u + 1) * B) - max(x0, u * B)
            if ox <= 0:
                continue
            for v in range(max(0, int(y0 // B)), min(M, int(y1 // B) + 1)):
                oy = min(y1, (v + 1) * B) - max(y0, v * B)
                if oy > 0:
                    rho[u][v] += ox * oy / (B * B)
    return rho


def _table(M, f):
    """T[u][x] = f(pi u (x + 1/2) / M), the basis at the bin centers."""
    return [[f(math.pi * u * (x + 0.5) / M) for x in range(M)] for u in range(M)]


def dct2(rho):
    """Coefficients a[u][v] with rho[x][y] = sum_uv a[u][v] cos(w_u (x + 1/2)) cos(w_v (y + 1/2)),
    w_u = pi u / M. Separable direct sums."""
    M = len(rho)
    C = _table(M, math.cos)
    c = [1.0 / M] + [2.0 / M] * (M - 1)
    t = [[c[v] * sum(rho[x][y] * C[v][y] for y in range(M)) for v in range(M)] for x in range(M)]
    return [[c[u] * sum(t[x][v] * C[u][x] for x in range(M)) for v in range(M)] for u in range(M)]


def _synth(A, fu, fv):
    """S[x][y] = sum_uv A[u][v] fu[u][x] fv[v][y]."""
    M = len(A)
    t = [[sum(A[u][v] * fv[v][y] for v in range(M)) for y in range(M)] for u in range(M)]
    return [[sum(t[u][y] * fu[u][x] for u in range(M)) for y in range(M)] for x in range(M)]


def potential_and_field(rho, B):
    """Solve -lap psi = rho - mean(rho) with zero normal derivative on the border, by the cosine series.
    Return (a, psi, ex, ey) at the bin centers; the field is xi = -grad psi, in length units."""
    M = len(rho)
    a = dct2(rho)  # @line L5
    w = [math.pi * u / M for u in range(M)]
    Ap = [[0.0 if u == v == 0 else a[u][v] / (w[u] ** 2 + w[v] ** 2) for v in range(M)] for u in range(M)]
    Ax = [[Ap[u][v] * w[u] for v in range(M)] for u in range(M)]
    Ay = [[Ap[u][v] * w[v] for v in range(M)] for u in range(M)]
    C, S = _table(M, math.cos), _table(M, math.sin)
    psi = _synth(Ap, C, C)
    ex = [[val / B for val in row] for row in _synth(Ax, S, C)]
    ey = [[val / B for val in row] for row in _synth(Ay, C, S)]
    return a, psi, ex, ey


def psi_at(a, tx, ty):
    """The potential at a point (tx, ty) in bin units, from the coefficients."""
    M = len(a)
    total = 0.0
    for u in range(M):
        for v in range(M):
            if u == v == 0:
                continue
            wu, wv = math.pi * u / M, math.pi * v / M
            total += a[u][v] / (wu * wu + wv * wv) * math.cos(wu * tx) * math.cos(wv * ty)
    return total


def overflow(rho, area_total, B, target=1.0):
    """tau = sum over bins of the area above the target density, divided by the total cell area."""
    return sum(max(0.0, r - target) for row in rho for r in row) * B * B / area_total


# ---------- the placer ----------

def eplace(n, nets, pads, W, H, M, B, gamma=None, tau_stop=0.1, max_iter=300, start=None, history=None):
    """Return the cell centers after Nesterov's method on W_gamma + lambda N, as in ePlace."""
    X = M * B
    weights = [1.0] * len(nets)
    q = [W[i] * H[i] for i in range(n)]
    area = sum(q)
    deg = [0] * n
    for e in nets:
        for p in e:
            if p < n:
                deg[p] += 1
    if gamma is None:
        gamma = 0.5 * B
    if start is None:
        start = [(X / 2, X / 2)] * n
    lo = [(W[i] / 2, H[i] / 2) for i in range(n)]
    hi = [(X - W[i] / 2, X - H[i] / 2) for i in range(n)]

    def clamp(px, py):
        return ([min(max(px[i], lo[i][0]), hi[i][0]) for i in range(n)],
                [min(max(py[i], lo[i][1]), hi[i][1]) for i in range(n)])

    def gradients(px, py):
        _, wx, wy = lse_wirelength(px, py, nets, weights, pads, gamma)
        rho = bin_density(px, py, W, H, M, B)  # @line L4
        _, _, fx, fy = potential_and_field(rho, B)
        cx, cy = [], []
        for i in range(n):
            u = min(M - 1, max(0, int(px[i] // B)))
            v = min(M - 1, max(0, int(py[i] // B)))
            cx.append(fx[u][v])
            cy.append(fy[u][v])
        return wx, wy, cx, cy, rho

    vx, vy = clamp([p[0] for p in start], [p[1] for p in start])  # @line L1
    wx, wy, cx, cy, rho = gradients(vx, vy)
    dens = sum(abs(q[i] * cx[i]) + abs(q[i] * cy[i]) for i in range(n))
    lam = sum(abs(g) for g in wx + wy) / dens if dens > 0 else 1.0  # @line L2
    ux, uy = vx, vy
    a = 1.0
    prev = None
    for it in range(max_iter):  # @line L3
        if it > 0:
            wx, wy, cx, cy, rho = gradients(vx, vy)
        tau = overflow(rho, area, B)
        if history is not None:
            history.append((tau, hpwl(vx, vy, nets, weights, pads)))
        if tau <= tau_stop:  # @line L11
            ux, uy = vx, vy
            break
        gx = [(wx[i] - lam * q[i] * cx[i]) / (deg[i] + lam * q[i]) for i in range(n)]  # @line L6
        gy = [(wy[i] - lam * q[i] * cy[i]) / (deg[i] + lam * q[i]) for i in range(n)]
        if prev is None:
            alpha = 0.1 * B  # @line L7
        else:
            dv = math.sqrt(sum((vx[i] - prev[0][i]) ** 2 + (vy[i] - prev[1][i]) ** 2 for i in range(n)))
            dg = math.sqrt(sum((gx[i] - prev[2][i]) ** 2 + (gy[i] - prev[3][i]) ** 2 for i in range(n)))
            alpha = dv / dg if dg > 0 else 0.1 * B
            alpha = min(alpha, B)
        prev = (vx, vy, gx, gy)
        nux, nuy = clamp([vx[i] - alpha * gx[i] for i in range(n)], [vy[i] - alpha * gy[i] for i in range(n)])  # @line L8
        an = (1 + math.sqrt(4 * a * a + 1)) / 2  # @line L9
        beta = (a - 1) / an
        vx, vy = clamp([nux[i] + beta * (nux[i] - ux[i]) for i in range(n)],
                       [nuy[i] + beta * (nuy[i] - uy[i]) for i in range(n)])
        ux, uy, a = nux, nuy, an
        lam *= 1.1  # @line L10
    return ux, uy  # @line L12
test_electrostatics_based_placement.py 138 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/electrostatics-based-placement/. The authors are listed in prov/KESX1E.jsonl.

import math
import random

from electrostatics_based_placement import (
    bin_density,
    dct2,
    eplace,
    lse_wirelength,
    overflow,
    potential_and_field,
    psi_at,
)


def random_rho(rng, M):
    return [[rng.choice([0.0, 0.25, 0.5, 1.0, 1.5, 2.0]) for _ in range(M)] for _ in range(M)]


def test_bin_density_against_a_direct_sum():
    rng = random.Random(1)
    for _ in range(80):
        M, B = 4, 2.0
        n = rng.randint(1, 7)
        W = [rng.choice([0.5, 1.0, 1.5, 3.0]) for _ in range(n)]
        H = [rng.choice([0.5, 1.0, 2.0]) for _ in range(n)]
        x = [rng.randint(0, 32) / 4 for _ in range(n)]  # multiples of 1/4: the float sums are exact
        y = [rng.randint(0, 32) / 4 for _ in range(n)]
        rho = bin_density(x, y, W, H, M, B)
        for u in range(M):
            for v in range(M):
                direct = 0.0
                for i in range(n):
                    ox = max(0.0, min(x[i] + W[i] / 2, (u + 1) * B) - max(x[i] - W[i] / 2, u * B))
                    oy = max(0.0, min(y[i] + H[i] / 2, (v + 1) * B) - max(y[i] - H[i] / 2, v * B))
                    direct += ox * oy
                assert rho[u][v] == direct / (B * B)


def test_dct_matches_the_direct_formula_and_inverts():
    rng = random.Random(2)
    for M in (1, 2, 3, 5, 8):
        rho = random_rho(rng, M)
        a = dct2(rho)
        for u in range(M):
            for v in range(M):
                cu = (1 if u == 0 else 2) / M
                cv = (1 if v == 0 else 2) / M
                direct = cu * cv * sum(rho[x][y] * math.cos(math.pi * u * (x + 0.5) / M) * math.cos(math.pi * v * (y + 0.5) / M)
                                       for x in range(M) for y in range(M))
                assert abs(a[u][v] - direct) < 1e-12
        for x in range(M):
            for y in range(M):
                back = sum(a[u][v] * math.cos(math.pi * u * (x + 0.5) / M) * math.cos(math.pi * v * (y + 0.5) / M)
                           for u in range(M) for v in range(M))
                assert abs(back - rho[x][y]) < 1e-12
        assert abs(a[0][0] - sum(map(sum, rho)) / (M * M)) < 1e-12


def test_potential_solves_the_poisson_equation_at_the_bin_centers():
    rng = random.Random(3)
    h = 1e-3
    for M in (4, 6):
        rho = random_rho(rng, M)
        mean = sum(map(sum, rho)) / (M * M)
        a, psi, _, _ = potential_and_field(rho, 1.0)
        for x in range(M):
            for y in range(M):
                tx, ty = x + 0.5, y + 0.5
                assert abs(psi_at(a, tx, ty) - psi[x][y]) < 1e-12
                lap = (psi_at(a, tx + h, ty) + psi_at(a, tx - h, ty) + psi_at(a, tx, ty + h)
                       + psi_at(a, tx, ty - h) - 4 * psi_at(a, tx, ty)) / (h * h)
                assert abs(-lap - (rho[x][y] - mean)) < 1e-4


def test_field_is_minus_the_gradient_and_vanishes_on_the_border():
    rng = random.Random(4)
    h = 1e-5
    M, B = 5, 2.0
    rho = random_rho(rng, M)
    a, _, ex, ey = potential_and_field(rho, B)
    for x in range(M):
        for y in range(M):
            tx, ty = x + 0.5, y + 0.5
            dx = (psi_at(a, tx + h, ty) - psi_at(a, tx - h, ty)) / (2 * h) / B
            dy = (psi_at(a, tx, ty + h) - psi_at(a, tx, ty - h)) / (2 * h) / B
            assert abs(ex[x][y] + dx) < 1e-6 and abs(ey[x][y] + dy) < 1e-6
    for t in (0.3, 1.7, 4.2):
        for edge in (0.0, float(M)):
            assert abs(psi_at(a, edge + h, t) - psi_at(a, edge - h, t)) / (2 * h) < 1e-8
            assert abs(psi_at(a, t, edge + h) - psi_at(a, t, edge - h)) / (2 * h) < 1e-8


def test_uniform_density_has_no_field():
    _, psi, ex, ey = potential_and_field([[0.7] * 4 for _ in range(4)], 1.0)
    assert all(abs(v) < 1e-12 for grid in (psi, ex, ey) for row in grid for v in row)


def test_lse_gradient_against_finite_differences():
    rng = random.Random(5)
    h = 1e-6
    for _ in range(30):
        n = rng.randint(1, 5)
        pads = [(rng.uniform(0, 8), rng.uniform(0, 8)) for _ in range(2)]
        nets = [rng.sample(range(n + 2), rng.randint(2, min(4, n + 2))) for _ in range(rng.randint(1, 6))]
        weights = [rng.randint(1, 3) for _ in nets]
        x = [rng.uniform(0, 8) for _ in range(n)]
        y = [rng.uniform(0, 8) for _ in range(n)]
        gamma = rng.choice([0.25, 1.0, 2.0])
        _, gx, gy = lse_wirelength(x, y, nets, weights, pads, gamma)
        for i in range(n):
            for coord, g in ((x, gx), (y, gy)):
                coord[i] += h
                up = lse_wirelength(x, y, nets, weights, pads, gamma)[0]
                coord[i] -= 2 * h
                down = lse_wirelength(x, y, nets, weights, pads, gamma)[0]
                coord[i] += h
                assert abs((up - down) / (2 * h) - g[i]) < 1e-5


def test_placer_spreads_cells_on_fixed_seeds():
    rng = random.Random(14)
    for k in range(4):
        n, M, B = 24, 8, 1.0
        q = 6
        pads = [(0.0, rng.uniform(0, 8)), (8.0, rng.uniform(0, 8)), (rng.uniform(0, 8), 0.0),
                (rng.uniform(0, 8), 8.0), (0.0, rng.uniform(0, 8)), (8.0, rng.uniform(0, 8))]
        nets = [[i, n + rng.randrange(q)] if i == 0 or rng.random() < 0.3 else [i, rng.randrange(i)] for i in range(n)]
        nets += [rng.sample(range(n + q), rng.randint(2, 3)) for _ in range(n)]
        r2 = random.Random(k)
        start = [(4 + r2.uniform(-0.1, 0.1), 4 + r2.uniform(-0.1, 0.1)) for _ in range(n)]
        hist = []
        x, y = eplace(n, nets, pads, [1.0] * n, [1.0] * n, M, B, start=start, history=hist, max_iter=200)
        assert hist[0][0] > 0.5 and hist[-1][0] <= 0.1
        assert overflow(bin_density(x, y, [1.0] * n, [1.0] * n, M, B), n, B) <= 0.1
        assert all(0.5 <= v <= 7.5 for v in x + y)

History

Lu, Chen, Chang, Sha, Huang, Teng and Cheng presented ePlace at DAC 2014 and in ACM TODAES in 2015. The electrostatic analogy of density was older: Kraftwerk (1998) used Poisson's equation for its spreading forces. The new parts of ePlace were the global charge model with a spectral solver, Nesterov's method with a Lipschitz step estimate, and the preconditioner. ePlace-MS (2015) handled mixed-size designs, and RePlAce (Cheng, Kahng, Kang and Wang, 2019) added local density control and routability; RePlAce became the global placer of the OpenROAD flow.

References

  1. Jingwei Lu, Pengwen Chen, Chin-Chih Chang, Lu Sha, Dennis Jen-Hsin Huang, Chin-Chi Teng and Chung-Kuan Cheng, ePlace, electrostatics-based placement using fast Fourier transform and Nesterov's method, ACM Transactions on Design Automation of Electronic Systems 20(2), article 17, 2015, the electrostatic density model, the spectral solution of Poisson's equation, Nesterov's method with a Lipschitz step estimate, and the preconditioner
  2. Jingwei Lu, Pengwen Chen, Chin-Chih Chang, Lu Sha, Dennis Jen-Hsin Huang, Chin-Chi Teng and Chung-Kuan Cheng, ePlace, electrostatics based placement using Nesterov's method, Proceedings of the 51st Design Automation Conference (DAC), 2014, the conference version of the method
  3. Chung-Kuan Cheng, Andrew B. Kahng, Ilgweon Kang and Lutong Wang, RePlAce, advancing solution quality and routability validation in global placement, IEEE Transactions on Computer-Aided Design of Integrated Circuits and Systems 38(9), 1717 to 1730, 2019, the extension of ePlace with local density functions and routability-driven cell inflation
  4. 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 (analytical global placement with a density penalty; 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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.average
  • a full proof of time.best
  • a full proof of time.worst
  • a full proof of space.extra
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