Skip to content
Tactiverge

School/✦ Research Engineer/Week 22

Hardware or software? Partitioning a task

Moving one function into custom hardware makes it 50 times faster. Why might the whole program only get twice as fast?

About 45 min · 3 goals · Checked by automated tests

1.Why should I care?

Phones contain video encoders, image processors and neural accelerators because some jobs are worth fixing in silicon. Others stay in software because they change, or because they barely affect total time. Deciding which is which, with numbers, is co-design.

  • ◇ Explain what hardware–software co-design is and the costs of moving a function into hardware.
  • ◇ Use Amdahl's law to bound the overall speed-up from accelerating part of a task.
  • ◇ Compare a software loop with a hardware circuit for the same function by operation count and gate delay.

2.Predict

Computing the parity of an 8-bit byte in software takes a loop of about 8 iterations. How many gate delays does a hardware XOR tree need?

Guess first. Being wrong here is how the surprise works.

3.Discover

Build a 4-input parity circuit from XOR gates in the logic lab, first as a chain (three XORs in a row) then as a balanced tree. Check the truth tables match, and count the gate levels from input to output in each.

Explorer 05 — Digital logic

Build answers out of ones and zeros

Every digital chip is built from a handful of simple gates. Flip the inputs to watch signals travel through a circuit, then wire your own gates to meet a target truth table.

ONE GATE · 1 GATE DEEP0A0BAND= 0G10Y
  • 1 (high)
  • 0 (low)
  • Unknown
  • Inverting output
Mode
Starting circuit

A single two-input gate. Change its type and watch the truth table change.

Inputs

Gates (1 of 8)

  1. G1 = 0

Add a gate

Outputs

Gates
1
Depth
1
Longest chain of gates a signal passes through
Rows
4
2^2 input combinations
Check
—
Truth table
Set inputsABY
000
010
100
111

State

Inputs A = 0, B = 0. Outputs Y = 0.

Inputs A = 0, B = 0. Outputs Y = 0.

How to read this

Signals flow left to right. Circles on the left are the inputs, boxes are gates and squares on the right are outputs. A thick bright wire carries a 1, a thin grey wire carries a 0, and each gate shows the value it outputs. A small circle on a gate's output means it inverts (NOT, NAND, NOR, XNOR).

The truth table lists every possible input combination and what the circuit outputs for each, so it describes the circuit completely. Two circuits with the same truth table are equivalent, however differently they are built. That is why NAND alone can make any other gate. Choose a row to set the inputs to it.

Assumptions & limitations
  • Ideal logic: every wire is exactly 0 or 1. Real gates use voltage ranges (for example near 0 V and near 3.3 V or 5 V) and an input between them is undefined.
  • Gates switch instantly here. Real gates take nanoseconds each, so a deeper circuit is slower, and outputs can glitch briefly while signals race through different paths.
  • Only combinational circuits are allowed: no output may feed back into its own input. Feedback is how memory (latches and flip-flops) works, but it needs timing this model does not simulate, so loops are reported as an error.
  • Gates here have one input (NOT) or two inputs. Real chips also have three- and four-input gates, and limits on how many inputs one output can drive (fan-out).
  • Gate counts are a simple measure of size; real designs also weigh speed, power and the number of transistors per gate.
Can't use the interactive? Read a description instead

In the logic lab, a 4-input parity chain is XOR(XOR(XOR(A, B), C), D): 3 gate levels. A balanced tree is XOR(XOR(A, B), XOR(C, D)): 2 levels. Both give 1 when an odd number of inputs are 1.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

80% of a program's run time is moved onto a hardware accelerator that makes that part 2× faster. What is the overall speed-up?

×

5.Understand

What co-design means

Co-design decides together which functions run as software on a processor and which become dedicated hardware (an accelerator, a peripheral, an FPGA block), and designs the interface between them. Software is flexible and cheap to change; hardware is fast and energy-efficient for a fixed job but costs area, design effort and verification.

When hardware wins

Good candidates are hot (a large share of run time), stable (the algorithm won't change), regular (lots of parallel, simple operations, like filtering, encryption or matrix multiply) and data-local (little traffic in and out). Poor candidates are rarely run, branchy or still evolving.

Count before you build

Profile first: measure where time goes. Use Amdahl's law to bound the gain. Then estimate the hardware: gates or multipliers needed, its latency in gate delays or cycles, and the data movement cost. The parity example shows the typical shape: software Θ(n) instructions, hardware Θ(log n) gate delays but Θ(n) gates.

Explain it another way

A kitchen buys a dough mixer because bread takes most of the morning, not a special egg-cracker for the one omelette a day.

Derivation: Amdahl's law
  1. Let the original run time be 1, and let a fraction p of it be the part you accelerate.

  2. The accelerated part becomes s times faster, so it takes p/s. The rest still takes 1 − p.

  3. New time = (1 − p) + p/s, so overall speed-up S = 1 / ((1 − p) + p/s).

  4. As s → ∞, S → 1/(1 − p). If only 50% of the time is accelerated, no accelerator can beat 2×.

  5. Example: p = 0.8, s = 50: S = 1/(0.2 + 0.016) ≈ 4.6×. Doubling s to 100 only gives 4.8×: optimise the other 20% next.

  6. Add the cost of moving data to and from the accelerator to the new time and the speed-up falls further.

Worked example 1

A rover's image pipeline spends 70% of its time in a 3×3 convolution. An accelerator makes convolution 30× faster but adds data copies costing 5% of the original time. Overall speed-up?

New time = 0.3 + 0.7/30 + 0.05 = 0.3 + 0.023 + 0.05 = 0.373. S ≈ 2.68×. Without the copies it would be 3.1×; the limit is 1/0.3 ≈ 3.3×.

Common mix-ups

  • Making the slowest function 100× faster makes the program nearly 100× faster.
    ✓ Only the accelerated fraction shrinks; the rest sets a hard ceiling of 1/(1 − p).
  • Hardware is always better than software.
    ✓ Hardware is faster for fixed, regular work but costs area, energy for idle logic, design time and flexibility.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

50% of a program's run time is moved onto a hardware accelerator that makes that part 20× faster. What is the overall speed-up?

×

Practice 3

Which function is the best candidate for a hardware accelerator in a drone?

Practice 4

How many levels of 2-input XOR gates does a balanced parity tree need for a 32-bit word?

levels

Practice 5

A task is 40% accelerable. What is the best overall speed-up any accelerator can give?

7.Apply it

Pick one subsystem from a robot you know (sensing, estimation, control, communication). Estimate (with stated assumptions) the fraction of CPU time each function takes, then argue which single function you would move into hardware, the Amdahl bound on the gain, and what interface it would need.

8.Reflect

Concepts: Pipelining, hazards and the Iron Law · Logic gates and Boolean algebra · Constraints, budgets and trade-offs · Real-time computing on robots