Skip to content

Commit

Permalink
- to save memory start to get rid of some BackendDAEUtil.equationList…
Browse files Browse the repository at this point in the history
… by traversing the equation array direct without convert to list

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@6966 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Nov 11, 2010
1 parent 82d3448 commit 4672844
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 36 deletions.
30 changes: 14 additions & 16 deletions Compiler/BackendDAEOptimize.mo
Expand Up @@ -344,33 +344,31 @@ protected Integer elimLevel;
algorithm
elimLevel := RTOpts.eliminationLevel();
RTOpts.setEliminationLevel(2) "Full elimination";
numEqns := countSimpleEquations2(BackendDAEUtil.equationList(eqns),0);
numEqns := BackendEquation.traverseBackendDAEEqns(eqns,countSimpleEquations2,0);
RTOpts.setEliminationLevel(elimLevel);
end countSimpleEquations;

protected function countSimpleEquations2
input list<BackendDAE.Equation> eqns;
input Integer partialSum "to enable tail-recursion";
output Integer numEqns;
input tuple<BackendDAE.Equation, Integer> inTpl;
output tuple<BackendDAE.Equation, Integer> outTpl;
algorithm
numEqns := matchcontinue(eqns,partialSum)
local BackendDAE.Equation e;
case({},partialSum) then partialSum;

case (e::eqns,partialSum) equation
outTpl := matchcontinue(inTpl)
local
BackendDAE.Equation e;
Integer partialSum;
case ((e,partialSum))
equation
(_,_,_) = simpleEquation(e,false);
partialSum = partialSum +1;
then countSimpleEquations2(eqns,partialSum);
then ((e,partialSum+1));

// Swaped args in simpleEquation
case (e::eqns,partialSum) equation
case ((e,partialSum))
equation
(_,_,_) = simpleEquation(e,true);
partialSum = partialSum +1;
then countSimpleEquations2(eqns,partialSum);
then ((e,partialSum+1));

//Not simple eqn.
case (e::eqns,partialSum)
then countSimpleEquations2(eqns,partialSum);
case inTpl then inTpl;
end matchcontinue;
end countSimpleEquations2;

Expand Down
30 changes: 11 additions & 19 deletions Compiler/BackendDAEUtil.mo
Expand Up @@ -54,7 +54,6 @@ protected import Absyn;
protected import BackendDump;
protected import BackendEquation;
protected import BackendVariable;
protected import Builtin;
protected import ComponentReference;
protected import Ceval;
protected import ClassInf;
Expand Down Expand Up @@ -152,8 +151,8 @@ algorithm
((_,expcrefs2)) = traverseBackendDAEExpsEqns(eqns,checkBackendDAEExp,(allvars,expcrefs1));
((_,expcrefs3)) = traverseBackendDAEExpsEqns(reqns,checkBackendDAEExp,(allvars,expcrefs2));
((_,expcrefs4)) = traverseBackendDAEExpsEqns(ieqns,checkBackendDAEExp,(allvars,expcrefs3));
((_,expcrefs5)) = traverseBackendDAEExpsArrayNoCopy(ae,checkBackendDAEExp,traverseBackendDAEExpsArrayEqn,1,arrayLength(ae),(allvars,expcrefs4));
//((_,expcrefs6)) = traverseBackendDAEExpsArrayNoCopy(algs,checkBackendDAEExp,traverseAlgorithmExps,1,arrayLength(algs),(allvars,expcrefs5));
((_,expcrefs5)) = traverseBackendDAEArrayNoCopy(ae,checkBackendDAEExp,traverseBackendDAEExpsArrayEqn,1,arrayLength(ae),(allvars,expcrefs4));
//((_,expcrefs6)) = traverseBackendDAEArrayNoCopy(algs,checkBackendDAEExp,traverseAlgorithmExps,1,arrayLength(algs),(allvars,expcrefs5));
then
expcrefs5;

