scatter_pipe: Tasks and Descriptor
The task bodies of scatter_pipe, in demos/scatter/pipe/. Where
scatter_seq has one task that runs a job start to finish, scatter_pipe has one
task per phase, so each phase can work on a different job.
One descriptor per job
Once the tasks work on different jobs, no task can be told about a job on the side: by the time
the store task needs job k’s y_addr, the control task is already on job k+2. So the control
task builds one descriptor per job, holding everything a later task needs, and the
descriptor travels in front of the job’s data. Each task reads it, uses what it needs, and
passes it on. Only the compute task changes it.
class ScatterDesc(DataList):
elements = {
"job_id": {"schema": Word32, "description": "The command's job ID"},
"status": {"schema": ScatterStatusField, "description": "OK, or why nothing is stored"},
"n": {"schema": Word32, "description": "Elements: 0 for a job refused for its size"},
"y_addr": {"schema": Word32, "description": "Word offset of the n outputs"},
}
Four words. The descriptor’s path:
| From | To | How it travels |
|---|---|---|
| control | read mover | framed behind the first MemRCmd, which says “relay 1 burst” (fwd_bursts = 1) |
| read mover | load | relayed unread, ahead of ind |
| load | compute | on desc_lc, once the ind and x blocks are full |
| compute | store | on desc_cs, once the y block is full, with status updated |
| store | write mover | framed behind the MemWCmd (fwd_bursts = 1), ahead of y |
| write mover | response | buffered across the write, echoed on wr_done after the last B |
The movers never parse it: they count bursts by the last bit and pass them through. A
completion that names its job is exactly what the plain movers’ MemComplete could not give.
In the industry this is a transaction tag, or a completion-queue entry; here it is in-band, in
the data stream itself.
Ping-pong blocks
ind, x and y are hls::stream_of_blocks<block_t>, with block_t a word_t[1024]. Each
is a pair of blocks with a lock handshake:
- the writer takes an empty block with
hls::write_lock, fills it, and releases it when the lock goes out of scope; the block then belongs to the reader; - the reader takes the full block with
hls::read_lock, uses it, and releases it back.
With two blocks per pair, the load task fills one ind and one x block with job k+1 while the
compute task reads the other pair, job k’s. This is the ping-pong (double) buffer, Vitis’s PIPO
in a DATAFLOW region; here it joins free-running tasks.
The tasks
control
static void scatter_control(hls::stream<word_t>& cmd_in, hls::stream<frame_t>& rd_cmd) {
ScatterCmd cmd;
cmd.read_stream<MEM_DW>(cmd_in);
const bool ok = (cmd.n <= N_MAX);
ScatterDesc d; // the job, as every later task needs it
d.job_id = cmd.job_id;
d.status = ok ? ScatterStatus::OK : ScatterStatus::N_TOO_LARGE;
d.n = ok ? (int)cmd.n : 0; // a refused job reads and stores nothing
d.y_addr = cmd.y_addr;
MemRCmd rc;
rc.addr = cmd.ind_addr; // read ind, after relaying the descriptor
rc.len = d.n;
rc.fwd_bursts = 1;
rc.write_framed_stream<MEM_DW>(rd_cmd);
d.write_framed_stream<MEM_DW>(rd_cmd);
rc.addr = cmd.x_addr; // then x, relaying nothing
rc.fwd_bursts = 0;
rc.write_framed_stream<MEM_DW>(rd_cmd);
}
The only task that sees the ScatterCmd, and the only one that owns no data. Its output is
[MemRCmd(ind, fwd=1) | ScatterDesc | MemRCmd(x, fwd=0)]: the read mover relays the descriptor,
reads ind, then reads x. The control task is free again as soon as those words are queued.
load
static void scatter_load(hls::stream<frame_t>& rd_data, hls::stream<frame_t>& desc_out,
hls::stream_of_blocks<block_t>& ind_blk,
hls::stream_of_blocks<block_t>& x_blk) {
ScatterDesc d;
d.read_framed_stream<MEM_DW>(rd_data); // the header the read mover relayed
const int n = (int)d.n;
bool last;
{
hls::write_lock<block_t> ib(ind_blk);
LOAD_IND: for (int i = 0; i < n; i++) {
#pragma HLS PIPELINE II=1
ib[i] = streamutils::read_boundary_word<frame_t, MEM_DW>(rd_data, last);
}
}
{
hls::write_lock<block_t> xb(x_blk);
LOAD_X: for (int i = 0; i < n; i++) {
#pragma HLS PIPELINE II=1
xb[i] = streamutils::read_boundary_word<frame_t, MEM_DW>(rd_data, last);
}
}
d.write_framed_stream<MEM_DW>(desc_out); // the blocks are full: compute may start
}
Each block is locked only for its own loop. The descriptor goes on last, after both blocks are full, and the compute task starts only when it arrives (see below for why it must be in that order).
compute
static void scatter_compute(hls::stream<frame_t>& desc_in, hls::stream<frame_t>& desc_out,
hls::stream_of_blocks<block_t>& ind_blk,
hls::stream_of_blocks<block_t>& x_blk,
hls::stream_of_blocks<block_t>& y_blk) {
ScatterDesc d;
d.read_framed_stream<MEM_DW>(desc_in);
const int n = (int)d.n;
bool bad = false;
{
hls::read_lock<block_t> ib(ind_blk);
hls::read_lock<block_t> xb(x_blk);
hls::write_lock<block_t> yb(y_blk);
SCATTER: for (int i = 0; i < n; i++) {
#pragma HLS PIPELINE II=1
const word_t k = ib[i];
if (k < (word_t)n) {
yb[k] = xb[i];
} else {
bad = true;
}
}
}
if (bad) {
d.status = ScatterStatus::BAD_INDEX;
}
d.write_framed_stream<MEM_DW>(desc_out);
}
The scatter itself, block to block, the same loop as scatter_seq’s. Two full blocks in, one
empty block out. The descriptor goes on only after the loop: it tells the store task the y
block is complete, and it carries whether every index was in range.
store
static void scatter_store(hls::stream<frame_t>& desc_in, hls::stream_of_blocks<block_t>& y_blk,
hls::stream<frame_t>& wr_data) {
ScatterDesc d;
d.read_framed_stream<MEM_DW>(desc_in);
const int n = (d.status == ScatterStatus::OK) ? (int)d.n : 0;
MemWCmd wc;
wc.addr = d.y_addr;
wc.len = n;
wc.fwd_bursts = 1;
wc.write_framed_stream<MEM_DW>(wr_data);
d.write_framed_stream<MEM_DW>(wr_data); // echoed on the done stream after the write
hls::read_lock<block_t> yb(y_blk);
STORE: for (int i = 0; i < n; i++) {
#pragma HLS PIPELINE II=1
streamutils::write_boundary_word<frame_t, MEM_DW>(wr_data, yb[i], i == n - 1);
}
}
[MemWCmd(y_addr, n, fwd=1) | ScatterDesc | y] to the write mover, which writes y, waits for
the memory’s last write response, and only then echoes the descriptor on wr_done.
response
static void scatter_response(hls::stream<word_t>& wr_done, hls::stream<word_t>& resp_out) {
ScatterDesc d;
d.read_stream<MEM_DW>(wr_done);
ScatterResp resp;
resp.job_id = d.job_id;
resp.status = d.status;
resp.write_stream<MEM_DW>(resp_out);
}
Turns the echoed descriptor back into the same ScatterResp scatter_seq sends, so both
designs look the same from outside and share their testbenches.
Jobs that store nothing
A job refused for its size, and one with a bad index, still pass every task:
- refused (
n > 1024): the control task sendsn = 0. The movers read and write nothing, but the descriptor still goes through, and every task still takes and releases its blocks, empty. - bad index: loaded and scattered in full; the compute task marks it
BAD_INDEX, and the store task frames a write of 0 words.
Skipping a task for such a job would leave the ping-pong pairs out of step: the next job’s
compute task would read a block that belonged to the job before. Sending every job through
every task, with n = 0 where there is nothing to move, keeps the pipeline in lock step for
free.
A C-simulation trap
The first version of the load task sent the descriptor first, then filled the blocks, as Waveflow’s interleaver does. The RTL was correct. C simulation passed job 1, then wrote mostly zeros for every later job.
The reason is in Vitis’s C model of stream_of_blocks (hls_streamofblocks.h): write_acquire
puts the block on the reader’s queue at once, and write_release does nothing. The model
assumes the writer finishes before the reader runs, which holds for a sequential DATAFLOW
call. Free-running tasks are threads, so the compute task got the descriptor, took the block the
load task had just acquired, and read it while it was still being filled.
The fix is the order above: each task passes the descriptor on only after the blocks it hands over are full. The descriptor becomes the “your blocks are ready” signal. It costs the hardware nothing, since the compute task could not have started on an unfilled block anyway, and C simulation now passes. The general lesson: C simulation and RTL can disagree in both directions. A passing C simulation is not proof of correct hardware, and a failing one is not proof of broken hardware. The XSI run is the check that counts.