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.

Five questions this page answers
Question
1When does moving a job into hardware pay?
2What are three ways to attach an engine, and who carries the data in each?
3Why can an engine be both a subordinate and a manager?
4Which options does module S9 add to ap_soc?
5With 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

New words on this page
WordMeaning
AcceleratorA block that does one kind of job much faster than the processor. Ours computes a CRC.
OffloadMoving a job from software into an accelerator.
Fixed cost of a callCycles that every use of an accelerator pays, whatever the size of the job: set-up, waiting and reading the result.
Break-even sizeThe smallest job for which the accelerator beats software.
Register-mapped attachmentThe processor writes every input word into a register of the engine, and reads its result back.
Stream port of an engineAn input of the engine that takes one word after another. Here the DMA writes into it.
Engine as a bus managerThe engine reads memory by itself, from an address and a length that software gives it.
DONEA status bit that the engine sets when its job is finished.

One Picture: Who Carries the Words

Three Ways to Attach One CRC EngineStep through the picture. The engine is the same each time. What changes is who carries the words to it.
  • Processor
  • Memory
  • Bus
  • Peripheral
Three Ways to Attach One CRC EngineStep through the picture. The engine is the same each time. What changes is who carries the words to it. component Processor ap_rv32, five-stagebuild component SRAM the buffer of words component DMA reads a word, thenwrites it on component Registers of theengine CTRL (control),DATA, STATUS,RESULT component CRC engine one word in each cycle component Manager port the engine readsSRAM by itself set up, read theresultload each wordDATAreadstream portreadwords
  1. component CRC engineone word in each cycle
  2. component Processorap_rv32, five-stage build
    • set up, read the result → Registers of the engine
  3. component SRAMthe buffer of words
    • load each word → Processor
    • read → DMA
    • read → Manager port
  4. component Registers of the engineCTRL (control), DATA, STATUS, RESULT
    • DATA → CRC engine
  5. component DMAreads a word, then writes it on
    • stream port → CRC engine
  6. component Manager portthe engine reads SRAM by itself
    • words → CRC engine
  1. 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.

  2. 1. Register-mapped

    The processor loads each word from SRAM and stores it into DATA. The engine waits for the processor.

  3. 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.

  4. 3. A bus manager

    The processor writes three registers. The engine then reads SRAM itself through a port of its own.

  5. 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.

Two words, four ways
WayCyclesWhere they go
Software CRC532263 for each word, and 6 more
Register-mapped22The processor loads each word and stores it into DATA: 8 cycles a word, and 6 more
Stream via DMA18Five set-up stores, one poll and the read of RESULT. The DMA is done before the first poll
Bus manager16Three 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

  1. 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.

  2. 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.

  3. 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.

  4. Rule 4: An engine that reads memory is a subordinate and a manager.

    Software programs its registers; then it fetches its data itself.

  5. 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.

  6. 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.

Printed by the lab on Icarus. Every cycle count equals the window that the real ap_rv32 read from mcycle.
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 a2fdb57f

Rule 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 choiceThe alternativeWhat the choice buys, and costs
An engine that reads memory itselfA register-mapped engineThe 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 enableA fixed delay in softwareSoftware learns exactly when the job ends. It needs a poll or an interrupt.
Registers that cannot change while busyRegisters that always take a writeA 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.

The options that module S9 adds
Parameterv1.0What setting it addsWhere it comes from
HARTS = 1 or 21Hart 1, its software-interrupt word at 0x0200_0004, and its reservationThe lab slice of S9.1; the release registers are your answer to S9.1.1
EXCLUSIVE = 0 or 10The exclusive monitor that answers a store-conditional on the busYour answer to S9.2.1
FIREWALL = 0 or 10ap_apb_firewall, the firewall between the bridge and the peripheralsYour answer to S9.5.1
BOOT_CHECK = 0 or 10The boot ROM checks mark, version and tag before the jumpThe check of S9.6; the gate is your answer to S9.6.1
LIFE_CYCLE = 0 or 10Debug and test doors opened by the life-cycle stateYour answer to S9.6.2
CRC_ENGINE = 0 or 10crc_accel: registers on APB, a manager port, an interruptYour 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.

Where the options meet
When both are onCheck
CRC_ENGINE and FIREWALLThe 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 = 2DONE goes to one source of ap_plic, the first spare input, 7. Decide which hart takes it.
BOOT_CHECK and any optionThe image that the boot ROM checks is the code that later programs every option.
HARTS = 2 and EXCLUSIVEThe monitor must watch the reservations of both harts.
CRC_ENGINE and a data cacheThe engine reads SRAM, not the cache. The next lesson shows what that costs.

At Scale

This page1 engine, 1 job
A phone chipimage, video, graphics, neural and security engines

This section looks ahead. Large chips hold many accelerators, and some are as big as the processors.

  1. SIMD

    SIMD (single instruction, multiple data) puts many small operations into one instruction of the core itself. No attachment is needed.

  2. 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.

  3. 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.

  4. 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
