Skip to content

Commit

Permalink
- try to remove aliases like state = state, but this needs some more …
Browse files Browse the repository at this point in the history
…thinks to do

- keep zero start values 
- dynamicstateselection, prever states with short names
- move some tests back to working group

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13545 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Oct 22, 2012
1 parent 7e8ca4d commit 55607d0
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 58 deletions.
60 changes: 42 additions & 18 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2041,20 +2041,26 @@ protected
BackendDAE.Variables vars;
BackendDAE.Var var1,var2;
Integer ipos1,ipos2;
Boolean b1,b2,s1,s2;
algorithm
BackendDAE.EQSYSTEM(orderedVars=vars) := syst;
((var1::_),(ipos1::_)) := BackendVariable.getVar(cr1,vars);
((var2::_),(ipos2::_)) := BackendVariable.getVar(cr2,vars);
(cr,exp,k,osyst,oshared) := selectAlias1(cr1,cr2,var1,var2,ipos1,ipos2,e1,e2,syst,shared,negate,source);
s1 := BackendVariable.isStateVar(var1);
s2 := BackendVariable.isStateVar(var2);
b1 := replaceableAlias(var1);
b2 := replaceableAlias(var2);
(cr,exp,k,osyst,oshared) := selectAlias1(s1,s2,b1,b2,cr1,cr2,var1,var2,ipos1,ipos2,e1,e2,syst,shared,negate,source);
end selectAlias;

protected function replaceableAlias
"function replaceableAlias
autor Frenkel TUD 2011-08
check if the variable is a replaceable alias."
input BackendDAE.Var var;
output Boolean res;
algorithm
_ := match (var)
res := matchcontinue (var)
local
BackendDAE.VarKind kind;
case _
Expand All @@ -2067,14 +2073,21 @@ algorithm
false = BackendVariable.isVarOnTopLevelAndInput(var);
false = BackendVariable.varHasUncertainValueRefine(var);
then
();
end match;
true;
else
then
false;
end matchcontinue;
end replaceableAlias;

protected function selectAlias1
"function selectAlias1
autor Frenkel TUD 2011-04
helper for selectAlias."
input Boolean isState1;
input Boolean isState2;
input Boolean replaceable1;
input Boolean replaceable2;
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
input BackendDAE.Var var1;
Expand All @@ -2094,40 +2107,51 @@ protected function selectAlias1
output BackendDAE.Shared oshared;
algorithm
(cr,exp,k,osyst,oshared) :=
matchcontinue (cr1,cr2,var1,var2,ipos1,ipos2,e1,e2,isyst,ishared,negate,source)
match (isState1,isState2,replaceable1,replaceable2,cr1,cr2,var1,var2,ipos1,ipos2,e1,e2,isyst,ishared,negate,source)
local
DAE.ComponentRef acr;
BackendDAE.Var avar,var;
DAE.Exp ae,e;
Integer aipos,i1,i2;
Integer aipos,i1,i2,is1,is2;
Boolean b;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;

case (_,_,_,_,_,_,_,_,syst,shared,_,_)
/* it is not so trivial to replace state alias variables, this is because there could be more than one equation
phi = phi1, w = der(phi), w1 = der=(phi), in this case index reduction run in trouble because now equal equations are there.
case (true,true,_,_,_,_,_,_,_,_,_,_,_,_,_,_)
equation
is1 = BackendVariable.varStateSelectPrioAlias(var1);
is2 = BackendVariable.varStateSelectPrioAlias(var2);
i1 = BackendVariable.calcAliasKey(cr1,var1);
i2 = BackendVariable.calcAliasKey(cr2,var2);
b = intGt(is1,is2) "always=3...never=-1";
b = Util.if_(intEq(is1,is2),intGt(i2,i1),b);
((acr,avar,aipos,ae,cr,var,e)) = Util.if_(b,(cr2,var2,ipos2,e2,cr1,var1,e1),(cr1,var1,ipos1,e1,cr2,var2,e2));
avar = BackendVariable.setVarKind(avar,BackendDAE.DUMMY_STATE());
(syst,shared) = selectAlias2(acr,cr,avar,var,ae,e,isyst,ishared,negate,source);
then
(acr,e,aipos,syst,shared);
*/
case (_,_,true,true,_,_,_,_,_,_,_,_,_,_,_,_)
equation
replaceableAlias(var1);
replaceableAlias(var2);
i1 = BackendVariable.calcAliasKey(cr1,var1);
i2 = BackendVariable.calcAliasKey(cr2,var2);
b = intGt(i2,i1);
((acr,avar,aipos,ae,cr,var,e)) = Util.if_(b,(cr2,var2,ipos2,e2,cr1,var1,e1),(cr1,var1,ipos1,e1,cr2,var2,e2));
(syst,shared) = selectAlias2(acr,cr,avar,var,ae,e,syst,shared,negate,source);
(syst,shared) = selectAlias2(acr,cr,avar,var,ae,e,isyst,ishared,negate,source);
then
(acr,e,aipos,syst,shared);
case (_,_,_,_,_,_,_,_,syst,shared,_,_)
case (_,_,true,false,_,_,_,_,_,_,_,_,_,_,_,_)
equation
replaceableAlias(var1);
(syst,shared) = selectAlias2(cr1,cr2,var1,var2,e1,e2,syst,shared,negate,source);
(syst,shared) = selectAlias2(cr1,cr2,var1,var2,e1,e2,isyst,ishared,negate,source);
then
(cr1,e2,ipos1,syst,shared);
case (_,_,_,_,_,_,_,_,syst,shared,_,_)
case (_,_,false,true,_,_,_,_,_,_,_,_,_,_,_,_)
equation
replaceableAlias(var2);
(syst,shared) = selectAlias2(cr2,cr1,var2,var1,e2,e1,syst,shared,negate,source);
(syst,shared) = selectAlias2(cr2,cr1,var2,var1,e2,e1,isyst,ishared,negate,source);
then
(cr2,e1,ipos2,syst,shared);
end matchcontinue;
end match;
end selectAlias1;


