Skip to content

Commit c9f5345

Browse files
authored
[NB] full adjacency matrix 1.0 (#12042)
- all cases implented (afiak)
1 parent 0d61675 commit c9f5345

File tree

1 file changed

+117
-45
lines changed

1 file changed

+117
-45
lines changed

OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo

Lines changed: 117 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -701,38 +701,42 @@ public
701701
algorithm
702702
str := match adj
703703
local
704-
list<ComponentRef> F, R, E, A, S;
705-
list<String> f = {}, r = {}, e = {}, a = {}, s = {};
706-
array<String> names, types, F_, R_, E_, A_, S_;
707-
Integer length1, length2, lengthf, lengthr, lengthe, lengtha, lengths;
704+
list<ComponentRef> F, R, E, A, S, K;
705+
list<String> f = {}, r = {}, e = {}, a = {}, s = {}, k = {};
706+
array<String> names, types, F_, R_, E_, A_, S_, K_;
707+
Integer length1, length2, lengthf, lengthr, lengthe, lengtha, lengths, lengthk;
708708

709709
case FULL() algorithm
710710
str := StringUtil.headline_2(str + " Dependency Adjacency Matrix") + "\n";
711711
types := listArray(list(dimsString(Type.arrayDims(ComponentRef.getSubscriptedType(name))) for name in adj.equation_names));
712712
names := listArray(list(ComponentRef.toString(name) for name in adj.equation_names));
713713
for i in arrayLength(names):-1:1 loop
714-
(F, R, E, A, S) := Dependency.categorize(adj.occurences[i], adj.dependencies[i], adj.repetitions[i]);
714+
(F, R, E, A, S, K) := Dependency.categorize(adj.occurences[i], adj.dependencies[i], adj.repetitions[i]);
715715
f := List.toString(F, ComponentRef.toString, "[!]", "{", ",", "}", false) :: f;
716716
r := List.toString(R, ComponentRef.toString, "[-]", "{", ",", "}", false) :: r;
717717
e := List.toString(E, ComponentRef.toString, "[+]", "{", ",", "}", false) :: e;
718718
a := List.toString(A, ComponentRef.toString, "[:]", "{", ",", "}", false) :: a;
719719
s := List.toString(S, ComponentRef.toString, "[.]", "{", ",", "}", false) :: s;
720+
k := List.toString(K, ComponentRef.toString, "[o]", "{", ",", "}", false) :: k;
720721
end for;
721722
F_ := listArray(f);
722723
R_ := listArray(r);
723724
E_ := listArray(e);
724725
A_ := listArray(a);
725726
S_ := listArray(s);
727+
K_ := listArray(k);
726728
length1 := max(stringLength(ty) for ty in types) + 1;
727729
length2 := max(stringLength(name) for name in names) + 3;
728-
lengthf := max(stringLength(s) for s in F_);
729-
lengthr := max(stringLength(s) for s in R_);
730-
lengthe := max(stringLength(s) for s in E_);
731-
lengtha := max(stringLength(s) for s in A_);
732-
lengths := max(stringLength(s) for s in S_);
730+
lengthf := max(stringLength(st) for st in F_);
731+
lengthr := max(stringLength(st) for st in R_);
732+
lengthe := max(stringLength(st) for st in E_);
733+
lengtha := max(stringLength(st) for st in A_);
734+
lengths := max(stringLength(st) for st in S_);
735+
lengthk := max(stringLength(st) for st in K_);
733736
for i in 1:arrayLength(names) loop
734737
str := str + arrayGet(types, i) + " " + StringUtil.repeat(".", length1 - stringLength(arrayGet(types, i))) + " "
735738
+ arrayGet(names, i) + " " + StringUtil.repeat(".", length2 - stringLength(arrayGet(names, i)))
739+
+ arrayGet(K_, i) + " " + StringUtil.repeat(".", lengthk - stringLength(arrayGet(K_, i)))
736740
+ arrayGet(S_, i) + " " + StringUtil.repeat(".", lengths - stringLength(arrayGet(S_, i)))
737741
+ arrayGet(A_, i) + " " + StringUtil.repeat(".", lengtha - stringLength(arrayGet(A_, i)))
738742
+ arrayGet(E_, i) + " " + StringUtil.repeat(".", lengthe - stringLength(arrayGet(E_, i)))
@@ -901,6 +905,7 @@ public
901905
CausalizeModes modes;
902906
algorithm
903907
adj := createFull(vars, eqns, st, funcTree);
908+
//print(EquationPointers.toString(eqns) + "\n");
904909
//print(solvabilityString(adj, "Full") + "\n");
905910
//print(dependencyString(adj, "Full") + "\n");
906911
//print(toString(adj, "Full") + "\n");
@@ -1188,25 +1193,31 @@ public
11881193
"the dependency kind to show how a component reference occurs in an equation.
11891194
for each dimension there has to be one dependency kind."
11901195
record DEPENDENCY
1196+
list<Integer> skip_pos;
11911197
Integer regular;
11921198
Integer reduction;
11931199
end DEPENDENCY;
11941200

11951201
function toString
11961202
input Dependency dep;
11971203
output String str;
1204+
protected
1205+
String str1, str2;
11981206
algorithm
1199-
str := List.toString(listAppend(List.fill(":", dep.regular), List.fill("-", dep.reduction)), StringUtil.id);
1207+
str1 := List.toString(dep.skip_pos, intString, "", "", ", ", "");
1208+
str2 := List.toString(listAppend(List.fill(":", dep.regular), List.fill("-", dep.reduction)), StringUtil.id, "", "", ", ", "");
1209+
str := if str1 == "" or str2 == "" then str1 + str2 else str1 + ", " + str2;
1210+
str := "{" + str + "}";
12001211
end toString;
12011212

12021213
function create
12031214
input Type sub_ty;
12041215
output Dependency dep;
12051216
algorithm
12061217
if Type.isArray(sub_ty) then
1207-
dep := DEPENDENCY(listLength(list(dim for dim guard(not Dimension.isOne(dim)) in Type.arrayDims(sub_ty))), 0);
1218+
dep := DEPENDENCY({}, listLength(list(dim for dim guard(not Dimension.isOne(dim)) in Type.arrayDims(sub_ty))), 0);
12081219
else
1209-
dep := DEPENDENCY(0, 0);
1220+
dep := DEPENDENCY({}, 0, 0);
12101221
end if;
12111222
end create;
12121223

@@ -1234,6 +1245,45 @@ public
12341245
end if;
12351246
end update;
12361247

1248+
function skip
1249+
input ComponentRef cref;
1250+
input Integer skip_pos;
1251+
input UnorderedMap<ComponentRef, Dependency> map;
1252+
protected
1253+
Option<Dependency> opt_dep = UnorderedMap.get(cref, map);
1254+
Dependency dep;
1255+
algorithm
1256+
if Util.isSome(opt_dep) then
1257+
SOME(dep) := opt_dep;
1258+
dep.skip_pos := skip_pos :: dep.skip_pos;
1259+
UnorderedMap.add(cref, dep, map);
1260+
else
1261+
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because cref "
1262+
+ ComponentRef.toString(cref) + " was not found in the map."});
1263+
fail();
1264+
end if;
1265+
end skip;
1266+
1267+
function updateList
1268+
input list<ComponentRef> lst;
1269+
input Integer num;
1270+
input UnorderedMap<ComponentRef, Dependency> map;
1271+
algorithm
1272+
for cref in lst loop
1273+
update(cref, num, map);
1274+
end for;
1275+
end updateList;
1276+
1277+
function skipList
1278+
input list<ComponentRef> lst;
1279+
input Integer skip_pos;
1280+
input UnorderedMap<ComponentRef, Dependency> map;
1281+
algorithm
1282+
for cref in lst loop
1283+
skip(cref, skip_pos, map);
1284+
end for;
1285+
end skipList;
1286+
12371287
function addList
12381288
input list<ComponentRef> lst;
12391289
input UnorderedMap<ComponentRef, Dependency> map;
@@ -1254,6 +1304,7 @@ public
12541304
output list<ComponentRef> E = {};
12551305
output list<ComponentRef> A = {};
12561306
output list<ComponentRef> S = {};
1307+
output list<ComponentRef> K = {};
12571308
protected
12581309
Boolean repeats;
12591310
algorithm
@@ -1262,6 +1313,8 @@ public
12621313
_ := match UnorderedMap.getSafe(cref, map, sourceInfo())
12631314
local
12641315
Integer red;
1316+
case DEPENDENCY(skip_pos = {_})
1317+
algorithm K := cref :: K; then ();
12651318
case DEPENDENCY(regular = 0, reduction = 0) guard(repeats)
12661319
algorithm E := cref :: E; then ();
12671320
case DEPENDENCY(regular = 0, reduction = 0)
@@ -1471,6 +1524,7 @@ public
14711524
Expression call_exp;
14721525
Call call;
14731526
Boolean isRep;
1527+
Integer ind;
14741528

