Skip to content

Fixed description of sparse inputs #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading