Skip to content

Commit

Permalink
Clarify that 10 is magic number (#1663)
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanmohan authored Aug 16, 2023
1 parent 51fc4b2 commit f457b6d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
3 changes: 2 additions & 1 deletion calyx-py/calyx/queue_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import calyx.builder_util as util

MAX_CMDS = 15
ANS_MEM_LEN = 10


def insert_raise_err_if_i_eq_max_cmds(prog):
Expand Down Expand Up @@ -58,7 +59,7 @@ def insert_main(prog, queue):
# - one ref register, `err`, which is raised if an error occurs.

commands = main.seq_mem_d1("commands", 32, MAX_CMDS, 32, is_external=True)
ans_mem = main.seq_mem_d1("ans_mem", 32, 10, 32, is_external=True)
ans_mem = main.seq_mem_d1("ans_mem", 32, ANS_MEM_LEN, 32, is_external=True)

# The two components we'll use:
queue = main.cell("myqueue", queue)
Expand Down
34 changes: 20 additions & 14 deletions calyx-py/test/correctness/fifo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import calyx.builder_util as util
import calyx.queue_call as qc

MAX_QUEUE_LEN = 10


def insert_fifo(prog, name):
"""Inserts the component `fifo` into the program.
It has:
- one input, `cmd`.
- one memory, `mem`, of size 10.
- one memory, `mem`, of size MAX_QUEUE_LEN.
- two registers, `next_write` and `next_read`.
- two ref registers, `ans` and `err`.
"""
Expand All @@ -18,11 +20,11 @@ def insert_fifo(prog, name):
cmd = fifo.input("cmd", 32)
# If this is 0, we pop. If it is 1, we peek. Otherwise, we push the value.

mem = fifo.seq_mem_d1("mem", 32, 10, 32)
mem = fifo.seq_mem_d1("mem", 32, MAX_QUEUE_LEN, 32)
write = fifo.reg("next_write", 32) # The next address to write to
read = fifo.reg("next_read", 32) # The next address to read from
# We will orchestrate `mem`, along with the two pointers above, to
# simulate a circular queue of size 10.
# simulate a circular queue of size MAX_QUEUE_LEN.

ans = fifo.reg("ans", 32, is_ref=True)
# If the user wants to pop, we will write the popped value to `ans`
Expand All @@ -37,12 +39,16 @@ def insert_fifo(prog, name):
cmd_eq_1 = util.insert_eq(fifo, cmd, 1, "cmd_eq_1", 32) # `cmd` == 1
cmd_gt_1 = util.insert_gt(fifo, cmd, 1, "cmd_gt_1", 32) # `cmd` > 1

write_eq_10 = util.insert_eq(
fifo, write.out, 10, "write_eq_10", 32
) # `write` == 10
read_eq_10 = util.insert_eq(fifo, read.out, 10, "read_eq_10", 32) # `read` == 10
write_eq_max_queue_len = util.insert_eq(
fifo, write.out, MAX_QUEUE_LEN, "write_eq_MAX_QUEUE_LEN", 32
) # `write` == MAX_QUEUE_LEN
read_eq_max_queue_len = util.insert_eq(
fifo, read.out, MAX_QUEUE_LEN, "read_eq_MAX_QUEUE_LEN", 32
) # `read` == MAX_QUEUE_LEN
len_eq_0 = util.insert_eq(fifo, len.out, 0, "len_eq_0", 32) # `len` == 0
len_eq_10 = util.insert_eq(fifo, len.out, 10, "len_eq_10", 32) # `len` == 10
len_eq_max_queue_len = util.insert_eq(
fifo, len.out, MAX_QUEUE_LEN, "len_eq_MAX_QUEUE_LEN", 32
) # `len` == MAX_QUEUE_LEN

# Cells and groups to increment read and write registers
write_incr = util.insert_incr(fifo, write, "write_incr") # write++
Expand Down Expand Up @@ -89,8 +95,8 @@ def insert_fifo(prog, name):
read_incr, # Increment the read pointer.
cb.if_(
# Wrap around if necessary.
read_eq_10[0].out,
read_eq_10[1],
read_eq_max_queue_len[0].out,
read_eq_max_queue_len[1],
read_wrap,
),
len_decr, # Decrement the length.
Expand Down Expand Up @@ -118,16 +124,16 @@ def insert_fifo(prog, name):
cmd_gt_1[1],
cb.if_(
# Yes, the user called push. But is the queue full?
len_eq_10[0].out,
len_eq_10[1],
len_eq_max_queue_len[0].out,
len_eq_max_queue_len[1],
[raise_err, zero_out_ans], # The queue is full: overflow.
[ # The queue is not full. Proceed.
write_to_mem, # Write to the queue.
write_incr, # Increment the write pointer.
cb.if_(
# Wrap around if necessary.
write_eq_10[0].out,
write_eq_10[1],
write_eq_max_queue_len[0].out,
write_eq_max_queue_len[1],
write_wrap,
),
len_incr, # Increment the length.
Expand Down
17 changes: 11 additions & 6 deletions calyx-py/test/correctness/pifo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import calyx.builder_util as util
import calyx.queue_call as qc

MAX_QUEUE_LEN = 10


def insert_flow_inference(comp: cb.ComponentBuilder, cmd, flow, boundary, group):
"""The flow is needed when the command is a push.
Expand Down Expand Up @@ -53,14 +55,15 @@ def insert_pifo(prog, name, queue_l, queue_r, boundary):
violation of the 50/50 rule) until the silent flow starts transmitting again.
At that point we go back to 50/50.
The PIFO's maximum capacity is 10.
The PIFO's maximum capacity is MAX_QUEUE_LEN.
Let's say the two flows are called `0` and `1`.
We orchestrate two sub-queues, `queue_l` and `queue_r`, each of capacity 10.
We orchestrate two sub-queues, `queue_l` and `queue_r`,
each of capacity MAX_QUEUE_LEN.
We maintain a register that points to which of these sub-queues is "hot".
Start off with `hot` pointing to `queue_l` (arbitrarily).
- `push(v, PIFO)`:
+ If len(PIFO) = 10, raise an "overflow" err and exit.
+ If len(PIFO) = MAX_QUEUE_LEN, raise an "overflow" err and exit.
+ Otherwise, the charge is to enqueue value `v`.
Find out which flow `f` the value `v` should go to;
`f` better be either `0` or `1`.
Expand Down Expand Up @@ -109,7 +112,9 @@ def insert_pifo(prog, name, queue_l, queue_r, boundary):
flow_eq_0 = util.insert_eq(pifo, flow.out, 0, "flow_eq_0", 1)
flow_eq_1 = util.insert_eq(pifo, flow.out, 1, "flow_eq_1", 1)
len_eq_0 = util.insert_eq(pifo, len.out, 0, "len_eq_0", 32)
len_eq_10 = util.insert_eq(pifo, len.out, 10, "len_eq_10", 32)
len_eq_max_queue_len = util.insert_eq(
pifo, len.out, MAX_QUEUE_LEN, "len_eq_MAX_QUEUE_LEN", 32
)
cmd_eq_0 = util.insert_eq(pifo, cmd, 0, "cmd_eq_0", 32)
cmd_eq_1 = util.insert_eq(pifo, cmd, 1, "cmd_eq_1", 32)
cmd_gt_1 = util.insert_gt(pifo, cmd, 1, "cmd_gt_1", 32)
Expand Down Expand Up @@ -267,8 +272,8 @@ def insert_pifo(prog, name, queue_l, queue_r, boundary):
cmd_gt_1[1],
cb.if_(
# Yes, the user called push. But is the queue full?
len_eq_10[0].out,
len_eq_10[1],
len_eq_max_queue_len[0].out,
len_eq_max_queue_len[1],
[raise_err, zero_out_ans], # The queue is full: overflow.
[ # The queue is not full. Proceed.
lower_err,
Expand Down

0 comments on commit f457b6d

Please sign in to comment.