Skip to content

Commit

Permalink
- speedup dynamic state selection (10 times faster)
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12904 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Sep 12, 2012
1 parent c78f7e5 commit dc540c3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 45 deletions.
8 changes: 8 additions & 0 deletions Compiler/BackEnd/BackendDump.mo
Expand Up @@ -1076,6 +1076,14 @@ algorithm
end matchcontinue;
end dumpEqnsSolved2;

public function dumpEqnsArray
"function: dumpEqnsArray
Helper function to dump."
input BackendDAE.EquationArray eqns;
algorithm
dumpEqns2(BackendDAEUtil.equationList(eqns), 1);
end dumpEqnsArray;

public function dumpEqns
"function: dumpEqns
Helper function to dump."
Expand Down
110 changes: 70 additions & 40 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -2274,8 +2274,8 @@ algorithm
case (_,_)
equation
true = intGt(varsSize(inDelVars),0);
newvars = BackendDAEUtil.emptyVars();
((_,newvars)) = traverseBackendDAEVars(inVariables,deleteVars2,(inDelVars,newvars));
newvars = traverseBackendDAEVars(inDelVars,deleteVars1,inVariables);
newvars = BackendDAEUtil.listVar1(BackendDAEUtil.varList(newvars));
then
newvars;
else
Expand All @@ -2284,30 +2284,23 @@ algorithm
end matchcontinue;
end deleteVars;

protected function deleteVars2
protected function deleteVars1
"autor: Frenkel TUD 2010-11"
input tuple<BackendDAE.Var, tuple<BackendDAE.Variables,BackendDAE.Variables>> inTpl;
output tuple<BackendDAE.Var, tuple<BackendDAE.Variables,BackendDAE.Variables>> outTpl;
input tuple<BackendDAE.Var, BackendDAE.Variables> inTpl;
output tuple<BackendDAE.Var, BackendDAE.Variables> outTpl;
algorithm
outTpl:=
matchcontinue (inTpl)
outTpl:= match (inTpl)
local
BackendDAE.Var v;
BackendDAE.Variables vars,vars1,delvars;
BackendDAE.Variables vars;
DAE.ComponentRef cr;
case ((v as BackendDAE.VAR(varName = cr),(delvars,vars)))
equation
(_::{},_) = getVar(cr,delvars) "alg var deleted" ;
then
((v,(delvars,vars)));
case ((v as BackendDAE.VAR(varName = cr),(delvars,vars)))
case ((v as BackendDAE.VAR(varName = cr),vars))
equation
failure((_,_) = getVar(cr,delvars)) "alg var not deleted" ;
vars1 = addVar(v,vars);
vars = removeCref(cr,vars) "alg var deleted" ;
then
((v,(delvars,vars1)));
end matchcontinue;
end deleteVars2;
((v,vars));
end match;
end deleteVars1;

public function deleteVar
"function: deleteVar
Expand All @@ -2319,41 +2312,78 @@ public function deleteVar
algorithm
outVariables := match (inComponentRef,inVariables)
local
BackendDAE.Variables v,newvars,newvars_1;
BackendDAE.Variables vars;
DAE.ComponentRef cr;
case (cr,v)
list<Integer> ilst;
case (cr,_)
equation
newvars = BackendDAEUtil.emptyVars();
((_,newvars_1)) = traverseBackendDAEVars(v,deleteVar2,(cr,newvars));
(_,ilst) = getVar(cr,inVariables);
(vars,_) = removeVars(ilst,inVariables,{});
vars = BackendDAEUtil.listVar1(BackendDAEUtil.varList(vars));
then
newvars_1;
vars;
end match;
end deleteVar;

protected function deleteVar2
"autor: Frenkel TUD 2010-11"
input tuple<BackendDAE.Var, tuple<DAE.ComponentRef,BackendDAE.Variables>> inTpl;
output tuple<BackendDAE.Var, tuple<DAE.ComponentRef,BackendDAE.Variables>> outTpl;
public function removeCref
"function: removeCref
author: Frenkel TUD 2012-09
Deletes a variable from Variables."
input DAE.ComponentRef inComponentRef;
input BackendDAE.Variables inVariables;
output BackendDAE.Variables outVariables;
algorithm
outTpl:=
matchcontinue (inTpl)
outVariables := matchcontinue (inComponentRef,inVariables)
local
BackendDAE.Variables vars;
DAE.ComponentRef cr;
list<Integer> ilst;
case (cr,_)
equation
(_,ilst) = getVar(cr,inVariables);
(vars,_) = removeVars(ilst,inVariables,{});
then
vars;
case (cr,_)
// equation
// BackendDump.debugStrCrefStr(("var ",cr," not in inVariables\n"));
then
inVariables;
end matchcontinue;
end removeCref;

