Skip to content

Commit

Permalink
+ Back-end and code generation support for parfor loop.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12534 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
mahge committed Aug 15, 2012
1 parent 24e1cfd commit 57cd95b
Show file tree
Hide file tree
Showing 17 changed files with 587 additions and 80 deletions.
25 changes: 25 additions & 0 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -1827,6 +1827,8 @@ algorithm
DAE.Ident iteratorName;
DAE.Exp e,iteratorExp;
list<DAE.Exp> iteratorexps, expExpLst;
list<tuple<DAE.ComponentRef,Absyn.Info>> loopPrlVars "list of parallel variables used/referenced in a parfor loop";

case (v,_,{},_) then v;
case (v,knv,(DAE.STMT_ASSIGN(exp1 =DAE.CREF(componentRef = cr)) :: xs),true)
equation
Expand Down Expand Up @@ -1868,6 +1870,19 @@ algorithm
v_2 = detectImplicitDiscreteAlgsStatemens(v_1,knv, xs,true);
then
v_2;

/*
case (v,knv,(DAE.STMT_PARFOR(type_= tp, iter = iteratorName, range = e,statementLst = statementLst,loopPrlVars=loopPrlVars) :: xs),true)
equation
cr = ComponentReference.makeCrefIdent(iteratorName, tp, {});
iteratorExp = Expression.crefExp(cr);
iteratorexps = extendRange(e,knv);
v_1 = detectImplicitDiscreteAlgsStatemensFor(iteratorExp,iteratorexps,v,knv,statementLst,true);
v_2 = detectImplicitDiscreteAlgsStatemens(v_1,knv, xs,true);
then
v_2;
*/

case (v,knv,(DAE.STMT_WHEN(statementLst = statementLst,elseWhen = NONE()) :: xs),_)
equation
v_1 = detectImplicitDiscreteAlgsStatemens(v,knv,statementLst,true);
Expand Down Expand Up @@ -2740,6 +2755,7 @@ algorithm
DAE.ElementSource source;
Algorithm.Else algElse;
tuple<DAE.Exp, list<DAE.Exp>, DAE.Exp, tuple<list<BackendDAE.ZeroCrossing>,Integer>, tuple<Integer,BackendDAE.Variables,BackendDAE.Variables>> extraArg;
list<tuple<DAE.ComponentRef,Absyn.Info>> loopPrlVars "list of parallel variables used/referenced in the parfor loop";

case ({},_,extraArg,knvars) then (({},extraArg));

Expand Down Expand Up @@ -2791,6 +2807,15 @@ algorithm
((xs_1, extraArg)) = traverseStmtsExps(xs, func, extraArg, knvars);
then ((DAE.STMT_FOR(tp,b1,id1,ix,e,stmts2,source) :: xs_1,extraArg));

case (((x as DAE.STMT_PARFOR(type_=tp,iterIsArray=b1,iter=id1,index=ix,range=e,statementLst=stmts, loopPrlVars= loopPrlVars, source = source)) :: xs),func, extraArg, knvars)
equation
cr = ComponentReference.makeCrefIdent(id1, tp, {});
iteratorExp = Expression.crefExp(cr);
iteratorexps = extendRange(e,knvars);
((stmts2, extraArg)) = traverseStmtsForExps(iteratorExp, iteratorexps,e, stmts, knvars, func, extraArg);
((xs_1, extraArg)) = traverseStmtsExps(xs, func, extraArg, knvars);
then ((DAE.STMT_PARFOR(tp,b1,id1,ix,e,stmts2,loopPrlVars,source) :: xs_1,extraArg));

case (((x as DAE.STMT_WHILE(exp = e,statementLst=stmts, source = source)) :: xs),func, extraArg, knvars)
equation
((stmts2, extraArg)) = traverseStmtsExps(stmts,func, extraArg, knvars);
Expand Down
9 changes: 9 additions & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -5478,6 +5478,15 @@ algorithm
extraArg = traverseStmts(stmts,func,extraArg);
then
traverseStmts(xs, func, extraArg);

case (((x as DAE.STMT_PARFOR(type_=tp,iterIsArray=b1,iter=id1,range=e,statementLst=stmts)) :: xs),func,extraArg)
equation
((_, extraArg)) = func((e, extraArg));
cr = ComponentReference.makeCrefIdent(id1, tp, {});
(stmts,_) = DAEUtil.traverseDAEEquationsStmts(stmts,Expression.traverseSubexpressionsHelper,(Expression.replaceCref,(cr,e)));
extraArg = traverseStmts(stmts,func,extraArg);
then
traverseStmts(xs, func, extraArg);

