Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Commit

Permalink
Attempt to speed up collectInitialBindings.
Browse files Browse the repository at this point in the history
- Change BackendEquation.addEquation so it doesn't iterate over the
  array in an attempt to find an empty slot.
- Removed some unnecessary tuple construction/deconstruction in
  Initialization.collectInitlaVarsEqnsSystem.
  • Loading branch information
perost authored and OpenModelica-Hudson committed Apr 10, 2017
1 parent 32a70d2 commit c224a4b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 85 deletions.
77 changes: 16 additions & 61 deletions Compiler/BackEnd/BackendEquation.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1365,71 +1365,26 @@ algorithm
outEqns := addEquations(equationList(inEqns1), inEqns2);
end mergeEquationArray;

protected function findFirstUnusedEquOptEntry
input Integer inPos "initially call this with 1";
input Integer inSize;
input array<Option<BackendDAE.Equation>> inEquOptArr;
output Integer outIndex;
algorithm
outIndex := findFirstUnusedEquOptEntryWork(inPos, inSize, inEquOptArr[inPos], inEquOptArr);
end findFirstUnusedEquOptEntry;

protected function findFirstUnusedEquOptEntryWork
input Integer inPos "initially call this with 1";
input Integer inSize;
input Option<BackendDAE.Equation> thisValue;
input array<Option<BackendDAE.Equation>> inEquOptArr;
output Integer outIndex;
public function addEquation
"Adds an equation to an EquationArray."
input BackendDAE.Equation inEquation;
input output BackendDAE.EquationArray equationArray;
protected
array<Option<BackendDAE.Equation>> eq_arr;
Integer eq_size, count;
algorithm
outIndex := match (inPos, inSize, thisValue, inEquOptArr)
case (_, _, NONE(), _)
then inPos;
BackendDAE.EQUATION_ARRAY(eq_size, count, eq_arr) := equationArray;
count := count + 1;

case (_, _, _, _)
then findFirstUnusedEquOptEntryWork(inPos+1, inSize, arrayGet(inEquOptArr,inPos+1), inEquOptArr);
end match;
end findFirstUnusedEquOptEntryWork;
// Expand the array if there's no space left.
if count >= arrayLength(eq_arr) then
eq_arr := Array.expand(max(count, realInt(arrayLength(eq_arr) * 1.4)), eq_arr, NONE());
end if;

public function addEquation "author: PA
Adds an equation to an EquationArray."
input BackendDAE.Equation inEquation;
input BackendDAE.EquationArray inEquationArray;
output BackendDAE.EquationArray outEquationArray;
algorithm
outEquationArray := matchcontinue (inEquationArray)
local
Integer n_1, numberOfElement, arrSize, expandsize, expandsize_1, newsize, size, index;
array<Option<BackendDAE.Equation>> arr_1, equOptArr, arr_2;
Real rsize, rexpandsize;
eq_size := eq_size + equationSize(inEquation);
eq_arr := arrayUpdate(eq_arr, count, SOME(inEquation));

case BackendDAE.EQUATION_ARRAY(size=size, numberOfElement=numberOfElement, equOptArr=equOptArr) guard
(numberOfElement < arrayLength(equOptArr)) "Have space to add array elt."
equation
arrSize = arrayLength(equOptArr);
n_1 = numberOfElement + 1;
index = findFirstUnusedEquOptEntry(n_1, arrSize, equOptArr);
arr_1 = arrayUpdate(equOptArr, index, SOME(inEquation));
size = equationSize(inEquation) + size;
then BackendDAE.EQUATION_ARRAY(size, n_1, arr_1);

case BackendDAE.EQUATION_ARRAY(size=size, numberOfElement=numberOfElement, equOptArr=equOptArr) guard /* Do NOT Have space to add array elt. Expand array 1.4 times */
not (numberOfElement < arrayLength(equOptArr))
equation
arrSize = arrayLength(equOptArr);
rsize = intReal(arrSize);
rexpandsize = rsize * 0.4;
expandsize = realInt(rexpandsize);
expandsize_1 = intMax(expandsize, 1);
arr_1 = Array.expand(expandsize_1, equOptArr, NONE());
n_1 = numberOfElement + 1;
arr_2 = arrayUpdate(arr_1, n_1, SOME(inEquation));
size = equationSize(inEquation) + size;
then BackendDAE.EQUATION_ARRAY(size, n_1, arr_2);

case BackendDAE.EQUATION_ARRAY(size=size, numberOfElement=numberOfElement, equOptArr=equOptArr) equation
print("- BackendEquation.addEquation failed\nArraySize: " + intString(arrayLength(equOptArr)) + "\nnumberOfElement " + intString(numberOfElement) + "\nSize " + intString(size) + "\narraySize " + intString(arrayLength(equOptArr)));
then fail();
end matchcontinue;
equationArray := BackendDAE.EQUATION_ARRAY(eq_size, count, eq_arr);
end addEquation;

