GeoZL

Fast on one CPU core

  • SSE2, AVX2 and NEON kernels with a plain C fallback, picked once at run time. Sources in core/src.

A tile is written once and read many times. Every one of those reads runs the decoder of each codec in the tile's graph, so the decoders decide how fast everything built on GeoZL feels. This first part is how we made them fast on a single CPU core, roughly in the order we learned it, from the first predictor kernels to the PFOR and fused codecs most recipes end in today.

The short version is that a core can work on eight or sixteen values per instruction, and nearly every speedup we found came from shaping a decoder so those values stop waiting on each other. The longer version has a predictor that will not be parallel, and a block layout chosen for SIMD that the GPU kernel later read the same way.

One instruction, many values

A CPU core can process several numbers with one instruction. A vector register has lanes that add, shift or compare values together. This is called SIMD, single instruction, multiple data. ARM chips, including Apple chips, use NEON with 128-bit registers. x86 uses SSE2, 128 bits on every 64-bit chip, and AVX2, 256 bits on most recent chips.

Scalar loop. The straightforward decoder handles one value per instruction. The simplest predictor shows it best. delta_n predicts each sample from the one above it, so decoding a row is the row above plus the residuals, one add per sample.

for (size_t c = 0; c < w; ++c)
  row[c] = res[c] + up[c];   // one sample per add

SIMD loop. A vector register is one wide register cut into lanes. A 128-bit NEON or SSE2 register holds eight 16-bit samples side by side, and one add instruction adds two such registers lane by lane, eight sums for the price of one. So the decoder loads eight residuals into one register, the eight samples above them into another, adds once, and stores eight finished samples.

uint16x8_t r = vld1q_u16(res + c);    // 8 residuals in one register
uint16x8_t u = vld1q_u16(up + c);     // the 8 samples above them
vst1q_u16(row + c, vaddq_u16(r, u));  // one add, 8 finished samples

This only works because no sample in the row needs the one next to it. Each lane does its own sum and never looks at its neighbour. That is also why we did not write this code for delta_n ourselves. The loop in GeoZL is the plain one above, and the compiler sees that its iterations are independent and turns it into exactly these NEON or SSE2 adds.

Before, decoding eight samples of delta_n takes eight add instructions of one value each, 3 plus 1520 is 1523, 2 plus 1522 is 1524, and so on. After, the eight residuals sit in one 128-bit register and the eight samples of the row above in another, and a single add instruction adds them lane by lane and gives all eight results at once.
Eight adds of one value each, or one add over eight lanes.

On our test tile, lanes make delta_n about six times faster than the same loop run one value at a time. That makes delta_n the cheapest predictor to decode, and a useful yardstick for the rest of this page. Every other decoder here has something that keeps it from being this simple.

How we measured. The test tile is the first four bands of a Landsat 8 OLI scene from the Earth Compress corpus, l8-oli-8-00, 4 × 512 × 512 uint16 or 2 MiB. Each kernel was built with clang -O3 and timed on one thread of an Apple M5, best of 2,000 runs, with the output checked against the tile. These are kernel times on a tile that stays in cache, faster than a full frame decode, so the ratios between versions say more than the absolute numbers. Older versions were built from the source at the commit before each change.

GeoZL carries kernels for NEON, SSE2 and AVX2. At run time it chooses the widest kernel the machine supports, once, and builds without SIMD use plain C. All paths write the same bytes, so a frame decodes identically on every machine. GEOZL_SIMD can only cap the AVX2 dispatch, since NEON and SSE2 are compiled in, so the one-value-at-a-time numbers on this page come from a build with GEOZL_NO_SIMD, plus -fno-vectorize where the compiler would still vectorize plain C on its own.

What a decoder needs to use it

SIMD only helps when the lanes can work without talking to each other. It needs the same operation on neighbouring values, adjacent memory and no branch per value. A row dependency makes value i wait for i − 1. A CPU can live with one such dependency, because it can pass one value between registers while it walks a row.

A GPU has different needs, since no thread can wait for a carry from another, so a codec can suit SIMD but not a GPU, or the reverse. What a GPU wants, the second part, is about that difference. The table below sums it up.

