Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 388bbcf

Browse files
hkielOpenModelica-Hudson
authored andcommitted
optimize Graph.partialDistance2colorInt
1 parent e8e2216 commit 388bbcf

File tree

2 files changed

+21
-37
lines changed

2 files changed

+21
-37
lines changed

Compiler/BackEnd/SymbolicJacobian.mo

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ protected
12461246
constant Boolean debug = false;
12471247
list<Integer> nodesList;
12481248
array<Integer> colored;
1249-
array<Option<list<Integer>>> forbiddenColor;
1249+
array<Integer> forbiddenColor;
12501250
list<tuple<Integer, list<Integer>>> sparseGraph, sparseGraphT;
12511251
array<tuple<Integer, list<Integer>>> arraysparseGraph;
12521252
Integer maxColor;
@@ -1270,20 +1270,23 @@ algorithm
12701270
end if;
12711271

12721272
// color sparse bipartite graph
1273-
forbiddenColor := arrayCreate(sizeVars,NONE());
1273+
forbiddenColor := arrayCreate(sizeVars,0);
12741274
colored := arrayCreate(sizeVars,0);
12751275
arraysparseGraph := listArray(sparseGraph);
12761276
if debug then execStat("generateSparsePattern -> coloring start "); end if;
12771277
if (sizeVars>0) then
12781278
Graph.partialDistance2colorInt(sparseGraphT, forbiddenColor, nodesList, arraysparseGraph, colored);
12791279
end if;
12801280
if debug then execStat("generateSparsePattern -> coloring end "); end if;
1281+
GC.free(forbiddenColor);
1282+
GC.free(arraysparseGraph);
12811283
// get max color used
12821284
maxColor := Array.fold(colored, intMax, 0);
12831285

12841286
// map index of that array into colors
12851287
coloredArray := arrayCreate(maxColor, {});
12861288
mapIndexColors(colored, sizeVars, coloredArray);
1289+
GC.free(colored);
12871290

12881291
if Flags.isSet(Flags.DUMP_SPARSE_VERBOSE) then
12891292
print("Print Coloring Cols: \n");

Compiler/Util/Graph.mo

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -949,22 +949,22 @@ forbiddenColors[color[x]] <- ui
949949
color[ui ] <- min{c > 0 : forbiddenColors[c] = ui }
950950
"
951951
input list<tuple<Integer, list<Integer>>> inGraphT;
952-
input array<Option<list<Integer>>> inforbiddenColor;
952+
input array<Integer> inforbiddenColor;
953953
input list<Integer> inColors;
954954
input array<tuple<Integer, list<Integer>>> inGraph;
955955
input array<Integer> inColored;
956956
protected
957957
Integer node, color;
958958
list<Integer> nodes;
959-
array<Option<list<Integer>>> forbiddenColor;
959+
array<Integer> forbiddenColor;
960960
Integer color;
961961
list<tuple<Integer, list<Integer>>> restGraph;
962962
algorithm
963963
try
964964
for tpl in inGraphT loop
965965
(node,nodes) := tpl;
966966
addForbiddenColorsInt(node, nodes, inColored, inforbiddenColor, inGraph);
967-
color := arrayFindMinColorIndexInt(inforbiddenColor, node, 1);
967+
color := arrayFindMinColorIndexInt(inforbiddenColor, node);
968968
arrayUpdate(inColored, node, color);
969969
end for;
970970
else
@@ -976,16 +976,15 @@ protected function addForbiddenColorsInt
976976
input Integer inNode;
977977
input list<Integer> nodes;
978978
input array<Integer> inColored;
979-
input array<Option<list<Integer>>> forbiddenColor;
979+
input array<Integer> forbiddenColor;
980980
input array<tuple<Integer, list<Integer>>> inGraph;
981981
protected
982982
list<Integer> indexes;
983-
Option<list<Integer>> nodeopt = SOME({inNode});
984983
algorithm
985984
try
986985
for node in nodes loop
987986
((_,indexes)) := arrayGet(inGraph,node);
988-
updateForbiddenColorArrayInt(indexes, inColored, forbiddenColor, nodeopt);
987+
updateForbiddenColorArrayInt(indexes, inColored, forbiddenColor, inNode);
989988
end for;
990989
else
991990
Error.addSourceMessage(Error.INTERNAL_ERROR, {"Graph.addForbiddenColors failed."}, sourceInfo());
@@ -996,8 +995,8 @@ end addForbiddenColorsInt;
996995
protected function updateForbiddenColorArrayInt
997996
input list<Integer> inIndexes;
998997
input array<Integer> inColored;
999-
input array<Option<list<Integer>>> inForbiddenColor;
1000-
input Option<list<Integer>> inNode;
998+
input array<Integer> inForbiddenColor;
999+
input Integer inNode;
10011000
protected
10021001
Integer colorIndex;
10031002
algorithm
@@ -1010,35 +1009,17 @@ algorithm
10101009
end updateForbiddenColorArrayInt;
10111010

10121011
protected function arrayFindMinColorIndexInt
1013-
input array<Option<list<Integer>>> inForbiddenColor;
1012+
input array<Integer> inForbiddenColor;
10141013
input Integer inNode;
1015-
input Integer inIndex;
1016-
output Integer outColor;
1014+
output Integer outColor = 1;
10171015
algorithm
1018-
outColor := matchcontinue(inForbiddenColor, inNode, inIndex)
1019-
local
1020-
list<Integer> nodes;
1021-
case (_, _, _)
1022-
equation
1023-
NONE() = arrayGet(inForbiddenColor, inIndex);
1024-
//print("Found color on index : " + intString(inIndex) + "\n");
1025-
then inIndex;
1026-
case (_, _, _)
1027-
equation
1028-
SOME(nodes) = arrayGet(inForbiddenColor, inIndex);
1029-
//inPrintFunc(nodes,"FobiddenColors:" );
1030-
failure(_ = List.getMemberOnTrue(inNode, nodes, intEq));
1031-
//print("Found color on index : " + intString(inIndex) + "\n");
1032-
then inIndex;
1033-
case (_, _, _)
1034-
equation
1035-
SOME(nodes) = arrayGet(inForbiddenColor, inIndex);
1036-
//inPrintFunc(nodes,"FobiddenColors:" );
1037-
List.getMemberOnTrue(inNode, nodes, intEq);
1038-
//print("Not found color on index : " + intString(inIndex) + "\n");
1039-
then
1040-
arrayFindMinColorIndexInt(inForbiddenColor, inNode, inIndex+1);
1041-
end matchcontinue;
1016+
while true loop
1017+
if arrayGet(inForbiddenColor, outColor) <> inNode then
1018+
return;
1019+
else
1020+
outColor := outColor + 1;
1021+
end if;
1022+
end while;
10421023
end arrayFindMinColorIndexInt;
10431024

10441025
public function filterGraph

0 commit comments

Comments
 (0)