Skip to content

Commit

Permalink
Add support for threaded reductions using +g=MetaModelica
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@22279 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Sep 11, 2014
1 parent 299cdd8 commit b7a5aca
Show file tree
Hide file tree
Showing 19 changed files with 204 additions and 118 deletions.
16 changes: 12 additions & 4 deletions Compiler/FrontEnd/Absyn.mo
Expand Up @@ -953,11 +953,18 @@ uniontype FunctionArgs "The FunctionArgs uniontype consists of a list of positio

record FOR_ITER_FARG
Exp exp "iterator expression";
ReductionIterType iterType;
ForIterators iterators;
end FOR_ITER_FARG;

end FunctionArgs;

uniontype ReductionIterType
record COMBINE "Reductions are by default calculated as all combinations of the iterators"
end COMBINE;
record THREAD "With this option, all iterators must have the same length"
end THREAD;
end ReductionIterType;

uniontype NamedArg "The NamedArg uniontype consist of an Identifier for the argument and an expression
giving the value of the argument"
Expand Down Expand Up @@ -1967,6 +1974,7 @@ algorithm
list<NamedArg> named_args;
ForIterators iters;
Argument arg;
ReductionIterType iterType;

case (FUNCTIONARGS(args = expl, argNames = named_args), _, _, arg)
equation
Expand All @@ -1975,12 +1983,12 @@ algorithm
then
(FUNCTIONARGS(expl, named_args), arg);

case (FOR_ITER_FARG(exp = e, iterators = iters), _, _, arg)
case (FOR_ITER_FARG(e, iterType, iters), _, _, arg)
equation
(e, arg) = traverseExpBidir(e, enterFunc, exitFunc, arg);
(iters, arg) = List.map2Fold(iters, traverseExpBidirIterator, enterFunc, exitFunc, arg);
then
(FOR_ITER_FARG(e, iters), arg);
(FOR_ITER_FARG(e, iterType, iters), arg);
end match;
end traverseExpBidirFunctionArgs;

Expand Down Expand Up @@ -3443,7 +3451,7 @@ algorithm
then
res;

case (FOR_ITER_FARG(exp,iterators),_,_)
case (FOR_ITER_FARG(exp,_,iterators),_,_)
equation
l1 = List.map2Option(List.map(iterators,iteratorRange),getCrefFromExp,includeSubs,includeFunctions);
l2 = List.map2Option(List.map(iterators,iteratorGuard),getCrefFromExp,includeSubs,includeFunctions);
Expand Down Expand Up @@ -4412,7 +4420,7 @@ algorithm
lst_2=findIteratorInForIteratorsBounds(id,forIterators);
lst=listAppend(lst_1,lst_2);
then lst; */
case (id, FOR_ITER_FARG(exp,forIterators))
case (id, FOR_ITER_FARG(exp,_,forIterators))
equation
lst_1=findIteratorInExp(id,exp);
(bool,lst_2)=findIteratorInForIteratorsBounds2(id,forIterators);
Expand Down
12 changes: 6 additions & 6 deletions Compiler/FrontEnd/Ceval.mo
Expand Up @@ -207,7 +207,7 @@ algorithm
list<Values.Value> orderd;
list<String> comp;
Absyn.ClockKind ck;
DAE.ReductionIterType iterType;
Absyn.ReductionIterType iterType;

// uncomment for debugging
// case (cache,env,inExp,_,st,_,_)
Expand Down Expand Up @@ -5706,7 +5706,7 @@ end extendFrameForIterators;

