Skip to content

Commit

Permalink
- fix #2336
Browse files Browse the repository at this point in the history
- move optimization module 'inlineArrayEqn' to InlineArrayEquations.mo
- add description for optimization module 'inlineArrayEqn'
- remove unused modules


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@19589 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Mar 15, 2014
1 parent 989344c commit 63c3e4c
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 228 deletions.
187 changes: 0 additions & 187 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -79,7 +79,6 @@ protected import Graph;
protected import HashSet;
protected import HashTableExpToIndex;
protected import HpcOmEqSystems;
protected import HpcOmTaskGraph;
protected import IndexReduction;
protected import List;
protected import RewriteRules;
Expand All @@ -90,192 +89,6 @@ protected import Util;
protected import Values;
protected import ValuesUtil;

// =============================================================================
// inline arrayeqns stuff
//
// public functions:
// - inlineArrayEqn
// - getScalarArrayEqns
// =============================================================================

public function inlineArrayEqn "author: Frenkel TUD 2011-3"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
(outDAE,_) := BackendDAEUtil.mapEqSystemAndFold(inDAE,inlineArrayEqn1,false);
end inlineArrayEqn;

public function getScalarArrayEqns
input list<BackendDAE.Equation> iEqsLst;
input list<BackendDAE.Equation> iAcc;
input Boolean iFound;
output list<BackendDAE.Equation> oEqsLst;
output Boolean oFound;
algorithm
(oEqsLst,oFound) := match(iEqsLst,iAcc,iFound)
local
BackendDAE.Equation eqn;
list<BackendDAE.Equation> eqns,eqns1;
Boolean b;
case ({},_,_) then (listReverse(iAcc),iFound);
case (eqn::eqns,_,_)
equation
(eqns1,b) = getScalarArrayEqns1(eqn,iAcc);
(eqns1,b) = getScalarArrayEqns(eqns,eqns1,b or iFound);
then
(eqns1,b);
end match;
end getScalarArrayEqns;

protected function inlineArrayEqn1 "author: Frenkel TUD 2011-5"
input BackendDAE.EqSystem isyst;
input tuple<BackendDAE.Shared,Boolean> sharedOptimized;
output BackendDAE.EqSystem osyst;
output tuple<BackendDAE.Shared,Boolean> osharedOptimized;
algorithm
(osyst,osharedOptimized) := matchcontinue (isyst,sharedOptimized)
local
BackendDAE.Shared shared;
BackendDAE.EqSystem syst;
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
list<BackendDAE.Equation> eqnslst;
BackendDAE.Matching matching;
Option<BackendDAE.IncidenceMatrix> m;
Option<BackendDAE.IncidenceMatrixT> mT;
BackendDAE.StateSets stateSets;
case (syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns,m=m,mT=mT,matching=matching,stateSets=stateSets),(shared,_))
equation
eqnslst = BackendEquation.equationList(eqns);
(eqnslst,true) = getScalarArrayEqns(eqnslst,{},false);
eqns = BackendEquation.listEquation(eqnslst);
then
(BackendDAE.EQSYSTEM(vars,eqns,NONE(),NONE(),BackendDAE.NO_MATCHING(),stateSets),(shared,true));
else
then
(isyst,sharedOptimized);
end matchcontinue;
end inlineArrayEqn1;

