Skip to content
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

Optimize after delays on broadcast connections and fix them for C #593

Merged
merged 4 commits into from
Oct 11, 2021
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
11 changes: 5 additions & 6 deletions org.lflang/src/org/lflang/ASTUtils.xtend
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ class ASTUtils {
output.variable = delayClass.outputs.get(0)
upstream.leftPorts.addAll(connection.leftPorts)
upstream.rightPorts.add(input)
upstream.iterated = connection.iterated
downstream.leftPorts.add(output)
downstream.rightPorts.addAll(connection.rightPorts)
downstream.iterated = connection.iterated

connections.add(upstream)
connections.add(downstream)
Expand Down Expand Up @@ -260,12 +260,11 @@ class ASTUtils {
if (connection.hasMultipleConnections) {
val widthSpec = factory.createWidthSpec
if (defineWidthFromConnection) {
// Add all right ports of the connection to the WidthSpec of the genertaed delay instance.
// Add all left ports of the connection to the WidthSpec of the generated delay instance.
// This allows the code generator to later infer the width from the involved ports.
// We only consider the right ports here, as the right hand side should always have a well-defined
// width. On the left hand side, we might use the broadcast operator from which we cannot infer
// the width.
for (port : connection.rightPorts) {
// We only consider the left ports here, as they could be part of a broadcast. In this case, we want
// to delay the ports first, and then broadcast the output of the delays.
for (port : connection.leftPorts) {
val term = factory.createWidthTerm()
term.port = EcoreUtil.copy(port)
widthSpec.terms.add(term)
Expand Down
38 changes: 38 additions & 0 deletions test/C/src/multiport/Broadcast.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target C {
timeout: 2 sec,
fast: true
};

reactor Source {
output out:int;

reaction(startup) -> out {=
SET(out, 42);
=}
}

reactor Destination(bank_index:int(0)) {
input in:int;
state received:bool(false);
reaction(in) {=
printf("Destination %d received %d.\n", self->bank_index, in->value);
if (in->value != 42) {
printf("ERROR: Expected 42.\n");
exit(1);
}
self->received = true;
=}
reaction(shutdown) {=
if (!self->received) {
fprintf(stderr, "ERROR: Destination %d received no input!\n", self->bank_index);
exit(1);
}
printf("Success.\n");
=}
}

main reactor {
a = new Source();
b = new[4] Destination();
(a.out)+ -> b.in;
}
42 changes: 42 additions & 0 deletions test/C/src/multiport/BroadcastAfter.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
target C {
timeout: 2 sec,
fast: true
};

reactor Source {
output out:int;

reaction(startup) -> out {=
SET(out, 42);
=}
}

reactor Destination(bank_index:int(0)) {
input in:int;
state received:bool(false);
reaction(in) {=
printf("Destination %d received %d.\n", self->bank_index, in->value);
if (in->value != 42) {
printf("ERROR: Expected 42.\n");
exit(1);
}
if (get_elapsed_logical_time() != SEC(1)) {
printf("ERROR: Expected to receive input after one second.\n");
exit(2);
}
self->received = true;
=}
reaction(shutdown) {=
if (!self->received) {
fprintf(stderr, "ERROR: Destination %d received no input!\n", self->bank_index);
exit(3);
}
printf("Success.\n");
=}
}

main reactor {
a = new Source();
b = new[4] Destination();
(a.out)+ -> b.in after 1 sec;
}
45 changes: 45 additions & 0 deletions test/C/src/multiport/BroadcastMultipleAfter.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
target C {
timeout: 2 sec,
fast: true
};

reactor Source(value:int(42)) {
output out:int;

reaction(startup) -> out {=
SET(out, self->value);
=}
}

reactor Destination(bank_index:int(0)) {
input in:int;
state received:bool(false);
reaction(in) {=
printf("Destination %d received %d.\n", self->bank_index, in->value);
int expected = (self->bank_index % 3) + 1;
if (in->value != expected) {
printf("ERROR: Expected %d.\n", expected);
exit(1);
}
if (get_elapsed_logical_time() != SEC(1)) {
printf("ERROR: Expected to receive input after one second.\n");
exit(2);
}
self->received = true;
=}
reaction(shutdown) {=
if (!self->received) {
fprintf(stderr, "ERROR: Destination %d received no input!\n", self->bank_index);
exit(3);
}
printf("Success.\n");
=}
}

main reactor {
a1 = new Source(value=1);
a2 = new Source(value=2);
a3 = new Source(value=3);
b = new[9] Destination();
(a1.out, a2.out, a3.out)+ -> b.in after 1 sec;
}
46 changes: 46 additions & 0 deletions test/Cpp/src/multiport/BroadcastMultipleAfter.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
target Cpp{
fast: true
}

reactor Source(value:unsigned(42)) {
output out:unsigned;

reaction (startup) -> out {=
out.set(value);
=}
}

reactor Sink(bank_index: size_t(0)) {
input in:unsigned;
state received: bool{false};

reaction (in) {=
std::cout << bank_index << " received " << *in.get() << '\n';
auto expected = (bank_index % 3) + 1;
if (*in.get() != expected) {
std::cerr << "Error: expected " << expected << "!\n";
exit(1);
}
if (get_elapsed_logical_time() != 1s) {
std::cerr << "ERROR: Expected to receive input after one second.\n";
exit(2);
}
received = true;
=}

reaction(shutdown) {=
if (!received) {
std::cerr << "ERROR: Destination " << bank_index << " received no input!\n";
exit(1);
}
std::cout << "Success.\n";
=}
}

main reactor {
source1 = new Source(value=1);
source2 = new Source(value=2);
source3 = new Source(value=3);
sink = new[9] Sink();
(source1.out, source2.out, source3.out)+ -> sink.in after 1 sec;
}