Skip to content

Commit

Permalink
[NB] update adjacency matrix (#11771)
Browse files Browse the repository at this point in the history
- update the order of subscripts in the case of subscripts at different depths
 - better dumping
 - ToDo: code generation for subscripts at different depths
  • Loading branch information
kabdelhak committed Dec 27, 2023
1 parent 9ba5fde commit b890045
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
58 changes: 34 additions & 24 deletions OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo
Expand Up @@ -847,77 +847,76 @@ public
unique_dependencies := list(ComponentRef.simplifySubscripts(dep) for dep in dependencies);
unique_dependencies := UnorderedSet.unique_list(unique_dependencies, ComponentRef.hash, ComponentRef.isEqual);
if Flags.isSet(Flags.BLT_MATRIX_DUMP) then
print("Finding dependencies for:\n" + Equation.toString(eqn) + "\n");
print("dependencies: " + List.toString(unique_dependencies, ComponentRef.toString) + "\n\n");
print("\nFinding dependencies for:\n" + Equation.toString(eqn) + "\n");
print("dependencies: " + List.toString(unique_dependencies, ComponentRef.toString) + "\n");
end if;
() := match eqn
local
list<Integer> row;

case Equation.FOR_EQUATION() algorithm
// get expanded matrix rows
fillMatrixArray(unique_dependencies, map, mapping, eqn_arr_idx, m, modes,
function Slice.getDependentCrefIndicesPseudoFor(iter = eqn.iter));
fillMatrixArray(eqn, unique_dependencies, map, mapping, eqn_arr_idx, m, modes, function Slice.getDependentCrefIndicesPseudoFor(iter = eqn.iter));
then ();

case Equation.ARRAY_EQUATION() algorithm
fillMatrixArray(unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray);
fillMatrixArray(eqn, unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray);
then ();

case Equation.RECORD_EQUATION() algorithm
fillMatrixArray(unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray);
fillMatrixArray(eqn, unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray);
then ();

case Equation.ALGORITHM() algorithm
(eqn_scal_idx, eqn_size) := mapping.eqn_AtS[eqn_arr_idx];
row := Slice.getDependentCrefIndicesPseudoScalar(unique_dependencies, map, mapping);
for i in 0:eqn_size-1 loop
arrayUpdate(m, eqn_scal_idx+i, listAppend(row, m[eqn_scal_idx+i]));
for i in 1:eqn_size loop
updateIntegerRow(m, eqn_scal_idx+(i-1), row);
end for;
then ();

case Equation.IF_EQUATION() algorithm
fillMatrixArray(unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray);
fillMatrixArray(eqn, unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray);
then ();

case Equation.WHEN_EQUATION() algorithm
fillMatrixArray(unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray);
fillMatrixArray(eqn, unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray);
then ();

else algorithm
(eqn_scal_idx, _) := mapping.eqn_AtS[eqn_arr_idx];
row := Slice.getDependentCrefIndicesPseudoScalar(unique_dependencies, map, mapping);
arrayUpdate(m, eqn_scal_idx, listAppend(row, m[eqn_scal_idx]));
updateIntegerRow(m, eqn_scal_idx, row);
then ();
end match;
end fillMatrix;

function fillMatrixArray
"adds multiple rows to the adjacency matrix at once.
used for equations with size > 1"
input Equation eqn "only for debug purposes";
input list<ComponentRef> unique_dependencies;
input UnorderedMap<ComponentRef, Integer> map;
input Adjacency.Mapping mapping;
input Integer eqn_arr_idx;
input array<list<Integer>> m;
input CausalizeModes modes;
input getDependentCrefIndices func;
input Slice.getDependentCrefIndices func;
protected
partial function getDependentCrefIndices
input list<ComponentRef> dependencies;
input UnorderedMap<ComponentRef, Integer> map;
input Adjacency.Mapping mapping;
input Integer eqn_arr_idx;
output array<list<Integer>> m_part;
output array<array<Integer>> mode_to_var_part;
end getDependentCrefIndices;
Integer eqn_scal_idx, eqn_size;
array<list<Integer>> m_part;
array<array<Integer>> mode_to_var_part;
algorithm
(eqn_scal_idx, eqn_size) := mapping.eqn_AtS[eqn_arr_idx];
(m_part, mode_to_var_part) := func(unique_dependencies, map, mapping, eqn_arr_idx);
// check for arrayLength(m_part) == eqn_size ?
if not arrayLength(m_part) == eqn_size then
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because equation size " + intString(eqn_size) +
" differs from adjacency matrix row size " + intString(arrayLength(m_part)) + " for equation:\n" + Equation.toString(eqn)});
fail();
end if;
// add matrix rows to correct locations and update causalize modes
expandRows(m, eqn_scal_idx, m_part);
copyRows(m, eqn_scal_idx, m_part);
if eqn_size > 1 then
CausalizeModes.update(modes, eqn_scal_idx, eqn_arr_idx, mode_to_var_part, unique_dependencies);
end if;
Expand Down Expand Up @@ -998,15 +997,26 @@ public
end if;
end maxDimTraverse;

