Skip to content

Commit

Permalink
- Changed DAE.REDUCTION to contain a list<Iterator> instead of just one
Browse files Browse the repository at this point in the history
  - Static, Simplify and Ceval do not yet use these, but should in the future


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8214 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Mar 14, 2011
1 parent 1920e45 commit 6f22f48
Show file tree
Hide file tree
Showing 16 changed files with 428 additions and 151 deletions.
27 changes: 19 additions & 8 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -206,8 +206,8 @@ algorithm
list<DAE.Exp> expl;
list<DAE.ExpVar> varLst;
DAE.Ident ident;
BackendDAE.Var backendVar;

list<BackendDAE.Var> backendVars;
DAE.ReductionIterators riters;

// special case for time, it is never part of the equation system
case ((e as DAE.CREF(componentRef = DAE.CREF_IDENT(ident="time")),(vars,crefs)))
Expand All @@ -222,13 +222,11 @@ algorithm
((e, (vars1,crefs1)));

// case for Reductions
case ((e as DAE.REDUCTION(reductionInfo = DAE.REDUCTIONINFO(ident = ident)),(vars,crefs)))
case ((e as DAE.REDUCTION(iterators = riters),(vars,crefs)))
equation
// add ident to vars
cr = ComponentReference.makeCrefIdent(ident,DAE.ET_INT(),{});
backendVar = BackendDAE.VAR(cr,BackendDAE.VARIABLE(),DAE.BIDIR(),BackendDAE.INT(),NONE(),NONE(),{},0,
DAE.emptyElementSource,NONE(),NONE(),DAE.NON_CONNECTOR(),DAE.NON_STREAM_CONNECTOR());
vars = BackendVariable.addVar(backendVar,vars);
// add idents to vars
backendVars = Util.listMap(riters,makeIterVariable);
vars = BackendVariable.addVars(backendVars,vars);
then
((e, (vars,crefs)));

Expand All @@ -253,6 +251,19 @@ algorithm
end matchcontinue;
end traversecheckBackendDAEExp;

protected function makeIterVariable
input DAE.ReductionIterator iter;
output BackendDAE.Var backendVar;
protected
String name;
DAE.ComponentRef cr;
algorithm
name := Expression.reductionIterName(iter);
cr := ComponentReference.makeCrefIdent(name,DAE.ET_INT(),{});
backendVar := BackendDAE.VAR(cr,BackendDAE.VARIABLE(),DAE.BIDIR(),BackendDAE.INT(),NONE(),NONE(),{},0,
DAE.emptyElementSource,NONE(),NONE(),DAE.NON_CONNECTOR(),DAE.NON_STREAM_CONNECTOR());
end makeIterVariable;

/*************************************************
* Initialisation and stuff
************************************************/
Expand Down
26 changes: 23 additions & 3 deletions Compiler/BackEnd/DAEQuery.mo
Expand Up @@ -723,6 +723,7 @@ algorithm
list<DAE.Exp> expl;
DAE.Operator op1;
list<list<tuple<DAE.Exp, Boolean>>> explTpl;
DAE.ReductionIterators iters;

case (DAE.CREF(componentRef = cr),vars)
equation
Expand Down Expand Up @@ -945,17 +946,36 @@ algorithm
then
pStr;

case (DAE.REDUCTION(expr = e1,range = e2),vars)
case (DAE.REDUCTION(expr = e1,iterators = iters),vars)
equation
s1 = incidenceRowExp(e1, vars);
s2 = incidenceRowExp(e2, vars);
pStr = listAppend(s1, s2);
lst = Util.listMap1(iters, incidenceRowIter, vars);
pStr = Util.listFlatten(s1::lst);
then
pStr;
case (_,_) then {};
end matchcontinue;
end incidenceRowExp;

protected function incidenceRowIter
input DAE.ReductionIterator iter;
input BackendDAE.Variables vars;
output list<String> strs;
algorithm
strs := match (iter,vars)
local
DAE.Exp e1,e2;
list<String> s1,s2;
case (DAE.REDUCTIONITER(guardExp = SOME(e1), exp = e2),_)
equation
s1 = incidenceRowExp(e1, vars);
s2 = incidenceRowExp(e2, vars);
then listAppend(s1,s2);
case (DAE.REDUCTIONITER(exp = e1),_)
then incidenceRowExp(e1, vars);
end match;
end incidenceRowIter;