case (((x as DAE.STMT_WHILE(exp = e,statementLst=stmts)) :: xs),func,extraArg)
equation
Expand Down
13 changes: 13 additions & 0 deletions Compiler/BackEnd/BackendVarTransform.mo
Expand Up @@ -1314,6 +1314,7 @@ algorithm
list<Integer> helpVarIndices;
Integer index;
Boolean b,b1,b2,b3;
list<tuple<DAE.ComponentRef,Absyn.Info>> loopPrlVars "list of parallel variables used/referenced in the parfor loop";

case ({},_,_,_) then (listReverse(inAcc),inBAcc);

Expand Down Expand Up @@ -1380,6 +1381,18 @@ algorithm
(es_1,b) = replaceStatementLst(es, repl,DAE.STMT_FOR(type_,iterIsArray,ident,index,e1_2,statementLst_1,source)::inAcc,true);
then
( es_1,b);

case ((DAE.STMT_PARFOR(type_=type_,iterIsArray=iterIsArray,iter=ident,index=index,range=e1,statementLst=statementLst,loopPrlVars=loopPrlVars,source=source)::es),repl,_,_)
equation
(statementLst_1,b1) = replaceStatementLst(statementLst, repl,{},false);
(e1_1,b2) = replaceExp(e1, repl,NONE());
true = b1 or b2;
source = DAEUtil.addSymbolicTransformationSubstitution(b2,source,e1,e1_1);
(e1_2,b1) = ExpressionSimplify.condsimplify(b2,e1_1);
source = DAEUtil.addSymbolicTransformationSimplify(b1,source,e1_1,e1_2);
(es_1,b) = replaceStatementLst(es, repl,DAE.STMT_PARFOR(type_,iterIsArray,ident,index,e1_2,statementLst_1,loopPrlVars,source)::inAcc,true);
then
( es_1,b);

case ((DAE.STMT_WHILE(exp=e1,statementLst=statementLst,source=source)::es),repl,_,_)
equation
Expand Down
6 changes: 6 additions & 0 deletions Compiler/BackEnd/DAEQuery.mo
Expand Up @@ -609,6 +609,12 @@ algorithm
print("- DAEQuery.incidenceRowStmts on FOR not implemented\n");
then
{};

case ((DAE.STMT_PARFOR(type_ = _) :: rest),vars)
equation
print("- DAEQuery.incidenceRowStmts on PARFOR not implemented\n");
then
{};

case ((DAE.STMT_WHILE(exp = _) :: rest),vars)
equation
Expand Down
18 changes: 18 additions & 0 deletions Compiler/BackEnd/PartFn.mo
Expand Up @@ -462,6 +462,8 @@ algorithm
DAE.Statement stmt,stmt_1;
DAE.ElementSource source;
Integer ix;
list<tuple<DAE.ComponentRef,Absyn.Info>> loopPrlVars "list of parallel variables used/referenced in the parfor loop";

case({},dae) then ({},dae);
case(DAE.STMT_ASSIGN(ty,e1,e2,source) :: cdr,dae)
equation
Expand Down Expand Up @@ -492,6 +494,13 @@ algorithm
(cdr_1,dae) = elabStmts(cdr,dae);
then
(DAE.STMT_FOR(ty,b,i,ix,e_1,stmts_1,source) :: cdr_1,dae);
case(DAE.STMT_PARFOR(ty,b,i,ix,e,stmts,loopPrlVars,source) :: cdr,dae)
equation
((e_1,dae)) = Expression.traverseExp(e,elabExp,dae);
(stmts_1,dae) = elabStmts(stmts,dae);
(cdr_1,dae) = elabStmts(cdr,dae);
then
(DAE.STMT_PARFOR(ty,b,i,ix,e_1,stmts_1,loopPrlVars,source) :: cdr_1,dae);
case(DAE.STMT_WHILE(e,stmts,source) :: cdr,dae)
equation
((e_1,dae)) = Expression.traverseExp(e,elabExp,dae);
Expand Down Expand Up @@ -1178,6 +1187,8 @@ algorithm
list<DAE.Exp> elst,elst_1;
DAE.ElementSource source;
Integer ix;
list<tuple<DAE.ComponentRef, Absyn.Info>> loopPrlVars "list of parallel variables used/referenced in the parfor loop";

