---
title: On-chip networks
heading: Routers, flits and flow control, zero-load latency and saturation throughput
eyebrow: S4 · Lesson 34 · On-chip networks
xp: 50
---

::: hook
A many-core chip's cores, cache slices and memory controllers talk over a network on the die: a mesh of routers, each joined to its neighbors by wide links. Every cache miss to a remote slice and every coherence message rides it. Its latency adds a router pipeline per hop, its throughput is capped by how much traffic can cross the chip's middle, and its buffers must cover the time it takes a credit to come back. This lesson computes all three.
:::

::: play
- The curve is a network's average latency against the load offered per node, with a zero-load latency $T_0$ and saturation at $\lambda_\text{max}$; a simple queueing shape, $T_0/(1 - \lambda/\lambda_\text{max})$, captures how latency climbs near saturation.
- For a k × k mesh under uniform random traffic the bisection caps the injection rate at $4/k$ flits per node per cycle; a torus doubles it.
- Real networks saturate a little below the bisection limit, because routing and arbitration can't use every link every cycle.
:::

```sim plot
view: graph
x: l = 0..1
y: T = 0..150 cycles
samples: 500
slider: k = 4..16 step 2 = 8
plot: (2*k/3*4 + 4)/(1 - l/(4/k)) | label: mesh latency
plot: (k/2*4 + 4)/(1 - l/(8/k)) | dashed | label: torus latency
```

::: predict
## An 8 × 8 mesh carries uniform random traffic; each link moves one flit per cycle in each direction. What is the highest injection rate per node the bisection allows?

- [ ] 1 flit per node per cycle
- [x] 0.5 flits per node per cycle
- [ ] 0.125 flits per node per cycle
- [ ] 4 flits per node per cycle

::: reveal
Cutting the mesh in half severs 8 links, 8 flits per cycle each way. Half the nodes, 32, are on each side, and half of their traffic crosses: $32 \times \lambda/2 = 16\lambda$ flits per cycle each way. Setting $16\lambda = 8$ gives $\lambda = 0.5$, or $4/k$ in general.
:::
:::

::: nameit
## Packets and flits

A message is split into **flits**, the units a link moves per cycle; a 64-byte cache line on 16-byte links is 4 flits plus usually a header flit. **Wormhole** flow control lets a packet's flits follow its head through routers without storing the whole packet at each.

## Latency

With H hops, a router pipeline of $t_r$ cycles and a link of $t_w$, and a packet of L bits on links of b bits:
$$T_0 = H(t_r + t_w) + \frac Lb$$
Average distances under uniform traffic: about $2k/3$ on a k × k mesh, about $k/2$ on a torus.

## Throughput

The bisection limits uniform traffic: $\lambda_\text{max} = 4/k$ flits per node per cycle on a k × k mesh, $8/k$ on a torus. Latency rises steeply as the offered load nears it.

## Flow control and buffers

Routers hold flits in buffers and send them on only when the next router has space, tracked by **credits**. A buffer needs as many slots as the credit round trip in cycles, or the link idles while credits return. **Virtual channels** share one physical link among several buffered queues, so a blocked packet doesn't block others behind it, and separate classes of messages (requests and replies) can't deadlock each other.
:::

::: example
## On an 8 × 8 mesh with 3-cycle routers and 1-cycle links, what is the zero-load latency of a 64-byte line on 16-byte links, at the average distance?

::: solution
1. **Hop latency.** The average distance is $2 \times 8/3 = 5.3$ hops, each costing 3 + 1 = 4 cycles:
   $$\tfrac{16}{3} \times 4 = 21.3\ \text{cycles}$$
2. **Serialization.** The 512-bit line crosses a 128-bit link in
   $$512/128 = 4\ \text{cycles}$$
3. **Total.**
   $$21.3 + 4 \approx 25\ \text{cycles}$$
   before any queueing.
4. **Round trip.** A request and its reply each pay it, which is why remote cache slices cost tens of cycles.

**Answer:** the zero-load latency is about 25 cycles.
:::
:::

::: example
## A link's credit round trip is 1 cycle across the link, 3 cycles through the next router to free a slot, and 1 cycle for the credit to return. How many flit buffers does each virtual channel need?

::: solution
1. **Credit round trip.** Five cycles pass between sending a flit and learning its slot is free again:
   $$1 + 3 + 1 = 5\ \text{cycles}$$
2. **Buffers.** So 5 buffers keep the link busy.
3. **With only 3.** The sender would wait 2 cycles in every 5: the link would run at 60% however light the traffic.

**Answer:** each virtual channel needs 5 flit buffers.
:::
:::

::: example
## Compare the saturation injection rates of an 8 × 8 mesh and an 8 × 8 torus, and their average distances.

::: solution
1. **Mesh.** Saturation at
   $$4/8 = 0.5$$
   flits per node per cycle, with 5.3 hops on average.
2. **Torus.** Saturation at
   $$8/8 = 1.0$$
   flits per node per cycle, with about 4.1 hops on average (exactly $k/2$ scaled for distinct pairs).
3. **The cost.** The torus doubles the bisection with its wraparound links but needs long wires or a folded layout to keep them short.

**Answer:** the mesh saturates at 0.5 flits per node per cycle with 5.3 hops on average; the torus at 1.0, with about 4.1 hops.
:::
:::

::: check
kind: parts
given: k = 4 | 8 | 16; r = 2 | 3; L = 64 | 128; b = 16 | 32

## A {k} × {k} mesh has {r}-cycle routers and 1-cycle links {b} bytes wide; a message carries {L} bytes.

::: part
answer: 2*k/3*(r + 1) + L/b
unit: cycles
tolerance: 0.5%

