Skip to content

Commit

Permalink
Merge pull request #275 from lf-lang/sparse
Browse files Browse the repository at this point in the history
Fixed description of sparse inputs
  • Loading branch information
edwardalee authored Jul 8, 2024
2 parents 949897f + c5431df commit fc09e34
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 16 deletions.
3 changes: 1 addition & 2 deletions docs/assets/code/c/src/Sparse.lf
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
target C;
reactor Sparse {
@sparse
input[100] in:int;
input[100] in: int
reaction(in) {=
// Create an iterator over the input channels.
struct lf_multiport_iterator_t i = lf_multiport_iterator(in);
Expand Down
9 changes: 2 additions & 7 deletions docs/writing-reactors/multiports-and-banks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,13 @@ In the Python target, multiports can be iterated on in a for loop (e.g., `for p

Sometimes, a program needs a wide multiport input, but when reactions are triggered by this input, few of the channels are present.
In this case, it can be inefficient to iterate over all the channels to determine which are present.
If you know that a multiport input will be **sparse** in this way, then you can provide a hint to the compiler and use a more efficient iterator to access the port. For example:
If you know that a multiport input will be **sparse** in this way, then you can use a more efficient iterator to access the port. For example:

import C_Sparse from '../assets/code/c/src/Sparse.lf';

<NoSelectorTargetCodeBlock c={C_Sparse} lf />

Notice the `@sparse` annotation on the input declaration.
This provides a hint to the compiler to optimize for sparse inputs.
Then, instead of iterating over all input channels, this code uses the built-in function `lf_multiport_iterator()` to construct an iterator. The function `lf_multiport_next()` returns the first (and later, the next) channel index that is present. It returns -1 when no more channels have present inputs.

The multiport iterator can be used for any input multiport, even if it is not marked sparse.
But if it is not marked sparse, then the `lf_multiport_next()` function will not optimize for sparse inputs and will simply iterate over the channels until it finds one that is present.
Instead of iterating over all input channels, this code uses the built-in function `lf_multiport_iterator()` to construct an iterator. The function `lf_multiport_next()` returns the first (and later, the next) channel index that is present. It returns -1 when no more channels have present inputs.

</ShowOnly>

Expand Down
1 change: 0 additions & 1 deletion versioned_docs/version-0.8.0/assets/code/c/src/Sparse.lf
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
target C;
reactor Sparse {
@sparse
input[100] in:int;
reaction(in) {=
// Create an iterator over the input channels.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,7 @@ import C_Sparse from '../assets/code/c/src/Sparse.lf';

<NoSelectorTargetCodeBlock c={C_Sparse} lf />

Notice the `@sparse` annotation on the input declaration.
This provides a hint to the compiler to optimize for sparse inputs.
Then, instead of iterating over all input channels, this code uses the built-in function `lf_multiport_iterator()` to construct an iterator. The function `lf_multiport_next()` returns the first (and later, the next) channel index that is present. It returns -1 when no more channels have present inputs.

The multiport iterator can be used for any input multiport, even if it is not marked sparse.
But if it is not marked sparse, then the `lf_multiport_next()` function will not optimize for sparse inputs and will simply iterate over the channels until it finds one that is present.
Instead of iterating over all input channels, this code uses the built-in function `lf_multiport_iterator()` to construct an iterator. The function `lf_multiport_next()` returns the first (and later, the next) channel index that is present. It returns -1 when no more channels have present inputs.

</ShowOnly>

Expand Down

0 comments on commit fc09e34

Please sign in to comment.