14751529
case Expression.CREF() guard(UnorderedMap.contains(exp.cref, map)) algorithm
14761530
if not UnorderedMap.contains(exp.cref, dep_map) then
@@ -1479,31 +1533,43 @@ public
14791533
Solvability.update(exp.cref, Solvability.EXPLICIT_LINEAR(false, NONE()), sol_map);
14801534
then UnorderedSet.fromList({exp.cref}, ComponentRef.hash, ComponentRef.isEqual);
14811535

1482-
// ToDo: need to update dependencies here
1536+
// Add skips for arrays
14831537
case Expression.ARRAY(literal = false) algorithm
1484-
for elem in exp.elements loop
1485-
sets := collectDependencies(elem, map, dep_map, sol_map, rep_set) :: sets;
1538+
for i in 1:arrayLength(exp.elements) loop
1539+
set1 := collectDependencies(exp.elements[i], map, dep_map, sol_map, rep_set);
1540+
Dependency.skipList(UnorderedSet.toList(set1), i, dep_map);
1541+
sets := set1 :: sets;
14861542
end for;
14871543
then UnorderedSet.union_list(sets, ComponentRef.hash, ComponentRef.isEqual);
14881544

1545+
// add skips for tuples
14891546
case Expression.TUPLE() algorithm
1547+
ind := 1;
14901548
for elem in exp.elements loop
1491-
sets := collectDependencies(elem, map, dep_map, sol_map, rep_set) :: sets;
1549+
set1 := collectDependencies(elem, map, dep_map, sol_map, rep_set);
1550+
Dependency.skipList(UnorderedSet.toList(set1), ind, dep_map);
1551+
sets := set1 :: sets;
1552+
ind := ind + 1;
14921553
end for;
1493-
then UnorderedSet.union_list(sets, ComponentRef.hash, ComponentRef.isEqual);
1554+
set := UnorderedSet.union_list(sets, ComponentRef.hash, ComponentRef.isEqual);
1555+
then set;
14941556

