Skip to content

Commit

Permalink
array optimizsations
Browse files Browse the repository at this point in the history
new function Array.createIntRange()
  • Loading branch information
hkiel committed Mar 16, 2016
1 parent 80dcee4 commit 0fe22a2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 34 deletions.
8 changes: 4 additions & 4 deletions Compiler/BackEnd/DAEQuery.mo
Expand Up @@ -187,7 +187,7 @@ protected function getIncidenceMatrix2 "author: adrpo
output String strIMatrix;
algorithm
strIMatrix :=
matchcontinue (inStringLstLst,rowIndex)
match (inStringLstLst,rowIndex)
local
list<String> row;
list<list<String>> rows;
Expand All @@ -206,7 +206,7 @@ algorithm
str = stringAppendList({"{", str1, "},", str2});
then
str;
end matchcontinue;
end match;
end getIncidenceMatrix2;

protected function getIncidenceRow "author: adrpo
Expand All @@ -216,7 +216,7 @@ protected function getIncidenceRow "author: adrpo
output String strRow;
algorithm
strRow :=
matchcontinue (inStringLst)
match (inStringLst)
local
String s, s2, x;
list<String> xs;
Expand All @@ -228,7 +228,7 @@ algorithm
s = stringAppendList({x, ",", s2});
then
s;
end matchcontinue;
end match;
end getIncidenceRow;

public function getVariables "This function returns the variables
Expand Down
9 changes: 5 additions & 4 deletions Compiler/BackEnd/DumpGraphML.mo
Expand Up @@ -39,6 +39,7 @@ encapsulated package DumpGraphML
public import BackendDAE;
public import DAE;

protected import Array;
protected import BackendDump;
protected import BackendEquation;
protected import BackendDAETransform;
Expand Down Expand Up @@ -81,7 +82,7 @@ algorithm
eqns = BackendEquation.getEqnsFromEqSystem(inSystem);
funcs = BackendDAEUtil.getFunctions(inShared);
(_,m,_) = BackendDAEUtil.getIncidenceMatrix(inSystem,BackendDAE.NORMAL(),SOME(funcs));
mapIncRowEqn = listArray(List.intRange(arrayLength(m)));
mapIncRowEqn = Array.createIntRange(arrayLength(m));
graphInfo = GraphML.createGraphInfo();
(graphInfo,(_,graph)) = GraphML.addGraph("G",false,graphInfo);
((_,_,(graphInfo,graph))) = BackendVariable.traverseBackendDAEVars(vars,addVarGraph,(numberMode,1,(graphInfo,graph)));
Expand All @@ -103,7 +104,7 @@ algorithm
neqns = BackendDAEUtil.equationArraySize(eqns);
//neqns = BackendDAEUtil.equationSize(eqns);
eqnsids = List.intRange(neqns);
mapIncRowEqn = listArray(List.intRange(arrayLength(m)));
mapIncRowEqn = Array.createIntRange(arrayLength(m));
((graphInfo,graph)) = List.fold3(eqnsids,addEqnGraph,eqns,mapIncRowEqn,numberMode,(graphInfo,graph));
((_,_,graphInfo)) = List.fold(eqnsids,addEdgesGraph,(1,m,graphInfo));
GraphML.dumpGraph(graphInfo,filename);
Expand All @@ -115,7 +116,7 @@ algorithm
eqns = BackendEquation.getEqnsFromEqSystem(inSystem);
funcs = BackendDAEUtil.getFunctions(inShared);
//(_,m,mt) = BackendDAEUtil.getIncidenceMatrix(inSystem, BackendDAE.NORMAL(), SOME(funcs));
//mapIncRowEqn = listArray(List.intRange(arrayLength(m)));
//mapIncRowEqn = Array.createIntRange(arrayLength(m));
//(_,m,mt,_,mapIncRowEqn) = BackendDAEUtil.getIncidenceMatrixScalar(inSystem,BackendDAE.SOLVABLE(), SOME(funcs)));
(_,m,_,_,mapIncRowEqn) = BackendDAEUtil.getIncidenceMatrixScalar(inSystem,BackendDAE.NORMAL(), SOME(funcs));
graphInfo = GraphML.createGraphInfo();
Expand Down Expand Up @@ -643,4 +644,4 @@ algorithm
end isUnMarked;

