Skip to content

Commit

Permalink
- New preOpt module stub: partitionIndependentBlocks
Browse files Browse the repository at this point in the history
  + Finds independent blocks of equations (and does nothing with them except prints a pretty dump if more than 1 block was found)


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@9641 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Aug 16, 2011
1 parent 262a3bc commit e5d81e8
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 2 deletions.
128 changes: 128 additions & 0 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -5637,4 +5637,132 @@ algorithm
end matchcontinue;
end mergeRelation;

/* Parallel backend stuff */

public function partitionIndependentBlocks
"Finds independent partitions of the equation system by "
input BackendDAE.BackendDAE dlow;
input DAE.FunctionTree ftree;
input Option<BackendDAE.IncidenceMatrix> inM;
input Option<BackendDAE.IncidenceMatrixT> inMT;
output BackendDAE.BackendDAE outDlow;
output Option<BackendDAE.IncidenceMatrix> outM;
output Option<BackendDAE.IncidenceMatrixT> outMT;
algorithm
(outDlow,outM,outMT) := match (dlow,ftree,inM,inMT)
local
BackendDAE.IncidenceMatrix m,mT;
array<Integer> ixs,ixsT;
list<Integer> lst,lst2;
Boolean b;
String str;
list<String> strs;
Integer i,i2;
case (dlow,ftree,inM,inMT)
equation
// print("partitionIndependentBlocks: TODO: Implement me\n");
(m,mT) = BackendDAEUtil.getIncidenceMatrixfromOption(dlow,inM,inMT);
ixs = arrayCreate(arrayLength(m),0);
ixsT = arrayCreate(arrayLength(mT),0);
i = partitionIndependentBlocks0(arrayLength(m),0,m,mT,ixs);
i2 = partitionIndependentBlocks0(arrayLength(mT),0,mT,m,ixsT);
b = i > 1;
Debug.bcall(b,BackendDump.dump,dlow);
printPartition(b,ixs);
printPartition(b,ixsT);
// BackendDump.printEquations(lst,dlow);
then (dlow,SOME(m),SOME(mT));
end match;
end partitionIndependentBlocks;

protected function partitionIndependentBlocks0
input Integer n;
input Integer n2;
input BackendDAE.IncidenceMatrix m;
input BackendDAE.IncidenceMatrixT mT;
input array<Integer> ixs;
output Integer on;
algorithm
on := match (n,n2,m,mT,ixs)
local
Boolean b;
case (0,n2,_,_,_) then n2;
case (n,n2,m,mT,ixs)
equation
b = partitionIndependentBlocks1(n,n2+1,m,mT,ixs);
then partitionIndependentBlocks0(n-1,Util.if_(b,n2+1,n2),m,mT,ixs);
end match;
end partitionIndependentBlocks0;

protected function partitionIndependentBlocks1
input Integer ix;
input Integer n;
input BackendDAE.IncidenceMatrix m;
input BackendDAE.IncidenceMatrixT mT;
input array<Integer> ixs;
output Boolean ochange;
algorithm
ochange := partitionIndependentBlocks2(ixs[ix] == 0,ix,n,m,mT,ixs);
end partitionIndependentBlocks1;

protected function partitionIndependentBlocks2
input Boolean b;
input Integer ix;
input Integer n;
input BackendDAE.IncidenceMatrix m;
input BackendDAE.IncidenceMatrixT mT;
input array<Integer> ixs;
output Boolean change;
algorithm
change := match (b,ix,n,m,mT,ixs)
local
Integer i;
list<Integer> lst;
list<list<Integer>> lsts;
case (false,ix,n,m,mT,ixs) then false;
case (true,ix,n,m,mT,ixs)
equation
// i = ixs[ix];
// print(intString(ix) +& "; update crap\n");
// print("mark\n");
ixs = arrayUpdate(ixs,ix,n);
// print("mark OK\n");
lst = Util.listMap(mT[ix],intAbs);
// print(Util.stringDelimitList(Util.listMap(lst,intString),",") +& "\n");
// print("len:" +& intString(arrayLength(m)) +& "\n");
lsts = Util.listMap1r(lst,arrayGet,m);
// print("arrayNth OK\n");
lst = Util.listMap(Util.listFlatten(lsts),intAbs);
// print(Util.stringDelimitList(Util.listMap(lst,intString),",") +& "\n");
// print("lst get\n");
_ = Util.listMap4(lst,partitionIndependentBlocks1,n,m,mT,ixs);
then true;
end match;
end partitionIndependentBlocks2;

protected function arrayUpdateForPartition
input Integer ix;
input array<Integer> ixs;
input Integer val;
output array<Integer> oixs;
algorithm
// print("arrayUpdate("+&intString(ix+1)+&","+&intString(val)+&")\n");
oixs := arrayUpdate(ixs,ix+1,val);
end arrayUpdateForPartition;

protected function printPartition
input Boolean b;
input array<Integer> ixs;
algorithm
_ := match (b,ixs)
case (true,ixs)
equation
print("Got partition!\n");
print(Util.stringDelimitList(Util.listMap(arrayList(ixs), intString), ","));
print("\n");
then ();
else ();
end match;
end printPartition;

