Skip to content

Commit

Permalink
Merge pull request #713 from lf-lang/python-iteratable-multiports
Browse files Browse the repository at this point in the history
Python iterable multiports
  • Loading branch information
lhstrh authored Nov 7, 2021
2 parents 11570eb + 5298b68 commit 28e7e38
Show file tree
Hide file tree
Showing 24 changed files with 95 additions and 253 deletions.
10 changes: 5 additions & 5 deletions test/Python/src/multiport/BankToBankMultiport.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ reactor Source(width(1)) {
output[width] out;
state s(0);
reaction(t) -> out {=
for i in range(len(out)):
out[i].set(self.s)
for port in out:
port.set(self.s)
self.s += 1
=}
}
Expand All @@ -18,9 +18,9 @@ reactor Destination(width(1)) {
input[width] _in;
reaction(_in) {=
sm = 0
for i in range(len(_in)):
if _in[i].is_present is True:
sm += _in[i].value
for port in _in:
if port.is_present:
sm += port.value

print("Sum of received: ", sm)
if sm != self.s:
Expand Down
34 changes: 1 addition & 33 deletions test/Python/src/multiport/BankToBankMultiportAfter.lf
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,7 @@ target Python {
timeout: 2 sec,
fast: true
};
reactor Source(width(1)) {
timer t(0, 200 msec);
output[width] out;
state s(0);
reaction(t) -> out {=
for i in range(len(out)):
out[i].set(self.s)
self.s += 1
=}
}
reactor Destination(width(1)) {
state s(6);
input[width] _in;
reaction(_in) {=
sm = 0
for i in range(len(_in)):
if _in[i].is_present is True:
sm += _in[i].value
print("Sum of received: ", sm)
if sm != self.s:
sys.stderr.write("ERROR: Expected {:d}.\n".format(self.s))
exit(1)

self.s += 16
=}
reaction(shutdown) {=
if self.s <= 6:
sys.stderr.write("ERROR: Destination received no input!\n")
exit(1)

print("Success.")
=}
}
import Source, Destination from "BankToBankMultiport.lf"
main reactor BankToBankMultiportAfter(bank_width(4)) {
a = new[bank_width] Source(width = 4);
b = new[bank_width] Destination(width = 4);
Expand Down
11 changes: 5 additions & 6 deletions test/Python/src/multiport/BankToMultiport.lf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ reactor Source(
bank_index(0)
) {
output out;

reaction (startup) -> out {=
out.set(self.bank_index)
=}
Expand All @@ -16,12 +15,12 @@ reactor Sink(width(4)) {
state received(false);

reaction (_in) {=
for i in range(len(_in)):
if _in[i].is_present is True:
print("Received on channel {:d}: {:d}\n".format(i, _in[i].value))
for (idx, port) in enumerate(_in):
if port.is_present is True:
print("Received on channel {:d}: {:d}".format(idx, port.value))
self.received = True
if _in[i].value != i:
sys.stderr.write("ERROR: expected {:d}\n".format(i))
if port.value != idx:
sys.stderr.write("ERROR: expected {:d}\n".format(idx))
exit(1)
=}
reaction(shutdown) {=
Expand Down
8 changes: 4 additions & 4 deletions test/Python/src/multiport/MultiportFromBank.lf
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ reactor Destination {
input[3] _in;
state received(0);
reaction(_in) {=
for i in range(len(_in)):
print("Destination channel " + str(i) + " received " + str(_in[i].value))
if i != _in[i].value:
sys.stderr.write("ERROR: Expected " + str(i))
for (idx, port) in enumerate(_in):
print("Destination channel " + str(idx) + " received " + str(port.value))
if idx != port.value:
sys.stderr.write("ERROR: Expected " + str(idx))
exit(1)

self.received = True
Expand Down
24 changes: 2 additions & 22 deletions test/Python/src/multiport/MultiportFromBankHierarchy.lf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
target Python {
timeout: 2 sec,
fast: true
};
};
import Destination from "MultiportFromBank.lf"
reactor Source(
bank_index(0)
) {
Expand All @@ -17,27 +18,6 @@ reactor Container {
s = new[3] Source();
s.out -> out;
}
reactor Destination {
input[3] _in;
state received(0);
reaction(_in) {=
for i in range(len(_in)):
print("Destination channel " + str(i) + " received " + str(_in[i].value));
if i != _in[i].value:
sys.stderr.write("ERROR: Expected "+ str(i) + ".\n")
exit(1)

self.received = True
=}
reaction(shutdown) {=
if self.received is not True:
sys.stderr.write("ERROR: Destination received no input!\n")
exit(1)

print("Success.")
=}
}

main reactor MultiportFromBankHierarchy {
a = new Container();
b = new Destination();
Expand Down
37 changes: 2 additions & 35 deletions test/Python/src/multiport/MultiportFromBankHierarchyAfter.lf
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,8 @@ target Python {
timeout: 2 sec,
fast: true
};
reactor Source(
bank_index(0)
) {
output out;
reaction(startup) -> out {=
out.set(self.bank_index)
=}
}
reactor Container {
output[3] out;
s = new[3] Source();
s.out -> out;
}
reactor Destination {
input[3] _in;
state received(false);
reaction(_in) {=
for i in range(len(_in)):
print("Destination channel {:d} received {:d}.\n".format(i, _in[i].value))
if i != _in[i].value:
sys.stderr.write("ERROR: Expected {:d}.\n".format(i))
exit(1)
if get_elapsed_logical_time() != SEC(1):
sys.stderr.write("ERROR: Expected to receive input after one second.\n")
exit(2)
self.received = True
=}
reaction(shutdown) {=
if self.received is not True:
sys.stderr.write("ERROR: Destination received no input!\n")
exit(1)
print("Success.\n")
=}
}

import Container from "MultiportFromBankHierarchy.lf"
import Destination from "MultiportFromBank.lf"
main reactor MultiportFromBankHierarchyAfter {
a = new Container();
b = new Destination();
Expand Down
10 changes: 5 additions & 5 deletions test/Python/src/multiport/MultiportFromHierarchy.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ reactor Source {
output[4] out;
state s(0);
reaction(t) -> out {=
for i in range(4):
out[i].set(self.s)
for port in out:
port.set(self.s)
self.s = self.s + 1
=}
}
Expand All @@ -18,9 +18,9 @@ reactor Destination {
input[4] _in;
reaction(_in) {=
sm = 0
for i in range(len(_in)):
if (_in[i].is_present):
sm += _in[i].value
for port in _in:
if port.is_present:
sm += port.value
print("Sum of received: " + str(sm))
if (sm != self.s):
sys.stderr.write("ERROR: Expected " + str(self.s) + ".\n")
Expand Down
18 changes: 9 additions & 9 deletions test/Python/src/multiport/MultiportFromReaction.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ reactor Destination(width(1)) {
input[width] _in;
reaction(_in) {=
sm = 0;
for i in range(len(_in)):
if _in[i].is_present:
sm += _in[i].value
for port in _in:
if port.is_present:
sm += port.value
print("Sum of received: ", sm)
if sm != self.s:
sys.stderr.write("ERROR: Expected {:d}.\n".format(self.s))
Expand All @@ -22,19 +22,19 @@ reactor Destination(width(1)) {
if self.s <= 6:
sys.stderr.write("ERROR: Destination received no input!\n")
exit(1)
print("Success.\n")
print("Success.")
=}
}
main reactor MultiportFromReaction {
timer t(0, 200 msec);
state s(0);
b = new Destination(width = 4);
reaction(t) -> b._in {=
for i in range(len(b._in)):
print("Before SET, b.in[{:d}].is_present has value {:d}".format(i, b._in[i].is_present))
b._in[i].set(self.s)
for (idx, port) in enumerate(b._in):
print("Before SET, b.in[{:d}].is_present has value {:d}".format(idx, port.is_present))
port.set(self.s)
self.s += 1
print("AFTER set, b.in[{:d}].is_present has value {:d}".format(i, b._in[i].is_present))
print("AFTER set, b.in[{:d}].value has value {:d}".format(i, b._in[i].value))
print("AFTER set, b.in[{:d}].is_present has value {:d}".format(idx, port.is_present))
print("AFTER set, b.in[{:d}].value has value {:d}".format(idx, port.value))
=}
}
4 changes: 2 additions & 2 deletions test/Python/src/multiport/MultiportIn.lf
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ reactor Destination {
input[4] _in;
reaction(_in) {=
sum = 0
for i in range(len(_in)):
sum += _in[i].value
for port in _in:
sum += port.value

print("Sum of received: " + str(sum))
if sum != self.s:
Expand Down
6 changes: 3 additions & 3 deletions test/Python/src/multiport/MultiportInParameterized.lf
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ reactor Destination(width(1)) {
input[width] _in;
reaction(_in) {=
sm = 0
for i in range(len(_in)):
sm += _in[i].value
for port in _in:
sm += port.value
print("Sum of received: ", sm)
if sm != self.s:
sys.stderr.write("ERROR: Expected {:d}.\n".format(self.s))
Expand All @@ -38,7 +38,7 @@ reactor Destination(width(1)) {
if self.s == 0:
sys.stderr.write("ERROR: Destination received no input!\n")
exit(1)
print("Success.\n");
print("Success.");
=}
}

Expand Down
12 changes: 6 additions & 6 deletions test/Python/src/multiport/MultiportMutableInput.lf
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ reactor Print(scale(1)) {
input[2] _in;
reaction(_in) {=
expected = 42
for j in range(2):
print("Received on channel {:d}: ".format(j), _in[j].value)
if _in[j].value != expected:
for (idx, port) in enumerate(_in):
print("Received on channel {:d}: ".format(idx), port.value)
if port.value != expected:
sys.stderr.write("ERROR: Expected {:d}!\n".format(expected))
exit(1)
expected *= 2
Expand All @@ -28,10 +28,10 @@ reactor Scale(scale(2)) {
mutable input[2] _in;
output[2] out;
reaction(_in) -> out {=
for j in range(2):
for (idx, port) in enumerate(_in):
# Modify the input, allowed because mutable.
_in[j].value *= self.scale
out[j].set(_in[j].value)
port.value *= self.scale
out[idx].set(port.value)
=}
}
main reactor MultiportMutableInput {
Expand Down
15 changes: 7 additions & 8 deletions test/Python/src/multiport/MultiportMutableInputArray.lf
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ reactor Source {
reactor Print(scale(1)) {
input[2] _in;
reaction(_in) {=
for j in range(2):
print("Received on channel ", _in[j].value)
if _in[j].value != [(self.scale*i) for i in range(3*j,(3*j)+3)]:
for (idx, port) in enumerate(_in):
print("Received on channel ", port.value)
if port.value != [(self.scale*i) for i in range(3*idx,(3*idx)+3)]:
sys.stderr.write("ERROR: Value received by Print does not match expectation!\n")
exit(1)
=}
Expand All @@ -27,11 +27,10 @@ reactor Scale(scale(2)) {
mutable input[2] _in;
output[2] out;
reaction(_in) -> out {=
for j in range(len(_in)):
for i in range(len(_in[j].value)):
if (_in[j].is_present):
_in[j].value[i] *= self.scale
out[j].set(_in[j].value)
for (idx, port) in enumerate(_in):
if port.is_present:
port.value = [value*self.scale for value in port.value]
out[idx].set(port.value)
=}
}

Expand Down
10 changes: 5 additions & 5 deletions test/Python/src/multiport/MultiportOut.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ reactor Source {
output[4] out;
state s(0);
reaction(t) -> out {=
for i in range(0,4):
out[i].set(self.s)
for port in out:
port.set(self.s)

self.s+=1
=}
Expand All @@ -26,9 +26,9 @@ reactor Destination {
input[4] _in;
reaction(_in) {=
sum = 0
for i in range(len(_in)):
if _in[i].is_present:
sum += _in[i].value
for port in _in:
if port.is_present:
sum += port.value

print("Sum of received: " + str(sum))
if sum != self.s:
Expand Down
4 changes: 2 additions & 2 deletions test/Python/src/multiport/MultiportToBank.lf
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ target Python {
reactor Source {
output[3] out;
reaction(startup) -> out {=
for i in range(len(out)):
out[i].set(i)
for (idx, port) in enumerate(out):
port.set(idx)
=}
}
reactor Destination(
Expand Down
8 changes: 1 addition & 7 deletions test/Python/src/multiport/MultiportToBankAfter.lf
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ target Python {
timeout: 2 sec,
fast: true
};
reactor Source {
output[3] out;
reaction(startup) -> out {=
for i in range(len(out)):
out[i].set(i);
=}
}
import Source from "MultiportToBank.lf"
reactor Destination(
bank_index(0)
) {
Expand Down
Loading

0 comments on commit 28e7e38

Please sign in to comment.