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

Commit

Permalink
Remove DAE.SUM in favor of DAE.REDUCTION
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - #2004
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Nov 10, 2017
1 parent ea3a1f6 commit 679140a
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 110 deletions.
37 changes: 35 additions & 2 deletions Compiler/BackEnd/Vectorization.mo
Expand Up @@ -212,6 +212,15 @@ protected function buildAccumExpInEquations2"sums up the terms and build accumul
input list<tuple<DAE.Exp,Integer,Integer>> minmaxTerm;
input list<DAE.Exp> foldIn;
output list<DAE.Exp> foldOut;
protected
constant DAE.ReductionInfo sumReductionInfo = DAE.REDUCTIONINFO(
Absyn.IDENT("sum"),Absyn.COMBINE(),DAE.T_REAL_DEFAULT,SOME(Values.REAL(0.0)),
"$sumFold","$sumRes",SOME(DAE.BINARY(
DAE.CREF(DAE.CREF_IDENT("$sumFold",DAE.T_REAL_DEFAULT,{}),DAE.T_REAL_DEFAULT),
DAE.ADD(DAE.T_REAL_DEFAULT),
DAE.CREF(DAE.CREF_IDENT("$sumRes",DAE.T_REAL_DEFAULT,{}),DAE.T_REAL_DEFAULT))
));
constant DAE.Exp sumExp = DAE.CREF(DAE.CREF_IDENT("$sumIter",DAE.T_REAL_DEFAULT,{}),DAE.T_REAL_DEFAULT);
algorithm
foldOut := matchcontinue(minmaxTerm,foldIn)
local
Expand All @@ -226,20 +235,44 @@ algorithm
equation
// build a sigma operator exp and start with the first term
true = intNe(min,max);
DAE.T_REAL() = Expression.typeof(exp1);
(_,rest) = List.split1OnTrue(rest,minmaxTermEqual,exp1); // remove other instances of the term
iter = DAE.CREF(DAE.CREF_IDENT("i",DAE.T_INTEGER_DEFAULT,{}),DAE.T_INTEGER_DEFAULT);
(exp1,_) = Expression.traverseExpBottomUp(exp1,replaceSubscriptInCrefExp,{DAE.INDEX(iter)});
exp1 = DAE.SUM(Expression.typeof(exp1),iter,DAE.ICONST(min),DAE.ICONST(max),exp1);
exp1 = DAE.REDUCTION(
sumReductionInfo,
sumExp,
{
DAE.REDUCTIONITER("$sumIter",
DAE.RANGE(DAE.T_ARRAY(DAE.T_INTEGER_DEFAULT,{DAE.DIM_INTEGER(max-min)}),
DAE.ICONST(min),
NONE(),
DAE.ICONST(max)),
NONE(),
DAE.T_INTEGER_DEFAULT)
});
resExp = buildAccumExpInEquations2(rest,{exp1});
then resExp;
case((exp1,min,max)::rest,{exp0})
equation
// build a sigma operator exp and add to folding expression
true = intNe(min,max);
DAE.T_REAL() = Expression.typeof(exp1);
(_,rest) = List.split1OnTrue(rest,minmaxTermEqual,exp1); // remove other instances of the term
iter = DAE.CREF(DAE.CREF_IDENT("i",DAE.T_INTEGER_DEFAULT,{}),DAE.T_INTEGER_DEFAULT);
(exp1,_) = Expression.traverseExpBottomUp(exp1,replaceSubscriptInCrefExp,{DAE.INDEX(iter)});
exp1 = DAE.SUM(Expression.typeof(exp1),iter,DAE.ICONST(min),DAE.ICONST(max),exp1);
exp1 = DAE.REDUCTION(
sumReductionInfo,
sumExp,
{
DAE.REDUCTIONITER("$sumIter",
DAE.RANGE(DAE.T_ARRAY(DAE.T_INTEGER_DEFAULT,{DAE.DIM_INTEGER(max-min)}),
DAE.ICONST(min),
NONE(),
DAE.ICONST(max)),
NONE(),
DAE.T_INTEGER_DEFAULT)
});
resExp = buildAccumExpInEquations2(rest,{DAE.BINARY(exp0,DAE.ADD(Expression.typeof(exp0)),exp1)});
then resExp;
case((exp1,_,_)::rest,{})
Expand Down
7 changes: 0 additions & 7 deletions Compiler/FrontEnd/DAE.mo
Expand Up @@ -1517,13 +1517,6 @@ uniontype Exp "Expressions
Pattern pattern;
end PATTERN;