protected function getScalarArrayEqns1 "author: Frenkel TUD 2012-06"
input BackendDAE.Equation iEqn;
input list<BackendDAE.Equation> iAcc;
output list<BackendDAE.Equation> outEqsLst;
output Boolean oFound;
algorithm
(outEqsLst,oFound) := matchcontinue (iEqn,iAcc)
local
DAE.ElementSource source;
DAE.Exp e1,e2,e1_1,e2_1;
list<DAE.Exp> ea1,ea2;
list<BackendDAE.Equation> eqns;
Boolean diffed;
case (BackendDAE.ARRAY_EQUATION(left=e1,right=e2,source=source,differentiated=diffed),_)
equation
true = Expression.isArray(e1) or Expression.isMatrix(e1);
true = Expression.isArray(e2) or Expression.isMatrix(e2);
ea1 = Expression.flattenArrayExpToList(e1);
ea2 = Expression.flattenArrayExpToList(e2);
((_,eqns)) = List.threadFold3(ea1,ea2,generateScalarArrayEqns2,source,diffed,DAE.EQUALITY_EXPS(e1,e2),(1,iAcc));
then
(eqns,true);
case (BackendDAE.ARRAY_EQUATION(left=e1 as DAE.CREF(componentRef =_),right=e2,source=source,differentiated=diffed),_)
equation
true = Expression.isArray(e2) or Expression.isMatrix(e2);
((e1_1,(_,_))) = BackendDAEUtil.extendArrExp((e1,(NONE(),false)));
ea1 = Expression.flattenArrayExpToList(e1_1);
ea2 = Expression.flattenArrayExpToList(e2);
((_,eqns)) = List.threadFold3(ea1,ea2,generateScalarArrayEqns2,source,diffed,DAE.EQUALITY_EXPS(e1,e2),(1,iAcc));
then
(eqns,true);
case (BackendDAE.ARRAY_EQUATION(left=e1,right=e2 as DAE.CREF(componentRef =_),source=source,differentiated=diffed),_)
equation
true = Expression.isArray(e1) or Expression.isMatrix(e1);
((e2_1,(_,_))) = BackendDAEUtil.extendArrExp((e2,(NONE(),false)));
ea1 = Expression.flattenArrayExpToList(e1);
ea2 = Expression.flattenArrayExpToList(e2_1);
((_,eqns)) = List.threadFold3(ea1,ea2,generateScalarArrayEqns2,source,diffed,DAE.EQUALITY_EXPS(e1,e2),(1,iAcc));
then
(eqns,true);
case (BackendDAE.ARRAY_EQUATION(left=e1 as DAE.CREF(componentRef =_),right=e2 as DAE.CREF(componentRef =_),source=source,differentiated=diffed),_)
equation
((e1_1,(_,_))) = BackendDAEUtil.extendArrExp((e1,(NONE(),false)));
((e2_1,(_,_))) = BackendDAEUtil.extendArrExp((e2,(NONE(),false)));
ea1 = Expression.flattenArrayExpToList(e1_1);
ea2 = Expression.flattenArrayExpToList(e2_1);
((_,eqns)) = List.threadFold3(ea1,ea2,generateScalarArrayEqns2,source,diffed,DAE.EQUALITY_EXPS(e1,e2),(1,iAcc));
then
(eqns,true);
case (BackendDAE.COMPLEX_EQUATION(left=e1,right=e2,source=source,differentiated=diffed),_)
equation
ea1 = Expression.splitRecord(e1,Expression.typeof(e1));
ea2 = Expression.splitRecord(e2,Expression.typeof(e2));
((_,eqns)) = List.threadFold3(ea1,ea2,generateScalarArrayEqns2,source,diffed,DAE.EQUALITY_EXPS(e1,e2),(1,iAcc));
then
(eqns,true);
case (_,_) then (iEqn::iAcc,false);
end matchcontinue;
end getScalarArrayEqns1;

protected function generateScalarArrayEqns2 "author: Frenkel TUD 2012-06"
input DAE.Exp inExp1;
input DAE.Exp inExp2;
input DAE.ElementSource inSource;
input Boolean diffed;
input DAE.EquationExp eqExp "Original expressions; for the symbolic trace";
input tuple<Integer /* Current index; for the symbolic trace */,list<BackendDAE.Equation>> iEqns;
output tuple<Integer,list<BackendDAE.Equation>> oEqns;
algorithm
oEqns := matchcontinue(inExp1,inExp2,inSource,diffed,eqExp,iEqns)
local
DAE.Type tp;
Integer size,i;
DAE.Dimensions dims;
list<Integer> ds;
Boolean b1,b2;
list<BackendDAE.Equation> eqns;
DAE.ElementSource source;
// complex types to complex equations
case (_,_,_,_,_,(i,eqns))
equation
tp = Expression.typeof(inExp1);
true = DAEUtil.expTypeComplex(tp);
size = Expression.sizeOf(tp);
source = DAEUtil.addSymbolicTransformation(inSource,DAE.OP_SCALARIZE(eqExp,i,DAE.EQUALITY_EXPS(inExp1,inExp2)));
then
((i+1,BackendDAE.COMPLEX_EQUATION(size,inExp1,inExp2,source,diffed)::eqns));