protected function incidenceRowMatrixExp "function: incidenceRowMatrixExp
author: PA
Traverses matrix expressions for building incidence matrix."
Expand Down
13 changes: 7 additions & 6 deletions Compiler/BackEnd/Derive.mo
Expand Up @@ -354,6 +354,7 @@ algorithm
Option<Values.Value> v;
list<DAE.ExpVar> varLst;
DAE.ReductionInfo reductionInfo;
DAE.ReductionIterators iters;

case (DAE.ICONST(integer = _),_) then DAE.RCONST(0.0);
case (DAE.RCONST(real = _),_) then DAE.RCONST(0.0);
Expand Down Expand Up @@ -717,12 +718,11 @@ algorithm
then
Expression.makeASUB(e,sub);

case (DAE.REDUCTION(reductionInfo = reductionInfo,expr = e1,guardExp = guardExp,range = e2),(timevars,functions))
case (DAE.REDUCTION(reductionInfo = reductionInfo,expr = e1,iterators = iters),(timevars,functions))
equation
e1_1 = differentiateExpTime(e1, (timevars,functions));
e2_1 = differentiateExpTime(e2, (timevars,functions));
then
DAE.REDUCTION(reductionInfo,e1_1,guardExp,e2_1);
DAE.REDUCTION(reductionInfo,e1_1,iters);

case (e,_)
equation
Expand Down Expand Up @@ -1221,6 +1221,7 @@ algorithm
Option<DAE.Exp> guardExp,foldExp;
Option<Values.Value> v;
DAE.ReductionInfo reductionInfo;
DAE.ReductionIterators riters;

case (DAE.ICONST(integer = _),_,_) then DAE.RCONST(0.0);

Expand Down Expand Up @@ -1429,12 +1430,12 @@ algorithm
then
Expression.makeASUB(e_1,sub);

case (DAE.REDUCTION(reductionInfo=reductionInfo,expr = e1,guardExp = guardExp, range = e2),tv,differentiateIfExp)
// TODO: Check if we are differentiating a local iterator?
case (DAE.REDUCTION(reductionInfo=reductionInfo,expr = e1,iterators = riters),tv,differentiateIfExp)
equation
e1_1 = differentiateExp(e1, tv, differentiateIfExp);
e2_1 = differentiateExp(e2, tv, differentiateIfExp);
then
DAE.REDUCTION(reductionInfo,e1_1,guardExp,e2_1);
DAE.REDUCTION(reductionInfo,e1_1,riters);