Expand Down Expand Up @@ -4185,8 +4184,8 @@ algorithm
ext_arg_3 = traverseBackendDAEExpsEqns(eqns,func,ext_arg_2);
ext_arg_4 = traverseBackendDAEExpsEqns(reqns,func,ext_arg_3);
ext_arg_5 = traverseBackendDAEExpsEqns(ieqns,func,ext_arg_4);
ext_arg_6 = traverseBackendDAEExpsArrayNoCopy(ae,func,traverseBackendDAEExpsArrayEqn,1,arrayLength(ae),ext_arg_5);
ext_arg_7 = traverseBackendDAEExpsArrayNoCopy(algs,func,traverseAlgorithmExps,1,arrayLength(algs),ext_arg_6);
ext_arg_6 = traverseBackendDAEArrayNoCopy(ae,func,traverseBackendDAEExpsArrayEqn,1,arrayLength(ae),ext_arg_5);
ext_arg_7 = traverseBackendDAEArrayNoCopy(algs,func,traverseAlgorithmExps,1,arrayLength(algs),ext_arg_6);
then
ext_arg_7;
case (_,_,_)
Expand Down Expand Up @@ -4219,7 +4218,7 @@ algorithm
Type_a ext_arg_1,ext_arg_2,ext_arg_3;
case (BackendDAE.VARIABLES(varArr = BackendDAE.VARIABLE_ARRAY(varOptArr=varOptArr)),func,inTypeA)
equation
ext_arg_1 = traverseBackendDAEExpsArrayNoCopy(varOptArr,func,traverseBackendDAEExpsVar,1,arrayLength(varOptArr),inTypeA);
ext_arg_1 = traverseBackendDAEArrayNoCopy(varOptArr,func,traverseBackendDAEExpsVar,1,arrayLength(varOptArr),inTypeA);
then
ext_arg_1;
case (_,_,_)
Expand All @@ -4230,7 +4229,7 @@ algorithm
end matchcontinue;
end traverseBackendDAEExpsVars;

public function traverseBackendDAEExpsArrayNoCopy "
public function traverseBackendDAEArrayNoCopy "
help function to traverseBackendDAEExps
author: Frenkel TUD"
replaceable type Type_a subtypeof Any;
Expand Down Expand Up @@ -4267,10 +4266,10 @@ algorithm

case(array,func,arrayfunc,pos,len,inTypeB) equation
ext_arg_1 = arrayfunc(array[pos],func,inTypeB);
ext_arg_2 = traverseBackendDAEExpsArrayNoCopy(array,func,arrayfunc,pos+1,len,ext_arg_1);
ext_arg_2 = traverseBackendDAEArrayNoCopy(array,func,arrayfunc,pos+1,len,ext_arg_1);
then ext_arg_2;
end matchcontinue;
end traverseBackendDAEExpsArrayNoCopy;
end traverseBackendDAEArrayNoCopy;

protected function traverseBackendDAEExpsVar "function: traverseBackendDAEExpsVar
author: Frenkel TUD
Expand Down Expand Up @@ -4376,7 +4375,7 @@ algorithm
local
array<Option<BackendDAE.Equation>> equOptArr;
case ((BackendDAE.EQUATION_ARRAY(equOptArr = equOptArr)),func,inTypeA)
then traverseBackendDAEExpsArrayNoCopy(equOptArr,func,traverseBackendDAEExpsOptEqn,1,arrayLength(equOptArr),inTypeA);
then traverseBackendDAEArrayNoCopy(equOptArr,func,traverseBackendDAEExpsOptEqn,1,arrayLength(equOptArr),inTypeA);
case (_,_,_)
equation
Debug.fprintln("failtrace", "- BackendDAE.traverseBackendDAEExpsEqns failed");
Expand All @@ -4386,7 +4385,7 @@ algorithm
end traverseBackendDAEExpsEqns;

