scatter_seq: One Phase at a Time

This is the design that opens unit 10. Its structure is unit 9’s hist_simple: the command arrives on an AXI4-Stream, s_cmd, the response leaves on another, s_done, and memory is reached through two of Waveflow’s data movers, one on each m_axi port. Between them sits one kernel task that runs each job start to finish.

s_cmd -> +---------+ -MRCmd x2-> mem_r_stream (m_rd) -ind, x-+
         |         | <-------------------------------------+
         | scatter |
         |         | -MWCmd + y-> mem_w_stream_done (m_wr) --+
s_done <-+---------+ <-------------- MemComplete -----------+

Nothing in Vitis is new. What is new is the kernel: its store is as large as its load, so the three phases of a job each take a real share of the time, and the cost of running them one after another is plain to see.

The top

void scatter_seq(hls::stream<word_t>& s_cmd, const word_t* m_rd, word_t* m_wr,
                 hls::stream<word_t>& s_done) {
#pragma HLS INTERFACE axis port=s_cmd
#pragma HLS INTERFACE m_axi port=m_rd offset=off bundle=gmem0 depth=65536
#pragma HLS stable variable=m_rd
#pragma HLS INTERFACE m_axi port=m_wr offset=off bundle=gmem1 depth=65536
#pragma HLS stable variable=m_wr
#pragma HLS INTERFACE axis port=s_done
#pragma HLS INTERFACE ap_ctrl_none port=return
    hls_thread_local hls::stream<word_t> rd_cmd, rd_data, wr_cmd, wr_data, wr_done;

    hls_thread_local hls::task t_rd(mem_r_stream_task<MEM_DW>, rd_cmd, m_rd, rd_data);
    hls_thread_local hls::task t_comp(scatter_compute, s_cmd, s_done,
                                      rd_cmd, rd_data, wr_cmd, wr_data, wr_done);
    hls_thread_local hls::task t_wr(mem_w_stream_done_task<MEM_DW>, wr_cmd, wr_data, m_wr, wr_done);
}

The same top as hist_simple, with a different kernel task: three hls::tasks, five FIFOs, ap_ctrl_none, and the two m_axi bundles in the only form Vitis supports on a task (offset=off, stable). See hist_simple’s top for each line.

The kernel task

static void scatter_compute(hls::stream<word_t>& s_cmd, hls::stream<word_t>& s_done,
                            hls::stream<word_t>& rd_cmd, hls::stream<word_t>& rd_data,
                            hls::stream<word_t>& wr_cmd, hls::stream<word_t>& wr_data,
                            hls::stream<word_t>& wr_done) {
    ScatterCmd cmd;
    cmd.read_stream<MEM_DW>(s_cmd);
    ScatterResp resp;
    resp.job_id = cmd.job_id;
    resp.status = (cmd.n <= N_MAX) ? ScatterStatus::OK : ScatterStatus::N_TOO_LARGE;

    if (resp.status == ScatterStatus::OK) {     // a refused job touches no memory
        const int n = (int)cmd.n;
        word_t ind_buf[N_MAX], x_buf[N_MAX], y_buf[N_MAX];

        // ---- load: two reads, one after the other, over the one read port
        MRCmd rc;
        rc.addr = cmd.ind_addr;
        rc.len = cmd.n;
        rc.write_stream<MEM_DW>(rd_cmd);
        rc.addr = cmd.x_addr;                   // queued: the mover starts it after the first
        rc.write_stream<MEM_DW>(rd_cmd);
    LOAD_IND: for (int i = 0; i < n; i++) {
#pragma HLS PIPELINE II=1
            ind_buf[i] = rd_data.read();
        }
    LOAD_X: for (int i = 0; i < n; i++) {
#pragma HLS PIPELINE II=1
            x_buf[i] = rd_data.read();
        }

        // ---- compute: the scatter, buffer to buffer
        bool bad = false;
    SCATTER: for (int i = 0; i < n; i++) {
#pragma HLS PIPELINE II=1
            const word_t k = ind_buf[i];
            if (k < (word_t)n) {
                y_buf[k] = x_buf[i];
            } else {
                bad = true;
            }
        }

        if (bad) {
            resp.status = ScatterStatus::BAD_INDEX;
        } else {
            // ---- store: n outputs to y_addr, then wait until they are in memory
            MWCmd wc;
            wc.addr = cmd.y_addr;
            wc.len = cmd.n;
            wc.write_stream<MEM_DW>(wr_cmd);
        STORE: for (int i = 0; i < n; i++) {
#pragma HLS PIPELINE II=1
                wr_data.write(y_buf[i]);
            }
            MemComplete done;
            done.read_stream<MEM_DW>(wr_done);
        }
    }
    resp.write_stream<MEM_DW>(s_done);
}

Read it as three phases:

  1. Load. Two MRCmds, ind then x, on the one read port; the mover runs them one after the other. Two loops land the words in on-chip buffers, one per cycle.
  2. Compute. The scatter, buffer to buffer. Each element is one plain write, with no read-modify-write, so nothing carries from one iteration to the next and Vitis schedules the loop at II=1.
  3. Store. One MWCmd, the n outputs, then wait for the write mover’s MemComplete, which it sends only once the memory has acknowledged the last write. Only then the response.

