Skip to content

Commit 638f287

Browse files
committed
Changes towards vectorization.
1 parent 3a51e34 commit 638f287

File tree

7 files changed

+330
-23
lines changed

7 files changed

+330
-23
lines changed

Compiler/BackEnd/Vectorization.mo

Lines changed: 197 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ algorithm
334334
case(DAE.CREF_QUAL(ident=ident, identType=identType, subscriptLst=subscriptLst, componentRef=cref),_)
335335
equation
336336
if List.hasOneElement(subscriptLst) then subscriptLst = {sub}; end if;
337+
cref = replaceFirstSubInCref(cref,sub);
337338
then DAE.CREF_QUAL(ident, identType, subscriptLst, cref);
338339
case(DAE.CREF_IDENT(ident=ident, identType=identType, subscriptLst=subscriptLst),_)
339340
equation
@@ -1232,8 +1233,8 @@ public function prepareVectorizedDAE0
12321233
output BackendDAE.Shared sharedOut;
12331234
protected
12341235
array<Integer> ass1, ass2;
1235-
BackendDAE.Variables vars, aliasVars;
1236-
list<BackendDAE.Var> varLst, addAlias;
1236+
BackendDAE.Variables vars, aliasVars,knownVars;
1237+
list<BackendDAE.Var> varLst, addAlias, aliasLst, knownLst;
12371238
list<BackendDAE.Equation> eqLst;
12381239
BackendDAE.EquationArray eqs;
12391240
Option<BackendDAE.IncidenceMatrix> m;
@@ -1242,22 +1243,102 @@ protected
12421243
BackendDAE.StrongComponents compsIn, comps;
12431244
BackendDAE.StateSets stateSets "the statesets of the system";
12441245
BackendDAE.BaseClockPartitionKind partitionKind;
1246+
BackendDAE.Shared shared;
12451247
algorithm
12461248
BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqs, m=m, mT=mT, matching=matching, stateSets=stateSets, partitionKind=partitionKind) := sysIn;
1249+
BackendDAE.SHARED(aliasVars=aliasVars, knownVars=knownVars) := sharedIn;
12471250

12481251
eqLst := BackendEquation.equationList(eqs);
12491252
//BackendDump.dumpEquationList(eqLst,"eqsIn");
12501253
//remove partly unrolled for-equations
12511254
// occasionally, there is a constantly indexed var in the for-equation
12521255
(eqLst,_) := updateIterCrefs(eqLst,({},{}));
12531256
// (eqLst,_) := List.fold(eqLst,markUnrolledForEqs,({},{}));
1254-
eqs := BackendEquation.listEquation(listReverse(eqLst));
1257+
1258+
// set subscripts at end of equation crefs
1259+
eqLst := List.map(listReverse(eqLst),setSubscriptsAtEndForEquation);
1260+
1261+
// set subscripts at end of vars
1262+
varLst := BackendVariable.varList(vars);
1263+
aliasLst := BackendVariable.varList(aliasVars);
1264+
knownLst := BackendVariable.varList(knownVars);
1265+
varLst := List.map(varLst,appendSubscriptsInVar);
1266+
aliasLst := List.map(aliasLst,appendSubscriptsInVar);
1267+
knownLst := List.map(knownLst,appendSubscriptsInVar);
1268+
vars := BackendVariable.listVar1(varLst);
1269+
aliasVars := BackendVariable.listVar1(aliasLst);
1270+
knownVars := BackendVariable.listVar1(knownLst);
1271+
1272+
eqs := BackendEquation.listEquation(eqLst);
12551273
//BackendDump.dumpEquationList(eqLst,"eqsOut");
1256-
//BackendDump.printShared(sharedIn);
1274+
//BackendDump.dumpVariables(vars,"VARSOUT");
1275+
1276+
//BackendDump.printShared(sharedIn);
12571277
sysOut := BackendDAE.EQSYSTEM(vars,eqs,m,mT,matching,stateSets,partitionKind);
1258-
sharedOut := BackendDAEUtil.replaceRemovedEqsInShared(sharedIn,BackendEquation.listEquation({}));
1278+
shared := BackendDAEUtil.replaceRemovedEqsInShared(sharedIn,BackendEquation.listEquation({}));
1279+
shared := BackendDAEUtil.replaceAliasVarsInShared(shared,aliasVars);
1280+
sharedOut := BackendDAEUtil.replaceKnownVarsInShared(shared,knownVars);
12591281
end prepareVectorizedDAE0;
12601282

