Heterogeneous Compute: From Sensor to Display

A large chip gives the same steps on much data to an engine built for them. The engine has a cost of its own, the engines share one memory, and every buffer has one owner at a time.

You Will Be Able To

  • Say when a job belongs on an engine, by the whole cost from the request to the result.
  • Count the bytes of a frame at the memory port, in the format of each crossing.
  • Say who owns a buffer at each moment, and when a writer may use it again.
Lesson
S10.6
Time
25 min
Runs on
Your browser
Check
5 questions

You wave at the camera, and on the screen your hand stops halfway. The cores of the phone are almost idle.

The frame was never theirs to draw. Another block had that job, and it was waiting for something.

Before You Start

You need From Microcontroller to Application Processor: ap_phone, its camera, and the compute engine.

You need High-Speed I/O Subsystems: registers for the core, data written to memory by DMA, and one interrupt at the end.

You need A Bus in Six Wires: the handshake of valid and ready.

It helps to know The Memory Subsystem: the memory of ap_phone carries 4,800,000,000 useful bytes each second.

Five questions this page answers
Question
1When does a job belong on an engine and not on a core?
2An engine does a job in 5 microseconds, after 400 for waking and hand-off. A core takes 50. Who should do it?
3A frame is written to memory once and read once. How many bytes cross the memory port each second?
4When may a writer use a buffer again?
5The camera runs at 60 frames a second. What does the lab print last?

The Idea: Give One Pixel a Job

Pick one grey pixel on your sleeve and follow it. The camera sends 2 bytes for it: one for brightness and one for colour. A grey preview needs only the brightness. It must go into three fields of a display pixel: red, green and blue.

The next pixel needs exactly the same steps. So do all 2,073,600 pixels of the frame, 30 times each second. A core would fetch and decode the same few instructions for every one of them.

So the chip gives the job to a circuit that does only this. Wires carry the brightness bits straight into the three fields. Such a block is a compute engine. A chip with cores and several kinds of engine is called heterogeneous.

An engine is not free. It must be woken, the job must be handed over, and somebody must be told at the end. For a large job that cost is small. For a small job it can be the whole bill.

The engines also share one memory. Every write of a frame and every read of it counts against one budget. And the block that writes a buffer must agree with the block that reads it on the moment it changes hands.

New Words

New words on this page
WordMeaning
Heterogeneous chipA chip with cores and several kinds of compute engine, each built for one kind of job.
ISPImage signal processor: the engine for camera pixels. It corrects and converts them.
GPUGraphics processing unit: the engine that draws. It applies the same steps to very many pixels.
NPUNeural processing unit: the engine for the sums of a trained model, such as finding a face in a picture.
DSPDigital signal processor: the engine for streams of samples, such as sound.
Display engineThe block that reads a finished frame from memory and sends it to the screen.
Display pixelOne pixel in the format of the screen. Here it is 2 bytes: 5 bits of red, 6 of green and 5 of blue.
Wake timeThe time until a sleeping engine can work.
Hand-off timeThe time to pass a job and its data to an engine, and to learn that it has finished.
Frame slotThe time for one frame. At 30 frames a second it is 33,333 microseconds.
Memory crossingOne passage of a frame through the memory port: one write of it, or one read of it.
Raw frameThe frame as the camera sent it, before any engine changed it.
Owner of a bufferThe one block that may use a buffer at this moment.
Visible writeA write is visible to a reader once a read of that address returns the new value. A write that still waits in a queue is not.

One Picture: One Frame, from the Camera to the Screen

One Frame, from the Camera to the ScreenStep through the picture. It starts with one pixel and one engine, then adds memory, the screen and the core.
  • External
  • Peripheral
  • Memory
  • Processor
One Frame, From the Camera to the ScreenStep through the picture. It starts with one pixel and one engine, then adds memory, the screen and the core. component Camera 2 bytes for eachpixel: brightness andcolour component ISP the engine for camerapixels component Frame buffer one finished frame inmemory: 4,147,200bytes component Core starts the job and istold the end component Display engine reads the finishedframe component Screen outside the chip camera pixelswritereaddisplay pixelsregisters
  1. component Camera2 bytes for each pixel: brightness and colour
    • camera pixels → ISP
  2. component ISPthe engine for camera pixels
    • write → Frame buffer
  3. component Frame bufferone finished frame in memory: 4,147,200 bytes
    • read → Display engine
  4. component Display enginereads the finished frame
    • display pixels → Screen
  5. component Screenoutside the chip
  6. component Corestarts the job and is told the end
    • registers → ISP
  1. One pixel, one job

    The camera sends 2 bytes for a pixel. The ISP takes the brightness byte and makes one display pixel of it. No instruction is fetched.

  2. The first crossing

    The ISP writes the finished frame to memory by itself. At 2 bytes a pixel that is 124,416,000 bytes each second.

  3. The second crossing

    The display engine reads the same bytes back, once for each frame. The memory port now carries the frame twice.

  4. The part of the core

    The core writes a few registers of the ISP to start a frame, and an interrupt tells it the end. It never touches a pixel.

  5. Who owns the buffer

    While the ISP writes, the buffer is its own. After the last write it passes to the display engine. Only one of them may use it at a time.