### What is its zero-load latency at the average distance?

::: misses
- [2*k/3*(r + 1)] Add the serialization, L/b cycles.
- [any] $H(t_r + t_w) + L/b$.
:::
:::

::: part
answer: 2*(k - 1)*(r + 1) + L/b
unit: cycles
tolerance: 0.5%

### And corner to corner?

::: misses
- [any] Diameter $2(k - 1)$ hops.
:::
:::
:::

::: check
kind: parts
given: k = 4 | 8 | 16; b = 16 | 32; f = 1 | 2

## A {k} × {k} network has links {b} bytes wide in each direction at {f} GHz.

::: part
answer: 4/k
unit: flits/cycle
tolerance: 0.5%

### What injection rate per node saturates a mesh under uniform traffic?

::: misses
- [any] $4/k$.
:::
:::

::: part
answer: 8/k
unit: flits/cycle
tolerance: 0.5%

### And a torus?

::: misses
- [any] Twice the mesh: $8/k$.
:::
:::

::: part
answer: k*b*f
unit: GB/s
tolerance: 0.5%

### What is the mesh's bisection bandwidth in each direction?

::: misses
- [any] k links × width × clock.
:::
:::
:::

::: check
kind: parts
given: w = 1 | 2; r = 2 | 3 | 4; c = 1 | 2; s = 3 | 4

## A flit takes {w} cycle(s) to cross a link and {r} cycles to leave the next router's buffer; the credit takes {c} cycle(s) to return. Each virtual channel has {s} buffer slots.

::: part
answer: w + r + c
unit: cycles
tolerance: 0

### What is the credit round trip?

::: misses
- [any] Link + router + credit return.
:::
:::

::: part
answer: (s/(w + r + c) - (s/(w + r + c) - 1)*step(s/(w + r + c) - 1))*100
unit: %
tolerance: 0.5%

### What fraction of the link's capacity can one virtual channel use?

::: misses
- [any] Slots over the round trip, at most 100%.
:::
:::
:::

::: check
kind: choice

## Why do on-chip routers use virtual channels?

- [ ] To make links wider
- [x] So a packet blocked downstream doesn't block other packets sharing the link, and so separate message classes (requests and replies) have separate buffers and can't deadlock each other
- [ ] To shorten the router pipeline
- [ ] To encrypt traffic between cores

::: misses
- [1] The physical link is unchanged; it's time-shared among queues.
- [3] Virtual channel allocation usually adds a pipeline stage.
- [4] Virtual channels are about flow control, not security.
:::
:::

::: check
kind: derivation
vars: k = 2..32, t = 1..5, L = 32..256, b = 8..64

## A k × k mesh has routers of t cycles and 1-cycle links of b bytes; messages carry L bytes.

::: step
answer: 2*k/3

### What is the average distance between distinct nodes?
:::

::: step
answer: 2*k/3*(t + 1) + L/b

### What is the zero-load latency at that distance?
:::

::: step
answer: 4/k

### What injection rate saturates it under uniform traffic?
:::

::: right
Right. Latency grows with k and throughput per node falls with k: big meshes need concentration or richer topologies.
:::
:::

::: check
kind: python

## Write `avg_hops(k, torus)`: the average shortest-path hop count between distinct nodes of a k × k mesh (or torus, with wraparound), by enumerating all pairs.

```python starter
def avg_hops(k, torus):
    return 2 * k / 3
```

```python solution
def avg_hops(k, torus):
    tot = n = 0
    for a in range(k * k):
        for b in range(k * k):
            if a == b:
                continue
            dx, dy = abs(a % k - b % k), abs(a // k - b // k)
            if torus:
                dx, dy = min(dx, k - dx), min(dy, k - dy)
            tot += dx + dy
            n += 1
    return tot / n
```

```python tests
assert abs(avg_hops(8, False) - 16 / 3) < 1e-12, "8 × 8 mesh: exactly 2k/3."
assert abs(avg_hops(8, True) - 4.0635) < 1e-4, "8 × 8 torus: k/2 scaled by k²/(k² − 1)."
assert avg_hops(4, True) < avg_hops(4, False), "Wraparound always shortens the average."
```

::: right
Right. The mesh's 2k/3 is exact for distinct pairs; the torus averages k/2 over all pairs including a node with itself.
:::
:::

::: explain
## Why does a network's latency climb steeply as its load approaches saturation?

::: model
At light load a packet finds every router and link free and arrives after the zero-load latency. As the offered load grows, packets increasingly arrive at a router or link that's busy and must wait in a buffer. The waiting time depends on how often the resource is busy, and, as in any queue, it grows like one over one minus the utilization: modest at half load, enormous near full load. The bisection links saturate first under uniform traffic, so packets crossing the middle of the chip queue the longest, and the average latency rises sharply as the injection rate nears the bisection limit.
:::
:::

::: practice
::: check
given: k = 6 | 10; r = 2 | 4
answer: 2*k/3*(r + 1) + 4
unit: cycles
tolerance: 0.5%

## Zero-load latency on a {k} × {k} mesh, {r}-cycle routers, 4 flits?

::: misses
- [any] $2k/3(t_r + 1) + 4$.
:::
:::

::: check
given: k = 4 | 6 | 8 | 10 | 12 | 16
answer: 4/k
unit: flits/cycle
tolerance: 0.5%

## Saturation injection rate for a {k} × {k} mesh?

::: misses
- [any] $4/k$.
:::
:::

::: check
given: r = 3 | 5; c = 1 | 2
answer: 1 + r + c
unit: cycles
tolerance: 0

## Credit round trip: 1-cycle link, {r}-cycle router, {c}-cycle credit?

::: misses
- [any] Sum them.
:::
:::
:::
