Skip to content

Commit

Permalink
- some more fixes for Vectorization.mo
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25843 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Volker Waurich committed Apr 30, 2015
1 parent 9510ad4 commit 8d6ce3a
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 34 deletions.
94 changes: 88 additions & 6 deletions Compiler/BackEnd/Vectorization.mo
Expand Up @@ -1133,15 +1133,46 @@ algorithm
end matchcontinue;
end insertSUMexp;

public function rollOutArrays
public function prepareVectorizedDAE0
input BackendDAE.EqSystem sysIn;
input BackendDAE.Shared sharedIn;
output BackendDAE.EqSystem sysOut;
output BackendDAE.Shared sharedOut;
protected
array<Integer> ass1, ass2;
BackendDAE.Variables vars, aliasVars;
list<BackendDAE.Var> varLst, addAlias;
BackendDAE.EquationArray eqs "ordered Equations";
list<BackendDAE.Equation> eqLst;
BackendDAE.EquationArray eqs;
Option<BackendDAE.IncidenceMatrix> m;
Option<BackendDAE.IncidenceMatrixT> mT;
BackendDAE.Matching matching;
BackendDAE.StrongComponents compsIn, comps;
BackendDAE.StateSets stateSets "the statesets of the system";
BackendDAE.BaseClockPartitionKind partitionKind;
algorithm
BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqs, m=m, mT=mT, matching=matching, stateSets=stateSets, partitionKind=partitionKind) := sysIn;
BackendDAE.MATCHING(comps=compsIn, ass1=ass1, ass2=ass2) := matching;

//remove partly unrolled for-equations and corresponding components
(eqLst,_) := List.fold(BackendEquation.equationList(eqs),markUnrolledForEqs,({},{}));
eqs := BackendEquation.listEquation(listReverse(eqLst));

//matching := BackendDAE.MATCHING(ass1, ass2, listReverse(comps));
sysOut := BackendDAE.EQSYSTEM(vars,eqs,m,mT,matching,stateSets,partitionKind);
sharedOut := sharedIn;
end prepareVectorizedDAE0;

public function prepareVectorizedDAE1
input BackendDAE.EqSystem sysIn;
input BackendDAE.Shared sharedIn;
output BackendDAE.EqSystem sysOut;
output BackendDAE.Shared sharedOut;
protected
BackendDAE.Variables vars, aliasVars;
list<BackendDAE.Var> varLst, addAlias;
list<BackendDAE.Equation> eqLst;
BackendDAE.EquationArray eqs;
Option<BackendDAE.IncidenceMatrix> m;
Option<BackendDAE.IncidenceMatrixT> mT;
BackendDAE.Matching matching;
Expand All @@ -1150,19 +1181,70 @@ protected
algorithm
BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqs, m=m, mT=mT, matching=matching, stateSets=stateSets, partitionKind=partitionKind) := sysIn;
BackendDAE.SHARED(aliasVars=aliasVars) := sharedIn;

//unroll variables
varLst := BackendVariable.varList(vars);
//BackendDump.dumpVarList(varLst,"varLst1");
(varLst,addAlias) := List.fold1(varLst,rollOutArrays1,BackendVariable.varList(aliasVars),({},{}));
(varLst,addAlias) := List.fold1(varLst,rollOutArrays,BackendVariable.varList(aliasVars),({},{}));
//BackendDump.dumpVarList(varLst,"the unrolled vars");
//BackendDump.dumpVarList(addAlias,"teh additional alias");
aliasVars := BackendVariable.mergeVariables(aliasVars,BackendVariable.listVar1(addAlias));
//BackendDump.dumpVariables(aliasVars,"final alias");
vars := BackendVariable.listVar(varLst);

sysOut := BackendDAE.EQSYSTEM(vars,eqs,m,mT,matching,stateSets,partitionKind);
sharedOut := BackendDAEUtil.replaceAliasVarsInShared(sharedIn,aliasVars);
end rollOutArrays;
end prepareVectorizedDAE1;

protected function rollOutArrays1

