Skip to content

Commit

Permalink
optimized index reduction so that incidence matrix is not rebuilt for…
Browse files Browse the repository at this point in the history
… each reduction.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@1352 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Nov 24, 2004
1 parent d5ce6c4 commit 2a0b9f2
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 34 deletions.
49 changes: 31 additions & 18 deletions modeq/daelow.rml
Expand Up @@ -55,9 +55,10 @@ module DAELow:
* Equation list (* Removed equations a=b*)


type IncidenceMatrix = int list list
type IncidenceMatrix = int list list (* a list of variable indexes, one for each equation*)

type IncidenceMatrixT = IncidenceMatrix

type IncidenceMatrixT = IncidenceMatrix (* a list of equation indexes, one for each variable*)

relation dump: DAELow => ()
relation dump_vars: Var list => ()
Expand Down Expand Up @@ -121,12 +122,12 @@ relation lower: (DAE.DAElist) => DAELow =
Util.string_delimit_list(crefs,", " ) => st &
print st & print "\n" &*)
lower2(lst,s) => (low as DAELOW(vars,knvars,eqns,reqns)) &
print "states :" &
Util.list_map(s,Exp.print_component_ref_str) => sl &
Util.string_delimit_list(sl,", ") => str & print str & print "\n" &
remove_simple_equations(low,s) => low' &
print "removed simple equations, dae:" &
dump low'
(* print "states :" &
Util.list_map(s,Exp.print_component_ref_str) => sl &
Util.string_delimit_list(sl,", ") => str & print str & print "\n" &*)
remove_simple_equations(low,s) => low'
(* print "removed simple equations, dae:" &
dump low' *)
(*list_length(vars) => varlen &
int_string(varlen) => vs &
list_length(eqns) => eqnlen &
Expand Down Expand Up @@ -963,6 +964,10 @@ relation transpose_matrix: IncidenceMatrix => IncidenceMatrixT =
transpose_matrix2(m,neq,0,[]) => mt
----------------
transpose_matrix(m) => mt

rule print "#transpose_matrix failed\n"
---------------------
transpose_matrix(_) => fail
end
relation transpose_matrix2: (IncidenceMatrix,int,int,IncidenceMatrixT)
=> IncidenceMatrixT =
Expand Down Expand Up @@ -1172,16 +1177,16 @@ relation reduce_index_dummy_der: (DAELow,
states_in_eqns(eqns,dae,m,mt) => (states,stateindx) &
differentiate_eqns(dae,m,mt,nv,nf,eqns)
=> (dae,m,mt,nv,nf,deqns) &
update_incidence_matrix(dae,m,mt,i) => (m,mt) &
(* update_incidence_matrix(dae,m,mt,i) => (m,mt) &*)
select_dummy_state(states,stateindx,dae,m,mt) => (state,stateno) &
new_dummy_var(state,dae) => (dummy_der,dae) &
eqns_for_var(mt,stateno) => reqns &
Util.list_union_p(deqns,reqns,int_eq) => changedeqns &
(* We need to change variables in the differentiated equations and in the
equations having the dummy derivative *)
replace_dummy_der(state,dummy_der,dae,m,mt,changedeqns) => (dae,m,mt) &
make_algebraic(dae,state) => dae &
update_incidence_matrix(dae,m,mt,i) => (m,mt)
make_algebraic(dae,state) => dae
(*update_incidence_matrix(dae,m,mt,i) => (m,mt) *)
---------------------------------------------
reduce_index_dummy_der(dae,m,mt,nv,nf,i)
=> (dae,m,mt)
Expand Down Expand Up @@ -1251,19 +1256,22 @@ relation replace_dummy_der: (Exp.ComponentRef, (* state *)
replace_dummy_der2(state,dummyder,eqn) => eqn' &
replace_dummy_der_others(v,eqn') => (eqn',v') &
Util.list_union_p(v,v',var_equal) => v'' &
incidence_row(v'',eqn') => row' &
Util.list_replaceat(row',e',m) => m' &
transpose_matrix(m') => mt' &
Util.list_replaceat(eqn',e',eqns) => eqns' &
replace_dummy_der(state,dummyder,DAELOW(v'',kv,eqns',seqns),m,mt,rest)
=> (dae,m,mt)
-------------------------------
replace_dummy_der(state,dummyder,DAELOW(v,kv,eqns,seqns),m,mt,e::rest)
=> (dae,m,mt)
=> (dae,m',mt')

rule print "-replace_dummy_der failed\n"
-------------------
replace_dummy_der(_,_,_,_,_,_) => fail
end

(* replaces der(state) with dummy_der variable in equationlist_append *)
(* replaces der(state) with dummy_der variable in equation *)
relation replace_dummy_der2:(Exp.ComponentRef,
Exp.ComponentRef,
Equation) => Equation =
Expand Down Expand Up @@ -1480,6 +1488,11 @@ relation differentiate_eqns: (DAELow,
list_nth(eqns,e') => eqn &
time_varying_vars(dae) => timevars &
Derive.differentiate_equation_time(eqn,timevars) => eqn' &
(* update equation row in IncidenceMatrix *)
incidence_row(v,eqn') => row' &
Util.list_replaceat(row',e',m) => m' &
transpose_matrix(m') => mt' &

Debug.fprint("bltdump", "High index problem, differentiated equation: " )&
print "differentiated equation " &
equation_str eqn => str &
Expand All @@ -1492,11 +1505,11 @@ relation differentiate_eqns: (DAELow,
Debug.fprint("bltdump", "\n") &
list_append(eqns,[eqn']) => eqns' &
list_length(eqns') => leneqns &
differentiate_eqns(DAELOW(v,kv,eqns',seqns),m,mt,nv,nf,es)
=> (dae,m,mt,nv,nf,reqns)
differentiate_eqns(DAELOW(v,kv,eqns',seqns),m',mt',nv,nf,es)
=> (dae,m',mt',nv,nf,reqns)
----------------------------
differentiate_eqns(dae as DAELOW(v,kv,eqns,seqns),m,mt,nv,nf,e::es)
=> (dae,m,mt,nv,nf,leneqns::reqns)
=> (dae,m',mt',nv,nf,leneqns::reqns)

rule print "-differentiate_eqns failed\n"
----------------------------
Expand Down Expand Up @@ -2025,11 +2038,11 @@ relation translate_dae: DAELow => DAELow =
calculate_indexes(kv) => kv' &
list_append(vars',kv') => totvars &
variable_replacements(totvars,eqns) => (s,t) &
Util.list_map(s,Exp.print_exp_str) => sl &
(*Util.list_map(s,Exp.print_exp_str) => sl &
Util.string_delimit_list(sl,", ") => sstr &
Util.list_map(t,Exp.print_exp_str) => tl &
Util.string_delimit_list(tl,", ") => tstr &
print "replace " & print sstr & print " with " & print tstr & print "\n" &
print "replace " & print sstr & print " with " & print tstr & print "\n" &*)
replace_variables(eqns,s,t) => eqns' &
transform_variables(vars',s,t) => vars'' &
transform_variables(kv',s,t) => kv''
Expand Down
4 changes: 2 additions & 2 deletions modeq/main.rml
Expand Up @@ -411,11 +411,11 @@ relation modpar: (DAELow.DAELow, int vector, int vector, int list list) => () =
modpar(_,_,_,_) => ()

rule (* Otherwise, build task graph *)
print "old dae:" & DAELow.dump dae &
(*print "old dae:" & DAELow.dump dae &*)
print "translating dae.\n" &
DAELow.translate_dae (dae) => indexed_dae &
DAELow.calculate_values(indexed_dae) => indexed_dae' &
print "new dae:" & DAELow.dump indexed_dae' &
(* print "new dae:" & DAELow.dump indexed_dae' &*)
print "building task graph\n" &
TaskGraph.build_taskgraph(indexed_dae',ass1,ass2,comps) &
TaskGraphExt.dump_graph("model.viz") &
Expand Down
8 changes: 4 additions & 4 deletions modeq/modpar/Codegen.cpp
Expand Up @@ -294,10 +294,10 @@ void Codegen::generateSubTaskCode(VertexID task)
// Edges must be sorted in priority order since e.g. a-b != b-a
EdgePrioQueue *queue = new EdgePrioQueue(EdgePrioCmp(m_tg));
for (tie(e,e_end) = in_edges(task,*m_tg); e != e_end; e++) {
cerr << " prio = " << getPriority(*e,m_tg) << " for " << getResultName(source(*e,*m_tg),m_tg) ;
//cerr << " prio = " << getPriority(*e,m_tg) << " for " << getResultName(source(*e,*m_tg),m_tg) ;
queue->push(*e);
}
cerr << endl;
//cerr << endl;

i=0;
while(!queue->empty()) {
Expand All @@ -311,11 +311,11 @@ void Codegen::generateSubTaskCode(VertexID task)
s.createQueue(); // Must create queue before iterating

while(!s.empty()) {
cerr << " Resultsset indx "<< i << " =" << s.top();
//cerr << " Resultsset indx "<< i << " =" << s.top();
parentnames[i++] = s.top();
s.pop();
}
cerr << endl;
//cerr << endl;
}
}
delete queue;
Expand Down
6 changes: 3 additions & 3 deletions modeq/modpar/MergeSiblings.cpp
Expand Up @@ -53,9 +53,9 @@ bool MergeSiblings::try_merge(ExecCostQueue & queue)
double tau_a,tau_b;
tau_a = getExecCost(a);
tau_b = getExecCost(b);
cerr << "tau_a + tau_b =" << tau_a + tau_b <<
endl;
cerr << "(2*m_latency + max(tau_a,tau_b) =" << (2*m_latency + max(tau_a,tau_b)) << endl;
//cerr << "tau_a + tau_b =" << tau_a + tau_b <<
// endl;
// cerr << "(2*m_latency + max(tau_a,tau_b) =" << (2*m_latency + max(tau_a,tau_b)) << endl;

if ( tau_a + tau_b < (2*m_latency + max(tau_a,tau_b))&& queueSize > m_nproc) {
//cerr << "queue size =" << queue.size() << endl;
Expand Down
14 changes: 7 additions & 7 deletions modeq/taskgraph.rml
Expand Up @@ -198,15 +198,15 @@ relation build_equation:(DAELow.DAELow, int vector, int vector, int) => () =
int_sub(v,1) => v' &
list_nth(vars,v') => (v as DAELow.VAR(cr,kind,_,_,_,_,_,_,_,origname)) &
is_non_state kind &
print "Solving for non-states\n" &
(*print "Solving for non-states\n" &*)
let varexp = Exp.CREF(cr,Exp.REAL) &
Exp.solve(e1,e2,varexp) => expr &
Exp.print_exp_str e1 => e1s &
(* Exp.print_exp_str e1 => e1s &
Exp.print_exp_str e2 => e2s &
print "Equation " & print e1s & print " = " & print e2s &
print " solved for " & Exp.print_exp_str varexp => s &
print s & print " giving " &
Exp.print_exp_str expr => s2 & print s2 & print "\n" &
Exp.print_exp_str expr => s2 & print s2 & print "\n" &*)
build_assignment(cr,expr,origname) => ()
--------------------------------
build_equation(DAELow.DAELOW(vars,_,eqns,_),ass1,ass2,e)
Expand All @@ -219,18 +219,18 @@ relation build_equation:(DAELow.DAELow, int vector, int vector, int) => () =
(* v == variable no solved in this equation *)
int_sub(v,1) => v' &
list_nth(vars,v') => DAELow.VAR(cr,DAELow.STATE,_,_,_,_,_,_,indx,origname) &
print "solving for state\n" &
(* print "solving for state\n" &*)
int_string(indx) => indxs &
Util.string_append_list(["xd[",indxs,"]"]) => id &
let cr' = Exp.CREF_IDENT(id,[]) &
let varexp = Exp.CREF(cr',Exp.REAL) &
Exp.solve(e1,e2,varexp) => expr &
Exp.print_exp_str e1 => e1s &
(* Exp.print_exp_str e1 => e1s &
Exp.print_exp_str e2 => e2s &
print "Equation " & print e1s & print " = " & print e2s &
print "solved for " & Exp.print_exp_str varexp => s &
print s & print "giving " &
Exp.print_exp_str expr => s2 & print s2 & print "\n" &
Exp.print_exp_str expr => s2 & print s2 & print "\n" &*)
build_assignment(cr' ,expr,origname) => ()
--------------------------------
build_equation(DAELow.DAELOW(vars,_,eqns,_),ass1,ass2,e)
Expand Down Expand Up @@ -297,7 +297,7 @@ end
relation add_variable: (DAELow.Var,int) => () =

rule Exp.cref_str(cf) => cfs &
print "adding variable " & print cfs & print "\n" &
(*print "adding variable " & print cfs & print "\n" &*)
TaskGraphExt.store_result(cfs,start,false,name)
------------------------------------
add_variable(DAELow.VAR(cf,_,_,_,_, _,_,_,_,name),start)
Expand Down
1 change: 1 addition & 0 deletions modeq/util.rml
Expand Up @@ -577,6 +577,7 @@ relation list_replaceat: (''a, int, ''a list) => ''a list =
list_replaceat(x,nn,ys) => res
-----------------------------
list_replaceat(x,n,y::ys) => y::res

end

(** relation: list_reduce
Expand Down

0 comments on commit 2a0b9f2

Please sign in to comment.