protected function backpatchArrayReduction
input Absyn.Path path;
input DAE.ReductionIterType iterType;
input Absyn.ReductionIterType iterType;
input Values.Value inValue;
input list<Integer> dims;
output Values.Value outValue;
Expand All @@ -5716,7 +5716,7 @@ algorithm
list<Values.Value> vals;
Values.Value value;
case (_,_,value,{_}) then value;
case (Absyn.IDENT("array"),DAE.COMBINE(),Values.ARRAY(valueLst=vals),_)
case (Absyn.IDENT("array"),Absyn.COMBINE(),Values.ARRAY(valueLst=vals),_)
equation
value = backpatchArrayReduction3(vals,listReverse(dims));
// print(ValuesUtil.valString(value));print("\n");
Expand Down Expand Up @@ -6261,12 +6261,12 @@ end cevalDimension;

protected function makeReductionAllCombinations
input list<list<Values.Value>> inValMatrix;
input DAE.ReductionIterType rtype;
input Absyn.ReductionIterType rtype;
output list<list<Values.Value>> valMatrix;
algorithm
valMatrix := match (inValMatrix,rtype)
case (_,DAE.COMBINE()) then Util.allCombinations(inValMatrix,SOME(100000),Absyn.dummyInfo);
case (_,DAE.THREAD()) then List.transposeList(inValMatrix);
case (_,Absyn.COMBINE()) then Util.allCombinations(inValMatrix,SOME(100000),Absyn.dummyInfo);
case (_,Absyn.THREAD()) then List.transposeList(inValMatrix);
end match;
end makeReductionAllCombinations;

Expand Down
9 changes: 1 addition & 8 deletions Compiler/FrontEnd/DAE.mo
Expand Up @@ -1445,17 +1445,10 @@ uniontype CallAttributes
end CALL_ATTR;
end CallAttributes;

public uniontype ReductionIterType
record COMBINE "Reductions are by default calculated as all combinations of the iterators"
end COMBINE;
record THREAD "With this option, all iterators must have the same length"
end THREAD;
end ReductionIterType;

public uniontype ReductionInfo
record REDUCTIONINFO "A separate uniontype containing the information not required by traverseExp, etc"
Absyn.Path path "array, sum,..";
ReductionIterType iterType;
Absyn.ReductionIterType iterType;
Type exprType;
Option<Values.Value> defaultValue "if there is no default value, the reduction is not defined for 0-length arrays/lists";
String foldName;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/Dump.mo
Expand Up @@ -4442,7 +4442,7 @@ algorithm
printListAsCorbaString(argNames, printNamedArgAsCorbaString, ",");
Print.printBuf(" end Absyn.FUNCTIONARGS;");
then ();
case Absyn.FOR_ITER_FARG(exp,iterators)
case Absyn.FOR_ITER_FARG(exp,_,iterators)
equation
Print.printBuf("record Absyn.FOR_ITER_FARG exp = ");
printExpAsCorbaString(exp);
Expand Down
15 changes: 8 additions & 7 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -167,6 +167,7 @@ algorithm
Absyn.ForIterators aiters;
DAE.Type ty;
DAE.Dimensions dims;
Absyn.ReductionIterType iterType;

case (DAE.ICONST(integer = i)) then Absyn.INTEGER(i);
case (DAE.RCONST(real = r))
Expand Down Expand Up @@ -301,13 +302,13 @@ algorithm

case(DAE.CODE(code,_)) then Absyn.CODE(code);

case DAE.REDUCTION(reductionInfo=DAE.REDUCTIONINFO(path=path),expr=e1,iterators=riters) equation
//print("unelab of reduction not impl. yet");
acref = Absyn.pathToCref(path);
ae1 = unelabExp(e1);
aiters = List.map(riters, unelabReductionIterator);
then
Absyn.CALL(acref, Absyn.FOR_ITER_FARG(ae1, aiters));
case DAE.REDUCTION(reductionInfo=DAE.REDUCTIONINFO(iterType=iterType,path=path),expr=e1,iterators=riters)
equation
//print("unelab of reduction not impl. yet");
acref = Absyn.pathToCref(path);
ae1 = unelabExp(e1);
aiters = List.map(riters, unelabReductionIterator);
then Absyn.CALL(acref, Absyn.FOR_ITER_FARG(ae1, iterType, aiters));

