Lite / Independent Engineering Research / 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?
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.
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?
- 8
- 3
- 1
- 64
Guess first, then reveal
A balanced tree of 2-input XORs halves the signals each level: 8 → 4 → 2 → 1, so 3 levels (log₂ 8). Hardware turns a linear loop into logarithmic depth, at the cost of 7 gates of fixed silicon.
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.
What the interactive shows (text version)
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.
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
Intuitive
A kitchen buys a dough mixer because bread takes most of the morning, not a special egg-cracker for the one omelette a day.
Technical
Partitioning is an optimisation over latency, throughput, energy, area and flexibility, subject to interface bandwidth. Typical interfaces are memory-mapped registers, DMA and shared buffers. Hardware–software interfaces must be specified precisely so both sides can be verified independently.
Mathematical
Amdahl: S = 1/((1 − p) + p/s + c), where c is the communication overhead as a fraction of the original time. With p = 0.6, s = 20, c = 0.05: S = 1/(0.4 + 0.03 + 0.05) ≈ 2.08×.
Applied
Microcontrollers include hardware CRC units, timers, PWM generators and DMA because those tasks are simple, constant and frequent. The robot's control logic stays in software, where it can be retuned.
Derivation: Amdahl's law
Let the original run time be 1, and let a fraction p of it be the part you accelerate.
The accelerated part becomes s times faster, so it takes p/s. The rest still takes 1 − p.
New time = (1 − p) + p/s, so overall speed-up S = 1 / ((1 − p) + p/s).
As s → ∞, S → 1/(1 − p). If only 50% of the time is accelerated, no accelerator can beat 2×.
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.
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.
Reflect
What does your software do that you would be happy to freeze into silicon forever, and what would you never freeze?
Write your answer on paper or in your own notes. Lite never stores what you write.