// array types to array equations
case (_,_,_,_,_,(i,eqns))
equation
tp = Expression.typeof(inExp1);
true = DAEUtil.expTypeArray(tp);
dims = Expression.arrayDimension(tp);
ds = Expression.dimensionsSizes(dims);
source = DAEUtil.addSymbolicTransformation(inSource,DAE.OP_SCALARIZE(eqExp,i,DAE.EQUALITY_EXPS(inExp1,inExp2)));
then
((i+1,BackendDAE.ARRAY_EQUATION(ds,inExp1,inExp2,source,diffed)::eqns));
// other types
case (_,_,_,_,_,(i,eqns))
equation
tp = Expression.typeof(inExp1);
b1 = DAEUtil.expTypeComplex(tp);
b2 = DAEUtil.expTypeArray(tp);
false = b1 or b2;
//Error.assertionOrAddSourceMessage(not b1,Error.INTERNAL_ERROR,{str}, Absyn.dummyInfo);
source = DAEUtil.addSymbolicTransformation(inSource,DAE.OP_SCALARIZE(eqExp,i,DAE.EQUALITY_EXPS(inExp1,inExp2)));
then
((i+1,BackendDAE.EQUATION(inExp1,inExp2,source,diffed)::eqns));
else
equation
// show only on failtrace!
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("- BackendDAEOptimize.generateScalarArrayEqns2 failed on: " +& ExpressionDump.printExpStr(inExp1) +& " = " +& ExpressionDump.printExpStr(inExp2) +& "\n");
then
fail();
end matchcontinue;
end generateScalarArrayEqns2;

// =============================================================================
// simplify time independent function calls
//
Expand Down
45 changes: 21 additions & 24 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -82,6 +82,7 @@ protected import Flags;
protected import Global;
protected import GlobalScript;
protected import IndexReduction;
protected import InlineArrayEquations;
protected import Initialization;
protected import Inline;
protected import List;
Expand Down Expand Up @@ -2431,10 +2432,8 @@ end getExtraInfo;
stuff that deals with extendArrExp
************************************/

public function extendArrExp "
Author: Frenkel TUD 2010-07
alternative name: vectorizeExp
"
public function extendArrExp "author: Frenkel TUD 2010-07
alternative name: vectorizeExp"
input tuple<DAE.Exp,tuple<Option<DAE.FunctionTree>,Boolean>> itpl;
output tuple<DAE.Exp,tuple<Option<DAE.FunctionTree>,Boolean>> otpl;
algorithm
Expand All @@ -2447,8 +2446,7 @@ algorithm
end matchcontinue;
end extendArrExp;

protected function traversingextendArrExp "
Author: Frenkel TUD 2010-07.
protected function traversingextendArrExp "author: Frenkel TUD 2010-07.
This function extend all array and record componentrefs to there
elements. This is necessary for BLT and substitution of simple
equations."
Expand Down Expand Up @@ -2871,29 +2869,28 @@ algorithm outExp := matchcontinue(inExp)
end matchcontinue;
end traversingcollateArrExp;

public function dimensionsToRange
"Converts a list of dimensions to a list of integer ranges."
protected function dimensionsToRange
"Converts a list of dimensions to a list of subscript lists."
input list<DAE.Dimension> idims;
output list<list<DAE.Subscript>> outRangelist;
algorithm
outRangelist := matchcontinue(idims)
local
Integer i;
list<list<DAE.Subscript>> rangelist;
list<Integer> range;
list<DAE.Subscript> subs;
DAE.Dimension d;
list<DAE.Dimension> dims;
local
Integer i;
list<list<DAE.Subscript>> rangelist;
list<Integer> range;
list<DAE.Subscript> subs;
DAE.Dimension d;
list<DAE.Dimension> dims;

case({}) then {};
case(DAE.DIM_UNKNOWN()::dims)
equation
rangelist = dimensionsToRange(dims);
then {}::rangelist;

case(DAE.DIM_UNKNOWN()::dims) equation
rangelist = dimensionsToRange(dims);
then {}::rangelist;