record SUM //i.e. accumulated sum over a range of array vars
Type ty;
Exp iterator;
Exp startIt;
Exp endIt;
Exp body;
end SUM;
/* --- */

end Exp;
Expand Down
20 changes: 0 additions & 20 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -2330,7 +2330,6 @@ algorithm
case (DAE.SHARED_LITERAL(exp = e)) then typeof(e);
// A little crazy, but sometimes we call typeof on things that will not be used in the end...
case (DAE.EMPTY(ty = tp)) then tp;
case (DAE.SUM(ty = tp)) then tp;

case e
equation
Expand Down Expand Up @@ -5306,16 +5305,6 @@ algorithm
(e, ext_arg) = inFunc(inExp, inExtArg);
then (e, ext_arg);

// SUM expressions
case DAE.SUM(ty=tp, iterator=e1, startIt=e2, endIt=e3, body=e4) equation
(e1_1, ext_arg) = traverseExpBottomUp(e1, inFunc, inExtArg);
(e2_1, ext_arg) = traverseExpBottomUp(e2, inFunc, ext_arg);
(e3_1, ext_arg) = traverseExpBottomUp(e3, inFunc, ext_arg);
(e4_1, ext_arg) = traverseExpBottomUp(e4, inFunc, ext_arg);
e = if referenceEq(e1, e1_1) and referenceEq(e2, e2_1) and referenceEq(e3, e3_1) and referenceEq(e4, e4_1)then inExp else DAE.SUM(tp,e1_1, e2_1, e3_1,e4_1);
(e, ext_arg) = inFunc(e, ext_arg);
then (e, ext_arg);

// Why don't we call inFunc() for these expressions?
case DAE.CODE() then (inExp, inExtArg);

Expand Down Expand Up @@ -5869,15 +5858,6 @@ algorithm
case (_,DAE.SHARED_LITERAL(),_,ext_arg)
then (inExp,ext_arg);

// SUM
case (_,(DAE.SUM(ty=tp, iterator = e1,startIt = e2,endIt = e3,body=e4)),rel,ext_arg)
equation
(e1_1,ext_arg_1) = traverseExpTopDown(e1, rel, ext_arg);
(e2_1,ext_arg_2) = traverseExpTopDown(e2, rel, ext_arg_1);
(e3_1,ext_arg_3) = traverseExpTopDown(e3, rel, ext_arg_2);
(e4_1,_) = traverseExpTopDown(e4, rel, ext_arg_3);
then (DAE.SUM(tp,e1_1,e2_1,e3_1,e4_1),ext_arg_3);

else
equation
str = ExpressionDump.printExpStr(inExp);
Expand Down
9 changes: 0 additions & 9 deletions Compiler/FrontEnd/ExpressionDump.mo
Expand Up @@ -1509,15 +1509,6 @@ algorithm
then
res_str;

case (DAE.SUM(startIt=start, endIt=stop, body=e),level)
equation
gen_str = genStringNTime(" |", level);
new_level1 = level + 1;
res_str = "("+dumpExpStr(start,new_level1)+" to "+dumpExpStr(stop,new_level1)+")["+dumpExpStr(e,new_level1)+"]";
res_str = stringAppendList({gen_str,"SIGMA ","\n",res_str,""});
then
res_str;

case (_,level)
equation
gen_str = genStringNTime(" |", level);
Expand Down
26 changes: 0 additions & 26 deletions Compiler/Template/CodegenCFunctions.tpl
Expand Up @@ -4346,7 +4346,6 @@ end getTempDeclMatchOutputName;
case e as BOX(__) then daeExpBox(e, context, &preExp, &varDecls, &auxFunction)
case e as UNBOX(__) then daeExpUnbox(e, context, &preExp, &varDecls, &auxFunction)
case e as SHARED_LITERAL(__) then daeExpSharedLiteral(e)
case e as SUM(__) then daeExpSum(e, context, &preExp, &varDecls, &auxFunction)
case e as CLKCONST(__) then '#error "<%ExpressionDumpTpl.dumpExp(e,"\"")%>"'
else error(sourceInfo(), 'Unknown expression: <%ExpressionDumpTpl.dumpExp(exp,"\"")%>')
end daeExp;
Expand Down Expand Up @@ -5392,31 +5391,6 @@ case IFEXP(__) then
resVar)
end daeExpIf;

template daeExpSum(Exp exp, Context context, Text &preExp,
Text &varDecls, Text &auxFunction)
"Generates code for an if expression."
::=
match exp
case SUM(__) then
let start = ExpressionDumpTpl.dumpExp(startIt,"\"")
let &anotherPre = buffer ""
let stop = ExpressionDumpTpl.dumpExp(endIt,"\"")
let bodyStr = daeExpIteratedCref(body)
let summationVar = <<sum>>
let iterVar = ExpressionDumpTpl.dumpExp(iterator,"\"")
let &preExp +=<<

