Skip to content

Commit

Permalink
Add a removeVerySimpleEquations pre-opt module
Browse files Browse the repository at this point in the history
The reason for adding an additional early and faster remove simple
equations is that it speeds up the subsequent modules since they need
to traverse fewer equations and allocate less data. This gives upwards
of a 20% performance increase for medium-size models.

This is disabled by default and marked Experimental due to the used
algorithm not working for all cases.

It is only used for the preOptModules, but for systems depending on
LateInline, it could be possible to use it as the first post-opt module
as well.
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Mar 23, 2016
1 parent 8fc40e6 commit f6f9906
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -7177,6 +7177,7 @@ protected function allPreOptimizationModules
(UnitCheck.unitChecking, "unitChecking"),
(EvaluateParameter.evaluateAllParameters, "evaluateAllParameters"),
(EvaluateParameter.evaluateReplaceProtectedFinalEvaluateParameters, "evaluateReplaceProtectedFinalEvaluateParameters"),
(RemoveSimpleEquations.removeVerySimpleEquations, "removeVerySimpleEquations"),
(StateMachineFeatures.stateMachineElab, "stateMachineElab"),
(BackendDAEOptimize.simplifyIfEquations, "simplifyIfEquations"),
(BackendDAEOptimize.expandDerOperator, "expandDerOperator"),
Expand Down
41 changes: 30 additions & 11 deletions Compiler/BackEnd/RemoveSimpleEquations.mo
Expand Up @@ -157,7 +157,7 @@ algorithm
outDAE := match(Flags.getConfigString(Flags.REMOVE_SIMPLE_EQUATIONS))
case "default" then causal(inDAE);
case "causal" then causal(inDAE);
case "new" then performAliasEliminationBB(inDAE);
case "new" then performAliasEliminationBB(inDAE, findAliases=true);
else inDAE;
end match;

Expand All @@ -169,14 +169,26 @@ algorithm
case "default" then fastAcausal(inDAE);
case "fastAcausal" then fastAcausal(inDAE);
case "allAcausal" then allAcausal(inDAE);
case "new" then performAliasEliminationBB(inDAE);
case "new" then performAliasEliminationBB(inDAE, true);
else inDAE;
end match;

outDAE := fixAliasVars(outDAE) "workaround for #3323";
end if;
end removeSimpleEquations;

public function removeVerySimpleEquations "This is a very simple removeSimpleEquations, finding a few variables and removing them to speed up the rest of the backend"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
if BackendDAEUtil.hasDAEMatching(inDAE) then
Error.addInternalError("Cannot run removeVerySimpleEquations on a matched system (continuing anyway)", sourceInfo());
outDAE := inDAE;
else
outDAE := performAliasEliminationBB(inDAE, findAliases=true);
end if;
end removeVerySimpleEquations;

protected function fixAliasVars "author: lochel
This is a workaround for #3323
TODO: Remove this once removeSimpleEquations is implemented properly.
Expand Down Expand Up @@ -4284,9 +4296,10 @@ protected function performAliasEliminationBB "BB,
This module changes the DAE by finding simple equations, doing appropriate substitutions, e.g. known and alias vars!
NOTE: This is currently an experimental prototype."
input BackendDAE.BackendDAE inDAE;
input Boolean findAliases;
output BackendDAE.BackendDAE outDAE;
algorithm
outDAE := BackendDAEUtil.mapEqSystem(inDAE, eliminateTrivialEquations);
outDAE := BackendDAEUtil.mapEqSystem(inDAE, function eliminateTrivialEquations(findAliases=findAliases));
outDAE := BackendDAEUtil.mapEqSystem(outDAE, getAliasAttributes);
end performAliasEliminationBB;

Expand All @@ -4295,6 +4308,7 @@ main module for eliminating trivial equations
"
input BackendDAE.EqSystem inSystem;
input BackendDAE.Shared inShared;
input Boolean findAliases;
output BackendDAE.EqSystem outSystem = inSystem;
output BackendDAE.Shared outShared = inShared;
algorithm
Expand Down Expand Up @@ -4352,7 +4366,7 @@ algorithm

