Skip to content

Commit

Permalink
- Started using priority queue for greedy static scheduling
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10676 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Dec 8, 2011
1 parent 0711327 commit 7cc8598
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 27 deletions.
62 changes: 38 additions & 24 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -64,7 +64,6 @@ public import HashTableExpToIndex;
public import HashTableStringToPath;
public import Inline;
public import Interactive;
public import PriorityQueue;
public import SCode;
public import Tpl;
public import Types;
Expand Down Expand Up @@ -101,6 +100,7 @@ protected import Inst;
protected import List;
protected import Mod;
protected import PartFn;
protected import PriorityQueue;
protected import SCodeUtil;
protected import Settings;
protected import SimCodeC;
Expand Down Expand Up @@ -12281,15 +12281,21 @@ algorithm
end fileName2fileIndex;

protected function makeEqualLengthLists
input list<list<A>> lst;
"Greedy algorithm for scheduling. Very simple:
Calculate the weight of each eq.system, sort these s.t.
the most expensive system is treated first. Add
this eq.system to the block with the least cost at the moment.
"
input list<list<SimEqSystem>> lst;
input Integer i;
output list<list<A>> olst;
replaceable type A subtypeof Any;
output list<list<SimEqSystem>> olst;
algorithm
olst := matchcontinue (lst,i)
local
Integer n;
list<A> l;
list<SimEqSystem> l;
PriorityQueue.T q;
list<tuple<Integer,list<SimEqSystem>>> prios;
case (lst,_)
equation
false = Flags.isSet(Flags.OPENMP) or Flags.isSet(Flags.PTHREADS);
Expand All @@ -12302,34 +12308,42 @@ algorithm
then l::{};
case (lst,i)
equation
/* TODO: Use priorityqueue for greedy algorithm here */
_ = PriorityQueue.empty;
n = listLength(lst);
n = intDiv(n,i) + Util.if_(intMod(n,i)>0,1,0);
then makeEqualLengthLists2(lst,n,n,{},{});
q = List.fold(List.fill((0,{}),i),PriorityQueue.insert,PriorityQueue.empty);
prios = List.map(lst,calcPriority);
q = makeEqualLengthLists2(prios,q);
lst = List.map(PriorityQueue.elements(q),Util.tuple22);
then lst;
end matchcontinue;
end makeEqualLengthLists;

protected function makeEqualLengthLists2
input list<list<A>> lst;
input Integer i;
input Integer n;
input list<A> acc1;
input list<list<A>> acc2;
output list<list<A>> olst;
replaceable type A subtypeof Any;
input list<tuple<Integer,list<SimEqSystem>>> lst;
input PriorityQueue.T q;
output PriorityQueue.T oq;
algorithm
olst := match (lst,i,n,acc1,acc2)
oq := match (lst,q)
local
list<A> l;
case ({},i,n,{},acc2) then acc2;
case ({},i,n,acc1,acc2) then acc1::acc2;
case (lst,0,n,{},acc2) then makeEqualLengthLists2(lst,n,n,{},acc2);
case (lst,0,n,acc1,acc2) then makeEqualLengthLists2(lst,n,n,{},acc1::acc2);
case (l::lst,i,n,acc1,acc2) then makeEqualLengthLists2(lst,i-1,n,listAppend(l,acc1),acc2);
list<SimEqSystem> l1,l2;
Integer i1,i2;
case ({},q) then q;
case ((i1,l1)::lst,q)
equation
(q,(i2,l2)) = PriorityQueue.deleteAndReturnMin(q);
q = PriorityQueue.insert((i1+i2,listAppend(l2,l1)),q);
then makeEqualLengthLists2(lst,q);
end match;
end makeEqualLengthLists2;

protected function calcPriority
input list<SimEqSystem> eqs;
output tuple<Integer,list<SimEqSystem>> prio;
protected
Integer i;
algorithm
(_,i) := traverseExpsEqSystems(eqs, Expression.complexityTraverse, 0, {});
prio := (i,eqs);
end calcPriority;

protected function traverseExpsSimCode
input SimCode simCode;
input Func func;
Expand Down
42 changes: 39 additions & 3 deletions Compiler/Util/PriorityQueue.mo
Expand Up @@ -52,15 +52,17 @@ TODO: Implement insert() in O(1) time
TODO: Implement meld() in O(1) time
"

import List;
import Util;
protected import List;
protected import Util;
public import SimCode;

public
/* protected */
/* TODO: Hide when RML is killed */

/* This specific version... */
replaceable type Priority = Integer;
replaceable type Data = list<Integer>;
replaceable type Data = list<SimCode.SimEqSystem>;

/* Replaceable types */

Expand Down Expand Up @@ -164,6 +166,40 @@ algorithm
ots := meld(listReverse(ts1),ts2);
end deleteMin;

function deleteAndReturnMin
input T ts;
output T ots;
output Element elt;
protected
T ts1,ts2;
algorithm
(NODE(elt=elt,trees=ts1),ts2) := getMin(ts);
ots := meld(listReverse(ts1),ts2);
end deleteAndReturnMin;

function elements
input T ts;
output list<Element> elts;
algorithm
elts := elements2(ts,{});
end elements;

function elements2
input T ts;
input list<Element> acc;
output list<Element> elts;
algorithm
elts := match (ts,acc)
local
Element elt;
case ({},acc) then listReverse(acc);
case (ts,acc)
equation
(ts,elt) = deleteAndReturnMin(ts);
then elements2(ts,elt::acc);
end match;
end elements2;

/* TODO: Hide from user when we remove RML... */

type Rank = Integer;
Expand Down

0 comments on commit 7cc8598

Please sign in to comment.