annotation(__OpenModelica_Interface="backend");
end DumpGraphML;
end DumpGraphML;
2 changes: 1 addition & 1 deletion Compiler/BackEnd/HpcOmEqSystems.mo
Expand Up @@ -1056,7 +1056,7 @@ algorithm
sysTmp = BackendDAEUtil.setEqSystMatching(sysTmp, matching);

// perform BLT to order the StrongComponents
mapIncRowEqn = listArray(List.intRange(nEqs));
mapIncRowEqn = Array.createIntRange(nEqs);
mapEqnIncRow = Array.map(mapIncRowEqn,List.create);
(sysTmp,compsTmp) = BackendDAETransform.strongComponentsScalar(sysTmp,shared,mapEqnIncRow,mapIncRowEqn);
compsTmp = listAppend(compsIn,compsTmp);
Expand Down
6 changes: 3 additions & 3 deletions Compiler/BackEnd/Matching.mo
Expand Up @@ -4715,7 +4715,7 @@ algorithm
row_degrees := arrayCreate(ne,0);
onerows := getOneRows(ne,mT,row_degrees,{});
onecolums := getOneRows(nv,m,col_degrees,{});
randarr := listArray(List.intRange(ne));
randarr := Array.createIntRange(ne);
setrandArray(ne,randarr);
ks_rand_cheapmatching1(1,ne,onecolums,onerows,col_degrees,row_degrees,randarr,m,mT,ass1,ass2);
outUnMatched := getUnassigned(ne,ass1,{});
Expand Down Expand Up @@ -6399,9 +6399,9 @@ algorithm
equation
ne = BackendDAEUtil.systemSize(isyst);
nv = BackendVariable.daenumVariables(isyst);
randarr = listArray(List.intRange(ne));
randarr = Array.createIntRange(ne);
setrandArray(ne, randarr);
randarr1 = listArray(List.intRange(nv));
randarr1 = Array.createIntRange(nv);
setrandArray(nv, randarr1);
syst.orderedEqs = randSortSystem1( ne, 0, randarr, eqns, BackendEquation.listEquation({}),
BackendEquation.equationNth1, BackendEquation.addEquation );
Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/Uncertainties.mo
Expand Up @@ -794,8 +794,8 @@ algorithm
Matching.matchingExternalsetIncidenceMatrix(size,size,mx);

//BackendDump.dumpIncidenceMatrix(mx);
ass1=listArray(List.fill(0,size));
ass2=listArray(List.fill(0,size));
ass1=arrayCreate(size,0);
ass2=arrayCreate(size,0);

true = BackendDAEEXT.setAssignment(size,size,ass2,ass1);
BackendDAEEXT.matching(size,size,1,-1,1.0,0);
Expand Down
34 changes: 17 additions & 17 deletions Compiler/Script/Refactor.mo
Expand Up @@ -1238,13 +1238,13 @@ algorithm

case(Absyn.MODIFICATION(finalPrefix = fi, eachPrefix = e, path = Absyn.IDENT(name = "pattern"), modification = SOME(Absyn.CLASSMOD( elementArgLst = args ,eqMod = Absyn.EQMOD(exp=Absyn.INTEGER(value = x)))), comment = com, info = mod_info) :: rest,context as ("Line" :: _),res,p)
equation
val = arrayGet(listArray(patternMapList),x+1);
val = listGet(patternMapList,x+1);
res = transformConnectAnnList(rest,context,res,p);
then Absyn.MODIFICATION(fi,e,Absyn.IDENT("pattern"), SOME(Absyn.CLASSMOD(args,Absyn.EQMOD(Absyn.CREF(Absyn.CREF_QUAL("LinePattern", {},Absyn.CREF_IDENT(val, {}))),Absyn.dummyInfo))),com, mod_info):: res;