1283+
protected function setSubscriptsAtEndForEquation
1284+
input BackendDAE.Equation eqIn;
1285+
output BackendDAE.Equation eqOut;
1286+
algorithm
1287+
eqOut := matchcontinue(eqIn)
1288+
local
1289+
DAE.Exp lhs,rhs;
1290+
DAE.ElementSource source;
1291+
BackendDAE.EquationAttributes attr;
1292+
BackendDAE.LoopInfo loopInfo;
1293+
BackendDAE.Equation eq;
1294+
list<BackendDAE.IterCref> iterCrefs;
1295+
Integer loopId;
1296+
DAE.Exp startIt;
1297+
DAE.Exp endIt;
1298+
case(BackendDAE.EQUATION(exp=lhs, scalar=rhs, source=source, attr = attr as BackendDAE.EQUATION_ATTRIBUTES(loopInfo=
1299+
BackendDAE.LOOP(loopId=loopId,startIt=startIt,endIt=endIt,crefs=iterCrefs))))
1300+
algorithm
1301+
lhs := Expression.traverseExpBottomUp(lhs,appendSubscriptsInExp,"bla");
1302+
rhs := Expression.traverseExpBottomUp(rhs,appendSubscriptsInExp,"bla");
1303+
eq := BackendDAE.EQUATION(lhs,rhs,source,attr);
1304+
iterCrefs := List.map(iterCrefs,setSubscriptAtEndForIterCref);
1305+
loopInfo := BackendDAE.LOOP(loopId,startIt,endIt,iterCrefs);
1306+
eq := setLoopInfoInEq(loopInfo,eq);
1307+
then eq;
1308+
case(BackendDAE.EQUATION(exp=lhs, scalar=rhs, source=source, attr=attr))
1309+
algorithm
1310+
lhs := Expression.traverseExpBottomUp(lhs,appendSubscriptsInExp,"bla");
1311+
rhs := Expression.traverseExpBottomUp(rhs,appendSubscriptsInExp,"bla");
1312+
eq := BackendDAE.EQUATION(lhs,rhs,source,attr);
1313+
then eq;
1314+
end matchcontinue;
1315+
end setSubscriptsAtEndForEquation;
1316+
1317+
protected function setSubscriptAtEndForIterCref
1318+
input BackendDAE.IterCref crefIn;
1319+
output BackendDAE.IterCref crefOut;
1320+
algorithm
1321+
crefOut := matchcontinue(crefIn)
1322+
local
1323+
DAE.ComponentRef cr;
1324+
DAE.Exp iterator;
1325+
DAE.Operator op;
1326+
DAE.Subscript sub;
1327+
case(BackendDAE.ITER_CREF(cref=cr, iterator=iterator))
1328+
algorithm
1329+
{sub} := ComponentReference.crefSubs(cr);
1330+
cr := replaceSubscriptAtEnd(sub,cr);
1331+
then (BackendDAE.ITER_CREF(cr,iterator));
1332+
case(BackendDAE.ACCUM_ITER_CREF(cref=cr, op=op))
1333+
algorithm
1334+
{sub} := ComponentReference.crefSubs(cr);
1335+
cr := replaceSubscriptAtEnd(sub,cr);
1336+
then (BackendDAE.ACCUM_ITER_CREF(cr,op));
1337+
else
1338+
then crefIn;
1339+
end matchcontinue;
1340+
end setSubscriptAtEndForIterCref;
1341+
12611342
protected function updateIterCrefs"checks if the iterated crefs still refer to an iterated index"
12621343
input list<BackendDAE.Equation> eqLstIn;
12631344
input tuple<list<BackendDAE.Equation>,list<Integer>> tplIn; //eqsFoldIn, indxFold
@@ -1320,8 +1401,8 @@ public function enlargeIteratedArrayVars
13201401
output BackendDAE.EqSystem sysOut;
13211402
output BackendDAE.Shared sharedOut;
13221403
protected
1323-
BackendDAE.Variables vars, aliasVars;
1324-
list<BackendDAE.Var> varLst, aliasLst;
1404+
BackendDAE.Variables vars, aliasVars, knownVars;
1405+
list<BackendDAE.Var> varLst, aliasLst, knownLst, knownLst2;
13251406
list<BackendDAE.Equation> eqLst;
13261407
BackendDAE.EquationArray eqs;
13271408
Option<BackendDAE.IncidenceMatrix> m;
@@ -1332,21 +1413,31 @@ protected
13321413
BackendDAE.BaseClockPartitionKind partitionKind;
13331414
algorithm
13341415
BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqs, m=m, mT=mT, matching=matching, stateSets=stateSets, partitionKind=partitionKind) := sysIn;
1335-
BackendDAE.SHARED(aliasVars=aliasVars) := sharedIn;
1416+
BackendDAE.SHARED(aliasVars=aliasVars, knownVars=knownVars) := sharedIn;
13361417

