Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 978d276

Browse files
hkielOpenModelica-Hudson
authored andcommitted
avoid recursion and matchcontinue in getSparsePattern
reduce memory overhead in ExpressionSimplify.simplifyAdd Belonging to [master]: - #2005
1 parent 679140a commit 978d276

File tree

2 files changed

+47
-71
lines changed

2 files changed

+47
-71
lines changed

Compiler/BackEnd/SymbolicJacobian.mo

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,7 @@ protected function getSparsePattern2
14511451
protected
14521452
list<Integer> localList;
14531453
algorithm
1454-
localList := getSparsePatternHelp(inInputVars, invarSparse, inMark, inUsed, inmarkValue, {});
1454+
localList := getSparsePatternHelp(inInputVars, invarSparse, inMark, inUsed, inmarkValue);
14551455
List.map2_0(inSolvedVars, Array.updateIndexFirst, localList, invarSparse);
14561456
List.map2_0(inEqns, Array.updateIndexFirst, localList, ineqnSparse);
14571457
end getSparsePattern2;
@@ -1462,47 +1462,32 @@ protected function getSparsePatternHelp
14621462
input array<Integer> inMark;
14631463
input array<Integer> inUsed;
14641464
input Integer inmarkValue;
1465-
input list<Integer> inLocalList;
1466-
output list<Integer> outLocalList;
1467-
algorithm
1468-
outLocalList := match (inInputVars, invarSparse, inMark, inUsed, inmarkValue, inLocalList)
1469-
local
1470-
list<Integer> localList, varSparse, rest;
1471-
Integer arrayElement, var;
1472-
case ({},_,_,_,_,_) then inLocalList;
1473-
case (var::rest,_,_,_,_,_)
1474-
equation
1475-
arrayElement = arrayGet(inUsed, var);
1476-
localList = if intEq(1, arrayElement) then getSparsePatternHelp2(var, inMark, inmarkValue, inLocalList) else inLocalList;
1465+
output list<Integer> outLocalList = {};
1466+
protected
1467+
Integer arrayElement;
1468+
list<Integer> varSparse;
1469+
algorithm
1470+
for var in inInputVars loop
1471+
arrayElement := arrayGet(inUsed, var);
1472+
if intEq(1, arrayElement) then
1473+
arrayElement := arrayGet(inMark, var);
1474+
if not intEq(inmarkValue, arrayElement) then
1475+
arrayUpdate(inMark, var, inmarkValue);
1476+
outLocalList := var::outLocalList;
1477+
end if;
1478+
end if;
14771479

1478-
varSparse = arrayGet(invarSparse, var);
1479-
localList = List.fold2(varSparse, getSparsePatternHelp2, inMark, inmarkValue, localList);
1480-
localList = getSparsePatternHelp(rest, invarSparse, inMark, inUsed, inmarkValue, localList);
1481-
then localList;
1482-
end match;
1480+
varSparse := arrayGet(invarSparse, var);
1481+
for v in varSparse loop
1482+
arrayElement := arrayGet(inMark, v);
1483+
if not intEq(inmarkValue, arrayElement) then
1484+
arrayUpdate(inMark, v, inmarkValue);
1485+
outLocalList := v::outLocalList;
1486+
end if;
1487+
end for;
1488+
end for;
14831489
end getSparsePatternHelp;
14841490

1485-
protected function getSparsePatternHelp2
1486-
input Integer inInputVar;
1487-
input array<Integer> inMark;
1488-
input Integer inmarkValue;
1489-
input list<Integer> inLocalList;
1490-
output list<Integer> outLocalList;
1491-
algorithm
1492-
outLocalList := matchcontinue(inInputVar, inMark, inmarkValue, inLocalList)
1493-
local
1494-
Integer arrayElement;
1495-
case (_,_,_,_)
1496-
equation
1497-
arrayElement = arrayGet(inMark, inInputVar);
1498-
false = intEq(inmarkValue, arrayElement);
1499-
arrayUpdate(inMark, inInputVar, inmarkValue);
1500-
then inInputVar::inLocalList;
1501-
else
1502-
then inLocalList;
1503-
end matchcontinue;
1504-
end getSparsePatternHelp2;
1505-
15061491
public function transposeSparsePattern
15071492
input list<list<Integer>> inSparsePattern;
15081493
input array<list<Integer>> inAccumList;

Compiler/FrontEnd/ExpressionSimplify.mo

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,15 +2864,17 @@ protected function simplifyAddJoinTerms
28642864
output list<tuple<DAE.Exp, Real>> outTplExpRealLst = {};
28652865
protected
28662866
list<tuple<DAE.Exp, Real>> tplExpRealLst = inTplExpRealLst;
2867+
tuple<DAE.Exp, Real> t;
28672868
DAE.Exp e;
28682869
Real coeff, coeff2;
28692870
algorithm
28702871