public function equationAddDAE "author: Frenkel TUD 2011-05"
Expand Down
53 changes: 29 additions & 24 deletions Compiler/BackEnd/Initialization.mo
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ algorithm
//end if;
execStat("collectInitialEqns (initialization)");

((vars, fixvars, eqns, reeqns, _, _)) := List.fold(dae.eqs, collectInitialVarsEqnsSystem, ((vars, fixvars, eqns, reeqns, hs, outAllPrimaryParameters)));
//((vars, fixvars, eqns, reeqns, _, _)) := List.fold(dae.eqs, collectInitialVarsEqnsSystem, ((vars, fixvars, eqns, reeqns, hs, outAllPrimaryParameters)));
(vars, fixvars, eqns, reeqns) := collectInitialVarsEqnsSystem(dae.eqs, vars, fixvars, eqns, reeqns, hs, outAllPrimaryParameters);
((eqns, reeqns)) := BackendVariable.traverseBackendDAEVars(vars, collectInitialBindings, (eqns, reeqns));
execStat("collectInitialBindings (initialization)");

Expand Down Expand Up @@ -2012,30 +2013,34 @@ end introducePreVarsForAliasVariables;
//
// =============================================================================

protected function collectInitialVarsEqnsSystem "author: lochel
This function collects variables and equations for the initial system out of an given EqSystem."
input BackendDAE.EqSystem inEqSystem;
input tuple<BackendDAE.Variables, BackendDAE.Variables, BackendDAE.EquationArray, BackendDAE.EquationArray, HashSet.HashSet, list<BackendDAE.Var>> inTpl;
output tuple<BackendDAE.Variables, BackendDAE.Variables, BackendDAE.EquationArray, BackendDAE.EquationArray, HashSet.HashSet, list<BackendDAE.Var>> outTpl;
protected
BackendDAE.Variables vars, fixvars;
BackendDAE.EquationArray eqns, reqns;
HashSet.HashSet hs;
list<BackendDAE.Var> allPrimaryParameters;
algorithm
(vars, fixvars, eqns, reqns, hs, allPrimaryParameters) := inTpl;

outTpl := match inEqSystem
case BackendDAE.EQSYSTEM(partitionKind = BackendDAE.CLOCKED_PARTITION(_)) equation
((vars, eqns)) = BackendVariable.traverseBackendDAEVars(inEqSystem.orderedVars, collectInitialClockedVarsEqns, (vars, eqns));
then (vars, fixvars, eqns, reqns, hs, allPrimaryParameters);
protected function collectInitialVarsEqnsSystem
"This function collects variables and equations for the initial system out of an given EqSystem."
input list<BackendDAE.EqSystem> eqSystems;
input output BackendDAE.Variables vars;
input output BackendDAE.Variables fixVars;
input output BackendDAE.EquationArray eqns;
input output BackendDAE.EquationArray reEqns;
input HashSet.HashSet hs;
input list<BackendDAE.Var> allPrimaryParams;
algorithm
for eq in eqSystems loop
() := match eq
case BackendDAE.EQSYSTEM(partitionKind = BackendDAE.CLOCKED_PARTITION())
algorithm
(vars, eqns) := BackendVariable.traverseBackendDAEVars(eq.orderedVars,
collectInitialClockedVarsEqns, (vars, eqns));
then
();

else equation
((vars, fixvars, eqns, hs, _)) = BackendVariable.traverseBackendDAEVars(inEqSystem.orderedVars, collectInitialVars, (vars, fixvars, eqns, hs, allPrimaryParameters));
((eqns, reqns)) = BackendEquation.traverseEquationArray(inEqSystem.orderedEqs, collectInitialEqns, (eqns, reqns));
//((fixvars, eqns)) = List.fold(inEqSystem.stateSets, collectInitialStateSetVars, (fixvars, eqns));
then (vars, fixvars, eqns, reqns, hs, allPrimaryParameters);
end match;
else
algorithm
(vars, fixVars, eqns, _, _) := BackendVariable.traverseBackendDAEVars(eq.orderedVars,
collectInitialVars, (vars, fixVars, eqns, hs, allPrimaryParams));
(eqns, reEqns) := BackendEquation.traverseEquationArray(eq.orderedEqs, collectInitialEqns, (eqns, reEqns));
then
();
end match;
end for;
end collectInitialVarsEqnsSystem;

protected function collectInitialVars "author: lochel
Expand Down

0 comments on commit c224a4b

Please sign in to comment.