13371418
varLst := BackendVariable.varList(vars);
13381419
aliasLst := BackendVariable.varList(aliasVars);
1420+
knownLst := BackendVariable.varList(knownVars);
13391421
//BackendDump.dumpVarList(varLst,"varLst0");
1422+
//BackendDump.dumpVarList(knownLst,"knownLst0");
13401423
//BackendDump.dumpVarList(aliasLst,"aliasVars0");
13411424

13421425
(varLst,aliasLst) := enlargeIteratedArrayVars1(varLst,aliasLst,{},{});
1426+
(knownLst,knownLst2) := enlargeIteratedArrayVars1(knownLst,{},{},{});
1427+
1428+
//varLst := List.map(varLst,appendSubscriptsInVar);
1429+
//aliasLst := List.map(aliasLst,appendSubscriptsInVar);
1430+
//knownLst := List.map(knownLst,appendSubscriptsInVar);
1431+
13431432
//BackendDump.dumpVarList(varLst,"varLst1");
13441433
//BackendDump.dumpVarList(aliasLst,"aliasVars1");
1434+
//BackendDump.dumpVarList(knownLst,"knownLst1");
13451435

13461436
vars := BackendVariable.listVar1(varLst);
13471437
aliasVars := BackendVariable.listVar1(aliasLst);
13481438
sysOut := BackendDAE.EQSYSTEM(vars,eqs,m,mT,matching,stateSets,partitionKind);
13491439
sharedOut := BackendDAEUtil.replaceAliasVarsInShared(sharedIn,aliasVars);
1440+
sharedOut := BackendDAEUtil.replaceKnownVarsInShared(sharedOut,BackendVariable.listVar1(knownLst));
13501441
end enlargeIteratedArrayVars;
13511442

13521443

@@ -1381,26 +1472,54 @@ algorithm
13811472
//BackendDump.dumpVarList(simVars,"similarVars");
13821473
//BackendDump.dumpVarList(simAlias,"similarAlias");
13831474