Expand Down
34 changes: 31 additions & 3 deletions Compiler/BackEnd/BackendVariable.mo
Original file line number Diff line number Diff line change
Expand Up @@ -3704,7 +3704,7 @@ protected function getNonZeroStart
input Option<DAE.Exp> so "StartOrigin";
input DAE.Exp exp2;
input Option<DAE.Exp> sao "StartOrigin";
input BackendDAE.Variables knVars "the KnownVars, needd to report Warnings";
input BackendDAE.Variables knVars "the KnownVars, need to report Warnings";
output DAE.Exp outExp;
output Option<DAE.Exp> outStartOrigin;
algorithm
Expand All @@ -3715,15 +3715,15 @@ algorithm
Integer i,ia;
Boolean b;
Option<DAE.Exp> origin;
case (_,_,_,_,_)
/* case (_,_,_,_,_)
equation
true = Expression.isZero(exp2);
then (exp1,so);
case (_,_,_,_,_)
equation
true = Expression.isZero(exp1);
then (exp2,sao);
case (_,_,_,_,_)
*/ case (_,_,_,_,_)
equation
true = Expression.expEqual(exp1,exp2);
// use highest origin
Expand Down Expand Up @@ -4056,4 +4056,32 @@ algorithm
end match;
end selfGeneratedVar;


public function varStateSelectPrioAlias
"function varStateSelectPrioAlias
Helper function to calculateVarPriorities.
Calculates a priority contribution bases on the stateSelect attribute."
input BackendDAE.Var v;
output Integer prio;
protected
DAE.StateSelect ss;
algorithm
ss := varStateSelect(v);
prio := varStateSelectPrioAlias2(ss);
end varStateSelectPrioAlias;

protected function varStateSelectPrioAlias2
"helper function to varStateSelectPrioAlias"
input DAE.StateSelect ss;
output Integer prio;
algorithm
prio := match(ss)
case (DAE.NEVER()) then -1;
case (DAE.AVOID()) then 0;
case (DAE.DEFAULT()) then 1;
case (DAE.PREFER()) then 2;
case (DAE.ALWAYS()) then 3;
end match;
end varStateSelectPrioAlias2;

end BackendVariable;
62 changes: 27 additions & 35 deletions Compiler/BackEnd/IndexReduction.mo
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ algorithm
eqns1 = List.map1r(eqns,arrayGet,mapIncRowEqn);
eqns1 = List.unique(eqns1);
Debug.fcall(Flags.BLT_DUMP, print, "Reduce Index\nmarked equations: ");
Debug.fcall(Flags.BLT_DUMP, BackendDump.debuglst, (eqns,intString," ","\n"));
Debug.fcall(Flags.BLT_DUMP, BackendDump.debuglst, (eqns1,intString," ","\n"));
Debug.fcall(Flags.BLT_DUMP, print, BackendDump.dumpMarkedEqns(isyst, eqns1));
// diff Alias does not yet work proper
//(syst,shared,ass1,ass2,so1,orgEqnsLst,mapEqnIncRow,mapIncRowEqn,changedeqns,eqns1) = differentiateAliasEqns(isyst,ishared,eqns1,inAssignments1,inAssignments2,so,orgEqnsLst,mapEqnIncRow,mapIncRowEqn,{},{});
Expand Down Expand Up @@ -652,8 +652,8 @@ algorithm
(cr1,i1,cr2,exp2,i2,iv);
else
equation
p1 = varStateSelectPrioAlias(var1);
p2 = varStateSelectPrioAlias(var2);
p1 = BackendVariable.varStateSelectPrioAlias(var1);
p2 = BackendVariable.varStateSelectPrioAlias(var2);
((cra,ia,exps,vara,crs,is)) = Util.if_(intGt(p1,p2),(cr2,i2,exp1,var2,cr1,i1),(cr1,i1,exp2,var1,cr2,i2));
vara = BackendVariable.setVarKind(vara, BackendDAE.DUMMY_STATE());
v = BackendVariable.addVar(vara,iv);
Expand All @@ -662,33 +662,6 @@ algorithm
end match;
end selectAliasState;