A CPU core with SIMD walks a row eight lanes per step and passes a carry from one register to the next for the cost of one add. A GPU runs thousands of threads at once, so a serial carry along the row limits parallel decoding.
A SIMD core can carry one number along the row. A serial carry limits GPU parallelism.
SIMD on a CPU corea GPU
planara row at a time, by prefix suma sum down the columns, then along the rows
med, averagea chain through W, one sample after anotherstays on the CPU
PFORshift, and, or on every laneeach thread jumps to slot × b
Huffmanone code after anotherreplaced by PivCo bitmaps

Planar, a row at a time

Planar predicts every sample as W + N − NW, the left neighbour plus the one above minus the one above left.

Direct recurrence. Written as it reads, the decoder is a loop in which every sample needs the sample just decoded on its left.

for (size_t c = 1; c < w; ++c)
  x[c] = x[c - 1] + N[c] - NW[c] + r[c];   // waits for x[c - 1]

Lanes cannot help here, because lane i would need the result of lane i − 1 from the same instruction.

Before, planar decoded as written, x equals W plus N minus NW plus r, so each of eight pixels waits for the one on its left, eight steps one after another. After, the row step c equals r plus N minus NW is computed for all eight lanes with one instruction, because N and NW come from the finished row above, and a prefix sum of c in three steps gives the row.
The part that needs W shrinks to a running total, and everything else moves into lanes.

Prefix-sum form. Move W to the other side and the decoder becomes x[i] − x[i−1] = r[i] + N[i] − N[i−1]. The right side uses only the residual and the row above, which is already decoded. That is a row step like delta_n, one vector instruction for eight samples. What is left of the chain is a running total of the row step, a prefix sum, and the next section shows how that runs inside a register.

Decoding one row of planar. Row above 10 12 15 15, residuals 1 0 -1 2. Step 1 computes r plus N minus the N on its left for every column at once, 11 2 2 2. Step 2 takes a running sum, 11 13 15 17, which is the decoded row.
One row with numbers. At the left edge W and NW count as zero.

Without lanes the rearrangement buys nothing. Run one value at a time it is as slow as the loop above, because each sample still puts one add on the chain. It pays off only because it moves everything except the running total into lanes.

A two-pass implementation writes the row step first, then reads the row again for the prefix sum.

pre16(d + off, s + off, d + off - w, w);   // row step, written to the row
scan16(d + off, d + off, w);               // prefix sum, reads the row back

GeoZL fuses the two. The row step is computed in a register and handed straight to the prefix sum, so each row is read and written once.

The other linear predictors are variations on it. delta_w is the same prefix sum without the row above. wp_static reads only finished rows, so it folds its weighted sum of the row above into the residual and closes the row with the same prefix sum as planar.

A prefix sum inside a register

Serial prefix. A prefix sum written plainly is a running total, one add per sample, and every add waits for the one before it. Eight samples take eight adds in a row, whatever the register width.

for (size_t i = 0; i < n; ++i) {
  acc += c[i];   // waits for the previous add
  x[i] = acc;
}
Before, the running total of 11 2 2 2 1 3 -1 -1 is eight adds one after another, 11, then 11 + 2 = 13, 13 + 2 = 15, up to 20 - 1 = 19, eight waits. After, the eight values sit in one register and three instructions each add all eight lanes at once, shifted by one, two and four lanes, giving 11 13 4 4 3 4 2 -2, then 11 13 15 17 7 8 5 2, then 11 13 15 17 18 21 20 19. Three waits for the same eight samples.
The same eight sums. Before, each add waits for the one before it. After, each instruction adds all eight lanes, and only three instructions wait.

Vector prefix. Inside a register the same sum takes three steps instead of eight, because each step doubles how far back every lane reaches. Shift the register by one lane and add, and every lane holds itself plus its left neighbour, a window of two. Shift by two and add, and two windows of two make a window of four. Shift by four and add, and the window is eight, the whole register, so every lane holds the sum of all the lanes up to it. Each step is one instruction over all eight lanes. The figure below follows lane 5 through the three steps.

A prefix sum of eight uint16 lanes in one register. C is 11 2 2 2 1 3 -1 -1. Shifting by one lane and adding gives 11 13 4 4 3 4 2 -2, by two lanes 11 13 15 17 7 8 5 2, by four lanes 11 13 15 17 18 21 20 19.
1 / 4
Each lane adds the lane k places to its left, k = 1, 2, 4. Every step joins two blocks already summed, so the block doubles and three steps reach the whole register.