1475+
13841476
if listLength(simVars)+listLength(simAlias) == dim then
1385-
// everything is correct
1386-
varFold := listAppend(simVars,varLstFoldIn);
1477+
// everything is correct, set subscripts at end
1478+
varFold := varLstFoldIn;
1479+
for var in simVars loop
1480+
cref := BackendVariable.varCref(var);
1481+
{sub} := ComponentReference.crefSubs(cref);
1482+
cref := replaceSubscriptAtEnd(sub,cref);
1483+
var := BackendVariable.copyVarNewName(cref,var);
1484+
varFold := var::varFold;
1485+
end for;
1486+
13871487
aliasFold := aliasFoldIn;
1488+
for var in simAlias loop
1489+
cref := BackendVariable.varCref(var);
1490+
{sub} := ComponentReference.crefSubs(cref);
1491+
cref := replaceSubscriptAtEnd(sub,cref);
1492+
var := BackendVariable.copyVarNewName(cref,var);
1493+
aliasFold := var::aliasFold;
1494+
end for;
1495+
1496+
1497+
//varFold := listAppend(simVars,varLstFoldIn);
1498+
//aliasFold := aliasFoldIn;
13881499
//print("its fine!\n");
13891500
elseif listLength(simVars) == 1 and intLe(listLength(simAlias),dim-1) then
13901501
// add new alias vars, if the array is mixed simulation and alias var, put everything in the simvar part
13911502
cref := BackendVariable.varCref(listHead(simAlias));
13921503
{sub} := ComponentReference.crefSubs(name);
13931504
subs := List.map(List.intRange(dim),Expression.intSubscript);
13941505
subs := List.deleteMember(subs,sub);
1395-
crefLst := List.map1r(subs,replaceFirstSubInCref,cref);
1506+
//crefLst := List.map1r(subs,replaceFirstSubInCref,name);
1507+
crefLst := List.map1(subs,replaceSubscriptAtEnd,name);
13961508
varLst := List.map1Reverse(crefLst,BackendVariable.copyVarNewName,listHead(simAlias));
1397-
varFold := listHead(varLstIn)::varLstFoldIn;
1509+
// put subscript at end for the var
1510+
cref := BackendVariable.varCref(listHead(varLstIn));
1511+
{sub} := ComponentReference.crefSubs(name);
1512+
cref := replaceSubscriptAtEnd(sub,name);
1513+
var := BackendVariable.copyVarNewName(cref,listHead(varLstIn));
1514+
1515+
varFold := var::varLstFoldIn;
13981516
aliasFold := listAppend(varLst,aliasFoldIn);
13991517
//print("add new aliase\n");
14001518
else
14011519
// expand the simVars
14021520
subs := List.map(List.intRange(dim),Expression.intSubscript);
1403-
crefLst := List.map1r(subs,replaceFirstSubInCref,name);
1521+
//crefLst := List.map1r(subs,replaceFirstSubInCref,name);
1522+
crefLst := List.map1(subs,replaceSubscriptAtEnd,name);
14041523
varLst := List.map1Reverse(crefLst,BackendVariable.copyVarNewName,listHead(varLstIn));
14051524
varFold := listAppend(varLst, varLstFoldIn);
14061525
aliasFold := aliasFoldIn;
@@ -1423,7 +1542,8 @@ algorithm
14231542
else
14241543
// expand the aliasVars
14251544
subs := List.map(List.intRange(dim),Expression.intSubscript);
1426-
crefLst := List.map1r(subs,replaceFirstSubInCref,name);
1545+
//crefLst := List.map1r(subs,replaceFirstSubInCref,name);
1546+
crefLst := List.map1(subs,replaceSubscriptAtEnd,name);
14271547
varLst := List.map1Reverse(crefLst,BackendVariable.copyVarNewName,listHead(aliasLstIn));
14281548
aliasFold := listAppend(varLst, aliasFoldIn);
14291549
varFold := varLstFoldIn;
@@ -1435,18 +1555,81 @@ algorithm
14351555
case(_::rest,_,_,_)
14361556
algorithm
14371557
// add this non-array var
1558+
//print("add non arry var: "+BackendDump.varString(listHead(varLstIn))+"\n");
14381559
(varFold,aliasFold) := enlargeIteratedArrayVars1(rest,aliasLstIn,listHead(varLstIn)::varLstFoldIn,aliasFoldIn);
14391560
then (varFold,aliasFold);
14401561

14411562
case({},_::restAlias,_,_)
14421563
algorithm
14431564
// add this non-array alias
1565+
//print("add non arraz alias var: "+BackendDump.varString(listHead(aliasLstIn))+"\n");
14441566
(varFold,aliasFold) := enlargeIteratedArrayVars1({},restAlias,varLstFoldIn,listHead(aliasLstIn)::aliasFoldIn);
14451567
then (varFold,aliasFold);
14461568

14471569
end matchcontinue;
14481570
end enlargeIteratedArrayVars1;
14491571