public function removeVars
"function: removeVar
author: Frenkel TUD 2012-09
Removes vars from the vararray but does not scaling down the array"
input list<Integer> inVarPos "Position of vars to delete 1 based";
input BackendDAE.Variables inVariables;
input list<BackendDAE.Var> iAcc;
output BackendDAE.Variables outVariables;
output list<BackendDAE.Var> outVars "deleted vars in reverse order";
algorithm
(outVariables,outVars) := matchcontinue(inVarPos,inVariables,iAcc)
local
BackendDAE.Variables vars;
list<Integer> ilst;
Integer i;
BackendDAE.Var v;
BackendDAE.Variables vars,vars1;
DAE.ComponentRef cr,dcr;
case ((v as BackendDAE.VAR(varName = cr),(dcr,vars)))
list<BackendDAE.Var> acc;
case({},_,_) then (inVariables,iAcc);
case(i::ilst,_,_)
equation
true = ComponentReference.crefEqualNoStringCompare(cr, dcr);
(vars,v) = removeVar(i,inVariables);
(vars,acc) = removeVars(ilst,vars,v::iAcc);
then
((v,(dcr,vars)));
case ((v as BackendDAE.VAR(varName = cr),(dcr,vars)))
(vars,acc);
case(i::ilst,_,_)
equation
vars1 = addVar(v,vars);
(vars,acc) = removeVars(ilst,inVariables,iAcc);
then
((v,(dcr,vars1)));
(vars,acc);
end matchcontinue;
end deleteVar2;

end removeVars;
public function removeVar
"function: removeVar
author: Frenkel TUD 2011-04
Expand Down
11 changes: 6 additions & 5 deletions Compiler/BackEnd/IndexReduction.mo
Expand Up @@ -841,7 +841,7 @@ algorithm
ht := HashTable2.emptyHashTable();
(systs,shared,ht) := mapdynamicStateSelection(systs,shared,inArgs,{},ht);
shared := replaceDummyDerivativesShared(shared,ht);
outDAE := BackendDAE.DAE(systs,shared);
outDAE := BackendDAE.DAE(systs,shared);
end dynamicStateSelection;

protected function mapdynamicStateSelection
Expand Down Expand Up @@ -940,7 +940,7 @@ algorithm
varlst = List.filter(varlst, notVarStateSelectAlways);
freestatevars = listLength(varlst);
orgeqnscount = countOrgEqns(orgEqnsLst,0);

(dummyStates,syst,shared) = processComps(freestatevars,varlst,orgeqnscount,comps,syst,ishared,ass2,(so,orgEqnsLst,mapEqnIncRow,mapIncRowEqn),hov,{});
enqnslst = List.flatten(List.map(orgEqnsLst,Util.tuple22));
syst = BackendEquation.equationsAddDAE(enqnslst, syst);
Expand Down Expand Up @@ -1637,11 +1637,12 @@ algorithm
equation
// if there is only one var select it because there is no choice
Debug.fcall(Flags.BLT_DUMP, print, "single var and eqn\n");
Debug.fcall(Flags.BLT_DUMP, BackendDump.dumpEqSystem, BackendDAE.EQSYSTEM(vars,eqns,NONE(),NONE(),BackendDAE.NO_MATCHING()));
Debug.fcall(Flags.BLT_DUMP, BackendDump.dumpVarsArray, vars);
Debug.fcall(Flags.BLT_DUMP, BackendDump.dumpEqnsArray, eqns);
v = BackendVariable.getVarAt(vars,1);
cr = BackendVariable.varCref(v);
Debug.fcall(Flags.BLT_DUMP, BackendDump.debugStrCrefStr, ("Select ",cr," as dummyState\n"));
hov1 = BackendVariable.deleteVar(cr,hov);
hov1 = BackendVariable.removeCref(cr,hov);
lov = BackendVariable.addVar(v,inLov);
then
(hov1,cr::dummystates,lov,isyst,ishared);
Expand Down Expand Up @@ -3095,7 +3096,7 @@ algorithm
case ((cr,s)::rest,_,_,_,_,_,_)
equation
v = BackendVariable.getVarAt(vars,s);
hov1 = BackendVariable.deleteVar(cr,hov);
hov1 = BackendVariable.removeCref(cr,hov);
lov = BackendVariable.addVar(v,inLov);
(hov1,lov, dummystates) = selectDummyStates(rest,i+1,nstates,vars,hov1,lov,cr::inDummyStates);
then
Expand Down

0 comments on commit dc540c3

Please sign in to comment.