protected function markUnrolledForEqs"checks the loop ids for every for-equation. the loop ids have to be unique."
input BackendDAE.Equation eqIn;
input tuple<list<BackendDAE.Equation>,list<Integer>> tplIn; //foldEqs, loopIds
output tuple<list<BackendDAE.Equation>,list<Integer>> tplOut;
algorithm
tplOut := matchcontinue(eqIn,tplIn)
local
Integer id;
list<Integer> ids0, ids;
list<BackendDAE.Equation> eqLst0, eqLst;
case(BackendDAE.EQUATION(attr=BackendDAE.EQUATION_ATTRIBUTES(loopInfo=BackendDAE.LOOP(loopId=id))),(eqLst0,ids0))
equation
if List.exist1(ids0,intEq,id) then
ids = ids0;
eqLst = setLoopId(eqIn,-1)::eqLst0;
else
ids = id::ids0;
eqLst = eqIn::eqLst0;
end if;
then (eqLst,ids);
case(_,(eqLst0,ids0))
then (eqIn::eqLst0,ids0);
end matchcontinue;
end markUnrolledForEqs;

protected function setLoopId
input BackendDAE.Equation eqIn;
input Integer id;
output BackendDAE.Equation eqOut;
protected
DAE.Exp exp,scalar,startIt,endIt;
list<BackendDAE.IterCref> iterCrefs;
DAE.ElementSource source;
BackendDAE.EquationAttributes attr;
Boolean differentiated;
BackendDAE.EquationKind kind;
Integer subPartitionIndex;
BackendDAE.LoopInfo loopInfo;
algorithm
try
BackendDAE.EQUATION(exp=exp,scalar=scalar,source=source,attr=BackendDAE.EQUATION_ATTRIBUTES(differentiated=differentiated,kind=kind,subPartitionIndex=subPartitionIndex,loopInfo=BackendDAE.LOOP(startIt=startIt,endIt=endIt,crefs=iterCrefs))) := eqIn;
eqOut := BackendDAE.EQUATION(exp,scalar,source,BackendDAE.EQUATION_ATTRIBUTES(differentiated,kind,subPartitionIndex,BackendDAE.LOOP(id,startIt,endIt,iterCrefs)));
else
eqOut := eqIn;
end try;
end setLoopId;

protected function rollOutArrays"expands the array vars. dispatch the unrolled vars to the algebraic and to the alias vars."
input BackendDAE.Var inVar;
input list<BackendDAE.Var> aliasVars;
input tuple<list<BackendDAE.Var>,list<BackendDAE.Var>> foldIn;
Expand Down Expand Up @@ -1214,7 +1296,7 @@ algorithm
case(_,(varLst0,aliasLst0))
then (inVar::varLst0,aliasLst0);
end matchcontinue;
end rollOutArrays1;
end rollOutArrays;

protected function additionalAlias"adds additional alias vars"
input BackendDAE.Var var;
Expand Down
83 changes: 55 additions & 28 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -1562,6 +1562,11 @@ algorithm
uniqueEqIndex = 1;
ifcpp = stringEqual(Config.simCodeTarget(), "Cpp");

if Flags.isSet(Flags.VECTORIZE) then
// prepare the equations
dlow = BackendDAEUtil.mapEqSystem(dlow, Vectorization.prepareVectorizedDAE0);
end if;

backendMapping = setUpBackendMapping(inBackendDAE);
if Flags.isSet(Flags.VISUAL_XML) then
VisualXML.visualizationInfoXML(inBackendDAE, filenamePrefix);
Expand Down Expand Up @@ -1633,8 +1638,10 @@ algorithm
(dlow, stateSets, uniqueEqIndex, tempvars, numStateSets) = createStateSets(dlow, {}, uniqueEqIndex, tempvars);

// create model info

if Flags.isSet(Flags.VECTORIZE) then
dlow = BackendDAEUtil.mapEqSystem(dlow, Vectorization.rollOutArrays);
// prepare the variables
dlow = BackendDAEUtil.mapEqSystem(dlow, Vectorization.prepareVectorizedDAE1);
end if;

