Algorithmadvanced
Clock skew scheduling
Also called useful skew, Fishburn skew optimization, clock schedule optimization.
Fishburn's choice of clock arrival times at registers to shorten the clock period, a system of difference constraints solved by linear programming or cycle ratio.
- Time
- O(R n m) totalO(n^3 m Δ) integer_dataO(n m) per_round
- Space
- O(n + m) extra
- Where
- R = the number of rounds; m = the number of register-to-register paths; n = the number of registers; Δ = the least period minus the start period, for integer delays
- Model
- Real RAM
- Properties
- deterministicexactiterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- John P. Fishburn, 1990
Clock skew scheduling chooses, for each register of a synchronous circuit, the time at which the clock edge reaches it, so that the clock period can be shorter than the longest path delay. The setup and hold constraints of every register-to-register path become difference constraints on these clock arrival times. John Fishburn stated the problem as a linear program in 1990. The least period is a cycle ratio of the constraint graph, and the page computes it exactly by raising the period to the ratio of each negative cycle that Bellman-Ford finds, until none is left.
In plain words
With a single clock, every register normally gets the clock edge at the same time. Then the clock period must be at least the slowest path between two registers. But some paths are fast and some are slow. If the clock reaches the end of a slow path a little later, that path gets more time, and the path that starts at that register gets less time. When the slow and the fast paths alternate, the clock period can go down.
There are limits. A path that is too fast can let the new data overwrite the old data before the next register has read it, so a register cannot be shifted too far. Each limit says that the clock time of one register minus that of another is at most some amount. A period works exactly when no loop of these limits adds up to a contradiction.
The problem
Registers are . A path from register to register has a largest delay and a smallest delay , from the clock pin of to the data pin of . The registers have setup time and hold time . With clock arrival times and period , each path must meet, in the notation of the setup and hold page with and ,
The task is the least for which some meets all constraints, and such a . The difference is the skew between the two registers. With all equal (zero skew), the least period is , valid when every .
Each constraint has the form . In the constraint graph it is an edge of weight : a setup edge of weight and a hold edge of weight . By the theorem of difference constraints, is valid if and only if no cycle has negative weight. A cycle with setup edges has weight , where is the sum of over its setup edges minus the sum of over its hold edges. So the least period is the largest cycle ratio
provided that no cycle of hold edges alone is negative; if one is, no period works. This is a fractional programming problem over the cycles of a graph.
The algorithm
CLOCK-SKEW-SCHEDULE(registers, paths, t_s, t_h)- 1
if Bellman-Ford finds a negative cycle among the hold edges alone then return "no period works" - 2
T ← max over paths p of d_max(p) − d_min(p) + t_s + t_h - 3
loop - 4
run Bellman-Ford on the constraint graph at period T, from distance 0 at every node - 5
if no negative cycle is found then - 6
return T and c ← the distances, shifted so that the smallest is 0 - 7
C ← the negative simple cycle found; k ← its number of setup edges; S ← k T − weight(C) - 8
T ← S / k
Line L2 is a lower bound: adding the setup and hold constraint of one path gives . Line L7 always has , because line L1 removed the case of a negative cycle with no setup edge. Line L8 raises to the value at which the cycle has weight exactly 0. This is the iteration of Dinkelbach applied to the cycle ratio: each round either proves the current period valid or finds a cycle with a larger ratio.
Example
Three registers have and four paths:
| path | setup edge and weight | hold edge and weight | ||
|---|---|---|---|---|
| 9 | 5 | , | , | |
| 3 | 2 | , | , | |
| 4 | 1 | , | , | |
| 6 | 2 | , | , |
With zero skew the period is . Line L2 starts at , from the paths and .
| round | negative cycle found | new | |||
|---|---|---|---|---|---|
| 1 | 6 | setup edges | 3 | 19 | 19/3 |
| 2 | 19/3 | hold , setup | 2 | 13 | 13/2 |
| 3 | 13/2 | none |
The least period is , with the schedule . At this schedule the setup constraints of () and () and the hold constraint of () are tight. These three form the critical cycle of round 2. The short path stops the schedule from moving later, which would help the slow path . Without the hold constraints the answer would be , the mean of over the loop . The period drops from 10 to 6.5, by 35 percent, so the clock frequency can rise by a factor of .
Correctness
The proof is in proofs/correctness.md. By the theorem of difference constraints a period is valid exactly when no simple cycle of the constraint graph is negative, so the valid periods form the half line . The start is at most , and each round moves to the ratio of a cycle that is negative at the current , which is larger than and at most . So holds all the time, and when Bellman-Ford finds no negative cycle, is valid, so . The distances of Bellman-Ford are a valid schedule.
Proof: Cycle ratio iteration returns the least clock period of a skew schedule and stopsfull
Registers are . Each path from register to register has delays , and , are the setup and hold times. A schedule and a period are valid when every path meets
The constraint graph has the registers as nodes, for each path a setup edge of weight , and a hold edge of weight . Each edge weight has the form with . For a cycle of let be its number of setup edges and the sum of the constants , so its weight at period is . Write .
Lemma 1 (validity and cycles). A period has a valid schedule if and only if for every simple cycle of .
Proof. This is the theorem of difference constraints. If is valid, each edge of weight has , and the sum around a cycle telescopes to . Conversely, if no cycle is negative, add a node with an edge of weight 0 to every register. No cycle through exists, so shortest path distances from are finite, and for every edge, so is valid. A closed walk of negative weight contains a simple cycle of negative weight, so simple cycles suffice.
Lemma 2 (the set of valid periods). If some simple cycle has and , no period is valid. Otherwise, and when there is at least one path, the valid periods are exactly the with
Proof. A cycle with has weight for every , so the first case is clear by Lemma 1. In the other case the cycles with never fail, and a cycle with holds if and only if . A path from to gives the 2-cycle of its two edges, with , and a path from to gives a setup loop with , so the maximum is over a nonempty finite set.
Lemma 3 (the start is a lower bound). Let . Then .
Proof. Take a valid pair and a path from to . Adding its setup and hold inequalities gives .
Lemma 4 (Bellman-Ford). With all distances set to 0 at the start, Bellman-Ford at a fixed either makes no change in some round among the first , and then the distances form a valid schedule, or it changes a distance in round , and then the walk back along the predecessor edges from that node reaches a simple cycle of negative weight.
Proof. The start with all distances 0 is the state after one round from the extra node of Lemma 1. A round with no change means every edge satisfies , which is validity. If no cycle is negative, every shortest path from has at most edges, so all distances are final after more rounds and round makes no change. If round makes a change, the standard argument of negative cycle detection applies: the predecessor graph then holds a cycle, every cycle of the predecessor graph has negative weight, and walking back steps from a node changed in round ends on that cycle.
Theorem 1 (correctness). If the hold edges alone have a negative cycle, the algorithm returns that no period is valid, and this is true. Otherwise it returns and a valid schedule for .
Proof. The first part is Lemma 2, since a cycle of hold edges has , and Lemma 4 run on the hold edges finds such a cycle when there is one (their weights do not depend on ). Now assume no such cycle. We show the invariant . It holds at the start by Lemma 3. Suppose Bellman-Ford finds a negative simple cycle at the current . Then , since cycles with are all hold cycles, which are not negative. So , and the new period satisfies , the second by the definition of . When Bellman-Ford finds no negative cycle, is valid with the schedule of Lemma 4, so by Lemma 2, and with the invariant . Subtracting the smallest distance from all of them keeps every difference, so the shifted schedule is valid and has .
Theorem 2 (termination). The loop stops after at most as many rounds as has simple cycles, plus one.
Proof. In each round that does not stop, the period becomes for the cycle found, and it only grows later. So in every later round , and is not found again. has finitely many simple cycles.
Theorem 3 (round bound for integer data). If all delays and the setup and hold times are integers, the number of rounds is at most , and each round takes time for paths.
Proof. Every period after the first is with an integer and , since a simple cycle has at most edges. Two distinct such values and differ by . The periods after the first round strictly increase and lie in , so there are at most of them. Bellman-Ford does rounds over edges.
Corollary (no hold constraints). If only the setup edges are present, every cycle consists of setup edges, so is its length and is the largest mean of over the cycles of paths. This is a maximum mean cycle, which Karp's algorithm finds in time after negating the weights.
Complexity
Each round is one run of Bellman-Ford over the constraint edges, time. A cycle found in one round has weight at least 0 in every later round, so no cycle comes back, and the loop stops. For integer delays and setup and hold times, every ratio is with , two distinct ratios differ by at least , and the number of rounds is at most . This bound is pseudo-polynomial; the page does not claim a polynomial bound on the rounds of this iteration. Polynomial methods exist: a binary search on the answer with one Bellman-Ford test per step, ended by rounding to the nearest fraction with denominator at most , or parametric search. The extra space is .
Properties
- Exact. The returned period is the least valid period for the given delays, and the schedule meets every constraint at it.
- Deterministic, with exact rational arithmetic.
- The schedule is not unique. Any schedule of the polytope of valid schedules at works, and adding a constant to all changes nothing.
- The least period is never more than the zero skew period when zero skew is valid, since zero skew is one choice of .
- Without hold constraints, is the largest mean of over the cycles of register-to-register paths, which Karp's algorithm finds in time after the weights are negated.
Variants and improvements
- Linear program. Fishburn minimizes subject to the constraints, a linear program in variables. He also maximizes the smallest safety margin of all constraints at a fixed period, which gives a schedule more robust against delay variation.
- Parametric shortest paths. Shenoy, Brayton and Sangiovanni-Vincentelli treat as the parameter of a parametric shortest path problem on the constraint graph.
- Maximum mean cycle. Albrecht, Korte, Schietke and Vygen show that, with setup constraints only, the least period is a maximum mean weight cycle, and they study fast algorithms for it on the graphs of large chips.
- Bounded skew windows. In practice each must lie in a window that the clock tree can deliver. A window is two more difference constraints with a fixed reference node, so the same algorithm applies.
When to use it
Use it when the registers are fixed and the clock tree can be built to deliver chosen arrival times, and when the paths around a loop of registers are unbalanced. If the clock tree must have zero skew, use retiming instead, which moves the registers across the logic and keeps the clock; minimum-area retiming does so with the fewest registers. Both methods reduce to difference constraints, but retiming changes the circuit while skew scheduling changes only the clock times.
Pitfalls
- Ignoring the hold constraints gives a shorter period that the circuit cannot use. In the example the setup constraints alone give , but no schedule meets the hold constraints at that period.
- A cycle of hold edges alone with negative weight makes every period invalid. No skew can fix a race of that kind; the short paths must be made slower.
- A schedule computed for nominal delays has zero margin on its critical cycle. Variation of the clock tree delays breaks it, so a schedule for a real chip keeps a margin, for example with Fishburn's second linear program at a period above .
- The word "skew" here is the difference of clock arrival times at two registers of one chip. In distributed systems, clock skew means the offset between the clocks of two processes, a different idea.
- Floating point Bellman-Ford can report a tiny negative cycle at itself and loop. Use exact rationals or stop when the increase of is below a tolerance.
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/clock-skew-scheduling/. The authors are listed in prov/K3PCDZ.jsonl.
"""Clock skew scheduling: the least clock period and a clock schedule.
Registers are 0 to n - 1. A path (i, j, dmax, dmin) is a combinational path
from register i to register j with largest delay dmax and smallest delay
dmin. With clock arrival times c, the period T is valid when every path has
setup: c_i + dmax + t_setup <= c_j + T
hold: c_i + dmin >= c_j + t_hold
Each is a difference constraint x_u - x_v <= w, an edge v -> u of weight w,
and the weight of an edge is a T + b with a in {0, 1}. The period T is valid
if and only if the graph has no negative cycle. The least valid period is
found by cycle ratio iteration: while Bellman-Ford finds a negative cycle at
the current T, raise T to the ratio at which that cycle has weight 0.
All arithmetic is exact with Fractions or integers.
"""
from fractions import Fraction
def constraint_edges(n, paths, t_setup, t_hold):
"""Edges (v, u, a, b) meaning c_u - c_v <= a T + b."""
edges = []
for i, j, dmax, dmin in paths:
edges.append((j, i, 1, -(dmax + t_setup))) # setup: c_i - c_j <= T - dmax - t_setup
edges.append((i, j, 0, dmin - t_hold)) # hold: c_j - c_i <= dmin - t_hold
return edges
def bellman_ford(n, edges, T):
"""Return (None, dist) when no cycle is negative at period T, else
(cycle, None) with the edge indices of one negative simple cycle."""
dist = [Fraction(0)] * n
pred = [None] * n
last = None
for _ in range(n):
last = None
for k, (v, u, a, b) in enumerate(edges):
w = a * T + b
if dist[v] + w < dist[u]:
dist[u] = dist[v] + w
pred[u] = k
last = u
if last is None:
return None, dist
x = last
for _ in range(n):
x = edges[pred[x]][0]
cycle, y = [], x
while True:
k = pred[y]
cycle.append(k)
y = edges[k][0]
if y == x:
break
cycle.reverse()
return cycle, None
def min_period(n, paths, t_setup=0, t_hold=0):
"""Return (T, c, rounds): the least clock period, a clock schedule that
meets it with min(c) = 0, and the number of Bellman-Ford rounds. Return
None when the hold constraints alone have a negative cycle, since then
no period works."""
edges = constraint_edges(n, paths, t_setup, t_hold)
hold = [e for e in edges if e[2] == 0]
cyc, _ = bellman_ford(n, hold, Fraction(0))
if cyc is not None: # @line L1
return None
if not paths:
return Fraction(0), [Fraction(0)] * n, 0
# start at the largest bound of a 2-cycle (the setup and hold edge of one path)
T = max(Fraction(dmax - dmin + t_setup + t_hold) for _, _, dmax, dmin in paths) # @line L2
rounds = 0
while True: # @line L3
rounds += 1
cyc, dist = bellman_ford(n, edges, T) # @line L4
if cyc is None: # @line L5
low = min(dist)
return T, [d - low for d in dist], rounds # @line L6
k = sum(edges[e][2] for e in cyc) # @line L7
s = -sum(edges[e][3] for e in cyc)
T = Fraction(s, k) # @line L8
test_clock_skew_scheduling.py 143 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/clock-skew-scheduling/. The authors are listed in prov/K3PCDZ.jsonl.
import random
from fractions import Fraction
from clock_skew_scheduling import constraint_edges, min_period
def simple_cycles(n, edges):
"""Every simple cycle of the multigraph, as a list of edge indices, each
found once from its smallest node."""
out = []
adj = [[] for _ in range(n)]
for k, (v, u, _, _) in enumerate(edges):
adj[v].append((u, k))
def dfs(start, v, seen, path):
for u, k in adj[v]:
if u == start:
out.append(path + [k])
elif u > start and u not in seen:
seen.add(u)
dfs(start, u, seen, path + [k])
seen.discard(u)
for s in range(n):
dfs(s, s, {s}, [])
return out
def exact_by_cycles(n, paths, ts, th):
"""The least period from the cycle condition: every cycle with k setup
edges and constant part b needs k T + b >= 0."""
edges = constraint_edges(n, paths, ts, th)
best = None
for cyc in simple_cycles(n, edges):
k = sum(edges[e][2] for e in cyc)
b = sum(edges[e][3] for e in cyc)
if k == 0:
if b < 0:
return "infeasible"
else:
r = Fraction(-b, k)
best = r if best is None or r > best else best
return best
def negative_cycle_fw(n, paths, ts, th, T):
"""Independent test of a negative cycle at period T (Floyd-Warshall)."""
INF = None
d = [[INF] * n for _ in range(n)]
for v, u, a, b in constraint_edges(n, paths, ts, th):
w = a * T + b
if d[v][u] is None or w < d[v][u]:
d[v][u] = w
for m in range(n):
for i in range(n):
if d[i][m] is None:
continue
for j in range(n):
if d[m][j] is None:
continue
w = d[i][m] + d[m][j]
if d[i][j] is None or w < d[i][j]:
d[i][j] = w
return any(d[i][i] is not None and d[i][i] < 0 for i in range(n))
def meets(paths, ts, th, T, c):
return all(c[i] + dmax + ts <= c[j] + T and c[i] + dmin >= c[j] + th for i, j, dmax, dmin in paths)
def random_instance(rng, n, m):
paths = []
for _ in range(m):
i, j = rng.randrange(n), rng.randrange(n)
dmin = rng.randint(0, 6)
paths.append((i, j, dmin + rng.randint(0, 6), dmin))
return paths, rng.randint(0, 2), rng.randint(0, 2)
def test_against_cycle_enumeration(rng=random.Random(3141)):
infeasible = 0
for _ in range(400):
n = rng.randint(1, 5)
paths, ts, th = random_instance(rng, n, rng.randint(1, 7))
want = exact_by_cycles(n, paths, ts, th)
got = min_period(n, paths, ts, th)
if want == "infeasible":
assert got is None
infeasible += 1
continue
T, c, _ = got
assert T == want
assert meets(paths, ts, th, T, c)
assert min(c) == 0
assert not negative_cycle_fw(n, paths, ts, th, T)
assert negative_cycle_fw(n, paths, ts, th, T - Fraction(1, 1000))
assert infeasible > 0
def test_grid_search_two_registers(rng=random.Random(27)):
# with two registers a simple cycle has at most 2 setup edges, so the
# optimum is a multiple of 1/2; search T and c_1 - c_0 on that grid
for _ in range(60):
paths, ts, th = random_instance(rng, 2, rng.randint(1, 4))
got = min_period(2, paths, ts, th)
bound = 2 * sum(dmax + ts + th + 1 for _, _, dmax, _ in paths)
found = None
for t2 in range(0, 2 * bound + 1):
T = Fraction(t2, 2)
for x2 in range(-2 * bound, 2 * bound + 1):
if meets(paths, ts, th, T, [Fraction(0), Fraction(x2, 2)]):
found = T
break
if found is not None:
break
if got is None:
assert found is None
else:
assert got[0] == found
def test_zero_skew_period_is_an_upper_bound(rng=random.Random(5)):
for _ in range(100):
n = rng.randint(1, 5)
paths, ts, th = random_instance(rng, n, rng.randint(1, 7))
got = min_period(n, paths, ts, th)
if got is None or any(dmin < th for _, _, _, dmin in paths):
continue
zero = max(dmax + ts for _, _, dmax, _ in paths)
assert got[0] <= zero
def test_example_of_the_page():
paths = [(0, 1, 9, 5), (1, 2, 3, 2), (2, 0, 4, 1), (0, 2, 6, 2)]
T, c, rounds = min_period(3, paths, 1, 1)
assert (T, c, rounds) == (Fraction(13, 2), [0, Fraction(7, 2), 1], 3)
assert exact_by_cycles(3, paths, 1, 1) == T
assert meets(paths, 1, 1, T, c)
assert not meets(paths, 1, 1, Fraction(10) - Fraction(1, 100), [0, 0, 0])
assert meets(paths, 1, 1, Fraction(10), [0, 0, 0])
History
Fishburn published clock skew optimization in IEEE Transactions on Computers in 1990, with the setup and hold constraints of each pair of registers and the linear program for the least period. Shenoy, Brayton and Sangiovanni-Vincentelli gave graph algorithms based on the constraint graph at ICCAD 1992. Albrecht, Korte, Schietke and Vygen presented the link to the maximum mean weight cycle at ICCAD 1999 and in the journal version in Discrete Applied Mathematics in 2002.
References
- John P. Fishburn, Clock skew optimization, IEEE Transactions on Computers 39(7), 945 to 951, 1990, the setup (zero clocking) and hold (double clocking) constraints on each pair of registers, and the linear program for the least period
- Narendra Shenoy, Robert K. Brayton and Alberto L. Sangiovanni-Vincentelli, Graph algorithms for clock schedule optimization, Proceedings of ICCAD 1992, 132 to 136, the constraint graph of the clock schedule and the parametric shortest path view of the least period
- Christoph Albrecht, Bernhard Korte, Jürgen Schietke and Jens Vygen, Maximum mean weight cycle in a digraph and minimizing cycle time of a logic chip, Discrete Applied Mathematics 123(1-3), 103 to 127, 2002, the least period as a maximum mean weight cycle when only the setup constraints are present
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 K3PCDZ Edit this page History Report an error Markdown