case({},_,_,_,_) then {};
case(DAE.STMT_ASSIGN(ty,e1,e2,source) :: cdr,dae,p,inputs,current)
equation
Expand Down Expand Up @@ -1214,6 +1225,13 @@ algorithm
cdr_1 = fixCallsAlg(cdr,dae,p,inputs,current);
then
DAE.STMT_FOR(ty,b,i,ix,e_1,stmts_1,source) :: cdr_1;
case(DAE.STMT_PARFOR(ty,b,i,ix,e,stmts,loopPrlVars,source) :: cdr,dae,p,inputs,current)
equation
((e_1,_)) = Expression.traverseExp(e,fixCall,(p,inputs,dae,current));
stmts_1 = fixCallsAlg(stmts,dae,p,inputs,current);
cdr_1 = fixCallsAlg(cdr,dae,p,inputs,current);
then
DAE.STMT_PARFOR(ty,b,i,ix,e_1,stmts_1,loopPrlVars,source) :: cdr_1;
case(DAE.STMT_WHILE(e,stmts,source) :: cdr,dae,p,inputs,current)
equation
((e_1,_)) = Expression.traverseExp(e,fixCall,(p,inputs,dae,current));
Expand Down
8 changes: 8 additions & 0 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -12316,6 +12316,14 @@ algorithm
files = getFilesFromStatements(rest, files);
then
files;

case (DAE.STMT_PARFOR(source = source, statementLst = stmts)::rest, files)
equation
files = getFilesFromDAEElementSource(source, files);
files = getFilesFromStatements(stmts, files);
files = getFilesFromStatements(rest, files);
then
files;

case (DAE.STMT_WHILE(source = source, statementLst = stmts)::rest, files)
equation
Expand Down
39 changes: 39 additions & 0 deletions Compiler/FrontEnd/Algorithm.mo
Expand Up @@ -605,6 +605,44 @@ algorithm
end matchcontinue;
end makeFor;

public function makeParFor "function: makeParFor
This function creates a DAE.STMT_PARFOR construct, checking
that the types of the parts are correct."
input Ident inIdent;
input DAE.Exp inExp;
input DAE.Properties inProperties;
input list<Statement> inStatementLst;
input list<tuple<DAE.ComponentRef,Absyn.Info>> inLoopPrlVars;
input DAE.ElementSource source;
output Statement outStatement;
algorithm
outStatement := matchcontinue (inIdent,inExp,inProperties,inStatementLst,inLoopPrlVars,source)
local
Boolean isArray;
DAE.Type et;
Ident i,e_str,t_str;
DAE.Exp e;
DAE.Type t;
list<Statement> stmts;
DAE.Dimensions dims;

case (i,e,DAE.PROP(type_ = DAE.T_ARRAY(ty = t, dims = dims)),stmts,_,source)
equation
isArray = Types.isArray(t, dims);
et = Types.simplifyType(t);
then
DAE.STMT_PARFOR(t,isArray,i,-1,e,stmts,inLoopPrlVars,source);

case (_,e,DAE.PROP(type_ = t),_,_,source)
equation
e_str = ExpressionDump.printExpStr(e);
t_str = Types.unparseType(t);
Error.addSourceMessage(Error.FOR_EXPRESSION_TYPE_ERROR, {e_str,t_str}, DAEUtil.getElementSourceFileInfo(source));
then
fail();
end matchcontinue;
end makeParFor;

public function makeWhile "function: makeWhile
This function creates a DAE.STMT_WHILE construct, checking that the types
of the parts are correct."
Expand Down Expand Up @@ -794,6 +832,7 @@ algorithm
case DAE.STMT_ASSIGN_ARR(source=source) then source;
case DAE.STMT_IF(source=source) then source;
case DAE.STMT_FOR(source=source) then source;
case DAE.STMT_PARFOR(source=source) then source;
case DAE.STMT_WHILE(source=source) then source;
case DAE.STMT_WHEN(source=source) then source;
case DAE.STMT_ASSERT(source=source) then source;
Expand Down
11 changes: 11 additions & 0 deletions Compiler/FrontEnd/DAE.mo
Expand Up @@ -587,6 +587,17 @@ uniontype Statement "There are four kinds of statements. Assignments (`a := b;\
list<Statement> statementLst;
ElementSource source "the origin of the component/equation/algorithm";
end STMT_FOR;

record STMT_PARFOR
Type type_ "this is the type of the iterator";
Boolean iterIsArray "True if the iterator has an array type, otherwise false.";
Ident iter "the iterator variable";
Integer index "the index of the iterator variable, to make it unique; used by the new inst";
Exp range "range for the loop";
list<Statement> statementLst;
list<tuple<ComponentRef,Absyn.Info>> loopPrlVars "list of parallel variables used/referenced in the parfor loop";
ElementSource source "the origin of the component/equation/algorithm";
end STMT_PARFOR;

