Skip to content

Commit

Permalink
some small optimizations for large models
Browse files Browse the repository at this point in the history
  • Loading branch information
hkiel authored and OpenModelica-Hudson committed Jul 19, 2017
1 parent f50884b commit b69a9eb
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 92 deletions.
10 changes: 5 additions & 5 deletions Compiler/BackEnd/BackendDAETransform.mo
Expand Up @@ -60,11 +60,11 @@ protected
import Flags;
import GC;
import List;
import MetaModelica.Dangerous;
import Sorting;
import SymbolicJacobian;
import System;
import Util;
import MetaModelica.Dangerous;

// =============================================================================
// strongComponents and stuff
Expand Down Expand Up @@ -130,11 +130,11 @@ public function varAssignmentNonScalar
input array<Integer> ass1;
input array<Integer> mapIncRowEqn;
output array<Integer> outAcc;
protected
list<Integer> acc;
algorithm
acc := list(if ass1[i] > 0 then mapIncRowEqn[ass1[i]] else -1 for i in 1:arrayLength(ass1));
outAcc := listArray(acc);
outAcc := Dangerous.arrayCreateNoInit(arrayLength(ass1),-1);
for i in 1:arrayLength(ass1) loop
Dangerous.arrayUpdateNoBoundsChecking(outAcc, i, if Dangerous.arrayGetNoBoundsChecking(ass1,i) > 0 then mapIncRowEqn[Dangerous.arrayGetNoBoundsChecking(ass1,i)] else -1);
end for;
end varAssignmentNonScalar;

protected function analyseStrongComponentsScalar "author: Frenkel TUD 2011-05
Expand Down
7 changes: 6 additions & 1 deletion Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -2048,7 +2048,12 @@ protected
array<Option<BackendDAE.Var>> varOptArr;
algorithm
BackendDAE.VARIABLE_ARRAY(varOptArr=varOptArr) := inArray;
outVars := list(Util.getOption(varOptArr[i]) for i guard isSome(varOptArr[i]) in 1:arrayLength(varOptArr));
outVars := {};
for i in arrayLength(varOptArr):-1:1 loop
if isSome(varOptArr[i]) then
outVars := Util.getOption(varOptArr[i])::outVars;
end if;
end for;
end vararrayList;