modelInfo = createModelInfo(class_, dlow, functions, {}, numStateSets, fileDir);
Expand Down Expand Up @@ -2485,14 +2492,19 @@ algorithm
bzceqns = BackendDAEUtil.blockIsDynamic({index}, zceqnsmark);
(equations1, uniqueEqIndex, tempvars) = createEquation(index, vindex, syst, shared, false, iuniqueEqIndex, itempvars);

firstSES = listHead(equations1); // check if the all equations occure with this index in the c file
isEqSys = isSimEqSys(firstSES);
firstEqIndex = if isEqSys then uniqueEqIndex-1 else iuniqueEqIndex;
//tmpEqSccMapping = List.fold1(List.intRange2(iuniqueEqIndex, uniqueEqIndex - 1), appendSccIdx, isccIndex, ieqSccMapping);

tmpEqSccMapping = List.fold1(List.intRange2(firstEqIndex, uniqueEqIndex - 1), appendSccIdx, isccIndex, ieqSccMapping);
tmpEqBackendSimCodeMapping = List.fold1(List.intRange2(firstEqIndex, uniqueEqIndex - 1), appendSccIdx, index, ieqBackendSimCodeMapping);
tmpBackendMapping = setEqMapping(List.intRange2(firstEqIndex, uniqueEqIndex - 1),{index}, iBackendMapping);
if listEmpty(equations1) then
// the component has been skipped
tmpEqSccMapping = ieqSccMapping;
tmpEqBackendSimCodeMapping = ieqBackendSimCodeMapping;
tmpBackendMapping = iBackendMapping;
else
firstSES = listHead(equations1); // check if the all equations occure with this index in the c file
isEqSys = isSimEqSys(firstSES);
firstEqIndex = if isEqSys then uniqueEqIndex-1 else iuniqueEqIndex;
tmpEqSccMapping = List.fold1(List.intRange2(firstEqIndex, uniqueEqIndex - 1), appendSccIdx, isccIndex, ieqSccMapping);
tmpEqBackendSimCodeMapping = List.fold1(List.intRange2(firstEqIndex, uniqueEqIndex - 1), appendSccIdx, index, ieqBackendSimCodeMapping);
tmpBackendMapping = setEqMapping(List.intRange2(firstEqIndex, uniqueEqIndex - 1),{index}, iBackendMapping);
end if;

odeEquations = if bdynamic and (not bwhen) then equations1::odeEquations else odeEquations;
algebraicEquations = if (not bdynamic) and (not bwhen) then equations1::algebraicEquations else algebraicEquations;
Expand Down Expand Up @@ -2633,14 +2645,18 @@ algorithm
bzceqns = BackendDAEUtil.blockIsDynamic({index}, zceqnsmark);
(equations1, uniqueEqIndex, tempvars) = createEquation(index, vindex, syst, shared, false, iuniqueEqIndex, itempvars);

firstSES = listHead(equations1); // check if the all equations occure with this index in the c file
isEqSys = isSimEqSys(firstSES);
firstEqIndex = if isEqSys then uniqueEqIndex-1 else iuniqueEqIndex;
//tmpEqSccMapping = List.fold1(List.intRange2(iuniqueEqIndex, uniqueEqIndex - 1), appendSccIdx, isccIndex, ieqSccMapping);

tmpEqSccMapping = List.fold1(List.intRange2(firstEqIndex, uniqueEqIndex - 1), appendSccIdx, isccIndex, ieqSccMapping);
tmpEqBackendSimCodeMapping = List.fold1(List.intRange2(firstEqIndex, uniqueEqIndex - 1), appendSccIdx, index, ieqBackendSimCodeMapping);
tmpBackendMapping = setEqMapping(List.intRange2(firstEqIndex, uniqueEqIndex - 1),{index}, iBackendMapping);
if listEmpty(equations1) then
tmpEqSccMapping = ieqSccMapping;
tmpEqBackendSimCodeMapping = ieqBackendSimCodeMapping;
tmpBackendMapping = iBackendMapping;
else
firstSES = listHead(equations1); // check if the all equations occure with this index in the c file
isEqSys = isSimEqSys(firstSES);
firstEqIndex = if isEqSys then uniqueEqIndex-1 else iuniqueEqIndex;
tmpEqSccMapping = List.fold1(List.intRange2(firstEqIndex, uniqueEqIndex - 1), appendSccIdx, isccIndex, ieqSccMapping);
tmpEqBackendSimCodeMapping = List.fold1(List.intRange2(firstEqIndex, uniqueEqIndex - 1), appendSccIdx, index, ieqBackendSimCodeMapping);
tmpBackendMapping = setEqMapping(List.intRange2(firstEqIndex, uniqueEqIndex - 1),{index}, iBackendMapping);
end if;