end BackendDAEOptimize;
2 changes: 2 additions & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -5990,6 +5990,7 @@ algorithm
(dae2,m2,mT2);
case (dae,funcs,(optModule,moduleStr)::rest,m,mT)
equation
Debug.execStat("<failed> preOpt " +& moduleStr,BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
str = stringAppendList({"Optimisation Module ",moduleStr," failed."});
Error.addMessage(Error.INTERNAL_ERROR, {str});
(dae1,m1,mT1) = preoptimiseDAE(dae,funcs,rest,m,mT);
Expand Down Expand Up @@ -6247,6 +6248,7 @@ algorithm
(BackendDAEOptimize.removeProtectedParameters,"removeProtectedParameters"),
(BackendDAEOptimize.removeUnusedParameter,"removeUnusedParameter"),
(BackendDAEOptimize.removeUnusedVariables,"removeUnusedVariables"),
(BackendDAEOptimize.partitionIndependentBlocks,"partitionIndependentBlocks"),
(BackendDAECreate.expandDerOperator,"expandDerOperator")};
strPreOptModules := getPreOptModulesString();
strPreOptModules := Util.getOptionOrDefault(ostrPreOptModules,strPreOptModules);
Expand Down
108 changes: 106 additions & 2 deletions Compiler/Util/Util.mo
Expand Up @@ -1223,6 +1223,40 @@ algorithm
end matchcontinue;
end arrayMapHelp1;

public function arrayMap0
input array<Type_a> array;
input FuncType func;
replaceable type Type_a subtypeof Any;
partial function FuncType
input Type_a x;
end FuncType;
algorithm
arrayMap0work(arrayLength(array),array,func);
end arrayMap0;

public function arrayMap0work
""
input Integer ix;
input array<Type_a> array;
input FuncType func;
replaceable type Type_a subtypeof Any;
partial function FuncType
input Type_a x;
end FuncType;
algorithm
_ := match (ix,array,func)
local
Integer i;
case (0,_,_) then ();
case (ix,array,func)
equation
i = arrayLength(array)-ix+1;
func(array[i]);
arrayMap0work(ix-1,array,func);
then ();
end match;
end arrayMap0work;

public function listMap "function: listMap
Takes a list and a function over the elements of the lists, which is applied
for each element, producing a new list.
Expand Down Expand Up @@ -2014,13 +2048,12 @@ algorithm
list<Type_f> r_1;
Type_a f;
list<Type_a> r;
mapFunc fn;
Type_b extraarg1;
Type_c extraarg2;
Type_d extraarg3;
Type_e extraarg4;
case ({},_,_,_,_,_) then {};
case ((f :: r),fn,extraarg1,extraarg2,extraarg3,extraarg4)
case ((f :: r),_,extraarg1,extraarg2,extraarg3,extraarg4)
equation
f_1 = fn(f, extraarg1, extraarg2, extraarg3,extraarg4);
r_1 = listMap4(r, fn, extraarg1, extraarg2, extraarg3,extraarg4);
Expand Down Expand Up @@ -2669,6 +2702,77 @@ algorithm
end match;
end listMap02;

public function listMap03
input list<Type_a> inList;
input FuncType inFunc;
input Type_b inArg1;
input Type_c inArg2;
input Type_d d;

replaceable type Type_a subtypeof Any;
replaceable type Type_b subtypeof Any;
replaceable type Type_c subtypeof Any;
replaceable type Type_d subtypeof Any;

partial function FuncType
input Type_a inElement;
input Type_b inArg1;
input Type_c inArg2;
input Type_d d;
end FuncType;
algorithm
_ := match(inList, inFunc, inArg1, inArg2, d)
local
Type_a element;
list<Type_a> rest;

case ({}, _, _, _, _) then ();
case (element :: rest, _, _, _, _)
equation
inFunc(element, inArg1, inArg2, d);
listMap03(rest, inFunc, inArg1, inArg2, d);
then
();
end match;
end listMap03;

public function listMap04
input list<Type_a> inList;
input FuncType inFunc;
input Type_b inArg1;
input Type_c inArg2;
input Type_d d;
input Type_e e;

replaceable type Type_a subtypeof Any;
replaceable type Type_b subtypeof Any;
replaceable type Type_c subtypeof Any;
replaceable type Type_d subtypeof Any;
replaceable type Type_e subtypeof Any;

partial function FuncType
input Type_a inElement;
input Type_b inArg1;
input Type_c inArg2;
input Type_d d;
input Type_e e;
end FuncType;
algorithm
_ := match(inList, inFunc, inArg1, inArg2, d, e)
local
Type_a element;
list<Type_a> rest;

case ({}, _, _, _, _, _) then ();
case (element :: rest, _, _, _, _, _)
equation
inFunc(element, inArg1, inArg2, d, e);
listMap04(rest, inFunc, inArg1, inArg2, d, e);
then
();
end match;
end listMap04;

public function listMapFlat "function: listMapFlat
Takes a list and a function over the elements of the lists, which is applied
for each element, producing a new list."
Expand Down

0 comments on commit e5d81e8

Please sign in to comment.