record STMT_WHILE
Exp exp;
Expand Down
16 changes: 16 additions & 0 deletions Compiler/FrontEnd/DAEDump.mo
Expand Up @@ -1642,6 +1642,22 @@ algorithm
Print.printBuf("end for;\n");
then
();

case (DAE.STMT_PARFOR(iter = id,index=index,range = e,statementLst = stmts),i)
equation
indent(i);
Print.printBuf("parfor ");
Print.printBuf(id);
Debug.bcall(index <> -1, Print.printBuf, " /* iter index " +& intString(index) +& " */");
Print.printBuf(" in ");
ExpressionDump.printExp(e);
Print.printBuf(" loop\n");
i_1 = i + 2;
ppStmtList(stmts, i_1);
indent(i);
Print.printBuf("end parfor;\n");
then
();

case (DAE.STMT_WHILE(exp = e,statementLst = stmts),i)
equation
Expand Down
8 changes: 8 additions & 0 deletions Compiler/FrontEnd/DAEUtil.mo
Expand Up @@ -4128,6 +4128,7 @@ algorithm
DAE.ElementSource source;
Algorithm.Else algElse;
Type_a extraArg;
list<tuple<DAE.ComponentRef,Absyn.Info>> loopPrlVars "list of parallel variables used/referenced in the parfor loop";

case ({},_,extraArg) then ({},extraArg);

Expand Down Expand Up @@ -4179,6 +4180,13 @@ algorithm
((e_1, extraArg)) = func((e, extraArg));
(xs_1, extraArg) = traverseDAEEquationsStmts(xs, func, extraArg);
then (DAE.STMT_FOR(tp,b1,id1,ix,e_1,stmts2,source) :: xs_1,extraArg);

case (((x as DAE.STMT_PARFOR(type_=tp,iterIsArray=b1,iter=id1,index=ix,range=e,statementLst=stmts, loopPrlVars=loopPrlVars, source = source)) :: xs),func,extraArg)
equation
(stmts2, extraArg) = traverseDAEEquationsStmts(stmts,func,extraArg);
((e_1, extraArg)) = func((e, extraArg));
(xs_1, extraArg) = traverseDAEEquationsStmts(xs, func, extraArg);
then (DAE.STMT_PARFOR(tp,b1,id1,ix,e_1,stmts2,loopPrlVars,source) :: xs_1,extraArg);

case (((x as DAE.STMT_WHILE(exp = e,statementLst=stmts, source = source)) :: xs),func,extraArg)
equation
Expand Down
16 changes: 13 additions & 3 deletions Compiler/FrontEnd/InstSection.mo
Expand Up @@ -4853,6 +4853,7 @@ algorithm
InstanceHierarchy ih;
DAE.ElementSource source;
list<tuple<DAE.ComponentRef,Absyn.Info>> loopPrlVars;
DAE.ComponentRef parforIter;

// one iterator
case (cache,env,ih,pre,ci_state,i,SOME(e),sl,info,source,initial_,impl,unrollForLoops)
Expand All @@ -4868,13 +4869,18 @@ algorithm
// situations. Start with empty list and collect all variables cref'ed
// in the loop body.
loopPrlVars = collectParallelVariables({},sl_1);

// Remove the parfor loop iterator from the list(implicitly declared).
parforIter = DAE.CREF_IDENT(i, t,{});
(loopPrlVars,_) = List.deleteMemberOnTrue(parforIter,loopPrlVars,crefInfoListCrefsEqual);

// Check the cref's in the list one by one to make
// sure that they are parallel variables.
// checkParallelVariables(cache,env_1,loopPrlVars);
List.map2_0(loopPrlVars, isCrefParGlobalOrForIterator, cache, env_1);

source = DAEUtil.addElementSourceFileInfo(source,info);
stmt = Algorithm.makeFor(i, e_2, prop, sl_1, source);
stmt = Algorithm.makeParFor(i, e_2, prop, sl_1, loopPrlVars, source);
then
(cache,{stmt});

Expand Down Expand Up @@ -4936,12 +4942,16 @@ algorithm

// is it parglobal var?
isParglobal = SCode.parallelismEqual(prl, SCode.PARGLOBAL());

// Now the iterator is already removed. No need for this.
// is it the iterator of the parfor loop(implicitly declared)?
isForiterator = Util.isSome(cnstForRange);
// isForiterator = Util.isSome(cnstForRange);

//is it either a parglobal var or for iterator
true = isParglobal or isForiterator;
//true = isParglobal or isForiterator;

true = isParglobal;

then ();

case((cref,info),_,_)
Expand Down

0 comments on commit 57cd95b

Please sign in to comment.