Skip to content

Commit

Permalink
Docs: use std_mult_pipe in example about guarded assignments. (#2122)
Browse files Browse the repository at this point in the history
* Require group names for comb_mem loads and stores

* Require group names for seq mem loads and stores

* Rename for consistency

* Unify two helpers

* Unify two helpers for seq mem reads

* Unify two more helpers

* Fix docs to use mult as example of comb cell

* Fix code to use mult as example of comb cell

* Update expect file
  • Loading branch information
anshumanmohan authored Jun 10, 2024
1 parent ef81ae5 commit c8d1633
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
16 changes: 8 additions & 8 deletions calyx-py/test/walkthrough.expect
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ component map(v: 32) -> () {
ref mem = comb_mem_d1(32, 10, 32);
reg_1 = std_reg(8);
reg_1_incr = std_add(8);
add_2 = std_add(32);
mult_pipe_2 = std_mult_pipe(32);
lt_3 = std_lt(8);
}
wires {
Expand All @@ -132,19 +132,19 @@ component map(v: 32) -> () {
lt_3.left = reg_1.out;
lt_3.right = 8'd10;
}
group add_at_position_i {
group mul_at_position_i {
mem.addr0 = reg_1.out;
add_2.left = mem.read_data;
add_2.right = v;
mem.write_en = add_2.done ? 1'd1;
mem.write_data = add_2.out;
add_at_position_i[done] = mem.done;
mult_pipe_2.left = mem.read_data;
mult_pipe_2.right = v;
mem.write_en = mult_pipe_2.done ? 1'd1;
mem.write_data = mult_pipe_2.out;
mul_at_position_i[done] = mem.done;
}
}
control {
while lt_3.out with lt_3_group {
seq {
add_at_position_i;
mul_at_position_i;
reg_1_incr_group;
}
}
Expand Down
22 changes: 11 additions & 11 deletions calyx-py/test/walkthrough.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def insert_map_component(prog):
"""Insert a map component into the program.
The user provides a 1-d memory of length 10, by reference.
The user also provides a single value, `v`.
We add `v` to each element in the memory.
We multiply each element in the memory by `v`.
"""
comp = prog.component("map")
# ANCHOR: comb_mem_d1_ref
Expand All @@ -155,24 +155,24 @@ def insert_map_component(prog):
# ANCHOR: incr_oneliner
incr_i = comp.incr(i)
# ANCHOR_END: incr_oneliner
add = comp.add(32)
mul = comp.mult_pipe(32)

# ANCHOR: width_inf
i_lt_10 = comp.lt_use(i.out, 10)
# ANCHOR_END: width_inf

# ANCHOR: add_at_position_i
with comp.group("add_at_position_i") as add_at_position_i:
# ANCHOR: mul_at_position_i
with comp.group("mul_at_position_i") as mul_at_position_i:
mem.addr0 = i.out
add.left = mem.read_data
add.right = v
mem.write_en = add.done @ cb.HI
mem.write_data = add.out
add_at_position_i.done = mem.done
# ANCHOR_END: add_at_position_i
mul.left = mem.read_data
mul.right = v
mem.write_en = mul.done @ cb.HI
mem.write_data = mul.out
mul_at_position_i.done = mem.done
# ANCHOR_END: mul_at_position_i

# ANCHOR: while_with
comp.control += cb.while_with(i_lt_10, [add_at_position_i, incr_i])
comp.control += cb.while_with(i_lt_10, [mul_at_position_i, incr_i])
# ANCHOR_END: while_with

return comp
Expand Down
12 changes: 6 additions & 6 deletions docs/builder/walkthrough.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,21 @@ The Python return value, `incr_i`, is a handle to the group that performs the in

## Guarded Assignments

Consider the group that adds value `v` to a memory at the cell pointed to by register `i`.
Consider the group that multiplies the memory at the cell pointed to by register `i` by the value `v`.

```python
{{#include ../../calyx-py/test/walkthrough.py:add_at_position_i}}
{{#include ../../calyx-py/test/walkthrough.py:mul_at_position_i}}
```

The first few lines are straightforward; we are setting the cell to be read from with `addr0`, reading from that cell and driving the value to the adder's left port, and setting the right port of the adder to the value `v`.
The first few lines are straightforward; we are setting the cell to be read from with `addr0`, reading from that cell and driving the value to the multiplier's left port, and setting the right port of the multiplier to the value `v`.

Now we wish to write the result to the memory at the cell pointed to by register `i`, but only once we know that the adder has finished its work. We do this with a guarded assignment, using the `@` operator:
Now we wish to write the result to the memory at the cell pointed to by register `i`, but only once we know that the multiplier has finished its work. We do this with a guarded assignment, using the `@` operator:
```python
mem.write_en = add.done @ cb.HI
mem.write_en = mul.done @ cb.HI
```
In Calyx, we would have written this guarded assignment with a question mark:
```
mem.write_en = add.done ? 1'd1;
mem.write_en = mul.done ? 1'd1;
```
We use the `@` operator in the builder library to avoid clashing with Python's ternary operator.

Expand Down

0 comments on commit c8d1633

Please sign in to comment.