else
equation
Expand Down
20 changes: 10 additions & 10 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -642,7 +642,7 @@ algorithm
DAE.Type ty;
DAE.ReductionIterators riters;
String foldName,resultName;
DAE.ReductionIterType rit;
Absyn.ReductionIterType rit;

case DAE.CALL(path=Absyn.IDENT("listAppend"),expLst={DAE.LIST(el),e2})
equation
Expand Down Expand Up @@ -3135,12 +3135,12 @@ algorithm
e2_1 = simplifyAsub(e2, sub);
then DAE.IFEXP(cond,e1_1,e2_1);

case(DAE.REDUCTION(DAE.REDUCTIONINFO(path=Absyn.IDENT("array"),iterType=DAE.THREAD()),exp,iters), sub)
case(DAE.REDUCTION(DAE.REDUCTIONINFO(path=Absyn.IDENT("array"),iterType=Absyn.THREAD()),exp,iters), sub)
equation
exp = List.fold1(iters,simplifyAsubArrayReduction,sub,exp);
then exp;

case(DAE.REDUCTION(DAE.REDUCTIONINFO(path=Absyn.IDENT("array"),iterType=DAE.COMBINE()),exp,{iter}), sub)
case(DAE.REDUCTION(DAE.REDUCTIONINFO(path=Absyn.IDENT("array"),iterType=Absyn.COMBINE()),exp,{iter}), sub)
equation
exp = simplifyAsubArrayReduction(iter,sub,exp);
then exp;
Expand Down Expand Up @@ -4968,7 +4968,7 @@ algorithm
then expr;

// iterType=THREAD() can handle multiple iterators
case DAE.REDUCTION(reductionInfo = DAE.REDUCTIONINFO(path = path, iterType = DAE.THREAD(), foldName=foldName, resultName=resultName, exprType = ty, foldExp=foldExp, defaultValue = defaultValue), expr = expr, iterators = iterators)
case DAE.REDUCTION(reductionInfo = DAE.REDUCTIONINFO(path = path, iterType = Absyn.THREAD(), foldName=foldName, resultName=resultName, exprType = ty, foldExp=foldExp, defaultValue = defaultValue), expr = expr, iterators = iterators)
equation
// Start like for the normal reductions
DAE.REDUCTIONITER(id = iter_name, guardExp = NONE(), exp = range)::iterators = iterators;
Expand All @@ -4982,22 +4982,22 @@ algorithm
then expr;