1572+
protected function appendSubscriptsInVar
1573+
input BackendDAE.Var varIn;
1574+
output BackendDAE.Var varOut;
1575+
algorithm
1576+
varOut := matchcontinue(varIn)
1577+
local
1578+
DAE.Exp bindExp;
1579+
DAE.ComponentRef name;
1580+
BackendDAE.Var var;
1581+
DAE.Subscript sub;
1582+
case(BackendDAE.VAR(varName=name,bindExp=SOME(bindExp)))
1583+
equation
1584+
{sub} = ComponentReference.crefSubs(name);
1585+
name = replaceSubscriptAtEnd(sub,name);
1586+
bindExp = Expression.traverseExpBottomUp(bindExp,appendSubscriptsInExp,"bla");
1587+
var = BackendVariable.setBindExp(varIn,SOME(bindExp));
1588+
var = BackendVariable.copyVarNewName(name,var);
1589+
then var;
1590+
case(BackendDAE.VAR(varName=name))
1591+
equation
1592+
{sub} = ComponentReference.crefSubs(name);
1593+
name = replaceSubscriptAtEnd(sub,name);
1594+
var = BackendVariable.copyVarNewName(name,varIn);
1595+
then var;
1596+
else
1597+
then varIn;
1598+
end matchcontinue;
1599+
end appendSubscriptsInVar;
1600+
1601+
protected function appendSubscriptsInExp
1602+
input DAE.Exp expIn;
1603+
input String blaIn;
1604+
output DAE.Exp expOut;
1605+
output String blaOut;
1606+
algorithm
1607+
(expOut, blaOut) := matchcontinue(expIn,blaIn)
1608+
local
1609+
DAE.ComponentRef cref;
1610+
DAE.Subscript sub;
1611+
DAE.Type ty;
1612+
case(DAE.CREF(componentRef=cref,ty=ty),_)
1613+
equation
1614+
{sub} = ComponentReference.crefSubs(cref);
1615+
cref = replaceSubscriptAtEnd(sub,cref);
1616+
then (DAE.CREF(cref,ty),blaIn);
1617+
else
1618+
then(expIn,blaIn);
1619+
end matchcontinue;
1620+
end appendSubscriptsInExp;
1621+
1622+
protected function replaceSubscriptAtEnd
1623+
input DAE.Subscript sub;
1624+
input DAE.ComponentRef crefIn;
1625+
output DAE.ComponentRef crefOut;
1626+
protected
1627+
DAE.ComponentRef cref;
1628+
algorithm
1629+
cref := ComponentReference.crefStripSubs(crefIn);
1630+
crefOut := ComponentReference.crefSetLastSubs(cref,{sub});
1631+
end replaceSubscriptAtEnd;
1632+
14501633
/*
14511634
public function prepareVectorizedDAE1
14521635
input BackendDAE.EqSystem sysIn;

Compiler/FrontEnd/ComponentReference.mo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3479,6 +3479,17 @@ algorithm
34793479
{sub_str, idx_str, dim_str, cref_str}, inInfo);
34803480
end printSubscriptBoundsError;
34813481

3482+
public function crefAppendedSubs
3483+
input DAE.ComponentRef cref;
3484+
output String s;
3485+
protected
3486+
String s1,s2;
3487+
algorithm
3488+
s1 := crefToStr("",cref,"_P");
3489+
s2 := stringDelimitList(List.map(List.map(crefSubs(cref),Expression.getSubscriptExp),ExpressionDump.printExpStr),",");
3490+
s := s1+"["+s2+"]";
3491+
end crefAppendedSubs;
3492+
34823493
annotation(__OpenModelica_Interface="frontend");
34833494
end ComponentReference;
34843495

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,7 @@ algorithm
15671567
dlow = BackendDAEUtil.mapEqSystem(dlow, Vectorization.prepareVectorizedDAE0);
15681568
end if;
15691569

1570+
15701571
backendMapping = setUpBackendMapping(inBackendDAE);
15711572
if Flags.isSet(Flags.VISUAL_XML) then
15721573
VisualXML.visualizationInfoXML(inBackendDAE, filenamePrefix);

Compiler/Template/CodegenC.tpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3819,7 +3819,10 @@ case SES_FOR_LOOP(__) then
38193819
<<
38203820
<%modelicaLine(eqInfo(eq))%>
38213821
modelica_integer $P<%printExpStr(iter)%> = 0; // the iterator
3822-
3822+
//BLUB
3823+
//<%crefStr(cref)%>
3824+
///<%printExpStr(crefExp(cref))%>
3825+
//BLUB2
38233826
// the for-equation
38243827
for($P<%printExpStr(iter)%> = <%start%>; $P<%printExpStr(iter)%> < <%stop%>; $P<%printExpStr(iter)%>++)
38253828
{

0 commit comments

Comments
 (0)