/* =======================================================
Expand Down
34 changes: 15 additions & 19 deletions Compiler/BackEnd/ResolveLoops.mo
Expand Up @@ -350,7 +350,7 @@ algorithm
varCrossLst := List.fold2(partitionVars,gatherCrossNodes,mTIn,mIn,{});

// search the partitions for loops
loops := resolveLoops_findLoops2(partition,partitionVars,eqCrossLst,varCrossLst,mIn,mTIn);
loops := resolveLoops_findLoops2(partition,eqCrossLst,varCrossLst,mIn,mTIn);
loopsOut := listAppend(loops,loopsOut);
crossEqsOut := listAppend(eqCrossLst,crossEqsOut);
crossVarsOut := listAppend(varCrossLst,crossVarsOut);
Expand All @@ -363,22 +363,21 @@ end resolveLoops_findLoops;
protected function resolveLoops_findLoops2 "author: Waurich TUD 2014-01
handles the given partition of eqs and vars depending whether there are only varCrossNodes, only EqCrossNodes, both of them or none of them."
input list<Integer> eqsIn;
input list<Integer> varsIn;
input list<Integer> eqCrossLstIn;
input list<Integer> varCrossLstIn;
input BackendDAE.IncidenceMatrix mIn; // the whole system of simpleEquations
input BackendDAE.IncidenceMatrixT mTIn;
output list<list<Integer>> loopsOut;
algorithm
loopsOut := match(eqsIn,varsIn,eqCrossLstIn,varCrossLstIn,mIn,mTIn)
loopsOut := match(eqsIn,eqCrossLstIn,varCrossLstIn,mIn,mTIn)
local
Boolean isNoSingleLoop;
Integer replaceIdx,eqIdx,varIdx,parEqIdx,daeEqIdx;
list<Integer> varCrossLst, eqCrossLst, crossNodes, restNodes, adjCrossNodes, partition, partition2, replEqs, subLoop;
list<list<Integer>> paths, allPaths, simpleLoops, varEqsLst, crossEqLst, paths0, paths1, closedPaths, loopConnectors, connectedPaths;
BackendDAE.Equation resolvedEq, startEq;
list<BackendDAE.Equation> eqLst;
case(_,_,_::_,{},_,_)
case(_,_::_,{},_,_)
equation
//print("partition has only eqCrossNodes\n");
// get the paths between the crossEqNodes and order them according to their length
Expand Down Expand Up @@ -407,7 +406,7 @@ algorithm
//print("all paths to be resolved: \n"+stringDelimitList(List.map(paths0,HpcOmTaskGraph.intLstString)," / ")+"\n");
then
paths0;
case(_,_,{},_::_,_,_)
case(_,{},_::_,_,_)
equation
//print("partition has only varCrossNodes\n");
// get the paths between the crossVarNodes and order them according to their length
Expand All @@ -427,7 +426,7 @@ algorithm
//print("solve the smallest loops: \n"+stringDelimitList(List.map(closedPaths,HpcOmTaskGraph.intLstString)," / ")+"\n");
then
closedPaths;
case(_,_,{},{},_,_)
case(_,{},{},_,_)
algorithm
// no crossNodes
//print("no crossNodes\n");
Expand All @@ -440,7 +439,7 @@ algorithm
end for;
then
{subLoop};
case(_,_,_::_,_::_,_,_)
case(_,_::_,_::_,_,_)
algorithm
//print("there are both varCrossNodes and eqNodes\n");
//at least get paths of length 2 between eqCrossNodes
Expand Down Expand Up @@ -500,7 +499,7 @@ algorithm
sharedVars := List.removeOnTrue(adjVar, intEq, sharedVars);
//print("all sharedVars "+stringDelimitList(List.map(sharedVars, intString),",")+"\n");
if (intGe(listLength(sharedVars),1)) then
newPath := listAppend({adjEq},{crossEq});
newPath := adjEq::{crossEq};
paths := newPath :: paths;
//print("found path "+stringDelimitList(List.map(newPath,intString)," ; ")+"\n");
end if;
Expand Down Expand Up @@ -894,7 +893,7 @@ protected function getDoubles "author: Waurich TUD 2014-01
output list<list<Integer>> lstOut;
replaceable type ElementType subtypeof Any;
algorithm
lstOut := matchcontinue(elemLstIn,lstIn)
lstOut := match(elemLstIn,lstIn)
local
list<Integer> elem;
list<list<Integer>> lst, elemLst;
Expand All @@ -904,17 +903,14 @@ algorithm
lstIn;
case(elem::elemLst,_)
equation
elem = List.getMember(elem,elemLst);
lst = getDoubles(elemLst,elem::lstIn);
then
lst;
else
equation
(_::elemLst) = elemLstIn;
lst = getDoubles(elemLst,lstIn);
if listMember(elem,elemLst) then
lst = getDoubles(elemLst,elem::lstIn);
else
lst = getDoubles(elemLst,lstIn);
end if;
then
lst;
end matchcontinue;
end match;
end getDoubles;

protected function getEqNodesForVarLoop "author: Waurich TUD 2013-01
Expand Down Expand Up @@ -2087,7 +2083,7 @@ algorithm
//print("eqs: \n"+stringDelimitList(List.map(eqs,intString)," / ")+"\n");
eqs := getDoublicates(eqs);
//print("eqs: \n"+stringDelimitList(List.map(eqs,intString)," / ")+"\n");
lstOut := List.unique(eq::eqs);
lstOut := List.consOnTrue(not listMember(eq, eqs), eq, eqs);
end getEqPairs;

protected function chooseEquation
Expand Down
4 changes: 2 additions & 2 deletions Compiler/FFrontEnd/FNode.mo
Expand Up @@ -241,7 +241,7 @@ algorithm
case (SCode.IMPORT(imp = imp as Absyn.UNQUAL_IMPORT()),
FCore.IMPORT_TABLE(hidden, qual_imps, unqual_imps))
equation
unqual_imps = List.unique(imp :: unqual_imps);
unqual_imps = List.unionElt(imp, unqual_imps);
then
FCore.IMPORT_TABLE(hidden, qual_imps, unqual_imps);

Expand All @@ -251,7 +251,7 @@ algorithm
equation
imp = translateQualifiedImportToNamed(imp);
checkUniqueQualifiedImport(imp, qual_imps, info);
qual_imps = List.unique(imp :: qual_imps);
qual_imps = List.unionElt(imp, qual_imps);
then
FCore.IMPORT_TABLE(hidden, qual_imps, unqual_imps);
end match;
Expand Down
49 changes: 22 additions & 27 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -359,7 +359,7 @@ protected function unelabDimensionToFillExp
input DAE.Dimension inDim;
output Absyn.Exp outExp;
algorithm
outExp := matchcontinue (inDim)
outExp := match (inDim)
local
Integer i;
DAE.Exp e;
Expand All @@ -370,7 +370,7 @@ algorithm

else Absyn.INTEGER(1); /* Probably bad, but only used with zero-length arrays */

end matchcontinue;
end match;
end unelabDimensionToFillExp;