odeEquations = if bdynamic and (not bwhen) then equations1::odeEquations else odeEquations;
algebraicEquations = if (not bdynamic) and (not bwhen) then equations1::algebraicEquations else algebraicEquations;
Expand All @@ -2652,6 +2668,7 @@ algorithm
// a system of equations
case (_, _, _, _, _, _, _, _, _, _, _, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings)
equation
print("a system comp!\n");
// block is dynamic, belong in dynamic section
(eqnslst, _) = BackendDAETransform.getEquationAndSolvedVarIndxes(comp);
bdynamic = BackendDAEUtil.blockIsDynamic(eqnslst, stateeqnsmark);
Expand Down Expand Up @@ -3103,10 +3120,13 @@ algorithm
source = DAEUtil.addSymbolicTransformationSolve(true, source, cr, e1, e2, exp_, asserts);
if Vectorization.isLoopEquation(eqn) then
//print("CREATE LOOP for "+BackendDump.equationString(eqn)+"\n");
(simEqSys,uniqueEqIndex) = makeSolvedSES_FOR_LOOP(eqn,cr,exp_,iuniqueEqIndex);
eqSystlst = {simEqSys};
//print("CREATED LOOP SES: "+dumpSimEqSystemLst(eqSystlst)+"\n");
tempvars = createTempVarsforCrefs(List.map(solveCr, Expression.crefExp),itempvars);
(eqSystlst,uniqueEqIndex) = makeSolvedSES_FOR_LOOP(eqn,cr,exp_,iuniqueEqIndex);
if listEmpty(eqSystlst) then
tempvars = itempvars;
else
//print("CREATED LOOP SES: "+dumpSimEqSystemLst(eqSystlst)+"\n");
tempvars = createTempVarsforCrefs(List.map(solveCr, Expression.crefExp),itempvars);
end if;
else
(eqSystlst, uniqueEqIndex) = List.mapFold(solveEqns, makeSolved_SES_SIMPLE_ASSIGN, iuniqueEqIndex);
(resEqs, uniqueEqIndex) = addAssertEqn(asserts, {SimCode.SES_SIMPLE_ASSIGN(uniqueEqIndex, cr, exp_, source)}, uniqueEqIndex+1);
Expand Down Expand Up @@ -3147,6 +3167,7 @@ algorithm
(resEqs, uniqueEqIndex, tempvars) = createNonlinearResidualEquations({eqn}, iuniqueEqIndex, itempvars);
cr = if BackendVariable.isStateVar(v) then ComponentReference.crefPrefixDer(cr) else cr;
(_, homotopySupport) = BackendDAETransform.traverseExpsOfEquation(eqn, containsHomotopyCall, false);
print("CREATED NONLINEAR SES: "+dumpSimEqSystemLst({SimCode.SES_NONLINEAR(uniqueEqIndex, resEqs, {cr}, 0, NONE(), false, homotopySupport, false)})+"\n");
then
({SimCode.SES_NONLINEAR(uniqueEqIndex, resEqs, {cr}, 0, NONE(), false, homotopySupport, false)}, uniqueEqIndex+1, tempvars);

Expand Down Expand Up @@ -3906,24 +3927,30 @@ algorithm
ouniqueEqIndex := iuniqueEqIndex+1;
end makeSolved_SES_SIMPLE_ASSIGN;