NameMeaning
cyclesFrom the first read of mcycle to the second: the set-up, the work and the read of RESULT. Measured on the real core.
processor transfersLoads and stores that the processor makes on the bus.
other transfersTransfers made by the DMA or by the engine itself.
done atThe cycle after START from which DONE reads 1.
pollsPasses of the loop that reads STATUS until DONE is 1. Each pass takes 5 cycles.
software sumA plain sum of the same words in software. Much lighter work than a CRC, shown for comparison.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 a2fdb57f

Check Yourself

Predict the outputIn this run WORDS is 40. What is the last line? Its form is: bus manager | cycles .. | processor transfers .. | other transfers .. | done at .. | polls . | crc 150aac32
// One CRC engine, attached three ways, against the same CRC as a software loop.
// Test code. The hardware (engine, DMA, engine as a bus manager) runs here cycle by cycle. The processor does
// not: its cycles are the constants below, measured on the real five-stage ap_rv32 by a probe.
module tb;
  localparam int WORDS = 40;            // words in the buffer, 1 to 64
  // ---- The processor side: cycles on the five-stage core, measured (no wait cycles anywhere).
  localparam int SW_PER_WORD = 263, SW_FIXED = 6;   // the CRC loop in software
  localparam int SUM_PER_WORD = 7, SUM_FIXED = 4;   // a plain sum: much lighter work
  localparam int RM_PER_WORD = 8, RM_FIXED = 6;     // load a word, store it into DATA
  localparam int MGR_SETUP = 16;    // store SRC, LEN, START; one poll; load RESULT
  localparam int DMA_SETUP = 18;    // store CTRL of the engine, the DMA's SRC, DST, LEN, START; one poll; load RESULT
  localparam int POLL = 5;          // each further pass of the poll loop
  localparam int FIRST_POLL = 6;    // the first poll sees DONE if DONE is high by this cycle after START

  logic [31:0] sram [0:63];                // the buffer in SRAM
  // The engine: XOR a word in, then 32 shift steps. One word in each cycle.
  function automatic logic [31:0] crc_word(input logic [31:0] c, input logic [31:0] w);
    c = c ^ w;
    for (int b = 0; b < 32; b++) c = {c[30:0], 1'b0} ^ (c[31] ? 32'h04c11db7 : 32'h0);
    return c;
  endfunction
  function automatic int polls(input int done_at);   // passes of the poll loop until DONE reads 1
    return (done_at <= FIRST_POLL) ? 1 : 1 + (done_at - FIRST_POLL + POLL - 1) / POLL;
  endfunction

  logic [31:0] by_loop, eng, held;
  int cycle, done_at, other, p;
  initial begin
    for (int i = 0; i < 64; i++) sram[i] = (32'(i + 1) * 32'h9e3779b9) ^ (32'(i) << 7);
    by_loop = 32'hffffffff;
    for (int i = 0; i < WORDS; i++) by_loop = crc_word(by_loop, sram[i]);
    $display("words %0d | software crc %h", WORDS, by_loop);
    $display("software        | cycles %0d | processor transfers %0d | other transfers 0 | crc %h", SW_PER_WORD * WORDS + SW_FIXED, WORDS, by_loop);
    $display("software sum    | cycles %0d | processor transfers %0d | other transfers 0 | lighter work, for comparison", SUM_PER_WORD * WORDS + SUM_FIXED, WORDS);

    // 1. Register-mapped: the processor loads each word and stores it into DATA; the engine takes it at once.
    eng = 32'hffffffff;
    for (int i = 0; i < WORDS; i++) eng = crc_word(eng, sram[i]);
    $display("register-mapped | cycles %0d | processor transfers %0d | other transfers 0 | crc %h", RM_PER_WORD * WORDS + RM_FIXED, 2 * WORDS + 2, eng);

    // 2. Stream: after START the DMA reads a word (odd cycle) and writes it into the engine's stream port (even cycle).
    eng = 32'hffffffff;  other = 0;
    for (cycle = 1; cycle <= 2 * WORDS; cycle++) begin
      if (cycle % 2 == 1) held = sram[(cycle - 1) / 2];      // read: the DMA holds the word
      else eng = crc_word(eng, held);                        // write: the engine takes it
      other++;
    end
    done_at = cycle;                                         // DONE is high from the next cycle on
    p = polls(done_at);
    $display("stream via DMA  | cycles %0d | processor transfers %0d | other transfers %0d | done at %0d | polls %0d | crc %h", DMA_SETUP + POLL * (p - 1), 5 + p + 1, other, done_at, p, eng);

    // 3. Bus manager: after START the engine reads one word in each cycle by itself.
    eng = 32'hffffffff;  other = 0;
    for (cycle = 1; cycle <= WORDS; cycle++) begin
      eng = crc_word(eng, sram[cycle - 1]);
      other++;
    end
    done_at = cycle;
    p = polls(done_at);
    $display("bus manager     | cycles %0d | processor transfers %0d | other transfers %0d | done at %0d | polls %0d | crc %h", MGR_SETUP + POLL * (p - 1), 3 + p + 1, other, done_at, p, eng);
  end
endmodule
ConceptWhen does moving a job into hardware pay?
ConceptIn the register-mapped way, who carries the words to the engine?
ConceptWhy is the engine of S9.8.1 both a subordinate and a manager?
ConceptWhich option of ap_soc does the CRC engine become in the capstone step?

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.

Practice

Report a problem with this page