Why the scatter cannot stream. ind and x arrive in order and could be used as they arrive. y is written at random, so it must be held whole: whatever order y is stored in, its first word might be the last one the scatter writes. The store cannot start before the scatter ends. That is the reason for the on-chip block, and for the load in load–compute–store.

Bad input. A job with n > N_MAX (1024, the size of each buffer) is refused before any read. An index ≥ n would write outside the block: it is dropped, nothing is stored, and the response says BAD_INDEX. ind is meant to be a permutation; with a repeated index the last x wins, and a y that no index names keeps whatever the buffer held.

The three buffers are three BRAMs; Vitis estimates the design at 137 MHz (FF 2439, LUT 3326, BRAM 8, DSP 0), with all four loops at II=1.

The messages

All words are 32 bits, and every address is a word offset. Every message is a generated struct; nothing is packed by hand (see messages and serialization).

Message Stream Words Fields
ScatterCmd s_cmd 5 job_id, ind_addr, x_addr, y_addr, n
MRCmd rd_cmd 2 addr, len (two per job)
MWCmd wr_cmd 2 addr, len
MemComplete wr_done 1 len
ScatterResp s_done 2 job_id, status (OK = 0, N_TOO_LARGE = 1, BAD_INDEX = 2)

How it is checked

Nine jobs, in one memory of 16,384 words. Each job owns three runs of n words, its ind, its x and its y, and every y starts filled with a sentinel, 0xDEADBEEF, so a word the kernel never wrote is caught.

Job n ind Expected
1 256 a random permutation OK
2 1024 a random permutation: the largest job OK
3 512 the FFT’s bit reversal, 9 bits OK
4 128 a permutation with ind[37] = n BAD_INDEX, nothing written
5 2000 none: n > N_MAX N_TOO_LARGE, nothing read or written
6–9 512 random permutations OK

scatter_seq.py runs them three times: Vitis C simulation, and the synthesized RTL under XSI with Waveflow’s bus models, once with all nine commands back to back and once with each job alone (6,000 idle cycles between commands). After each run the whole memory is compared with the expected image: each good job’s y equal to the scatter, every other word unchanged, so a stray write anywhere fails, not only a wrong output. There must be nine responses, in order, with the right status. All three runs pass.

The timing of a job

From the waveform, in clock cycles (100 MHz) after the command’s last word was accepted, each job alone:

n AR first ind first x last x read MWCmd AW last output last B response
256 11 12 297 572 832 858 1106 1145 1152
512 11 12 569 1116 1632 1658 2194 2233 2240
1024 11 12 1113 2204 3232 3258 4370 4409 4416
128 (bad index) 11 12 161 300 - - - - 435
2000 (refused) - - - - - - - - 4

Reading the n = 512 row as phases:

Cycles Phase What is busy
1 to 1116 load the read port: 512 indices, then 512 samples, 17 cycles per 16-word burst
1117 to 1631 compute the scatter: 515 cycles, about one per element
1632 to 2240 store the write port: the outputs, then the last B (2233), the completion and the response
  • Each read of n words takes about n·17/16: the testbench memory answers in a cycle, and each 16-word burst costs one extra. Between the two reads the port is idle about 14 cycles: the mover takes the second command only once the first read is done.
  • The job is about 4.25·n + 64 cycles: two reads, a scatter and a write of n each, plus burst overhead.
  • The job with a bad index is loaded and scattered, then answers without storing; the refused one answers in 4 cycles.

Who is busy, and when

scatter_seq: the first three jobs back to back, and job 1 alone, one row per resource

Top: three jobs back to back, one row per resource. Bottom: job 1 alone. The colours are the phases: blue load, orange compute, green store; hatched, the kernel waiting on a mover. Each resource is busy in one phase of each job and idle in the other two:

n job read port busy compute busy write port busy busiest
256 1,152 557 (48%) 259 (22%) 288 (25%) read port
512 2,240 1,101 (49%) 515 (23%) 576 (26%) read port
1024 4,416 2,189 (50%) 1,027 (23%) 1,152 (26%) read port

The read port runs from its first request to its last beat, both reads and the gap between them; the write port from its first request to its last B. The shares do not depend on n.

Back to back, nothing overlaps. The nine jobs take 17,239 cycles from the first command to the last response, against 17,207 for the same jobs run one at a time: the task reads the next command only after this job’s response.

What overlap could give. If load, compute and store ran on different jobs at once, a job could start as often as the busiest resource allows: unit 9’s rule, the largest sets the rate. At n = 512 that is the read port, 1,101 cycles, against 2,240 now: about 2×, from the same hardware. Speeding up the compute instead gains little. Even a scatter that took no time at all would save only its 515 cycles, 1.30×. scatter_pipe builds the overlap.

What the testbench’s memory hides

The memory model answers a read on the next cycle and serves one burst at a time. A DRAM behind an interconnect takes tens of cycles to answer, but accepts several requests at once. On the board each read and the write’s acknowledgement would take longer, so the load and store would grow against the compute, and overlapping them would matter more, not less.


This site uses Just the Docs, a documentation theme for Jekyll.