modelica_integer $P<%iterVar%> = 0; // the iterator
modelica_real <%summationVar%> = 0.0; //the sum
for($P<%iterVar%> = <%start%>; $P<%iterVar%> < <%stop%>; $P<%iterVar%>++)
{
<%summationVar%> += <%bodyStr%>($P<%iterVar%>);
}

>>
summationVar
end daeExpSum;

template daeExpIteratedCref(Exp exp)
::=
match exp
Expand Down
33 changes: 0 additions & 33 deletions Compiler/Template/CodegenCppCommon.tpl
Expand Up @@ -998,7 +998,6 @@ template daeExp(Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls
case e as ARRAY(__) then '/*t4*/<%daeExpArray(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>'
case e as SIZE(__) then daeExpSize(e, context, &preExp, &varDecls, simCode , &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
case e as SHARED_LITERAL(__) then daeExpSharedLiteral(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/, useFlatArrayNotation)
case e as SUM(__) then daeExpSum(e, context, &preExp, &varDecls, simCode , &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
case e as PARTEVALFUNCTION(__)then daeExpPartEvalFunction(e, context, &preExp, &varDecls, simCode , &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
case e as BOX(__) then daeExpBox(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
case e as UNBOX(__) then daeExpUnbox(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
Expand Down Expand Up @@ -2947,38 +2946,6 @@ template daeExpSharedLiteral(Exp exp, Context context, Text &preExp /*BUFP*/, Te
'<%contextFunName(lit, context)%>'
end daeExpSharedLiteral;


template daeExpSum(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl,
Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
"Generates code for a match expression."
::=
match exp case exp as SUM(__) then
let bodyExp = daeExp(body, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
let iterExp = daeExp(iterator, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
let startItExp = daeExp(startIt, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
let endItExp = daeExp(endIt, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
let &preExp += 'double sum = 0.0;<%\n%>for(size_t <%iterExp%> = <%startItExp%>; <%iterExp%> != <%endItExp%>+1; <%iterExp%>++)<%\n%> sum += <%bodyExp%>[<%iterExp%>]<%\n%>'
<<
sum
>>

//C-Codegen:
//let start = ExpressionDumpTpl.dumpExp(startIt,"\"")
//let &anotherPre = buffer ""
//let stop = ExpressionDumpTpl.dumpExp(endIt,"\"")
//let bodyStr = daeExpIteratedCref(body)
//let summationVar = <<sum>>
//let iterVar = ExpressionDumpTpl.dumpExp(iterator,"\"")
//let &preExp +=<<

//modelica_integer $P<%iterVar%> = 0; // the iterator
//modelica_real <%summationVar%> = 0.0; //the sum
//for($P<%iterVar%> = <%start%>; $P<%iterVar%> < <%stop%>; $P<%iterVar%>++)
//{
// <%summationVar%> += <%bodyStr%>($P<%iterVar%>);
//}
end daeExpSum;

template daeExpPartEvalFunction(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
"Generates code for a function reference and a closure."
::=
Expand Down
6 changes: 0 additions & 6 deletions Compiler/Template/ExpressionDumpTpl.tpl
Expand Up @@ -152,12 +152,6 @@ match exp
case SHARED_LITERAL(__) then
if typeinfo() then '/* Shared literal <%index%> */ <%dumpExp(exp, stringDelimiter)%>' else dumpExp(exp, stringDelimiter)
case PATTERN(__) then (if typeinfo() then '/*pattern*/') + dumpPattern(pattern)
case SUM(__) then
let bodyStr = dumpExp(body,stringDelimiter)
let iterStr = dumpExp(iterator,stringDelimiter)
let startStr = dumpExp(startIt,stringDelimiter)
let endStr = dumpExp(endIt,stringDelimiter)
'SIGMA[<%iterStr%>:<%startStr%>to<%endStr%>](<%bodyStr%>)'
else errorMsg("ExpressionDumpTpl.dumpExp: Unknown expression.")
end dumpExp;
Expand Down
7 changes: 0 additions & 7 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -1715,13 +1715,6 @@ package DAE
record PATTERN
Pattern pattern;
end PATTERN;
record SUM
Type ty;
DAE.Exp iterator;
DAE.Exp startIt;
DAE.Exp endIt;
DAE.Exp body;
end SUM;
end Exp;

uniontype CallAttributes
Expand Down

0 comments on commit 679140a

Please sign in to comment.