Accelerators, and the Capstone Step
An accelerator does one job much faster than the processor. It pays only if its set-up is cheap and something other than the processor carries the data. Module S9 then lands in ap_soc as a set of options.
You Will Be Able To
- Say when moving work into hardware pays, from the work per item and the fixed cost of a call.
- Compare three ways to attach one engine by who carries the data, and count the cycles and transfers of each.
- List the options that module S9 adds to ap_soc, and the checks when they meet.
- Lesson
- S9.8
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
A CRC over 16 words takes the processor 4214 cycles. A small engine does a word in one cycle.
Yet one way of attaching it needs 134 cycles, and another needs 31. Where did the time go?
Before You Start
You need DMA: a block that moves data while the processor does other work. It holds each item between a read and a write.
You need Many Managers: a bus can have several managers, and a bus matrix lets them reach different subordinates at once.
You need Benchmarks: a window between two reads of mcycle on the five-stage core.
It helps to have read Hardware or Software and the lessons of this module on harts, walls and boot.
| Question | |
|---|---|
| 1 | When does moving a job into hardware pay? |
| 2 | What are three ways to attach an engine, and who carries the data in each? |
| 3 | Why can an engine be both a subordinate and a manager? |
| 4 | Which options does module S9 add to ap_soc? |
| 5 | With 40 words, what does the bus manager way print? |
The Idea: Move the Work, and the Data with It
Some jobs are slow in software and simple in hardware. A CRC is one: 32 shift steps for each word. The processor runs them one instruction at a time. A small engine does all 32 in one clock cycle.
Moving such a job into hardware is called offload. The block that takes it is an accelerator. It is never free. Software must set it up, wait until it is done, and read its result. That fixed cost comes with every call.
There is a second cost. The words must reach the engine. If the processor carries each word, it is still busy for each word. If a DMA or the engine itself carries them, the processor is free.
So the engine is the same in every attachment, and the cost is not. This page measures three attachments. Then it lists what module S9 adds to the chip.
New Words
| Word | Meaning |
|---|---|
| Accelerator | A block that does one kind of job much faster than the processor. Ours computes a CRC. |
| Offload | Moving a job from software into an accelerator. |
| Fixed cost of a call | Cycles that every use of an accelerator pays, whatever the size of the job: set-up, waiting and reading the result. |
| Break-even size | The smallest job for which the accelerator beats software. |
| Register-mapped attachment | The processor writes every input word into a register of the engine, and reads its result back. |
| Stream port of an engine | An input of the engine that takes one word after another. Here the DMA writes into it. |
| Engine as a bus manager | The engine reads memory by itself, from an address and a length that software gives it. |
| DONE | A status bit that the engine sets when its job is finished. |
One Picture: Who Carries the Words
- Processor
- Memory
- Bus
- Peripheral
- The engine
A small block that does one job fast: one word of the CRC in each cycle. Software needs 263 cycles for the same word.
- 1. Register-mapped
The processor loads each word from SRAM and stores it into DATA. The engine waits for the processor.
- 2. A stream fed by the DMA
The processor sets up the DMA and the engine. The DMA reads each word and writes it into the stream port.
- 3. A bus manager
The processor writes three registers. The engine then reads SRAM itself through a port of its own.
- The same end
In every way the processor waits for DONE and reads RESULT. All three give the same CRC.
Read the diagram as text
- Processor: ap_rv32, five-stage build
- SRAM: the buffer of words
- DMA: reads a word, then writes it on
- Registers of the engine: CTRL (control), DATA, STATUS, RESULT
- CRC engine: one word in each cycle
- Manager port: the engine reads SRAM by itself
- Processor → Registers of the engine: set up, read the result
- SRAM → Processor: load each word
- Registers of the engine → CRC engine: DATA
- SRAM → DMA: read
- DMA → CRC engine: stream port
- SRAM → Manager port: read
- Manager port → CRC engine: words
The three attachments of the lab. Registers on the fast bus with no wait cycles are a choice of this lab.
The Smallest Example, Worked by Hand
Take a buffer of two words. All cycles below were measured on the real five-stage core, between two reads of mcycle.
In software, each word needs a load and the XOR. Then come 32 rounds of six instructions, and three to close the loop: 198 instructions. Thirty-two taken branches add 64 cycles, and the load-use adds 1. That is 263 cycles a word.
| Way | Cycles | Where they go |
|---|---|---|
| Software CRC | 532 | 263 for each word, and 6 more |
| Register-mapped | 22 | The processor loads each word and stores it into DATA: 8 cycles a word, and 6 more |
| Stream via DMA | 18 | Five set-up stores, one poll and the read of RESULT. The DMA is done before the first poll |
| Bus manager | 16 | Three set-up stores, one poll and the read of RESULT |
With two words the fixed costs decide. Now look at lighter work. A plain sum of two words takes software only 18 cycles. The bus manager would win by just 2 cycles. The register-mapped way, at 8 cycles a word, never beats a sum at 7.
The Rules
Rule 1: Offload pays when the work for each item is large.
The CRC costs software 263 cycles a word and the engine 1. A sum costs software 7.
Rule 2: Every call pays a fixed cost first.
Set-up, waiting for DONE and reading RESULT: 16 cycles for the bus manager here. Small jobs may not cover it.
Rule 3: Whoever carries the data pays for it.
Register-mapped: the processor, 8 cycles a word. Stream: the DMA, 2 bus transfers a word. Manager: the engine, 1.
Rule 4: An engine that reads memory is a subordinate and a manager.
Software programs its registers; then it fetches its data itself.
Rule 5: The processor is free only if something else carries the data.
While it waits it can do other work, if DONE raises an interrupt instead of being polled.
Rule 6: Offload speeds up only the part it takes.
The rest of the program runs as before. That limits the gain for the whole program.
Sources for These Rules
Rule 6 is Amdahl's law: Patterson and Hennessy, Computer Organization and Design, ARM edition, section 1.10. See also Performance: CPU Time and Amdahl's Law.
A DMA component is a subordinate while it is programmed and a manager while it moves data: Arm IHI 0022H.c, Glossary. Our engine follows the same pattern.
Rules 1 to 3 and 5 are stated from the measured runs of this page. The words offload and fixed cost of a call are common practice.
The CRC rule, the registers, the DMA timing and the engine timing are choices of this lab. The processor cycles were measured on the real ap_rv32.
Now Run It
The lab at the end of this page runs the three attachments on a buffer of 16 words. The engine, the DMA and the manager port run cycle by cycle. The processor cycles come from runs of the same programs on the real core.
words 16 | software crc a2fdb57f
software | cycles 4214 | processor transfers 16 | other transfers 0 | crc a2fdb57f
software sum | cycles 116 | processor transfers 16 | other transfers 0 | lighter work, for comparison
register-mapped | cycles 134 | processor transfers 34 | other transfers 0 | crc a2fdb57f
stream via DMA | cycles 48 | processor transfers 13 | other transfers 32 | done at 33 | polls 7 | crc a2fdb57f
bus manager | cycles 31 | processor transfers 8 | other transfers 16 | done at 17 | polls 4 | crc a2fdb57fRule 1 in the first two rows of work: 4214 cycles in software, and 31 with the engine as a manager. That is 135 times faster.
Rule 3 in the transfer counts. Register-mapped: the processor makes 34 transfers, two for each word, a CTRL store and the RESULT load. Stream: the DMA makes 32. Manager: the engine makes 16, and the processor 8.
Rule 2 in the polls. The manager is done at cycle 17, so the processor polls 4 times. The DMA needs two bus cycles a word and is done at 33: 7 polls.
Rule 5, last. In the stream and manager rows the processor spends most cycles polling. With an interrupt on DONE, those cycles are free for other work.
This lab leaves things out. The engine reads SRAM through its own port with no waiting. In ap_soc it would share the bus matrix, as in S4.4.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| An engine that reads memory itself | A register-mapped engine | The processor is free and the bus carries each word once. The engine needs a manager port, and it is a new route to memory. |
| DONE as a status bit, with an interrupt enable | A fixed delay in software | Software learns exactly when the job ends. It needs a poll or an interrupt. |
| Registers that cannot change while busy | Registers that always take a write | A run cannot be broken halfway by a stray write. Software must wait for DONE before the next set-up. |
The Trap: An Engine That Made Things Slower
A team added an engine for a checksum that was a plain sum. They attached it register-mapped, because that was quickest to build. The firmware got slower.
The lab shows why. The register-mapped way costs 8 cycles a word: the processor still loads every word, and now stores it too. The sum in software cost 7. The engine did the easy part, and the processor kept the hard part.
The second trap is a tiny job. The bus manager needs 16 cycles before any result, even for one word. A job of one sum word is done in software in 11.
The third trap is forgetting rule 6. If the CRC is a tenth of the work, even an infinitely fast engine saves at most a tenth.
Capstone Step: The Options of ap_soc After Module S9
This is the capstone step of the module. Each part that module S9 built becomes an option of ap_soc: a parameter with a default. ap_soc v1.0 keeps every option off, as section 14 of its facts says.
| Parameter | v1.0 | What setting it adds | Where it comes from |
|---|---|---|---|
| HARTS = 1 or 2 | 1 | Hart 1, its software-interrupt word at 0x0200_0004, and its reservation | The lab slice of S9.1; the release registers are your answer to S9.1.1 |
| EXCLUSIVE = 0 or 1 | 0 | The exclusive monitor that answers a store-conditional on the bus | Your answer to S9.2.1 |
| FIREWALL = 0 or 1 | 0 | ap_apb_firewall, the firewall between the bridge and the peripherals | Your answer to S9.5.1 |
| BOOT_CHECK = 0 or 1 | 0 | The boot ROM checks mark, version and tag before the jump | The check of S9.6; the gate is your answer to S9.6.1 |
| LIFE_CYCLE = 0 or 1 | 0 | Debug and test doors opened by the life-cycle state | Your answer to S9.6.2 |
| CRC_ENGINE = 0 or 1 | 0 | crc_accel: registers on APB, a manager port, an interrupt | Your answer to S9.8.1 |
Five of the six blocks are graded answers, so no lab on this site holds them. A parameter that is 0 leaves ap_soc exactly as v1.0.
Options are cheap to list and costly to combine. Before a chip turns on two of them, check where they meet.
| When both are on | Check |
|---|---|
| CRC_ENGINE and FIREWALL | The engine is a new manager, so it is a new route (S9.5). Give its reads the attributes of the least privileged user that may start it. |
| CRC_ENGINE and HARTS = 2 | DONE goes to one source of ap_plic, the first spare input, 7. Decide which hart takes it. |
| BOOT_CHECK and any option | The image that the boot ROM checks is the code that later programs every option. |
| HARTS = 2 and EXCLUSIVE | The monitor must watch the reservations of both harts. |
| CRC_ENGINE and a data cache | The engine reads SRAM, not the cache. The next lesson shows what that costs. |
At Scale
This section looks ahead. Large chips hold many accelerators, and some are as big as the processors.
SIMD
SIMD (single instruction, multiple data) puts many small operations into one instruction of the core itself. No attachment is needed.
GPU
A GPU (graphics processing unit) runs many threads at once on many simple cores. It is a manager with heavy memory traffic of its own.
Neural engines
An NPU (neural processing unit) does matrix work for neural networks. It is common practice on phone chips; no source in our library covers it.
Many engines, one memory
Each engine adds traffic and a route to memory. See Heterogeneous Compute and I/O Memory Management.
The next lesson, An Engine That Shares Memory, adds a data cache to the processor. Then the engine can read old data.
Try It
Run the lab. Which way is fastest, and how many transfers does the processor make in it?
Set WORDS to 1, then to 64. Watch the bus manager: when does its cycle count stop being flat?
Compare "software sum" with "register-mapped" for any number of words. Can feeding the engine word by word ever win against a sum?
Names Printed by the Lab
| Name | Meaning |
|---|---|
| cycles | From the first read of mcycle to the second: the set-up, the work and the read of RESULT. Measured on the real core. |
| processor transfers | Loads and stores that the processor makes on the bus. |
| other transfers | Transfers made by the DMA or by the engine itself. |
| done at | The cycle after START from which DONE reads 1. |
| polls | Passes of the loop that reads STATUS until DONE is 1. Each pass takes 5 cycles. |
| software sum | A plain sum of the same words in software. Much lighter work than a CRC, shown for comparison. |
words 16 | software crc a2fdb57f software | cycles 4214 | processor transfers 16 | other transfers 0 | crc a2fdb57f software sum | cycles 116 | processor transfers 16 | other transfers 0 | lighter work, for comparison register-mapped | cycles 134 | processor transfers 34 | other transfers 0 | crc a2fdb57f stream via DMA | cycles 48 | processor transfers 13 | other transfers 32 | done at 33 | polls 7 | crc a2fdb57f bus manager | cycles 31 | processor transfers 8 | other transfers 16 | done at 17 | polls 4 | crc a2fdb57fCheck Yourself
Common Mistakes
Wrong Attach an engine register-mapped and expect the processor to be free.
The processor still carries every word to the engine.
Fix Let a DMA or the engine itself fetch the data.
Wrong Judge an engine by its speed for each item alone.
Every call pays a fixed cost first.
Fix Find the break-even size, and compare it with the real job sizes.
Wrong Offload light work, such as a sum.
Moving each word can cost more than the work itself.
Fix Offload work that is heavy for each item, or move the data in hardware.
Remember
- Offload pays when the work for each item is large and the job is bigger than the break-even size.
- Every call pays a fixed cost: set-up, waiting and reading the result.
- Whoever carries the data pays for it. A bus manager carries its own.
- Module S9 lands in ap_soc as options that are off by default. Check where they meet.