1557+
// this ultimately should not occur, but handle just as arrays and tuples
14951558
case Expression.RECORD() algorithm
1559+
ind := 1;
14961560
for elem in exp.elements loop
1497-
sets := collectDependencies(elem, map, dep_map, sol_map, rep_set) :: sets;
1561+
set1 := collectDependencies(elem, map, dep_map, sol_map, rep_set);
1562+
Dependency.skipList(UnorderedSet.toList(set1), ind, dep_map);
1563+
sets := set1 :: sets;
1564+
ind := ind + 1;
14981565
end for;
1499-
then UnorderedSet.union_list(sets, ComponentRef.hash, ComponentRef.isEqual);
1566+
set := UnorderedSet.union_list(sets, ComponentRef.hash, ComponentRef.isEqual);
1567+
then set;
15001568

15011569
// reduce the dependency for these
15021570
case Expression.SUBSCRIPTED_EXP() algorithm
15031571
set := collectDependencies(exp.exp, map, dep_map, sol_map, rep_set);
1504-
for cref in UnorderedSet.toList(set) loop
1505-
Dependency.update(cref, listLength(exp.subscripts), dep_map);
1506-
end for;
1572+
Dependency.updateList(UnorderedSet.toList(set), listLength(exp.subscripts), dep_map);
15071573
then set;
15081574

15091575
// should not change anything
@@ -1517,8 +1583,8 @@ public
15171583
set2 := collectDependencies(exp.exp2, map, dep_map, sol_map, rep_set);
15181584
set := UnorderedSet.union(set1, set2);
15191585
// add repetitions if needed
1520-
addRepetition(set1, exp.exp1, isRep, rep_set);
1521-
addRepetition(set2, exp.exp2, isRep, rep_set);
1586+
addRepetitionsCond(set1, exp.exp1, isRep, rep_set);
1587+
addRepetitionsCond(set2, exp.exp2, isRep, rep_set);
15221588
then set;
15231589

15241590
// mostly equal to binary
@@ -1529,14 +1595,14 @@ public
15291595
for arg in exp.arguments loop
15301596
set1 := collectDependencies(arg, map, dep_map, sol_map, rep_set);
15311597
// add repetitions if needed
1532-
addRepetition(set1, arg, isRep, rep_set);
1598+
addRepetitionsCond(set1, arg, isRep, rep_set);
15331599
sets := set1 :: sets;
15341600
end for;
15351601
// traverse inverse arguments
15361602
for arg in exp.inv_arguments loop
15371603
set2 := collectDependencies(arg, map, dep_map, sol_map, rep_set);
15381604
// add repetitions if needed
1539-
addRepetition(set2, arg, isRep, rep_set);
1605+
addRepetitionsCond(set2, arg, isRep, rep_set);
15401606
sets := set2 :: sets;
15411607
end for;
15421608
set := UnorderedSet.union_list(sets, ComponentRef.hash, ComponentRef.isEqual);
@@ -1588,36 +1654,34 @@ public
15881654
Solvability.updateList(UnorderedSet.toList(set), Solvability.UNSOLVABLE(), sol_map);
15891655
then UnorderedSet.union_list({set, set1, set2}, ComponentRef.hash, ComponentRef.isEqual);
15901656

