---
title: Near-data and in-memory computing
heading: Processing near memory, processing in memory, analog crossbars and when offloading pays
eyebrow: S4 · Lesson 41 · Near-data and in-memory computing
xp: 50
---

::: hook
If moving a byte from DRAM costs far more time and energy than computing with it, then compute where the data is. Logic in the base die of a memory stack can use the stack's internal bandwidth; small processors inside DRAM chips can work on every bank at once; arrays of resistive memory cells can multiply a matrix by a vector in one analog step, using Ohm's and Kirchhoff's laws. Each trades generality and precision for bandwidth and energy, and each pays off only for the right fraction of a workload.
:::

::: play
- The curve is the speedup from offloading the data-movement part of a kernel to near-memory logic: a fraction m of the time is data movement, made s times faster, plus an overhead o for launching and synchronizing: $1/((1 - m) + m/s + o)$.
- Kernels dominated by data movement gain the most; compute-bound ones gain little and can lose to the overhead.
- The dashed line is the limit with free data movement, $1/(1 - m + o)$.
:::

```sim plot
view: graph
x: m = 0..0.99
y: S = 0..12
slider: s = 2..50 = 10
slider: o = 0..0.2 = 0.05
plot: 1/((1 - m) + m/s + o) | label: offload speedup
plot: 1/((1 - m) + o) | dashed | label: limit
```

::: predict
## A 256 × 256 crossbar of resistive cells holds a weight matrix as conductances. Applying input voltages to its rows and reading every column's current (with its converter) takes 100 ns. How many multiply-accumulates does it perform per second?

- [ ] 2.56 billion
- [x] About 655 billion
- [ ] 65 536
- [ ] 10 million

