Checking how many agents are working

How this works
Contact
← Blog
A steel worker standing idle at a bench beside a long conveyor belt that carries a single brass part, lit in cold teal with a warm amber highlight, the AMARBARO mark and wordmark centered over it.
✎blog post

Waiting on memory or on math: the two reasons code is slow

The short version

Code is slow because the chip waits for data or because it does arithmetic, and a hardware counter names where cycles are charged, which needs one experiment to confirm the cause.

Part 10 in Measuring things honestly. Earlier parts include finding the slow part of a program.

What this is about

Think of a cook in a kitchen. The cook can be slow for two reasons. Either the cook is chopping, mixing and cooking as fast as they can, or the cook is standing still, waiting for ingredients to arrive from the store room.

A computer chip is the same. It is either calculating, or waiting for data from memory. The fix is very different for each. If it waits for data, faster maths does not help. If it calculates all day, a faster supply line does not help.

This article shows how to tell which one you have.

Why it matters

Programmers lose weeks on the wrong fix. They rewrite the maths of a program that was only waiting for memory. Knowing the reason first saves that time.

How it works

Memory bound and compute bound

When a program is limited by memory, we call it memory bound. The chip has the speed to do more work, but the data arrives too slowly. When a program is limited by arithmetic, we call it compute bound.

Step 1: Count the bytes

For memory bound code, count how many bytes move. One launch of our fly physics engine at 65,536 animals reads 40.0 GB and writes 15.9 GB. That is 55.9 GB per launch.

Divide by the time and compare with the card's top speed, 960 GB per second. The answer depends on which time you use. Using a time of 66.7 ms gives 838 GB per second, 87 percent of the top. Using the 99.2 ms the profiler recorded while it was counting gives 563 GB per second, 59 percent.

We have to be honest about that range. The bytes were counted in one clock mode and the 66.7 ms was timed in another, and the profiler adds its own cost. We have not taken the one measurement that would settle it. The firm part is that the step moves a lot of data. The 82 percent figure in our first write-up was wrong, because we mislabelled units and mishandled two counters, and an outside review caught it.

Step 2: Check that your explanation counts accesses

Our first explanation said that the code re-reads some tables over and over. When a reviewer counted the reads in the source code, three of our claims fell. We had looked at how big the tables were, and treated that as how much was read. Those are different numbers. One stored piece, for example, takes 3.6 KB to store but about 54 KB of reads to build. The rule we kept: count the reads, not the size of what is stored.

Step 3: Run an experiment that tells the two apart

Counters tell you where the cycles are charged. They do not always tell you which instruction is the real cost.

On a OnePlus 7T Pro phone, we wanted to know why a faster kernel for a 4096 by 4096 matrix product was faster. The hardware counters said the arithmetic bucket held 87 percent of the busy time. After our first improvement it held 85 percent, after the second 81 percent. The busy cycles fell by half, but the counter named the same bucket each time.

The reason: on that GPU, issuing a cached load is charged to the arithmetic bucket. The counter was telling us where the cycles were charged, not what the cost was.

So we ran a test called an ablation. We replaced the reads of one input with a constant, and left the maths alone. The result is wrong on purpose, so the run is for timing only. The time fell from 1353.5 to 589.4 microseconds, a 56 percent drop. The loads were the cost. The kernel was limited by how many loads it issued, not by the arithmetic.

That told us where to look. If four rows share one set of loads of the input, the eight loads per weight fall to two. The kernel we kept does this, and together with a second change it halved the time.

Step 4: Look at the whole picture

For the physics engine, we also tried two code changes. One saved 2.9 percent, too little to keep, so we undid it. The other cut the run from 88.9 to 70.6 ms, about 20 percent. Neither change tells memory and maths apart cleanly, and we have not read a counter for the maths side. So we say it plainly: "memory" rests on the byte count, and "not maths" is our best guess, not a measurement.

Tools that help

Our tool wavewait reads a hardware trace from an AMD RDNA3 GPU. For every instruction it shows how many cycles each wave waited before the instruction issued, and sorts the waits by cause: memory, shared memory, barrier, dependency or instruction issue. A wait that costs 670 cycles for a memory load can look like one cheap line in a normal listing.

Our tool corewait does the same for CPU code. It records counters while the program runs and gives each instruction row one cause: memory, branch, dependency, front end or busy.

Glossary

  • Memory bound: slow because data arrives too slowly.
  • Compute bound: slow because of arithmetic.
  • Counter: a hardware tally of events, such as cache misses.
  • Bandwidth: how many bytes per second can move.
  • Ablation: removing one part to see how much it mattered.
  • Cycle: one tick of the chip's clock.
  • Kernel: a small program running on a GPU.

Try it

Take a loop that adds up a long list of numbers. Time it. Now change it to add a constant instead of reading from the list. If the time falls a lot, the loop was waiting on the list.

Then try the tools. The wavewait page explains the per-instruction table for AMD GPUs, and the corewait page does the same for desktop and phone CPUs. Our related posts are The Fly Engine Waits on Memory, Not Math and The Counters Said ALU.

What to remember

A chip is either calculating or waiting. A counter points to a bucket. An experiment finds the cause.

Comments

No comments yet.

Log in to comment.