Read the diagram as text
  • Camera: 2 bytes for each pixel: brightness and colour
  • ISP: the engine for camera pixels
  • Frame buffer: one finished frame in memory: 4,147,200 bytes
  • Core: starts the job and is told the end
  • Display engine: reads the finished frame
  • Screen: outside the chip
  • Camera → ISP: camera pixels
  • ISP → Frame buffer: write
  • Frame buffer → Display engine: read
  • Display engine → Screen: display pixels
  • Core → ISP: registers

The preview path of ap_phone, at box level. The GPU, the NPU and the DSP are attached to the core and to memory in the same way.

The Smallest Example, Worked by Hand

No simulator yet. First, who should do a job. Add the three parts of the cost, in microseconds: millionths of a second. The times are exercise values.

Two jobs, and who does them
JobDone byWakeHand-offWorkTotal
One frame to greyA core0020,00020,000
One frame to greyThe ISP3001002,0002,400
One small icon to greyA core005050
One small icon to greyThe ISP3001005405

A frame slot is 33,333 microseconds. A core would spend more than half of every slot on the frame. The ISP needs 2,400. For the icon the answer turns round: the engine works ten times faster and still finishes eight times later.

Second, the bytes. The frame has 2,073,600 pixels, so 62,208,000 pixels pass each second. Count each crossing of the memory port in its own format.

One second at the memory port
CrossingFormatBytes each second
The ISP writes the preview2 bytes for a display pixel124,416,000
The display engine reads it2 bytes for a display pixel124,416,000
Total248,832,000

Inside the ISP the brightness is a stream of 1 byte for each pixel, 62,208,000 bytes each second. It never touches memory, so it is not in the table.

The Rules

  1. Rule 1: A job of the same steps on much data belongs on an engine.

    The engine does the steps with wires. A core fetches instructions for every item.

  2. Rule 2: Compare whole costs: wake time, hand-off time and work.

    An engine pays only when the time it saves is more than the time it takes to reach it.

  3. Rule 3: An engine is attached like a fast port.

    The core writes its registers. The engine moves its data by itself. One interrupt ends the job.

  4. Rule 4: Count every memory crossing, in the format of that crossing.

    A read costs as much as a write. A stream that goes from block to block costs nothing here.

  5. Rule 5: A buffer has one owner at a time, and it changes hands only after the last write is visible.

    The interrupt of the writer is the earliest moment. The reader must not start before it.

  6. Rule 6: A writer may use a buffer again only after the last reader has released it.

    Until then the reader may still be in the middle of the old frame.

Sources for These Rules

Engines for the same steps on much data, and a memory built for bytes each second: Patterson and Hennessy, Computer Organization and Design, ARM edition, section 6.6.

Rules 2, 5 and 6 are common practice of drivers for cameras and displays. No document for them is in the library of this site.

Rule 3 repeats the three paths of the lesson on fast ports. Rule 4 is arithmetic.

Now Run It

The lab at the end of this page prints the two hand tables. Between them it sends the grey pixel of your sleeve, brightness 84 in hex, through a small stage of the ISP.

Printed by the lab on Icarus. Four rows of costs, two rows of one pixel, four rows of bytes.
job=frame on=core wake_us=0 handoff_us=0 work_us=20000 total_us=20000
job=frame on=isp wake_us=300 handoff_us=100 work_us=2000 total_us=2400
job=icon on=core wake_us=0 handoff_us=0 work_us=50 total_us=50
job=icon on=isp wake_us=300 handoff_us=100 work_us=5 total_us=405
hold valid=1 ready=0 rgb=8430
take valid=1 ready=1 rgb=8430
stream B/s camera=124416000 brightness=62208000 preview=124416000
memory B/s write=124416000 read=124416000 total=248832000
raw_spill B/s total=497664000
frame bytes=4147200

Rule 2 at work, in the first four rows. For the frame the ISP wins with total_us=2400. For the icon it loses with total_us=405 against 50.

Rule 5 in small, in the hold row. The stage has made the display pixel 8430 and says valid=1. The next block says ready=0, so the pixel waits, unchanged. In the take row ready is 1, and the same 8430 moves on. Nothing was lost, and nothing was written twice.

Rule 4, in the stream row and the memory row. The brightness stream of 62208000 stays inside the ISP. The preview is written once and read once: total=248832000. The raw_spill row is the quick check: two more crossings, 497664000.