protected function unelabReductionIterator
Expand Down Expand Up @@ -1430,7 +1430,7 @@ public function arrayAppend
input DAE.Exp rest;
output DAE.Exp array;
algorithm
array := matchcontinue(head, rest)
array := match(head, rest)
local
DAE.Type ty;
Boolean scalar;
Expand All @@ -1454,7 +1454,7 @@ algorithm
Debug.traceln("- Expression.arrayAppend failed.");
then
fail();
end matchcontinue;
end match;
end arrayAppend;


Expand Down Expand Up @@ -3325,21 +3325,21 @@ end makeSign;


public function makeNestedIf "creates a nested if expression given a list of conditions and
guared expressions and a default value (the else branch)"
guarded expressions and a default value (the else branch)"
input list<DAE.Exp> inConds "conditions";
input list<DAE.Exp> inTbExps " guarded expressions, for each condition";
input DAE.Exp fExp "default value, else branch";
output DAE.Exp ifExp;
algorithm
ifExp := matchcontinue(inConds,inTbExps,fExp)
ifExp := match(inConds,inTbExps,fExp)
local DAE.Exp c,tbExp; list<DAE.Exp> conds, tbExps;
case({c},{tbExp},_)
then DAE.IFEXP(c,tbExp,fExp);
case(c::conds,tbExp::tbExps,_)
equation
ifExp = makeNestedIf(conds,tbExps,fExp);
then DAE.IFEXP(c,tbExp,ifExp);
end matchcontinue;
end match;
end makeNestedIf;

public function makeCrefExp
Expand Down Expand Up @@ -4756,7 +4756,7 @@ public function arrayFill
input DAE.Exp inExp;
output DAE.Exp oExp;
algorithm
oExp := matchcontinue(dims,inExp)
oExp := match(dims,inExp)

case({},_) then inExp;

Expand All @@ -4766,7 +4766,7 @@ algorithm
then
oExp;

end matchcontinue;
end match;
end arrayFill;

protected function arrayFill2
Expand Down Expand Up @@ -10647,17 +10647,17 @@ end isNotWild;

public function dimensionsToExps "Takes a list of dimensions and select the expressions dimensions, returning a list of expressions"
input list<DAE.Dimension> dims;
input list<DAE.Exp> acc;
output list<DAE.Exp> exps;
output list<DAE.Exp> exps = {};
algorithm
exps := match (dims,acc)
local
list<DAE.Dimension> rest;
DAE.Exp exp;
case ({},_) then listReverse(acc);
case (DAE.DIM_EXP(exp)::rest,_) then dimensionsToExps(rest,exp::acc);
case (_::rest,_) then dimensionsToExps(rest,acc);
end match;
for d in dims loop
exps := match (d)
local
DAE.Exp exp;
case DAE.DIM_EXP(exp) then exp::exps;
else exps;
end match;
end for;
exps := listReverse(exps);
end dimensionsToExps;

public function splitRecord
Expand Down Expand Up @@ -12124,11 +12124,8 @@ algorithm
case(DAE.WHOLEDIM()::rest,_,_)
equation
// found a wholedim, replace with value, insert in lst
lsts = List.map(value,listReverse);
lsts = List.map1(lsts,listAppend,lstIn);
rest = listReverse(rest);
lsts = List.map1(lsts,List.appendr,rest);
lsts = List.map(lsts,listReverse);
lsts = List.map1(value,listAppend,lstIn);
lsts = List.map1(lsts,List.append_reverser,rest);
then
lsts;
case(DAE.INDEX()::rest,_,_)
Expand All @@ -12137,9 +12134,7 @@ algorithm
lsts = insertSubScripts(rest,value,sub::lstIn);
then
lsts;
else
then
value;
else value;
end matchcontinue;
end insertSubScripts;

Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/InstUtil.mo
Expand Up @@ -6497,7 +6497,7 @@ algorithm
case (_,_,_,_,{}) then inMod;
case (cache,env,pre,mod,inst_dims)
equation
exps = List.map1(inst_dims,Expression.dimensionsToExps,{});
exps = List.map(inst_dims,Expression.dimensionsToExps);
aexps = List.mapList(exps, Expression.unelabExp);
mod2 = traverseModAddDims4(cache,env,pre,mod, aexps, true);

Expand Down
7 changes: 6 additions & 1 deletion Compiler/Util/ExpandableArray.mo
Expand Up @@ -231,7 +231,12 @@ algorithm
elseif capacity == 1 then
listT := {Util.getOption(data[1])};
else
listT := list(Util.getOption(data[i]) for i guard isSome(data[i]) in 1:capacity);
listT := {};
for i in capacity:-1:1 loop
if isSome(data[i]) then
listT := Util.getOption(data[i])::listT;
end if;
end for;
end if;
end toList;

Expand Down

0 comments on commit b69a9eb

Please sign in to comment.