This is the NEON code, from core/src/common/vprefix.h, with the values of the figure above in the comments and the window size in brackets. vextq_u16(zero, x, 7) is x moved one lane to the right with a zero coming in on the left, and vaddq_u16 adds two registers lane by lane.

const uint16x8_t zero = vdupq_n_u16(0);
uint16x8_t x = vld1q_u16(c);              // 11  2  2  2  1  3 -1 -1
x = vaddq_u16(x, vextq_u16(zero, x, 7));  // 11 13  4  4  3  4  2 -2 (2)
x = vaddq_u16(x, vextq_u16(zero, x, 6));  // 11 13 15 17  7  8  5  2 (4)
x = vaddq_u16(x, vextq_u16(zero, x, 4));  // 11 13 15 17 18 21 20 19 (8)

Three instructions that wait on each other, instead of eight. That is more arithmetic, not less. Three steps of eight lanes are 24 adds against the 8 of the plain loop, and some of them only add zeros. It is still faster, because the eight adds of a step cost one instruction, and what sets the speed is how many steps have to wait for the one before, three instead of eight. The lanes are unsigned 16-bit, so a −1 is stored as 65535 and every sum wraps around the same way, which leaves the result exact.

Together, the row step in lanes and the prefix sum in a register are what make planar fast. On the test tile planar decodes about eight times faster with them than one value at a time.

One add per register

Think of a bank account with one statement per month. You can add up each month's movements on their own, but the balance on any day of February also needs everything that happened in January. The amount carried over from one month to the next is the carry.

A row works the same way, with registers for months. A row of 16 samples fills two registers of eight lanes, and the three shifts and adds above sum each register on its own, as if the row started there. The first register ends at 19. The second sums to 2 3 5 5 6 7 7 10, short by those 19, so the decoder adds 19 to every lane and gets the row, 21 to 29.

Two registers hold one row. The first ends at 19, which is added to every local prefix sum in the second; the next carry is 19 plus the second register's local total, 10.
The first register ends at 19, so 19 is added to every lane of the second. Both ways give 29 for the third, only the order of the work differs.

Each register's local prefix sums depend on nothing outside that register, so the CPU can compute several of them side by side. Only the carry has to go in order. The speed of the row is therefore set by the chain that passes one carry to the next register.

Output-derived carry. The next carry can be copied from the last lane of the finished row, but only after the old carry has been added to all eight lanes. That makes the dependency chain two steps per register.

uint16x8_t x = geozl_vprefix16_neon(vld1q_u16(src + i));
x = vaddq_u16(x, carry);         // add the carry to every lane
vst1q_u16(dst + i, x);
carry = geozl_vlast16_neon(x);   // copy the last lane of the output

Local-total carry. The next carry is the old carry plus the last local sum, 19 + 10 = 29. The 10 is ready before the 19 arrives, so one add advances the chain. Adding 19 to all eight output lanes still happens, but nothing waits for it. In the bank-account example, February's closing balance is January's plus February's net; the individual statement lines can be written alongside it. The 16-bit NEON implementation is geozl_vscan16_neon.

static inline uint16x8_t geozl_vscan16_neon(uint16x8_t x, uint16x8_t *carry) {
  const uint16x8_t p = geozl_vprefix16_neon(x);
  const uint16x8_t out = vaddq_u16(p, *carry);
  *carry = vaddq_u16(*carry, geozl_vlast16_neon(p));  // old carry + this sum
  return out;
}
Before, the carry for the next register was the last lane of the finished output, so the chain from one register to the next was an add followed by a lane copy, two steps per register. After, the carry grows by the last lane of the register's own prefix sum, which is ready early, so the chain from register to register is a single add.
The output still adds the carry, but nothing waits for it. Only the carry itself is on the chain.

Both versions write the same bytes. One step less per register made planar about 45% faster on the test tile, because everything else in the row already runs side by side and the carry chain is all that is left of the serial part.

When the guess needs W

Planar uses a sum, so W cancels when rearranged. average and med do not use a sum, so W never cancels. Each sample needs its reconstructed left neighbour, which no prefix sum can supply, and the decoder has to go through the row one sample after another.