protected function traverseBackendDAEExpsOptEqn "function: traverseBackendDAEExpsOptEqn
author: PA
author: Frenkel TUD 2010-11
Helper for traverseBackendDAEExpsEqn."
replaceable type Type_a subtypeof Any;
input Option<BackendDAE.Equation> inEquation;
Expand All @@ -4401,14 +4400,7 @@ algorithm
outTypeA:= matchcontinue (inEquation,func,inTypeA)
local
BackendDAE.Equation eqn;
DAE.Exp e1,e2,e;
list<DAE.Exp> expl,exps;
DAE.ExpType tp;
DAE.ComponentRef cr;
BackendDAE.Value ind;
BackendDAE.WhenEquation elsePart;
DAE.ElementSource source;
Type_a ext_arg_1,ext_arg_2,ext_arg_3;
Type_a ext_arg_1;
case (NONE(),func,inTypeA) then inTypeA;
case (SOME(eqn),func,inTypeA)
equation
Expand Down
61 changes: 61 additions & 0 deletions Compiler/BackendEquation.mo
Expand Up @@ -42,6 +42,7 @@ public import Absyn;
public import BackendDAE;
public import DAE;

protected import BackendDAEUtil;
protected import ComponentReference;
protected import DAEUtil;
protected import Debug;
Expand Down Expand Up @@ -310,6 +311,66 @@ algorithm
end matchcontinue;
end traverseBackendDAEExpList;

public function traverseBackendDAEEqns "function: traverseBackendDAEEqns
author: Frenkel TUD

traverses all equations of a BackendDAE.EquationArray.
"
replaceable type Type_a subtypeof Any;
input BackendDAE.EquationArray inEquationArray;
input FuncExpType func;
input Type_a inTypeA;
output Type_a outTypeA;
partial function FuncExpType
input tuple<BackendDAE.Equation, Type_a> inTpl;
output tuple<BackendDAE.Equation, Type_a> outTpl;
end FuncExpType;
algorithm
outTypeA :=
matchcontinue (inEquationArray,func,inTypeA)
local
array<Option<BackendDAE.Equation>> equOptArr;
case ((BackendDAE.EQUATION_ARRAY(equOptArr = equOptArr)),func,inTypeA)
then BackendDAEUtil.traverseBackendDAEArrayNoCopy(equOptArr,func,traverseBackendDAEOptEqn,1,arrayLength(equOptArr),inTypeA);
case (_,_,_)
equation
Debug.fprintln("failtrace", "- BackendEquation.traverseBackendDAEEqns failed");
then
fail();
end matchcontinue;
end traverseBackendDAEEqns;

protected function traverseBackendDAEOptEqn "function: traverseBackendDAEOptEqn
author: Frenkel TUD 2010-11
Helper for traverseBackendDAEExpsEqns."
replaceable type Type_a subtypeof Any;
input Option<BackendDAE.Equation> inEquation;
input FuncExpType func;
input Type_a inTypeA;
output Type_a outTypeA;
partial function FuncExpType
input tuple<BackendDAE.Equation, Type_a> inTpl;
output tuple<BackendDAE.Equation, Type_a> outTpl;
end FuncExpType;
algorithm
outTypeA:= matchcontinue (inEquation,func,inTypeA)
local
BackendDAE.Equation eqn;
Type_a ext_arg;
case (NONE(),func,inTypeA) then inTypeA;
case (SOME(eqn),func,inTypeA)
equation
((_,ext_arg)) = func((eqn,inTypeA));
then
ext_arg;
case (_,_,_)
equation
Debug.fprintln("failtrace", "- BackendEquation.traverseBackendDAEOptEqn failed");
then
fail();
end matchcontinue;
end traverseBackendDAEOptEqn;

public function equationEqual "Returns true if two equations are equal"
input BackendDAE.Equation e1;
input BackendDAE.Equation e2;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackendVariable.mo
Expand Up @@ -2758,7 +2758,7 @@ algorithm
Type_a ext_arg_1,ext_arg_2,ext_arg_3;
case (BackendDAE.VARIABLES(varArr = BackendDAE.VARIABLE_ARRAY(varOptArr=varOptArr)),func,inTypeA)
equation
ext_arg_1 = BackendDAEUtil.traverseBackendDAEExpsArrayNoCopy(varOptArr,func,traverseBackendDAEVar,1,arrayLength(varOptArr),inTypeA);
ext_arg_1 = BackendDAEUtil.traverseBackendDAEArrayNoCopy(varOptArr,func,traverseBackendDAEVar,1,arrayLength(varOptArr),inTypeA);
then
ext_arg_1;
case (_,_,_)
Expand Down

0 comments on commit 4672844

Please sign in to comment.