Checking how many agents are working

How this works
Contatti
← Blog
A small matte grey resin spider on a brushed steel plate beside a tall tower of dense steel blocks, in teal and steel with a warm amber rim light, the AMARBARO mark and wordmark centered over it.
✎post del blog

Simulating a spider: when the CPU beats the GPU

The short version

One simulated spider ran 57 times faster on the CPU than on the GPU, 0.62 against 35.2 milliseconds a frame, and the GPU won only at 1,024 animals.

What this is about

We simulated a spider and timed it on two chips: the processor in the computer, the CPU, and the processor on the graphics card, the GPU. For one spider, the CPU was far faster. One frame took 35.2 milliseconds on the GPU and 0.62 milliseconds on the CPU. That is about 57 times faster on the CPU.

But the result flips for a crowd. With 1,024 animals, the GPU wins. This article explains why, in plain words, and what it means for anything you want to run fast.

Why it matters

It is easy to believe that a graphics card is always faster. It is built for speed, and people buy it for that reason. But a graphics card is built for large, repeated jobs. For one small job, the cost of starting it can be bigger than the job itself.

Knowing this saves effort. If you only simulate one animal, you do not need the GPU, and you get a faster result without it. If you simulate thousands, you do.

A picture of the difference

Think of a CPU as one very quick chef. Think of a GPU as a huge kitchen with thousands of cooks, each of whom can do only simple steps.

  • Make one omelette. The quick chef finishes before the big kitchen has handed out its tasks.
  • Make a thousand omelettes. The big kitchen starts everything at once and wins by a mile.

Our spider is the one omelette. The crowd is the thousand.

How we measured it

Here are the steps we took, so you can reuse them for any speed question.

  1. Fix the job. We ran one spider, one frame at a time, at a screen size of 640 by 360 pixels. A frame is one update of the simulation. At 60 frames per second, each frame has about 16.7 milliseconds to fit in.
  2. Time both chips. Same build, same inputs, same frames.
  3. Check they agree. The test compares the results of the two chips, so that a faster answer is not simply a wrong one.
  4. Change the size. Run again for 1, 16, 64, 256 and 1,024 animals.
  5. Read the shape. Look at the cost per animal as the crowd grows.

One assumption to flag: our log records "K=10", and our reading is that it means ten physics steps per frame. The log does not say so outright, so we treat it as an assumption.

What the numbers say

Cost per animal and per step, in microseconds, for three ways of running:

Animals CPU (host) GPU, wave GPU, thread
1 54 3,553 4,912
16 37 232 330
64 12 62 90
256 10 21 24
1,024 43 9.9 7.1

Two things stand out.

The GPU gets cheaper per animal as the crowd grows. At one animal it costs 3,553 microseconds. At 1,024 it costs 9.9. The shape looks like a large fixed cost that more animals spread out, like the price of switching on a big kitchen shared among more and more meals.

The CPU jumps up at the end. It falls to 10 at 256 animals and climbs back to 43 at 1,024. Our log ties that jump to the 96 MB L3 cache, the fast memory on the processor. When the crowd's data stops fitting in it, the CPU slows down.

At 256 animals, the CPU is still ahead: 10 against 21. At 1,024, the GPU is ahead: 9.9 against 43. The crossing happens somewhere between those two sizes.

Where we draw the line

Our simulation program picks the chip on its own. It uses the CPU up to 256 animals, one kind of GPU run below 1,024, and another from 1,024 up. Eyes force the GPU, because the eye rendering cannot run on the CPU build.

An honest note about that line. The 256 threshold is a pick from five sweep points. It is not a fitted crossover, and we did not measure any size between 256 and 1,024. So the true switch point is somewhere in that range. We also tried splitting the work between the CPU and the GPU, and rejected it. Our log says the GPU frame time stayed flat below 1,024.

This also agrees with our body engine's own benchmark. For an actuated fly, 65,536 bodies ran 11.0 times faster than six CPU workers, 16,384 bodies ran 6.0 times faster, and 1,024 bodies ran at 0.65 times, so the CPU won. See what MuJoCo is for the engine behind it.

What this means for a viewer

At 35.2 ms per frame, one spider's GPU physics would allow about 28 frames per second. At 0.62 ms, the CPU physics leave almost all of a 16.7 ms frame free. For a viewer that watches one spider, the CPU is the better choice. For a lab that runs a thousand animals at once, the GPU is.

A bug we found on the way

We found a real bug while timing. On the CPU build, the spider stood still. The CPU build never saw the controls from the device brain. We fixed it in the same commit, and the test now compares the GPU and CPU root positions to within 1 percent of travel. A fast result that does the wrong thing is not a result. That is why the check comes first.

Glossary

  • CPU: the main processor in a computer. Fast at one task at a time.
  • GPU: the processor on a graphics card. Slow to start, but fast at the same task for many items.
  • GPU wave and GPU thread: two ways our code can spread the work over the graphics card.
  • Frame: one update of the simulation.
  • Cache: small, very fast memory close to the processor.
  • Crossover: the size at which one method becomes faster than another.
  • Benchmark: a timing test run the same way for every candidate.

Try it

Previous in the series: Learning inside a simulated brain. Next: The checks behind every claim about our fly.

Commenti

Ancora nessun commento.

Accedi per commentare.