1591-
/* this does not really work in some cases. is this needed?
15921657
// for array constructors replace all iterators (temporarily)
15931658
case Expression.CALL(call = call as Call.TYPED_ARRAY_CONSTRUCTOR(exp = call_exp)) algorithm
1594-
print("got array const: " + Expression.toString(exp) + "\n");
15951659
for iter in call.iters loop
15961660
call_exp := Expression.replaceIterator(call_exp, Util.tuple21(iter), Util.tuple22(iter));
15971661
end for;
1598-
then collectDependencies(call_exp, dep_map, sol_map, rep_set);
1599-
*/
1662+
// if these are not simplified before this step, they can only be solved implicitely
1663+
set := collectDependencies(call_exp, map, dep_map, sol_map, rep_set);
1664+
Solvability.updateList(UnorderedSet.toList(set), Solvability.IMPLICIT(), sol_map);
1665+
then set;
16001666

1601-
// for reductions set the dependency to reduction
1667+
// for reductions set the dependency to full reduction
16021668
case Expression.CALL(call = call as Call.TYPED_REDUCTION(exp = call_exp)) algorithm
16031669
for iter in call.iters loop
16041670
call_exp := Expression.replaceIterator(call_exp, Util.tuple21(iter), Util.tuple22(iter));
16051671
end for;
16061672
set := collectDependencies(call_exp, map, dep_map, sol_map, rep_set);
1607-
for cref in UnorderedSet.toList(set) loop
1608-
Dependency.update(cref, -1, dep_map);
1609-
end for;
1673+
Dependency.updateList(UnorderedSet.toList(set), -1, dep_map);
16101674
then set;
16111675

1612-
// for functions set the dependency to reduction
1676+
// for functions set the dependency to full reduction (+ repetition) and solvability to implicit
16131677
case Expression.CALL(call = call as Call.TYPED_CALL()) algorithm
16141678
for arg in call.arguments loop
16151679
sets := collectDependencies(arg, map, dep_map, sol_map, rep_set) :: sets;
16161680
end for;
16171681
set := UnorderedSet.union_list(sets, ComponentRef.hash, ComponentRef.isEqual);
1618-
for cref in UnorderedSet.toList(set) loop
1619-
Dependency.update(cref, -1, dep_map);
1620-
end for;
1682+
Dependency.updateList(UnorderedSet.toList(set), -1, dep_map);
1683+
Solvability.updateList(UnorderedSet.toList(set), Solvability.IMPLICIT(), sol_map);
1684+
addRepetitions(set, rep_set);
16211685
then set;
16221686

16231687
// nothing is solvable from ranges
@@ -1635,20 +1699,28 @@ public
16351699
end match;
16361700
end collectDependencies;
16371701

1638-
function addRepetition
1639-
"adds component references to the repetition set,
1702+
function addRepetitionsCond
1703+
"adds component references from a set to the repetition set,
16401704
only if the expression they appear in is of size 1"
16411705
input UnorderedSet<ComponentRef> occ;
16421706
input Expression exp;
16431707
input Boolean isRep;
16441708
input UnorderedSet<ComponentRef> rep_set;
16451709
algorithm
16461710
if isRep and Type.sizeOf(Expression.typeOf(exp)) == 1 then
1647-
for cref in UnorderedSet.toList(occ) loop
1648-
UnorderedSet.add(cref, rep_set);
1649-
end for;
1711+
addRepetitions(occ, rep_set);
16501712
end if;
1651-
end addRepetition;
1713+
end addRepetitionsCond;
1714+
1715+
function addRepetitions
1716+
"adds component references to the repetition set"
1717+
input UnorderedSet<ComponentRef> occ;
1718+
input UnorderedSet<ComponentRef> rep_set;
1719+
algorithm
1720+
for cref in UnorderedSet.toList(occ) loop
1721+
UnorderedSet.add(cref, rep_set);
1722+
end for;
1723+
end addRepetitions;
16521724

16531725
function collectDependenciesIf
16541726
"collects all relevant component references from an if equation body

0 commit comments

Comments
 (0)