protected function varStateSelectPrioAlias
"function varStateSelectPrioAlias
Helper function to calculateVarPriorities.
Calculates a priority contribution bases on the stateSelect attribute."
input BackendDAE.Var v;
output Integer prio;
protected
DAE.StateSelect ss;
algorithm
ss := BackendVariable.varStateSelect(v);
prio := varStateSelectPrioAlias2(ss);
end varStateSelectPrioAlias;

protected function varStateSelectPrioAlias2
"helper function to varStateSelectPrioAlias"
input DAE.StateSelect ss;
output Integer prio;
algorithm
prio := match(ss)
case (DAE.NEVER()) then -1;
case (DAE.AVOID()) then 0;
case (DAE.DEFAULT()) then 1;
case (DAE.PREFER()) then 2;
case (DAE.ALWAYS()) then 3;
end match;
end varStateSelectPrioAlias2;

protected function replaceAliasState
"function: replaceAliasState
author: Frenkel TUD 2012-06"
Expand Down Expand Up @@ -1823,36 +1796,55 @@ protected function varStateSelectHeuristicPrio
input BackendDAE.StateOrder so;
output Real prio;
protected
Real prio1,prio2,prio3,prio4;
Real prio1,prio2,prio3,prio4,prio5;
algorithm
prio1 := varStateSelectHeuristicPrio1(v);
prio2 := varStateSelectHeuristicPrio2(v);
prio3 := varStateSelectHeuristicPrio3(v);
prio4 := varStateSelectHeuristicPrio4(v,so,vars);
prio:= prio1 +. prio2 +. prio3 +. prio4;
dumpvarStateSelectHeuristicPrio(prio1,prio2,prio3,prio4);
prio5 := varStateSelectHeuristicPrio5(v);
prio:= prio1 +. prio2 +. prio3 +. prio4 +. prio5;
dumpvarStateSelectHeuristicPrio(prio1,prio2,prio3,prio4,prio5);
end varStateSelectHeuristicPrio;

protected function dumpvarStateSelectHeuristicPrio
input Real Prio1;
input Real Prio2;
input Real Prio3;
input Real Prio4;
input Real Prio5;
algorithm
_ := matchcontinue(Prio1,Prio2,Prio3,Prio4)
case(_,_,_,_)
_ := matchcontinue(Prio1,Prio2,Prio3,Prio4,Prio5)
case(_,_,_,_,_)
equation
true = Flags.isSet(Flags.DUMMY_SELECT);
print("Prio 1 : " +& realString(Prio1) +& "\n");
print("Prio 2 : " +& realString(Prio2) +& "\n");
print("Prio 3 : " +& realString(Prio3) +& "\n");
print("Prio 4 : " +& realString(Prio4) +& "\n");
print("Prio 5 : " +& realString(Prio5) +& "\n");
then
();
else then ();
end matchcontinue;
end dumpvarStateSelectHeuristicPrio;

protected function varStateSelectHeuristicPrio5
"function varStateSelectHeuristicPrio5
author: Frenkel TUD 2012-10
Helper function to varStateSelectHeuristicPrio.
added prio for states/variables, good if name is short"
input BackendDAE.Var v;
output Real prio;
protected
DAE.ComponentRef cr;
Integer d;
algorithm
BackendDAE.VAR(varName=cr) := v;
d := ComponentReference.crefDepth(cr);
prio := realDiv(intReal(d),-10.0);
end varStateSelectHeuristicPrio5;

protected function varStateSelectHeuristicPrio4
"function varStateSelectHeuristicPrio4
author: Frenkel TUD 2012-08
Expand Down
2 changes: 1 addition & 1 deletion Compiler/runtime/BackendDAEEXT_omc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ extern void BackendDAEEXT_setIncidenceMatrix(modelica_integer nvars, modelica_in
col_ptrs[i] = j;
while(MMC_GETHDR(ie) == MMC_CONSHDR) {
i1 = MMC_UNTAGFIXNUM(MMC_CAR(ie));
if (i1>=0) {
if (i1>0) {
col_ids[j++] = i1-1;
}
ie = MMC_CDR(ie);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/runtime/BackendDAEEXT_rml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ RML_BEGIN_LABEL(BackendDAEEXT__setIncidenceMatrix)
col_ptrs[i] = j;
while(RML_GETHDR(ie) == RML_CONSHDR) {
i1 = RML_UNTAGFIXNUM(RML_CAR(ie));
if (i1>=0) {
if (i1>0) {
col_ids[j++] = i1-1;
}
ie = RML_CDR(ie);
Expand Down

0 comments on commit 55607d0

Please sign in to comment.