This lab leaves things out. It hands over one pixel, and a real driver hands over whole buffers. It has no memory controller, so no write ever waits in a queue. Rules 5 and 6 at the size of a frame are not run here.

Why It Is Built This Way

ChoiceThe alternativeIts cost
Engines beside the coresMore coresA core fetches and decodes for every pixel. The frame job alone would take more than half of one core.
One memory for all enginesA private memory for each engineEvery hand-over would be a copy of 4,147,200 bytes. Shared memory passes an address.
A stream from block to blockEvery step through memoryEach step through memory is a write and a read. The raw spill doubles the load for the same picture.

The second row has a price. Shared memory is why a buffer needs an owner. With private memories nobody could read a frame that is half written.

The Trap: A Sleeve from Two Frames

The ISP finishes frame 2 and its interrupt arrives. The driver starts frame 3 in the same buffer at once. The display engine is still halfway down frame 2. The top of your sleeve comes from one frame and the cuff from the next.

Rule 6 was broken: the reader had not released the buffer. The usual cure is a second buffer, so that the two blocks swap.

The second trap is the idle core. The picture freezes and every core is idle, so the cores are blamed for nothing. Look at the engine: it was asleep, and the frame waited for its wake time.

The third trap counts only what is on the screen. The display reads 124,416,000 bytes each second. The memory port carried twice that, or four times with the raw spill.

At Scale

This lab1 pixel, 1 stage
ap_phone4 engines and a display engine beside 8 cores

This section looks ahead, to many engines at once and to the cost of waking them.

  1. One budget for all

    The preview takes 248,832,000 of 4,800,000,000 useful bytes each second, about a twentieth. A face detector on the NPU and a game on the GPU draw on the same budget.

  2. Three engines for one picture

    To draw a box round a face, the ISP converts, the NPU finds the face and the GPU draws the box. Each hand-over is a buffer that changes its owner.

  3. Asleep to save power

    An idle engine is switched off, and that is where its wake time comes from. The system must wake it before the frame that needs it.

  4. A late frame

    Mark one frame and follow it from the camera to the screen. Measure each wait: for power, for a place in a queue, for a buffer. An engine that works faster does not shorten a wait.

The cost of waking continues in Power Domains at Scale.

Try It

Run the lab. Find the two rows of the frame job, and read which engine is faster and by how much.

Change the wake time of the ISP in the icon row to 0. Predict the new total, and say who should do the icon now.

Change FPS to 60. Predict which rows double and which stay.

Change the brightness byte to ff. Follow the value through the hold row and the take row.

Names Printed by the Lab
NameMeaning
job, onThe job, and the block that does it: a core or the ISP.
wake_us, handoff_us, work_usThe three parts of the cost, in microseconds. The lab writes us.
total_usTheir sum: the time from the request to the result.
hold, takeThe stage holds one finished pixel. In the take row the next block accepts it.
valid, readyThe handshake between the stage and the next block. A pixel moves on when both are 1 at a clock edge.
rgbThe display pixel, in hex: 5 bits of red, 6 of green, 5 of blue.
B/sBytes each second.
camera, brightness, previewThe stream from the camera, the stream of brightness bytes inside the ISP, and the stream of display pixels.
write, read, totalWhat crosses the memory port: the ISP writes the preview, and the display engine reads it.
raw_spillThe total when the frame of the camera is also stored in memory and read back.
frame bytesThe size of one finished frame.
Sources and What the Lab Leaves Out

The stage is a small circuit, preview_gray. The rows of costs and of bytes are sums of test code. The lab has no GPU, no memory controller and no screen.

Every number is an exercise value of ap_phone. That holds for the camera, for the times of the core and the ISP, and for the display pixel.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: job=frame on=core wake_us=0 handoff_us=0 work_us=20000 total_us=20000 job=frame on=isp wake_us=300 handoff_us=100 work_us=2000 total_us=2400 job=icon on=core wake_us=0 handoff_us=0 work_us=50 total_us=50 job=icon on=isp wake_us=300 handoff_us=100 work_us=5 total_us=405 hold valid=1 ready=0 rgb=8430 take valid=1 ready=1 rgb=8430 stream B/s camera=124416000 brightness=62208000 preview=124416000 memory B/s write=124416000 read=124416000 total=248832000 raw_spill B/s total=497664000 frame bytes=4147200
Read included file: preview_gray.sv
// Teaching fixture for the stream attachment. One unsigned luma sample per beat.
// The registered output holds its pixel while the consumer applies backpressure.
module preview_gray (
  input  logic        clk, reset_n,
  input  logic        in_valid,
  output logic        in_ready,
  input  logic [7:0]  luma,
  output logic        out_valid,
  input  logic        out_ready,
  output logic [15:0] rgb565
);
  assign in_ready = !out_valid || out_ready;
  always_ff @(posedge clk) begin
    if (!reset_n) begin
      out_valid <= 0;
      rgb565 <= 0;
    end else if (in_ready) begin
      out_valid <= in_valid;
      if (in_valid)
        rgb565 <= {luma[7:3], luma[7:2], luma[7:3]};
    end
  end