// derivative of arbitrary function, not dependent of variable, i.e. constant
/* Caught by rule below...
Expand Down
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/Ceval.mo
Expand Up @@ -812,7 +812,7 @@ algorithm
then
(cache,v,stOpt);

case (cache, env, DAE.REDUCTION(reductionInfo=DAE.REDUCTIONINFO(path = path, ident = iter, foldExp = foldExp, defaultValue = ov, exprType = ty), expr = daeExp, guardExp = guardExp, range = iterexp), impl, stOpt, dimOpt, msg)
case (cache, env, DAE.REDUCTION(reductionInfo=DAE.REDUCTIONINFO(path = path, foldExp = foldExp, defaultValue = ov, exprType = ty), expr = daeExp, iterators = {DAE.REDUCTIONITER(id=iter,exp=iterexp,guardExp=guardExp)}), impl, stOpt, dimOpt, msg)
equation
(cache, v, stOpt) = ceval(cache, env, iterexp, impl, stOpt, dimOpt, msg);
vals = ValuesUtil.arrayOrListVals(v,true);
Expand Down Expand Up @@ -5053,7 +5053,7 @@ algorithm
// REDUCTION bindings
case (cache,env,cr,DAE.EQBOUND(exp = exp,constant_ = DAE.C_CONST()),impl,msg)
equation
DAE.REDUCTION(reductionInfo=DAE.REDUCTIONINFO(path = Absyn.IDENT(name = rfn), ident = iter),expr = elexp,range = iterexp) = exp;
DAE.REDUCTION(reductionInfo=DAE.REDUCTIONINFO(path = Absyn.IDENT(name = rfn)),expr = elexp, iterators = {DAE.REDUCTIONITER(id=iter,exp=iterexp)}) = exp;
cr_1 = ComponentReference.crefStripLastSubs(cr) "lookup without subscripts, so dimension sizes can be determined." ;
(cache,_,tp,_,_,_,_,_,_) = Lookup.lookupVar(cache,env, cr_1);
sizelst = Types.getDimensionSizes(tp);
Expand Down
41 changes: 22 additions & 19 deletions Compiler/FrontEnd/CevalFunction.mo
Expand Up @@ -1785,6 +1785,7 @@ algorithm
FunctionVar e;
DAE.Ident iter;
list<DAE.Ident> iters;
DAE.ReductionIterators riters;

// Check if the crefs matches any of the iterators that might shadow a
// function variable, and don't add it as a depency if that's the case.
Expand All @@ -1808,11 +1809,13 @@ algorithm

// If we encounter a reduction, add the iterator to the iterator list so
// that we know which iterators shadow function variables.
case ((exp as DAE.REDUCTION(reductionInfo=DAE.REDUCTIONINFO(ident = iter)), (all_el, accum_el, iters)))
case ((exp as DAE.REDUCTION(iterators = riters), (all_el, accum_el, iters)))
equation
iters = listAppend(Util.listMap(riters, Expression.reductionIterName), iters);
then
((exp, (all_el, accum_el, iter :: iters)));
((exp, (all_el, accum_el, iters)));

else then inTuple;
else inTuple;
end matchcontinue;
end getElementDependenciesTraverserEnter;

Expand All @@ -1828,37 +1831,37 @@ algorithm
list<FunctionVar> all_el, accum_el;
DAE.Ident iter, iter2;
list<DAE.Ident> iters;
DAE.ReductionIterators riters;

// If we encounter a reduction, make sure that its iterator matches the
// first iterator in the iterator list, and if so remove it from the list.
case ((exp as DAE.REDUCTION(reductionInfo = DAE.REDUCTIONINFO(ident = iter)), (all_el, accum_el, iter2 :: iters)))
case ((exp as DAE.REDUCTION(iterators = riters), (all_el, accum_el, iters)))
equation
compareIterators(iter, iter2);
then
((exp, (all_el, accum_el, iters)));
iters = compareIterators(listReverse(riters), iters);
then getElementDependenciesTraverserExit((exp, (all_el, accum_el, iters)));

else then inTuple;
else inTuple;
end match;
end getElementDependenciesTraverserExit;

protected function compareIterators
input DAE.Ident inIterator1;
input DAE.Ident inIterator2;
input DAE.ReductionIterators riters;
input list<String> iters;
output list<String> outIters;
algorithm
_ := matchcontinue(inIterator1, inIterator2)
case (_, _)
outIters := matchcontinue(riters,iters)
local
String id1,id2;
case (DAE.REDUCTIONITER(id=id1)::riters, id2::iters)
equation
true = stringEqual(inIterator1, inIterator2);
then
();
true = stringEqual(id1,id2);
then compareIterators(riters,iters);

// This should never happen, print an error if it does.
else
equation
Error.addMessage(Error.INTERNAL_ERROR,
{"Different iterators in CevalFunction.compareIterators."});
then
fail();
Error.addMessage(Error.INTERNAL_ERROR, {"Different iterators in CevalFunction.compareIterators."});
then fail();
end matchcontinue;
end compareIterators;

Expand Down
15 changes: 12 additions & 3 deletions Compiler/FrontEnd/DAE.mo
Expand Up @@ -1125,8 +1125,7 @@ uniontype Exp "Expressions
record REDUCTION "e.g. sum(i*i+1 for i in 1:4)"
ReductionInfo reductionInfo;
Exp expr "expr, e.g i*i+1" ;
Option<Exp> guardExp "Boolean guard-expression";
Exp range "range Reduction expression e.g. 1:4" ;
ReductionIterators iterators;
end REDUCTION;

record END "array index to last element, e.g. a{end}:=1;" end END;
Expand Down Expand Up @@ -1198,12 +1197,22 @@ public uniontype ReductionInfo
record REDUCTIONINFO "A separate uniontype containing the information not required by traverseExp, etc"
Absyn.Path path "array, sum,..";
Type exprType;
Ident ident "e.g. i";
Option<Values.Value> defaultValue "if there is no default value, the reduction is not defined for 0-length arrays/lists";
Option<Exp> foldExp "For example, max(ident,$res) or ident+$res; array() does not use this feature; DO NOT TRAVERSE THIS EXPRESSION!";
end REDUCTIONINFO;
end ReductionInfo;

public uniontype ReductionIterator
record REDUCTIONITER
String id;
Exp exp;
Option<Exp> guardExp;
Type ty;
end REDUCTIONITER;
end ReductionIterator;

public type ReductionIterators = list<ReductionIterator> "NOTE: OMC only handles one iterator for now";

public uniontype MatchCase
record CASE
list<Pattern> patterns "ELSE is handled by not doing pattern-matching";
Expand Down

0 comments on commit 6f22f48

Please sign in to comment.