From dc540c3e4a3cf8f65545d1b440b5f5b4a013c4c3 Mon Sep 17 00:00:00 2001 From: Jens Frenkel Date: Wed, 12 Sep 2012 23:13:41 +0000 Subject: [PATCH] - speedup dynamic state selection (10 times faster) git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12904 f25d12d1-65f4-0310-ae8a-bbce733d8d8e --- Compiler/BackEnd/BackendDump.mo | 8 ++ Compiler/BackEnd/BackendVariable.mo | 110 ++++++++++++++++++---------- Compiler/BackEnd/IndexReduction.mo | 11 +-- 3 files changed, 84 insertions(+), 45 deletions(-) diff --git a/Compiler/BackEnd/BackendDump.mo b/Compiler/BackEnd/BackendDump.mo index 20f051ba1f5..cdcf7c52089 100644 --- a/Compiler/BackEnd/BackendDump.mo +++ b/Compiler/BackEnd/BackendDump.mo @@ -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." diff --git a/Compiler/BackEnd/BackendVariable.mo b/Compiler/BackEnd/BackendVariable.mo index 8206397b06c..0eb451a5430 100644 --- a/Compiler/BackEnd/BackendVariable.mo +++ b/Compiler/BackEnd/BackendVariable.mo @@ -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 @@ -2284,30 +2284,23 @@ algorithm end matchcontinue; end deleteVars; -protected function deleteVars2 +protected function deleteVars1 "autor: Frenkel TUD 2010-11" - input tuple> inTpl; - output tuple> outTpl; + input tuple inTpl; + output tuple 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 @@ -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 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> inTpl; - output tuple> 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 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 inVarPos "Position of vars to delete 1 based"; + input BackendDAE.Variables inVariables; + input list iAcc; + output BackendDAE.Variables outVariables; + output list outVars "deleted vars in reverse order"; +algorithm + (outVariables,outVars) := matchcontinue(inVarPos,inVariables,iAcc) + local + BackendDAE.Variables vars; + list ilst; + Integer i; BackendDAE.Var v; - BackendDAE.Variables vars,vars1; - DAE.ComponentRef cr,dcr; - case ((v as BackendDAE.VAR(varName = cr),(dcr,vars))) + list 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 diff --git a/Compiler/BackEnd/IndexReduction.mo b/Compiler/BackEnd/IndexReduction.mo index 3a2bdc12f42..74a7999f3a0 100644 --- a/Compiler/BackEnd/IndexReduction.mo +++ b/Compiler/BackEnd/IndexReduction.mo @@ -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 @@ -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); @@ -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); @@ -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