endmodule

Check Yourself

Predict the outputIn this run the camera sends 60 frames each second. What is the last line? Its form is: frame bytes=..
module tb;
  // The camera of ap_phone. Change the rate and run again.
  localparam int FRAME_W = 1920, FRAME_H = 1080, FPS = 60;
  bit clk;
  logic reset_n = 0, in_valid = 0, out_ready = 0;
  logic [7:0] luma = 0;        // the brightness byte of one pixel
  wire in_ready, out_valid;
  wire [15:0] rgb565;          // one display pixel: 5 bits of red, 6 of green, 5 of blue
  preview_gray stage (.*);
  always #5 clk = !clk;
  longint pixels, camera, brightness, preview;

  // What a job costs from the request to the result, in microseconds.
  task automatic place(input string job, engine, input int wake_us, handoff_us, work_us);
    $display("job=%s on=%s wake_us=%0d handoff_us=%0d work_us=%0d total_us=%0d",
             job, engine, wake_us, handoff_us, work_us, wake_us + handoff_us + work_us);
  endtask

  initial begin
    place("frame", "core", 0, 0, 20000);
    place("frame", "isp", 300, 100, 2000);
    place("icon", "core", 0, 0, 50);
    place("icon", "isp", 300, 100, 5);
    // One pixel through the stage. The consumer is not ready at first.
    repeat (2) @(negedge clk);
    reset_n = 1;
    @(negedge clk);
    luma = 8'h84;
    in_valid = 1;
    @(negedge clk);
    in_valid = 0;
    $display("hold valid=%b ready=%b rgb=%04h", out_valid, out_ready, rgb565);
    @(negedge clk);
    out_ready = 1;
    @(posedge clk);
    $display("take valid=%b ready=%b rgb=%04h", out_valid, out_ready, rgb565);
    // Bytes each second, in the format of each crossing.
    pixels = longint'(FRAME_W) * FRAME_H * FPS;
    camera = pixels * 2;       // a brightness byte and a colour byte
    brightness = pixels;       // the grey preview uses the brightness byte only
    preview = pixels * 2;      // one display pixel is 2 bytes
    $display("stream B/s camera=%0d brightness=%0d preview=%0d", camera, brightness, preview);
    $display("memory B/s write=%0d read=%0d total=%0d", preview, preview, 2 * preview);
    $display("raw_spill B/s total=%0d", 2 * preview + 2 * camera);
    $display("frame bytes=%0d", preview / FPS);
    $finish;
  end
endmodule
Read included file: preview_gray.sv
// Teaching fixture for the stream attachment. One unsigned luma sample per beat.
// The registered output holds its pixel while the consumer applies backpressure.
module preview_gray (
  input  logic        clk, reset_n,
  input  logic        in_valid,
  output logic        in_ready,
  input  logic [7:0]  luma,
  output logic        out_valid,
  input  logic        out_ready,
  output logic [15:0] rgb565
);
  assign in_ready = !out_valid || out_ready;
  always_ff @(posedge clk) begin
    if (!reset_n) begin
      out_valid <= 0;
      rgb565 <= 0;
    end else if (in_ready) begin
      out_valid <= in_valid;
      if (in_valid)
        rgb565 <= {luma[7:3], luma[7:2], luma[7:3]};
    end
  end
endmodule
ConceptWhen does a job belong on an engine?
ConceptAn engine does a job in 5 microseconds, after 400 for waking and hand-off. A core takes 50. Who should do it?
ConceptA frame of 124,416,000 bytes each second is written to memory once and read once. What crosses the memory port?
ConceptWhen may a writer use a buffer again?

Common Mistakes

Wrong Count only the reads of the display.

The frame was written to memory first.

Fix Count every crossing, reads and writes.

Wrong Choose an engine by its work time alone.

Wake time and hand-off time can be larger than the work.

Fix Compare the whole time from the request to the result.

Wrong Use the old buffer again when the writer has finished.

The reader may still be in the middle of it.

Fix Wait until the last reader has released it.

Remember

  • An engine does the same steps on much data with wires, where a core fetches instructions.
  • Compare whole costs: wake time, hand-off time and work.
  • Count every memory crossing, in its own format.
  • A buffer has one owner at a time. It changes hands after the last visible write, and returns after the last reader.

Practice

Report a problem with this page