case(Absyn.MODIFICATION(finalPrefix = fi, eachPrefix = e, path = Absyn.IDENT(name = "thickness"), modification = SOME(Absyn.CLASSMOD( elementArgLst = args ,eqMod = Absyn.EQMOD(exp=Absyn.INTEGER(value = x)))), comment = com, info = mod_info) :: rest,context as ("Line" :: _),res,p)
equation
thick = arrayGet(listArray(thicknessMapList),x);
thick = listGet(thicknessMapList,x);
res = transformConnectAnnList(rest,context,res,p);
s = realString(thick);
then Absyn.MODIFICATION(fi,e,Absyn.IDENT("thickness"), SOME(Absyn.CLASSMOD(args,Absyn.EQMOD(Absyn.REAL(s),Absyn.dummyInfo))),com,mod_info):: res;
Expand All @@ -1255,9 +1255,9 @@ algorithm

case(Absyn.MODIFICATION(finalPrefix = fi, eachPrefix = e, path = Absyn.IDENT(name = "arrow"), modification = SOME(Absyn.CLASSMOD( elementArgLst = args ,eqMod = Absyn.EQMOD(exp=Absyn.INTEGER(value = x)))), comment = com, info = mod_info) :: rest,context as ("Line" :: _),res,p)
equation
arrows = arrayGet(listArray(arrowMapList),x+1);
val1 = arrayGet(listArray(arrows),1);
val2 = arrayGet(listArray(arrows),2);
arrows = listGet(arrowMapList,x+1);
val1 = listGet(arrows,1);
val2 = listGet(arrows,2);
res = transformConnectAnnList(rest,context,res,p);
then Absyn.MODIFICATION(fi,e,Absyn.IDENT("arrow"), SOME(Absyn.CLASSMOD(args,Absyn.EQMOD(Absyn.ARRAY({Absyn.CREF(Absyn.CREF_QUAL("Arrow", {},Absyn.CREF_IDENT(val1, {}))),Absyn.CREF(Absyn.CREF_QUAL("Arrow",{},Absyn.CREF_IDENT(val2,{})))}),Absyn.dummyInfo))),com, mod_info):: res;

Expand Down Expand Up @@ -1588,33 +1588,33 @@ algorithm

case(Absyn.MODIFICATION(path = Absyn.IDENT(name = "pattern"), modification = SOME(Absyn.CLASSMOD(eqMod = Absyn.EQMOD(exp=Absyn.INTEGER(value = x)) ))) :: rest,context )
equation
val = arrayGet(listArray(patternMapList),x+1);
val = listGet(patternMapList,x+1);
restRes = transAnnLstToNamedArgs(rest,context);
then Absyn.NAMEDARG("pattern",Absyn.CREF(Absyn.CREF_QUAL("LinePattern", {},Absyn.CREF_IDENT(val, {})))) :: restRes;

case(Absyn.MODIFICATION(path = Absyn.IDENT(name = "fillPattern"), modification = SOME(Absyn.CLASSMOD(eqMod = Absyn.EQMOD(exp=Absyn.INTEGER(value = x)) ))) :: rest,context )
equation
val = arrayGet(listArray(fillPatternMapList),x+1);
val = listGet(fillPatternMapList,x+1);
restRes = transAnnLstToNamedArgs(rest,context);
then Absyn.NAMEDARG("fillPattern",Absyn.CREF(Absyn.CREF_QUAL("FillPattern", {},Absyn.CREF_IDENT(val, {})))) :: restRes;

case(Absyn.MODIFICATION(path = Absyn.IDENT(name = "thickness"), modification = SOME(Absyn.CLASSMOD(eqMod = Absyn.EQMOD(exp=Absyn.INTEGER(value = x)) ))) :: rest,context as ("Line" :: _))
equation
thick = arrayGet(listArray(thicknessMapList),x);
thick = listGet(thicknessMapList,x);
restRes = transAnnLstToNamedArgs(rest,context);
s = realString(thick);
then Absyn.NAMEDARG("thickness",Absyn.REAL(s)) :: restRes;

case(Absyn.MODIFICATION(path = Absyn.IDENT(name = "thickness"), modification = SOME(Absyn.CLASSMOD(eqMod = Absyn.EQMOD(exp=Absyn.INTEGER(value = x)) ))) :: rest,context )
equation
thick = arrayGet(listArray(thicknessMapList),x);
thick = listGet(thicknessMapList,x);
restRes = transAnnLstToNamedArgs(rest,context);
s = realString(thick);
then Absyn.NAMEDARG("lineThickness",Absyn.REAL(s)) :: restRes;