protected function makeSolvedSES_FOR_LOOP
input BackendDAE.Equation inEqn;
input DAE.ComponentRef lhsCrefIn;
input DAE.Exp rhs;
input Integer iuniqueEqIndex;
output SimCode.SimEqSystem outSimEqn;
output list<SimCode.SimEqSystem> outSimEq;
output Integer ouniqueEqIndex;
algorithm
(outSimEqn,ouniqueEqIndex) := matchcontinue(inEqn,lhsCrefIn,rhs,iuniqueEqIndex)
(outSimEq,ouniqueEqIndex) := matchcontinue(inEqn,lhsCrefIn,rhs,iuniqueEqIndex)
local
Integer id;
DAE.ComponentRef cref,lhsCref;
DAE.Exp iterator,startIt,endIt, rhsExp, body,sigma;
DAE.ElementSource source;
list<BackendDAE.IterCref> iterCrefs;
BackendDAE.IterCref itCref;
SimCode.SimEqSystem ses;
case(BackendDAE.EQUATION(source=source,attr=BackendDAE.EQUATION_ATTRIBUTES(loopInfo=BackendDAE.LOOP(startIt=startIt,endIt=endIt,crefs=iterCrefs as BackendDAE.ACCUM_ITER_CREF(cref=cref)::{}))),_,_,_)
case(BackendDAE.EQUATION(source=source,attr=BackendDAE.EQUATION_ATTRIBUTES(loopInfo=BackendDAE.LOOP(loopId=id,startIt=startIt,endIt=endIt,crefs=iterCrefs))),_,_,_)
equation
// dont generate another for-loop
true = intEq(id,-1);
then ({},iuniqueEqIndex);

case(BackendDAE.EQUATION(source=source,attr=BackendDAE.EQUATION_ATTRIBUTES(loopInfo=BackendDAE.LOOP(loopId=id,startIt=startIt,endIt=endIt,crefs=iterCrefs as BackendDAE.ACCUM_ITER_CREF(cref=cref)::{}))),_,_,_)
equation
// has an accumulated expression (i.e. SIGMA)
iterator = DAE.CREF(ComponentReference.makeCrefIdent("i",DAE.T_INTEGER_DEFAULT,{}),DAE.T_INTEGER_DEFAULT);
Expand All @@ -3937,15 +3964,15 @@ algorithm
(rhsExp,_) = Vectorization.insertSUMexp(rhsExp,(cref,sigma)); // replace the only left array var with the sigma exp
//print("rhsExp 2:"+ExpressionDump.printExpStr(rhsExp)+"\n");
ses = SimCode.SES_SIMPLE_ASSIGN(iuniqueEqIndex,lhsCrefIn,rhsExp,source);
then (ses,iuniqueEqIndex+1);
then ({ses},iuniqueEqIndex+1);

case(BackendDAE.EQUATION(source=source,attr=BackendDAE.EQUATION_ATTRIBUTES(loopInfo=BackendDAE.LOOP(startIt=startIt,endIt=endIt,crefs=iterCrefs))),_,_,_)
case(BackendDAE.EQUATION(source=source,attr=BackendDAE.EQUATION_ATTRIBUTES(loopInfo=BackendDAE.LOOP(loopId=id,startIt=startIt,endIt=endIt,crefs=iterCrefs))),_,_,_)
equation
// is a for equation
iterator = DAE.CREF(ComponentReference.makeCrefIdent("i",DAE.T_INTEGER_DEFAULT,{}),DAE.T_INTEGER_DEFAULT);
(DAE.CREF(componentRef=cref),(_,iterCrefs)) = Expression.traverseExpTopDown(Expression.crefExp(lhsCrefIn),Vectorization.setIteratedSubscriptInCref,(iterator,iterCrefs));
(rhsExp,(_,iterCrefs)) = Expression.traverseExpTopDown(rhs,Vectorization.setIteratedSubscriptInCref,(iterator,iterCrefs));
then (SimCode.SES_FOR_LOOP(iuniqueEqIndex,iterator,startIt,endIt,cref,rhsExp,source),iuniqueEqIndex+1);
then ({SimCode.SES_FOR_LOOP(iuniqueEqIndex,iterator,startIt,endIt,cref,rhsExp,source)},iuniqueEqIndex+1);

else
equation
Expand Down

0 comments on commit 8d6ce3a

Please sign in to comment.