//SimCodeUtil.execStat("START :");
// Find known variables and all simple equations and add known variables to shared object!!!
(_, HTCrToExp, HTCrToCrEqLst, eqList, simpleEqList) = BackendEquation.traverseEquationArray(orderedEqs, findSimpleEquations,
(_, HTCrToExp, HTCrToCrEqLst, eqList, simpleEqList) = BackendEquation.traverseEquationArray(orderedEqs, function findSimpleEquations(findAliases=findAliases),
(orderedVars, HTCrToExp, HTCrToCrEqLst, {}, {}));
(tplExp) = BaseHashTable.hashTableList(HTCrToExp);
// (knvars, orderedVars) = moveVars(tplExp, knvars, orderedVars);
Expand Down Expand Up @@ -4541,7 +4555,8 @@ protected function findSimpleEquations "BB,
main function for detecting simple equations
"
input BackendDAE.Equation inEq;
input tuple <BackendDAE.Variables, HashTableCrToExp.HashTable, HashTableCrToCrEqLst.HashTable, list<BackendDAE.Equation>, list<BackendDAE.Equation>> inTuple;
input tuple <BackendDAE.Variables, HashTableCrToExp.HashTable, HashTableCrToCrEqLst.HashTable, list<BackendDAE.Equation>, list<BackendDAE.Equation>> inTuple;
input Boolean findAliases;
output BackendDAE.Equation outEq;
output tuple <BackendDAE.Variables, HashTableCrToExp.HashTable, HashTableCrToCrEqLst.HashTable, list<BackendDAE.Equation>, list<BackendDAE.Equation>> outTuple;
algorithm
Expand All @@ -4557,7 +4572,7 @@ algorithm
list<BackendDAE.Equation> simpleEqList;
Integer count, paramCount;
DAE.ComponentRef cr, cr1, cr2;
DAE.Exp res, value;
DAE.Exp res, value, exp1, exp2;
BackendDAE.Variables vars;
Boolean keepEquation, cont;
DAE.ElementSource source;
Expand All @@ -4578,15 +4593,16 @@ algorithm
false = BackendVariable.isClockedState(cr,vars);
false = BackendVariable.isOutput(cr,vars);
false = BackendVariable.isDiscrete(cr,vars);
eqSolved as BackendDAE.EQUATION(scalar=res) = BackendEquation.solveEquation(eq,Expression.crefExp(cr),NONE());
exp1 = Expression.crefExp(cr);
true = Types.isSimpleType(Expression.typeof(exp1));
eqSolved as BackendDAE.EQUATION(scalar=res) = BackendEquation.solveEquation(eq,exp1,NONE());
true = isSimple(res);
if Flags.isSet(Flags.DEBUG_ALIAS) then
print("Found Equation knw1: " + BackendDump.equationString(eq) + "\n");
end if;
HTCrToExp = addToCrToExp(cr, eqSolved, HTCrToExp, HTCrToCrEqLst);
keepEquation = false;
end if;
if (count == 2) then
elseif (count == 2) and findAliases then
if Flags.isSet(Flags.DEBUG_ALIAS) then
print("Found Equation al0: " + BackendDump.equationString(eq) + "\n");
end if;
Expand All @@ -4599,10 +4615,13 @@ algorithm
false = BackendVariable.isClockedState(cr1,vars) or BackendVariable.isClockedState(cr2,vars);
false = BackendVariable.isOutput(cr1,vars) or BackendVariable.isOutput(cr2,vars);
false = BackendVariable.isDiscrete(cr1,vars) or BackendVariable.isDiscrete(cr2,vars);
exp1 = Expression.crefExp(cr1);
true = Types.isSimpleType(Expression.typeof(exp1));
exp2 = Expression.crefExp(cr2);

eqSolved as BackendDAE.EQUATION(scalar=res) = BackendEquation.solveEquation(eq,Expression.crefExp(cr2),NONE());
eqSolved as BackendDAE.EQUATION(scalar=res) = BackendEquation.solveEquation(eq,exp2,NONE());
true = isSimple(res);
eqSolved as BackendDAE.EQUATION(scalar=res) = BackendEquation.solveEquation(eq,Expression.crefExp(cr1),NONE());
eqSolved as BackendDAE.EQUATION(scalar=res) = BackendEquation.solveEquation(eq,exp1,NONE());
true = isSimple(res);
if Flags.isSet(Flags.DEBUG_ALIAS) then
print("Found Equation al1: " + BackendDump.equationString(eq) + "\n");
Expand Down
1 change: 1 addition & 0 deletions Compiler/Util/Flags.mo
Expand Up @@ -742,6 +742,7 @@ constant ConfigFlag PRE_OPT_MODULES = CONFIG_FLAG(12, "preOptModules",
("removeSimpleEquations", removeSimpleEquationDesc),
("removeUnusedParameter", Util.gettext("Strips all parameter not present in the equations from the system.")),
("removeUnusedVariables", Util.gettext("Strips all variables not present in the equations from the system.")),
("removeVerySimpleEquations", Util.gettext("[Experimental] Like removeSimpleEquations, but less thorough. Note that this always uses the experimental new alias elimination, --removeSimpleEquations=new, which makes it unstable. In particular, MultiBody systems fail to translate correctly. It can be used for simple (but large) systems of equations.")),
("replaceEdgeChange", Util.gettext("Replace edge(b) = b and not pre(b) and change(b) = v <> pre(v).")),
("residualForm", Util.gettext("Transforms simple equations x=y to zero-sum equations 0=y-x.")),
("resolveLoops", Util.gettext("resolves linear equations in loops")),
Expand Down

0 comments on commit f6f9906

Please sign in to comment.