28712872
while not listEmpty(tplExpRealLst) loop
2872-
(e, coeff) :: tplExpRealLst := tplExpRealLst;
2873+
t :: tplExpRealLst := tplExpRealLst;
2874+
(e, coeff) := t;
28732875
(coeff2, tplExpRealLst) := simplifyAddJoinTermsFind(e, tplExpRealLst);
28742876
coeff := coeff + coeff2;
2875-
outTplExpRealLst := (e, coeff) :: outTplExpRealLst;
2877+
outTplExpRealLst := (if coeff2==0 then t else (e, coeff)) :: outTplExpRealLst;
28762878
end while;
28772879
//outTplExpRealLst := listReverse(outTplExpRealLst);
28782880
end simplifyAddJoinTerms;
@@ -2882,31 +2884,21 @@ protected function simplifyAddJoinTermsFind
28822884
Helper function to simplifyAddJoinTerms, finds all occurences of Expression."
28832885
input DAE.Exp inExp;
28842886
input list<tuple<DAE.Exp, Real>> inTplExpRealLst;
2885-
output Real outReal;
2886-
output list<tuple<DAE.Exp, Real>> outTplExpRealLst;
2887+
output Real outReal = 0.0;
2888+
output list<tuple<DAE.Exp, Real>> outTplExpRealLst = {};
2889+
protected
2890+
DAE.Exp e;
2891+
Real coeff;
28872892
algorithm
2888-
(outReal,outTplExpRealLst) := matchcontinue (inExp,inTplExpRealLst)
2889-
local
2890-
Real coeff2,coeff3,coeff;
2891-
list<tuple<DAE.Exp, Real>> res,rest;
2892-
DAE.Exp e,e2;
2893-
2894-
case (_,{}) then (0.0,{});
2895-
2896-
case (e,((e2,coeff) :: rest))
2897-
equation
2898-
true = Expression.expEqual(e, e2);
2899-
(coeff2,res) = simplifyAddJoinTermsFind(e, rest);
2900-
coeff3 = coeff + coeff2;
2901-
then
2902-
(coeff3,res);
2903-
2904-
case (e,((e2,coeff) :: rest)) /* not Expression.expEqual */
2905-
equation
2906-
(coeff2,res) = simplifyAddJoinTermsFind(e, rest);
2907-
then
2908-
(coeff2,((e2,coeff) :: res));
2909-
end matchcontinue;
2893+
for t in inTplExpRealLst loop
2894+
(e,coeff) := t;
2895+
if Expression.expEqual(inExp, e) then
2896+
outReal := outReal + coeff;
2897+
else
2898+
outTplExpRealLst := t::outTplExpRealLst;
2899+
end if;
2900+
end for;
2901+
outTplExpRealLst := Dangerous.listReverseInPlace(outTplExpRealLst);
29102902
end simplifyAddJoinTermsFind;
29112903

29122904
protected function simplifyAddMakeMul
@@ -2919,7 +2911,6 @@ protected
29192911
tuple<DAE.Exp, Real> tplExpReal;
29202912
algorithm
29212913
for tplExpReal in inTplExpRealLst loop
2922-
29232914
outExpLst := matchcontinue (tplExpReal)
29242915
local
29252916
DAE.Exp e;
@@ -2951,14 +2942,14 @@ protected function simplifyBinaryAddCoeff2
29512942
input DAE.Exp inExp;
29522943
output tuple<DAE.Exp, Real> outRes;
29532944
algorithm
2954-
outRes := matchcontinue (inExp)
2945+
outRes := match (inExp)
29552946
local
29562947
DAE.Exp exp,e1,e2,e;
29572948
Real coeff,coeff_1;
29582949
Integer icoeff;
29592950
Type tp;
29602951

2961-
case ((exp as DAE.CREF())) then ((exp,1.0));
2952+
case (DAE.CREF()) then ((inExp,1.0));
29622953

29632954
case (DAE.UNARY(operator = DAE.UMINUS(ty = DAE.T_REAL()), exp = exp))
29642955
equation
@@ -2985,14 +2976,14 @@ algorithm
29852976
((e1,coeff_1));
29862977

29872978
case (DAE.BINARY(exp1 = e1,operator = DAE.ADD(),exp2 = e2))
2988-
equation
2989-
true = Expression.expEqual(e1, e2);
2979+
guard
2980+
Expression.expEqual(e1, e2)
29902981
then
29912982
((e1,2.0));
29922983

29932984
else ((inExp,1.0));
29942985

2995-
end matchcontinue;
2986+
end match;
29962987
end simplifyBinaryAddCoeff2;
29972988

29982989
protected function simplifyBinaryMulCoeff2

0 commit comments

Comments
 (0)