That chain is part of what these predictors are, and no rearrangement removes it. What we could do was take everything else off it. We took both as far as we know how on a CPU, with one change they share and one for each, and what is left today is the chain itself.

Edges, in both

Both predictors read three neighbours, W on the left, N above and NW above left. On the first row and the first column some of them fall outside the tile and count as zero.

Per-sample checks. A direct decoder tests for tile edges on every sample.

W  = (c > 0)          ? d[i - 1]     : 0;
N  = (r > 0)          ? d[i - w]     : 0;
NW = (r > 0 && c > 0) ? d[i - w - 1] : 0;

Separate edge loops. The current kernels decode the first row and the first column in loops of their own, so the interior loop reads its three neighbours with no test at all.

Before, every sample of the tile runs the edge tests c greater than 0 and r greater than 0, even far from any edge. After, the first row and the first column are decoded by their own loops with the tests, and the interior loop reads W, N and NW with no tests at all.
Only the first row and column still test where they are.

On the M5 we could not measure a difference for either predictor. Those tests are almost always true, which a branch predictor guesses well, and the chain through W costs far more.

average

average guesses each sample as the mean of W and N. In integers that is (W >> 1) + (N >> 1) + (W & N & 1), half of each plus the low bit they share, so the mean never overflows.

One serial expression. Computing the whole mean per sample keeps half of N in the serial loop, even though it does not depend on W.

Split expression. The N half is known before the row starts. A vectorized pass computes it ahead of the row, and the serial loop keeps only what depends on W.

Before, the serial loop of average computes the whole mean for each sample, half of N, half of W, the shared low bit W and N and 1, and the residual, one sample at a time, although half of N does not need W. After, half of N plus the residual, B, and the low bit of N, L, are computed for every column at once in lanes before the row starts, and the serial loop keeps only the three steps that need W, half of W, W and L, and plus B.
Everything that needs only N runs for the whole row at once. The loop keeps what needs W.
for (c = 0; c < w; ++c) {        // in lanes, ahead of the row
  B[c] = res[c] + (up[c] >> 1);
  L[c] = up[c] & 1;
}
for (c = 1; c < w; ++c)          // the chain through W
  t = o[c] = B[c] + (t >> 1) + (t & L[c]);

That made average about 8% faster on the test tile. A modest gain, which is about what a shorter chain can give when the chain is still there.

med

med guesses each sample as the median of W, N and the planar guess W + N − NW. Picking the middle one of the three lets it follow an edge in the image, where planar would overshoot.

One row. A direct med decoder runs one serial chain through the west neighbour, with the median written as a three-way choice.

P = (NW >= max(W, N)) ? min(W, N)
  : (NW <= min(W, N)) ? max(W, N)
  : W + N - NW;

Each sample waits for the previous one, and the core has nothing else to work on while it waits.

Four-row wavefront. The current kernel keeps four rows in flight, each one column behind the row above. Each row is still serial, but four independent chains keep the core busy, and the rows below read N and NW from registers just written by the row above. The median uses masks instead of branches.

Before, med decodes one row at a time, a single serial chain through the west neighbour, and the next row starts only when this one ends. After, med decodes four rows at once, row 2 one column behind row 1, row 3 behind row 2 and row 4 behind row 3, so the four chains are independent and the lower rows read N and NW from registers.
Four chains a column apart. The pixel above each ? is already done.

On the test tile that made med about two and a half times faster. To see which half did it, we also built the masked median on its own, one row at a time. It was no faster than before, so on this machine the gain comes from the four chains, not from the masks.

That staggered wavefront is the best we found for med on a CPU, and on a GPU the chain does not go at all. med still decodes about seven times more slowly than planar on the same tile, so start with planar when decode speed matters.

PFOR in 16-byte lanes

PFOR packs each block of 256 values at the bit width b that most of them need. How PFOR works covers the format. Here the question is how to unpack it fast.

Contiguous bitstream. The textbook layout puts the values one after another in one long bit string. With b = 3, value i starts at bit 3i, somewhere inside a byte, so every value needs its own position and shift.