case(Absyn.MODIFICATION(path = Absyn.IDENT(name = "gradient"), modification = SOME(Absyn.CLASSMOD(eqMod = Absyn.EQMOD(exp=Absyn.INTEGER(value = x)) ))) :: rest,context)
equation
val = arrayGet(listArray(gradientMapList),x+1);
val = listGet(gradientMapList,x+1);
restRes = transAnnLstToNamedArgs(rest,context);
then Absyn.NAMEDARG("fillPattern",Absyn.CREF(Absyn.CREF_QUAL("FillPattern", {},Absyn.CREF_IDENT(val, {})))) :: restRes ;

Expand All @@ -1625,9 +1625,9 @@ algorithm

case(Absyn.MODIFICATION(path = Absyn.IDENT(name = "arrow"), modification = SOME(Absyn.CLASSMOD(eqMod = Absyn.EQMOD(exp=Absyn.INTEGER(value = x)) ))) :: rest,context)
equation
arrows = arrayGet(listArray(arrowMapList),x+1);
val1 = arrayGet(listArray(arrows),1);
val2 = arrayGet(listArray(arrows),2);
arrows = listGet(arrowMapList,x+1);
val1 = listGet(arrows,1);
val2 = listGet(arrows,2);
restRes = transAnnLstToNamedArgs(rest,context);
then Absyn.NAMEDARG("arrow",Absyn.ARRAY({Absyn.CREF(Absyn.CREF_QUAL("Arrow", {},Absyn.CREF_IDENT(val1, {}))),Absyn.CREF(Absyn.CREF_QUAL("Arrow",{},Absyn.CREF_IDENT(val2,{})))})):: restRes ;

Expand Down Expand Up @@ -2155,10 +2155,10 @@ algorithm
Integer color;
case(color)
equation
rcol = arrayGet(listArray(colorMapList),color+1);
color1 = arrayGet(listArray(rcol),1);
color2 = arrayGet(listArray(rcol),2);
color3 = arrayGet(listArray(rcol),3);
rcol = listGet(colorMapList,color+1);
color1 = listGet(rcol,1);
color2 = listGet(rcol,2);
color3 = listGet(rcol,3);
then
(color1,color2,color3);
end match;
Expand Down
18 changes: 15 additions & 3 deletions Compiler/Util/Array.mo
Expand Up @@ -37,7 +37,7 @@ encapsulated package Array
"

protected
import MetaModelica.Dangerous.{arrayGetNoBoundsChecking, arrayCreateNoInit};
import MetaModelica.Dangerous.{arrayGetNoBoundsChecking, arrayUpdateNoBoundsChecking, arrayCreateNoInit};

public function mapNoCopy<T>
"Takes an array and a function over the elements of the array, which is
Expand Down Expand Up @@ -163,10 +163,10 @@ algorithm
// If the array isn't empty, use the first element to create the new array.
res := inFunc(arrayGetNoBoundsChecking(inArray, 1));
outArray := arrayCreateNoInit(len, res);
arrayUpdate(outArray, 1, res);
arrayUpdateNoBoundsChecking(outArray, 1, res);

for i in 2:len loop
arrayUpdate(outArray, i, inFunc(arrayGetNoBoundsChecking(inArray, i)));
arrayUpdateNoBoundsChecking(outArray, i, inFunc(arrayGetNoBoundsChecking(inArray, i)));
end for;
end if;
end map;
Expand Down Expand Up @@ -655,6 +655,18 @@ algorithm
end for;
end copyN;

public function createIntRange
"Creates an array<Integer> of size inLen with the values set to the range of 1:inLen."
input Integer inLen;
output array<Integer> outArray;
algorithm
outArray := arrayCreateNoInit(inLen, 0);

for i in 1:inLen loop
arrayUpdateNoBoundsChecking(outArray, i, i);
end for;
end createIntRange;

public function setRange<T>
"Sets the elements in positions inStart to inEnd to inValue."
input Integer inStart;
Expand Down

0 comments on commit 0fe22a2

Please sign in to comment.