function expandRows
function copyRows
input array<list<Integer>> m;
input Integer eqn_scal_idx;
input array<list<Integer>> m_part;
algorithm
for i in 1:arrayLength(m_part) loop
arrayUpdate(m, eqn_scal_idx+(i-1), listAppend(m_part[i], m[eqn_scal_idx+(i-1)]));
updateIntegerRow(m, eqn_scal_idx+(i-1), m_part[i]);
end for;
end expandRows;
end copyRows;

function updateIntegerRow
input array<list<Integer>> m;
input Integer idx;
input list<Integer> row;
algorithm
arrayUpdate(m, idx, listAppend(row, m[idx]));
if Flags.isSet(Flags.BLT_MATRIX_DUMP) then
print("Adding to row " + intString(idx) + " " + List.toString(row, intString) + "\n");
end if;
end updateIntegerRow;
end Matrix;

annotation(__OpenModelica_Interface="backend");
Expand Down
23 changes: 11 additions & 12 deletions OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo
Expand Up @@ -201,6 +201,15 @@ public
input UnorderedSet<ComponentRef> acc;
end filterCref;

partial function getDependentCrefIndices
input list<ComponentRef> dependencies "dependent var crefs";
input UnorderedMap<ComponentRef, Integer> map "unordered map to check for relevance";
input Mapping mapping "array <-> scalar index mapping";
input Integer eqn_arr_idx;
output array<list<Integer>> indices;
output array<array<Integer>> mode_to_var;
end getDependentCrefIndices;

// ############################################################
// cref accumulation Functions
// use with:
Expand Down Expand Up @@ -334,12 +343,7 @@ public
function getDependentCrefIndicesPseudoArray
"[Adjacency.MatrixType.PSEUDO] Array equations.
Turns cref dependencies into index lists, used for adjacency."
input list<ComponentRef> dependencies "dependent var crefs";
input UnorderedMap<ComponentRef, Integer> map "unordered map to check for relevance";
input Mapping mapping "array <-> scalar index mapping";
input Integer eqn_arr_idx;
output array<list<Integer>> indices;
output array<array<Integer>> mode_to_var;
extends getDependentCrefIndices;
protected
ComponentRef stripped;
Integer eqn_start, eqn_size, var_arr_idx, var_scal_idx, mode = 1;
Expand Down Expand Up @@ -397,13 +401,8 @@ public
function getDependentCrefIndicesPseudoFor
"[Adjacency.MatrixType.PSEUDO] For-Loop equations.
Turns cref dependencies into index lists, used for adjacency."
input list<ComponentRef> dependencies "dependent var crefs";
input UnorderedMap<ComponentRef, Integer> map "unordered map to check for relevance";
input Mapping mapping "array <-> scalar index mapping";
extends getDependentCrefIndices;
input Iterator iter "iterator frames";
input Integer eqn_arr_idx;
output array<list<Integer>> indices;
output array<array<Integer>> mode_to_var;
protected
list<ComponentRef> names;
list<Expression> ranges;
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -746,7 +746,7 @@ public
"Returns all subscripts of a cref as a flat list in the correct order.
Ex: a[1, 2].b[4].c[6, 3] => {1, 2, 4, 6, 3}"
input ComponentRef cref;
output list<Subscript> subscripts = List.flattenReverse(subscriptsAllWithWhole(cref));
output list<Subscript> subscripts = List.flatten(subscriptsAllWithWhole(cref));
end subscriptsAllWithWholeFlat;

function subscriptsAll
Expand Down

0 comments on commit b890045

Please sign in to comment.