// array can handle multiple iterators
case DAE.REDUCTION(reductionInfo = DAE.REDUCTIONINFO(path = path as Absyn.IDENT("array"), iterType = DAE.COMBINE(), foldName=foldName, resultName=resultName, exprType = ty), expr = expr, iterators = iter::(iterators as _::_))
case DAE.REDUCTION(reductionInfo = DAE.REDUCTIONINFO(path = path as Absyn.IDENT("array"), iterType = Absyn.COMBINE(), foldName=foldName, resultName=resultName, exprType = ty), expr = expr, iterators = iter::(iterators as _::_))
equation
foldName2 = Util.getTempVariableIndex();
resultName2 = Util.getTempVariableIndex();
ty1 = Types.unliftArray(ty);
expr = DAE.REDUCTION(DAE.REDUCTIONINFO(path,DAE.COMBINE(),ty1,NONE(),foldName,resultName,NONE()),expr,iterators);
expr = DAE.REDUCTION(DAE.REDUCTIONINFO(path,DAE.COMBINE(),ty,NONE(),foldName2,resultName2,NONE()),expr,{iter});
expr = DAE.REDUCTION(DAE.REDUCTIONINFO(path,Absyn.COMBINE(),ty1,NONE(),foldName,resultName,NONE()),expr,iterators);
expr = DAE.REDUCTION(DAE.REDUCTIONINFO(path,Absyn.COMBINE(),ty,NONE(),foldName2,resultName2,NONE()),expr,{iter});
then expr;
// rest can also handle multiple iterators
case DAE.REDUCTION(reductionInfo = DAE.REDUCTIONINFO(path = path, iterType = DAE.COMBINE(), foldName=foldName, resultName=resultName, exprType = ty, foldExp=foldExp, defaultValue = defaultValue), expr = expr, iterators = iter::(iterators as _::_))
case DAE.REDUCTION(reductionInfo = DAE.REDUCTIONINFO(path = path, iterType = Absyn.COMBINE(), foldName=foldName, resultName=resultName, exprType = ty, foldExp=foldExp, defaultValue = defaultValue), expr = expr, iterators = iter::(iterators as _::_))
equation
// foldName2 = Util.getTempVariableIndex();
// resultName2 = Util.getTempVariableIndex();
// TODO: Rename foldName, resultName in foldExp of the inner reduction?
expr = DAE.REDUCTION(DAE.REDUCTIONINFO(path,DAE.COMBINE(),ty,defaultValue,foldName,resultName,foldExp),expr,{iter});
expr = DAE.REDUCTION(DAE.REDUCTIONINFO(path,DAE.COMBINE(),ty,defaultValue,foldName,resultName,foldExp),expr,iterators);
expr = DAE.REDUCTION(DAE.REDUCTIONINFO(path,Absyn.COMBINE(),ty,defaultValue,foldName,resultName,foldExp),expr,{iter});
expr = DAE.REDUCTION(DAE.REDUCTIONINFO(path,Absyn.COMBINE(),ty,defaultValue,foldName,resultName,foldExp),expr,iterators);
then expr;

else inReduction;
Expand Down
4 changes: 1 addition & 3 deletions Compiler/FrontEnd/InstUtil.mo
Expand Up @@ -7508,9 +7508,7 @@ algorithm
case (e,n::names,r::ranges)
equation
e2 = wrapIntoFor(e, names, ranges);
then
Absyn.CALL(Absyn.CREF_IDENT("array",{}),
Absyn.FOR_ITER_FARG(e2,{Absyn.ITERATOR(n,NONE(),SOME(Absyn.RANGE(Absyn.INTEGER(1),NONE(),r)))}));
then Absyn.CALL(Absyn.CREF_IDENT("array",{}), Absyn.FOR_ITER_FARG(e2, Absyn.COMBINE() ,{Absyn.ITERATOR(n,NONE(),SOME(Absyn.RANGE(Absyn.INTEGER(1),NONE(),r)))}));
end match;
end wrapIntoFor;