::: reveal
Every cell multiplies its row voltage by its conductance (Ohm's law) and each column wire sums its cells' currents (Kirchhoff's current law): $256 \times 256 = 65\,536$ multiply-accumulates per read. At one read per 100 ns that's $6.6 \times 10^{11}$ a second from one small array, with no weight ever moved.
:::
:::

::: nameit
## Why move the compute

From lesson 27: reading a bit from DRAM costs about 10 to 20 pJ, while moving it a millimeter across a logic die costs about 0.1 pJ. Off-chip pins are scarce, but inside a memory chip or stack there are thousands of banks and very wide internal paths.

## Processing near memory

Logic on the base die of a stacked memory (HBM, HMC) reaches the stack's banks through its through-silicon vias, with more bandwidth and less energy than the host's interface. Suitable for streaming kernels: scans, reductions, graph traversals.

## Processing in memory

Simple processors placed beside DRAM banks work on their own bank's data in parallel. Aggregate bandwidth = number of banks × per-bank bandwidth, often ten times the host channels' bandwidth. Data must be laid out so each unit's work stays in its bank; moving data between units is slow.

## Analog crossbars

A crossbar of resistive cells stores a matrix G as conductances. Driving row voltages $V_i$ gives column currents $I_j = \sum_i G_{ij}V_i$: a whole matrix-vector product in one step. The catches: converters (ADCs) to read the columns dominate area and energy, device variation and noise limit precision to a few bits, and writing new weights is slow.

## When offloading pays

If a fraction m of a kernel's time is data movement that the near-data hardware speeds up by s, with an overhead o (as a fraction of the original time):
$$\text{speedup} = \frac{1}{(1 - m) + m/s + o}$$
Amdahl's law again, with the offload's setup as part of the serial part.
:::

::: example
## Moving a 64-bit word off-chip costs 15 pJ per bit; moving it 1 mm on a logic die costs 0.1 pJ per bit. How much energy does computing next to the memory save per word?

::: solution
1. **Off-chip.**
   $$64 \times 15 = 960\ \text{pJ}$$
2. **On-die, 1 mm.**
   $$64 \times 0.1 = 6.4\ \text{pJ}$$
3. **Compare.** The near-data unit spends 150 times less energy delivering each operand, which is why even modest logic beside memory can win on energy-limited workloads.

**Answer:** computing next to the memory spends 6.4 pJ per word instead of 960 pJ, 150 times less.
:::
:::

::: example
## A processing-in-memory system has 2560 DRAM banks, each feeding its processor 1 GB/s. The host has 8 channels of 25.6 GB/s. Compare the bandwidths.

::: solution
1. **In memory.**
   $$2560 \times 1\ \text{GB/s} = 2.56\ \text{TB/s}$$
2. **Host.**
   $$8 \times 25.6\ \text{GB/s} = 205\ \text{GB/s}$$
3. **Compare.** The in-memory units see 12.5 times the bandwidth, but only if each works on data in its own bank; anything that needs data from other banks goes back through the slow host path.

**Answer:** 2.56 TB/s in memory against 205 GB/s for the host, 12.5 times as much.
:::
:::

::: example
## A kernel spends 70% of its time moving data. Near-memory logic makes that part 10 times faster but costs 5% of the original time in setup. What is the speedup?

::: solution
1. **Speedup.**
   $$\begin{aligned} S &= \frac{1}{0.3 + 0.7/10 + 0.05} \\ &= \frac{1}{0.42} \\ &= 2.38 \end{aligned}$$
2. **The new bottleneck.** The compute part, 30% of the original time, now dominates; even infinitely fast data movement would give only
   $$1/(0.3 + 0.05) = 2.86$$

**Answer:** the speedup is 2.38.
:::
:::

::: check
kind: parts
given: n = 64 | 128 | 256; t = 50 | 100 | 500; E = 1 | 10

## An n × n crossbar (n = {n}) performs one matrix-vector product per {t} ns, using {E} nJ per product, converters included.

::: part
answer: n^2/(t*1e-9)/1e9
unit: GMAC/s
tolerance: 0.5%

### How many multiply-accumulates does it perform per second?

::: misses
- [n/(t*1e-9)/1e9] Every cell is a multiply; there are n² of them.
- [any] $n^2/t$.
:::
:::

::: part
answer: E*1e-9/n^2*1e15
unit: fJ
tolerance: 0.5%

### What energy does each multiply-accumulate cost?

::: misses
- [any] Energy per product over $n^2$.
:::
:::
:::

::: check
kind: parts
given: b = 1024 | 2560 | 4096; r = 0.5 | 1; c = 4 | 8; w = 25.6 | 38.4

## A processing-in-memory system has {b} banks, each feeding its unit {r} GB/s; the host has {c} channels of {w} GB/s.

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

### What aggregate bandwidth do the in-memory units see?

::: misses
- [any] Banks × per-bank bandwidth.
:::
:::

::: part
answer: b*r/(c*w)
tolerance: 0.5%

### How many times the host's bandwidth is that?

::: misses
- [any] In-memory over host.
:::
:::
:::

::: check
kind: parts
given: m = 0.5 | 0.7 | 0.9; s = 5 | 20; o = 0.02 | 0.1

## A fraction {m} of a kernel's time is data movement that offloading speeds up {s} times, at an overhead of {o} of the original time.

::: part
answer: 1/((1 - m) + m/s + o)
tolerance: 0.5%

### What speedup results?

::: misses
- [1/((1 - m) + m/s)] Include the overhead.
- [any] $1/((1 - m) + m/s + o)$.
:::
:::

::: part
answer: 1/((1 - m) + o)
tolerance: 0.5%

### What is the most any speedup of data movement could give?

::: misses
- [any] Let s grow without bound.
:::
:::
:::

::: check
kind: parts
given: e = 10 | 15 | 20; d = 0.05 | 0.1; L = 1 | 5

## Reading a bit from DRAM costs {e} pJ; moving a bit across a logic die costs {d} pJ per mm, and near-memory logic sits {L} mm from its data.

::: part
answer: 64*e
unit: pJ
tolerance: 0.5%

### What does fetching a 64-bit word off-chip cost?

::: misses
- [any] 64 bits × energy per bit.
:::
:::

::: part
answer: e/(d*L)
tolerance: 0.5%

### How many times cheaper is delivering it to the near-memory logic?

::: misses
- [any] Off-chip energy over on-die energy, per bit.
:::
:::
:::

::: check
kind: choice

## Why do analog crossbar accelerators usually compute at only a few bits of precision?

- [ ] Resistive cells can store only one bit
- [x] Device variation, noise and drift in the conductances and currents limit the accuracy of each column's sum, and higher-resolution ADCs to read it cost area and energy that grow quickly with bits
- [ ] Ohm's law is only approximately linear
- [ ] Digital logic can't read analog currents

::: misses
- [1] Many resistive cells store several levels; the limit is the accuracy of the analog sum.
- [3] For these purposes the cells are close to linear; variation is the problem.
- [4] ADCs read them; their cost is the issue.
:::
:::

::: check
kind: derivation
vars: m = 0..0.99, s = 1..100, o = 0..0.3

## A fraction m of a kernel's time is data movement, sped up s times by near-memory logic at an overhead o.

::: step
answer: (1 - m) + m/s + o

### What fraction of the original time does the offloaded kernel take?
:::

::: step
answer: 1/((1 - m) + m/s + o)

### What is the speedup?
:::

::: step
answer: 1/((1 - m) + o)

### What does the speedup approach as s grows without bound?
:::

::: right
Right. Offloading pays only when the movement saved outweighs the overhead added: $m(1 - 1/s) > o$.
:::
:::

::: check
kind: python

## Write `crossbar(G, v, bits)`: G is a list of rows of conductances in [0, 1] (siemens, scaled) and v a list of row voltages in [0, 1]; compute each column's exact current $\sum_iG_{ij}v_i$, then quantize it with a `bits`-bit converter spanning 0 to the number of rows (round to the nearest level). Return (exact currents, quantized currents).

```python starter
def crossbar(G, v, bits):
    return ([], [])
```

```python solution
def crossbar(G, v, bits):
    rows, cols = len(G), len(G[0])
    exact = [sum(G[i][j] * v[i] for i in range(rows)) for j in range(cols)]
    step = rows / (2 ** bits - 1)
    quant = [round(x / step) * step for x in exact]
    return (exact, quant)
```

```python tests
import random
r = random.Random(2)
G = [[r.random() for _ in range(16)] for _ in range(64)]
v = [r.random() for _ in range(64)]
ex, q = crossbar(G, v, 8)
assert all(abs(a - sum(G[i][j] * v[i] for i in range(64))) < 1e-12 for j, a in enumerate(ex)), "Column currents are the dot products."
assert max(abs(a - b) for a, b in zip(ex, q)) <= 64 / 255 / 2 + 1e-12, "Within half a converter step."
ex4, q4 = crossbar(G, v, 4)
assert max(abs(a - b) for a, b in zip(ex4, q4)) > max(abs(a - b) for a, b in zip(ex, q)), "Fewer bits, larger errors."
```

::: right
Right. The converter's step, the full-scale sum over its levels, sets the precision of every product the array computes.
:::
:::

::: explain
## Why can near-memory computing save energy even when its processors are slower than the host's?

::: model
In data-intensive programs most of the energy goes not into arithmetic but into moving data: each bit read from DRAM and driven across the board to the processor costs tens of picojoules, while an arithmetic operation on it costs a few. Logic placed beside the memory banks reads the same data over short internal wires, at a small fraction of that energy, and has far more internal bandwidth available than the pins provide. Even if each operation runs more slowly, the total energy falls because the expensive part, moving the data, mostly disappears; the processors can be simple because the work is limited by bandwidth rather than by computation.
:::
:::

::: practice
::: check
given: n = 128 | 512; t = 100 | 200
answer: n^2/(t*1e-9)/1e9
unit: GMAC/s
tolerance: 0.5%

## MAC rate of an {n} × {n} crossbar at one product per {t} ns?

::: misses
- [any] $n^2/t$.
:::
:::

::: check
given: m = 0.6 | 0.8; s = 8 | 30
answer: 1/((1 - m) + m/s + 0.05)
tolerance: 0.5%

## Offload speedup, movement fraction {m}, {s} times faster, 5% overhead?

::: misses
- [any] Amdahl with overhead.
:::
:::

::: check
given: b = 2048 | 8192; r = 0.5 | 2
answer: b*r
unit: GB/s
tolerance: 0.5%

## Aggregate PIM bandwidth, {b} banks at {r} GB/s?

::: misses
- [any] Banks × rate.
:::
:::
:::