for (size_t i = 0; i < 256; ++i) {
  size_t pos = i * b;                                  // where value i starts
  v[i] = (load64(p + pos / 8) >> (pos % 8)) & mask;  // a different shift each time
}

SIMD lanes. GeoZL takes its layout from TurboPFor. Instead of one long string, the block is cut into eight lanes for two-byte values and sixteen for one-byte values. Values are dealt to them like cards. A value's lane is i mod 8, and its slot inside that lane is i div 8.

Each lane packs its own values one after another at b bits, so with b = 3 slot 0 sits at bits 0 to 2 of every lane, slot 1 at bits 3 to 5, and so on. Every lane keeps its slots at the same bits, so one 16-byte load, one shift and one mask take out the same slot of all eight lanes at once.

The figure walks through it with a block of 16 values at b = 3. A real block has 256, and the steps are the same.

A block of 16 values at b = 3 bits. Packed one after another, value i starts at bit 3i, so values cross byte boundaries and each needs its own shift. Dealt to eight lanes instead, value i goes to lane i mod 8 at slot i div 8, values 0 to 7 in slot 0 and values 8 to 15 in slot 1. The eight lanes of 16 bits are one 16-byte load, and one shift and one mask, x shifted by 0 and anded with 7, take out slot 0 of all eight lanes, values 0 to 7. Shifting by 3 takes out slot 1, values 8 to 15.
1 / 7
Dealt out like cards, every lane keeps its slots at the same bits, so one shift and one mask unpack eight values at once.

On the residuals that planar>zigzag>pfor feeds it, that makes PFOR close to twice as fast as one value at a time. Most of the gain comes from the layout alone, because the compiler vectorizes a plain loop over lanes that never touch each other. The same layout later let a GPU thread read its values with one load, which part three comes back to.

Fewer trips through memory

Fast kernels expose a cost that slow ones hide, the memory traffic between them.

Separate nodes. A graph of separate nodes runs each codec over the whole tile before the next one starts. For a 2 MiB uint16 tile through planar, zigzag and pfor, pfor writes 2 MiB of codes, zigzag reads them and writes 2 MiB of residuals, and planar reads those and writes the tile. That is about 10 MiB through memory besides the frame.

Before, a 2 MiB uint16 tile decoded by three separate nodes. pfor writes 2 MiB of codes, zigzag reads them and writes 2 MiB of residuals, planar reads those and writes the 2 MiB tile, about 10 MiB through memory besides the frame. After, the fused planar_zigzag_pfor codec unpacks, unzigzags and reconstructs 256 values at a time in registers and a small buffer that stays in cache, so only the frame is read and only the tile is written.
Same tile, same output. The fused codec never writes the two streams in between.

Fused codec. The fused codec unpacks one PFOR block into a small buffer that stays in cache, undoes Zigzag in registers and writes the finished samples once. It moves about 2 MiB plus the frame. That is why planar>zigzag>pfor runs as planar_zigzag_pfor. Fused follows one tile through both forms.

We built both graphs for the test tile with the low-level Python nodes, Planar, OpenZL's Zigzag and Pfor against PlanarZigzagPfor, and timed decompress on one thread of the M5, best of 1,500 runs. The frames then differed by 6 bytes. The fused codec decoded the tile about a third faster than the separate nodes, and geozl.graph(tile, "planar>zigzag>pfor") ran at the same speed as the fused codec. The separate graph also makes one more call into Python, which is a small part of the difference.

Timed as bare kernels with buffers that stay warm in cache, the three passes cost only about 7% more than the fused one. So on the M5, where a 2 MiB tile fits in cache, most of what fusion saves is the two whole streams the graph has to create, fill and pass from node to node. On a tile that does not fit in cache the passes themselves would cost more, but we have not measured that here.

The fused codec uses a layout that also suits GPU decoding. Prediction restarts at independent band and strip boundaries, and each band is packed in 256-value blocks. Fused describes the exact geometry.

From a register to a GPU

Rumi uses GeoZL to feed training batches to models. Those models want tensors in GPU memory, which motivates decoding the frames on the device itself.

Some of what we built for SIMD carries over and some does not. Planar, PFOR and the PivCo lanes also decode on a GPU. med, average and plain Huffman do not. What a GPU wants explains why, and Bytes to tensors follows a batch through the GPU decoder.