Expand Down
16 changes: 8 additions & 8 deletions Compiler/FrontEnd/OperatorOverloading.mo
Expand Up @@ -719,7 +719,7 @@ algorithm
cr = DAE.CREF(DAE.CREF_IDENT(iterName,newType1,{}),newType1);
(cache,exp,foldType,resType) = binaryUserdef(cache,env,op,cr,inExp2,newType1,inType2,impl,st,pre,info);
resType = Types.liftArray(resType,dim1);
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("array"),DAE.COMBINE(),resType,NONE(),foldName,resultName,NONE()),exp,DAE.REDUCTIONITER(iterName,inExp1,NONE(),newType1)::{});
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("array"),Absyn.COMBINE(),resType,NONE(),foldName,resultName,NONE()),exp,DAE.REDUCTIONITER(iterName,inExp1,NONE(),newType1)::{});
// exp = ExpressionSimplify.simplify1(exp);
then (cache,{(exp,NONE())});
case (cache,_,true,_,_,false,_,_,_,_,_,_,_,_,_,_,_) // scalar op non-scalar
Expand All @@ -739,7 +739,7 @@ algorithm
cr = DAE.CREF(DAE.CREF_IDENT(iterName,newType2,{}),newType2);
(cache,exp,foldType,resType) = binaryUserdef(cache,env,op,inExp1,cr,inType1,newType2,impl,st,pre,info);
resType = DAE.T_ARRAY(resType,{dim2},{});
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("array"),DAE.COMBINE(),resType,NONE(),foldName,resultName,NONE()),exp,DAE.REDUCTIONITER(iterName,inExp2,NONE(),newType2)::{});
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("array"),Absyn.COMBINE(),resType,NONE(),foldName,resultName,NONE()),exp,DAE.REDUCTIONITER(iterName,inExp2,NONE(),newType2)::{});
// exp = ExpressionSimplify.simplify1(exp);
then (cache,{(exp,NONE())});
// '*' invalid operations: vector*vector or vector*matrix
Expand Down Expand Up @@ -780,8 +780,8 @@ algorithm
iter = DAE.REDUCTIONITER(iterName1,cr,NONE(),newType1);
iter1 = DAE.REDUCTIONITER(iterName,inExp1,NONE(),newType1);
iter2 = DAE.REDUCTIONITER(iterName2,inExp2,NONE(),newType2);
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("sum"),DAE.THREAD(),resType,zeroConstructor,foldName1,resultName1,SOME(foldExp)),exp,iter::iter2::{});
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("array"),DAE.COMBINE(),resType,NONE(),foldName2,resultName2,NONE()),exp,iter1::{});
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("sum"),Absyn.THREAD(),resType,zeroConstructor,foldName1,resultName1,SOME(foldExp)),exp,iter::iter2::{});
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("array"),Absyn.COMBINE(),resType,NONE(),foldName2,resultName2,NONE()),exp,iter1::{});
then (cache,{(exp,NONE())});
// matrix-matrix-multiply
case (cache,_,_,_,true,_,_,true,Absyn.MUL(),_,_,_,_,_,_,_,_)
Expand Down Expand Up @@ -819,11 +819,11 @@ algorithm
iter3 = DAE.REDUCTIONITER(iterName3,cr1,NONE(),newType1_1);
iter4 = DAE.REDUCTIONITER(iterName4,cr2,NONE(),newType2_1);

exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("sum"),DAE.THREAD(),ty,zeroConstructor,foldName,resultName,SOME(foldExp)),mulExp,iter3::iter4::{});
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("sum"),Absyn.THREAD(),ty,zeroConstructor,foldName,resultName,SOME(foldExp)),mulExp,iter3::iter4::{});
ty = Types.liftArray(ty,dim2_2);
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("array"),DAE.COMBINE(),ty,NONE(),foldName2,resultName2,NONE()),exp,iter2::{});
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("array"),Absyn.COMBINE(),ty,NONE(),foldName2,resultName2,NONE()),exp,iter2::{});
ty = Types.liftArray(ty,dim1_1);
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("array"),DAE.COMBINE(),ty,NONE(),foldName1,resultName1,NONE()),exp,iter1::{});
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("array"),Absyn.COMBINE(),ty,NONE(),foldName1,resultName1,NONE()),exp,iter1::{});
then (cache,{(exp,NONE())});
// The rest are array op array, which are element-wise operations
// We thus change the operator to the element-wise one to avoid other vector operations than this one
Expand Down Expand Up @@ -853,7 +853,7 @@ algorithm
resType = DAE.T_ARRAY(resType,{dim2},{});
iter1 = DAE.REDUCTIONITER(iterName1,inExp1,NONE(),newType1);
iter2 = DAE.REDUCTIONITER(iterName2,inExp2,NONE(),newType2);
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("array"),DAE.THREAD(),resType,NONE(),foldName,resultName,NONE()),exp,iter1::iter2::{});
exp = DAE.REDUCTION(DAE.REDUCTIONINFO(Absyn.IDENT("array"),Absyn.THREAD(),resType,NONE(),foldName,resultName,NONE()),exp,iter1::iter2::{});
then (cache,{(exp,NONE())});
end match;
end binaryUserdefArray2;
Expand Down

0 comments on commit b7a5aca

Please sign in to comment.