Skip to content

Commit

Permalink
Merge pull request #1956 from lf-lang/fix-fed-multiport-params
Browse files Browse the repository at this point in the history
Fix bug when top-level multiport width in federation depends on parameter
  • Loading branch information
petervdonovan authored Aug 23, 2023
2 parents d2d2b76 + 24ed790 commit 8505fb5
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public String generateNetworkSenderBody(
CoordinationType coordinationType,
MessageReporter messageReporter) {
return """
if (%1$s%2$s !== undefined) {
this.util.sendRTITimedMessage(%1$s%2$s, %3$s, %4$s, %5$s);
if (%1$s%2$s[sender_index as number] !== undefined) {
this.util.sendRTITimedMessage(%1$s%2$s[sender_index as number], %3$s, %4$s, %5$s);
}
"""
.formatted(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,15 @@ private static Reactor getNetworkSenderReactor(
Input in = factory.createInput();
in.setName("msg");
in.setType(type);
in.setWidthSpec(
EcoreUtil.copy(connection.getSourcePortInstance().getDefinition().getWidthSpec()));
var width =
ASTUtils.width(
connection.getSourcePortInstance().getDefinition().getWidthSpec(),
List.of(connection.getSrcFederate().instantiation));
var widthSpec = factory.createWidthSpec();
var widthTerm = factory.createWidthTerm();
widthTerm.setWidth(width);
widthSpec.getTerms().add(widthTerm);
in.setWidthSpec(widthSpec);
inRef.setVariable(in);

destRef.setContainer(connection.getDestinationPortInstance().getParent().getDefinition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,11 @@ private Reactor indexer(ReactorInstance reactorInstance, PortInstance input, Res
FedASTUtils.addReactorDefinition(
"_" + reactorInstance.getName() + input.getName(), resource);
var output = LfFactory.eINSTANCE.createOutput();
output.setWidthSpec(EcoreUtil.copy(input.getDefinition().getWidthSpec()));
var widthSpec = LfFactory.eINSTANCE.createWidthSpec();
var widthTerm = LfFactory.eINSTANCE.createWidthTerm();
widthTerm.setWidth(input.getWidth());
widthSpec.getTerms().add(widthTerm);
output.setWidthSpec(widthSpec);
output.setType(EcoreUtil.copy(input.getDefinition().getType()));
output.setName("port");
indexer.getOutputs().add(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.lflang.MessageReporter;
import org.lflang.ast.ASTUtils;
import org.lflang.ast.FormattingUtil;
Expand All @@ -27,13 +28,15 @@ String generateMainReactor(
.error("Modes at the top level are not supported under federated execution.");
}
var renderer = FormattingUtil.renderer(federate.targetConfig.target);
var instantiation = EcoreUtil.copy(federate.instantiation);
instantiation.setWidthSpec(null);

return String.join(
"\n",
generateMainSignature(federate, originalMainReactor, renderer),
String.join(
"\n",
renderer.apply(federate.instantiation),
renderer.apply(instantiation),
ASTUtils.allStateVars(originalMainReactor).stream()
.map(renderer)
.collect(Collectors.joining("\n")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,10 +785,13 @@ private static String deferredOutputNumDestinations(ReactorInstance reactor) {
if (output.eventualDestinations().size() == 0) {
// Dangling output. Still set the source reactor
code.pr(
CUtil.portRef(output)
"for (int index486184027c8990b = 0; index486184027c8990b < "
+ output.getWidth()
+ "; index486184027c8990b++) { "
+ CUtil.portRef(output, false, true, null, null, "index486184027c8990b")
+ "._base.source_reactor = (self_base_t*)"
+ CUtil.reactorRef(reactor)
+ ";");
+ "; }");
}
}
return code.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ target C {
import Source, Destination from "../../federated/DistributedMultiport.lf"

federated reactor DistributedMultiportContainerized at rti {
s = new Source()
d = new Destination()
s = new Source(width=4)
d = new Destination(width=4)
s.out -> d.in
}
5 changes: 4 additions & 1 deletion test/C/src/federated/DistributedBank.lf
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ target C {
coordination: centralized
}

reactor Node {
reactor Node(bank_index: int = 0) {
timer t(0, 100 msec)
state count: int = 0

reaction(t) {= lf_print("Hello world %d.", self->count++); =}

reaction(shutdown) {=
if (self->bank_index) {
lf_print_error_and_exit("The only bank index should be zero because there should be only one bank member per federate.");
}
if (self->count == 0) {
lf_print_error_and_exit("Timer reactions did not execute.");
}
Expand Down
12 changes: 6 additions & 6 deletions test/C/src/federated/DistributedMultiport.lf
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ target C {
coordination: centralized
}

reactor Source {
output[4] out: int
reactor Source(width: int = 2) {
output[width] out: int
timer t(0, 100 msec)
state count: int = 0

Expand All @@ -16,8 +16,8 @@ reactor Source {
=}
}

reactor Destination {
input[4] in: int
reactor Destination(width: int = 3) {
input[width] in: int
state count: int = 0

reaction(in) {=
Expand All @@ -39,7 +39,7 @@ reactor Destination {
}

federated reactor DistributedMultiport {
s = new Source()
d = new Destination()
s = new Source(width=4)
d = new Destination(width=4)
s.out -> d.in
}
2 changes: 1 addition & 1 deletion test/TypeScript/src/federated/DistributedCount.lf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ reactor Print {
const elapsedTime = util.getElapsedLogicalTime();
console.log("At time " + elapsedTime + ", received " + inp);
if (inp !== c) {
util.requestErrorStop("Expected to receive " + c + ".");
util.requestErrorStop("Expected to receive " + JSON.stringify(c) + " but received " + JSON.stringify(inp) + ".");
}
if (!elapsedTime.isEqualTo(TimeValue.msec(200).add(TimeValue.sec(c - 1)))) {
util.requestErrorStop("Expected received time to be " + TimeValue.msec(200).add(TimeValue.sec(c - 1)) + ".");
Expand Down

0 comments on commit 8505fb5

Please sign in to comment.