case(d::dims) equation
i = Expression.dimensionSize(d);
range = List.intRange(i);
subs = rangesToSubscript(range);
subs = ComponentReference.expandDimension(d);
rangelist = dimensionsToRange(dims);
then subs::rangelist;
end matchcontinue;
Expand Down Expand Up @@ -8746,7 +8743,7 @@ protected
algorithm
allPreOptModules := {(RemoveSimpleEquations.fastAcausal, "removeSimpleEquations", false),
(RemoveSimpleEquations.allAcausal, "removeAllSimpleEquations", false),
(BackendDAEOptimize.inlineArrayEqn, "inlineArrayEqn", false),
(InlineArrayEquations.inlineArrayEqn, "inlineArrayEqn", false),
(EvaluateParameter.evaluateFinalParameters, "evaluateFinalParameters", false),
(EvaluateParameter.evaluateEvaluateParameters, "evaluateEvaluateParameters", false),
(EvaluateParameter.evaluateFinalEvaluateParameters, "evaluateFinalEvaluateParameters", false),
Expand Down Expand Up @@ -8797,7 +8794,7 @@ algorithm
(EvaluateParameter.evaluateReplaceFinalParameters, "evaluateReplaceFinalParameters", false),
(EvaluateParameter.evaluateReplaceEvaluateParameters, "evaluateReplaceEvaluateParameters", false),
(EvaluateParameter.evaluateReplaceFinalEvaluateParameters, "evaluateReplaceFinalEvaluateParameters", false),
(BackendDAEOptimize.inlineArrayEqn,"inlineArrayEqn",false),
(InlineArrayEquations.inlineArrayEqn,"inlineArrayEqn",false),
(BackendDAEOptimize.removeUnusedParameter,"removeUnusedParameter",false),
(BackendDAEOptimize.removeUnusedVariables,"removeUnusedVariables",false),
(BackendDAEOptimize.constantLinearSystem,"constantLinearSystem",false),
Expand Down
1 change: 0 additions & 1 deletion Compiler/BackEnd/Differentiate.mo
Expand Up @@ -69,7 +69,6 @@ protected import Flags;
protected import Inline;
protected import List;
//protected import System;
protected import Tpl;
protected import Util;


Expand Down
6 changes: 3 additions & 3 deletions Compiler/BackEnd/IndexReduction.mo
Expand Up @@ -46,7 +46,6 @@ protected import Absyn;
protected import BackendDAEEXT;
protected import BackendDump;
protected import BackendEquation;
protected import BackendDAEOptimize;
protected import BackendDAETransform;
protected import BackendDAEUtil;
protected import BackendVariable;
Expand All @@ -69,6 +68,7 @@ protected import HashTable3;
protected import HashTableCG;
protected import HashTableCrIntToExp;
protected import Inline;
protected import InlineArrayEquations;
protected import List;
protected import Matching;
protected import SCode;
Expand Down Expand Up @@ -2004,7 +2004,7 @@ algorithm
funcs = BackendDAEUtil.getFunctions(ishared);
(eqnslst,_) = BackendEquation.traverseBackendDAEExpsEqnList(eqnslst, forceInlinEqn,funcs);
// try to make scalar
(eqnslst,_) = BackendDAEOptimize.getScalarArrayEqns(eqnslst,{},false);
(eqnslst,_) = InlineArrayEquations.getScalarArrayEqns(eqnslst);
// convert x:STATE(n) if n>1 to DER.DER....x
(hov,ht) = List.map1Fold(iHov,getLevelStates,level,HashTableCrIntToExp.emptyHashTable());
(eqnslst,_) = BackendEquation.traverseBackendDAEExpsEqnList(eqnslst, replaceDummyDerivatives, ht);
Expand Down Expand Up @@ -4411,7 +4411,7 @@ protected
list<Integer> unassigned,assigned;
algorithm
vars := BackendVariable.listVar1(inVarLst);
(eqnslst,_) := BackendDAEOptimize.getScalarArrayEqns(inEqnsLst,{},false);
(eqnslst,_) := InlineArrayEquations.getScalarArrayEqns(inEqnsLst);
eqns := BackendEquation.listEquation(eqnslst);
syst := BackendDAE.EQSYSTEM(vars,eqns,NONE(),NONE(),BackendDAE.NO_MATCHING(),{});
(me,_,mapEqnIncRow,mapIncRowEqn) := BackendDAEUtil.getAdjacencyMatrixEnhancedScalar(syst, shared);
Expand Down

0 comments on commit 63c3e4c

Please sign in to comment.