Skip to content

Commit

Permalink
- improve Simplification of Add operations by using HashTable instead…
Browse files Browse the repository at this point in the history
… of comparing all Elements in the list by List seach

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15456 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Mar 4, 2013
1 parent df214de commit 14aead2
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 30 deletions.
119 changes: 89 additions & 30 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public type Type = DAE.Type;
public type Subscript = DAE.Subscript;

// protected imports
protected import BaseHashTable;
protected import ComponentReference;
protected import Config;
protected import DAEUtil;
Expand All @@ -62,6 +63,7 @@ protected import ErrorExt;
protected import Expression;
protected import ExpressionDump;
protected import Flags;
protected import HashTableExpToReal;
protected import List;
protected import Prefix;
protected import Static;
Expand Down Expand Up @@ -2699,11 +2701,11 @@ algorithm
local
list<tuple<DAE.Exp, Real>> exp_const,exp_const_1;
list<DAE.Exp> expl_1,expl;

Integer n;
case (expl)
equation
exp_const = simplifyAdd2(expl);
exp_const_1 = simplifyAddJoinTerms(exp_const);
(exp_const,n) = simplifyAdd2(expl,{},0);
exp_const_1 = simplifyAddJoinTerms(exp_const,intLt(n,5),n);
expl_1 = simplifyAddMakeMul(exp_const_1);
then
expl_1;
Expand All @@ -2721,25 +2723,26 @@ protected function simplifyAdd2
author: PA
Helper function to simplifyAdd"
input list<DAE.Exp> inExpLst;
input list<tuple<DAE.Exp, Real>> inTplExpRealLst;
input Integer iN;
output list<tuple<DAE.Exp, Real>> outTplExpRealLst;
output Integer oN;
algorithm
outTplExpRealLst := matchcontinue (inExpLst)
(outTplExpRealLst,oN) := matchcontinue (inExpLst,inTplExpRealLst,iN)
local
DAE.Exp e_1,e;
Real coeff;
list<tuple<DAE.Exp, Real>> rest;
DAE.Exp e;
tuple<DAE.Exp, Real> tpl;
list<DAE.Exp> es;

case ({}) then {};
case ({},_,_) then (inTplExpRealLst,iN);

case ((e :: es))
case ((e :: es),_,_)
equation
(e_1,coeff) = simplifyBinaryAddCoeff2(e);
rest = simplifyAdd2(es);
tpl = simplifyBinaryAddCoeff2(e);
(outTplExpRealLst,oN) = simplifyAdd2(es,tpl::inTplExpRealLst,iN+1);
then
((e_1,coeff) :: rest);

case (_)
(outTplExpRealLst,oN);
else
equation
Debug.fprint(Flags.FAILTRACE,"- ExpressionSimplify.simplifyAdd2 failed\n");
then
Expand All @@ -2749,6 +2752,61 @@ end simplifyAdd2;

protected function simplifyAddJoinTerms
"function: simplifyAddJoinTerms
author: Frenkel TUD
Helper function to simplifyAdd.
check if listLength(inTplExpRealLst) is small ore huge and use
appropriate join functions."
input list<tuple<DAE.Exp, Real>> inTplExpRealLst;
input Boolean small;
input Integer n;
output list<tuple<DAE.Exp, Real>> outTplExpRealLst;
algorithm
outTplExpRealLst := match (inTplExpRealLst,small,n)
local
HashTableExpToReal.HashTable ht;

case (_,false,_)
equation
ht = HashTableExpToReal.emptyHashTableSized(2*n);
ht = List.fold(inTplExpRealLst,simplifyAddJoinTermsHuge,ht);
then
BaseHashTable.hashTableList(ht);
else
then
simplifyAddJoinTermsSmall(inTplExpRealLst);
end match;
end simplifyAddJoinTerms;

protected function simplifyAddJoinTermsHuge
"function: simplifyAddJoinTermsHuge
author: Frenkel TUD 2013-02
Helper function to simplifyAdd.
Join all terms with the same expression.
i.e. 2a+4a gives an element (a,6) in the list."
input tuple<DAE.Exp, Real> inTplExpReal;
input HashTableExpToReal.HashTable iHt;
output HashTableExpToReal.HashTable oHt;
algorithm
oHt := matchcontinue (inTplExpReal,iHt)
local
Real coeff,coeff1,coeff2;
DAE.Exp e;

case ((e,coeff),_)
equation
coeff1 = BaseHashTable.get(e,iHt);
coeff2 = coeff +. coeff1;
then
BaseHashTable.add((e,coeff2),iHt);

case ((e,coeff),_)
then
BaseHashTable.add((e,coeff),iHt);
end matchcontinue;
end simplifyAddJoinTermsHuge;

protected function simplifyAddJoinTermsSmall
"function: simplifyAddJoinTermsSmall
author: PA
Helper function to simplifyAdd.
Join all terms with the same expression.
Expand All @@ -2767,12 +2825,12 @@ algorithm
case (((e,coeff) :: rest))
equation
(coeff2,rest_1) = simplifyAddJoinTermsFind(e, rest);
res = simplifyAddJoinTerms(rest_1);
res = simplifyAddJoinTermsSmall(rest_1);
coeff3 = coeff +. coeff2;
then
((e,coeff3) :: res);
end match;
end simplifyAddJoinTerms;
end simplifyAddJoinTermsSmall;

protected function simplifyAddJoinTermsFind
"function: simplifyAddJoinTermsFind
Expand Down Expand Up @@ -2852,51 +2910,52 @@ protected function simplifyBinaryAddCoeff2
"function: simplifyBinaryAddCoeff2
This function checks for x+x+x+x and returns (x,4.0)"
input DAE.Exp inExp;
output DAE.Exp outExp;
output Real outReal;
output tuple<DAE.Exp,Real> outExp;
algorithm
(outExp,outReal) := matchcontinue (inExp)
outExp := match (inExp)
local
DAE.Exp exp,e1,e2,e;
Real coeff,coeff_1;
Integer icoeff;
Type tp;
Boolean b;

case ((exp as DAE.CREF(componentRef = _))) then (exp,1.0);
case ((exp as DAE.CREF(componentRef = _))) then ((exp,1.0));

case (DAE.UNARY(operator = DAE.UMINUS(ty = DAE.T_REAL(varLst = _)), exp = exp))
equation
(exp,coeff) = simplifyBinaryAddCoeff2(exp);
((exp,coeff)) = simplifyBinaryAddCoeff2(exp);
coeff = realMul(-1.0,coeff);
then (exp,coeff);
then ((exp,coeff));

case (DAE.BINARY(exp1 = DAE.RCONST(real = coeff),operator = DAE.MUL(ty = _),exp2 = e1))
then (e1,coeff);
then ((e1,coeff));

case (DAE.BINARY(exp1 = e1,operator = DAE.MUL(ty = _),exp2 = DAE.RCONST(real = coeff)))
then (e1,coeff);
then ((e1,coeff));

case (DAE.BINARY(exp1 = e1,operator = DAE.MUL(ty = _),exp2 = DAE.ICONST(integer = icoeff)))
equation
coeff_1 = intReal(icoeff);
then
(e1,coeff_1);
((e1,coeff_1));

case (DAE.BINARY(exp1 = DAE.ICONST(integer = icoeff),operator = DAE.MUL(ty = _),exp2 = e1))
equation
coeff_1 = intReal(icoeff);
then
(e1,coeff_1);
((e1,coeff_1));

case (DAE.BINARY(exp1 = e1,operator = DAE.ADD(ty = tp),exp2 = e2))
equation
true = Expression.expEqual(e1, e2);
b = Expression.expEqual(e1, e2);
coeff_1 = Util.if_(b,2.0,1.0);
then
(e1,2.0);
((e1,coeff_1));

case (e) then (e,1.0);
else then ((inExp,1.0));

end matchcontinue;
end match;
end simplifyBinaryAddCoeff2;

protected function simplifyBinaryMulCoeff2
Expand Down
1 change: 1 addition & 0 deletions Compiler/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ HashTablePathToFunction.mo \
HashTableStringToPath.mo \
HashTableExpToExp.mo \
HashTableExpToIndex.mo \
HashTableExpToReal.mo \
IOStream.mo \
List.mo \
Util.mo \
Expand Down
86 changes: 86 additions & 0 deletions Compiler/Util/HashTableExpToReal.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
encapsulated package HashTableExpToReal "
This file is an extension to OpenModelica.

Copyright (c) 2013 TU Dresden

All rights reserved.

file: HashTableExpToReal.mo
package: HashTableExpToReal
description: DAE.Exp to Integer

RCS: $Id: HashTableExpToReal.mo 15361 2013-02-27 15:44:14Z lochel $

"

/* Below is the instance specific code. For each hashtable the user must define:
Key - The key used to uniquely define elements in a hashtable
Value - The data to associate with each key
hashFunc - A function that maps a key to a positive integer.
keyEqual - A comparison function between two keys, returns true if equal.
*/

/* HashTable instance specific code */

public import BaseHashTable;
public import DAE;
protected import Expression;
protected import ExpressionDump;

public type Key = DAE.Exp;
public type Value = Real;

public type HashTableCrefFunctionsType = tuple<FuncHashCref,FuncCrefEqual,FuncCrefStr,FuncExpStr>;
public type HashTable = tuple<
array<list<tuple<Key,Integer>>>,
tuple<Integer,Integer,array<Option<tuple<Key,Value>>>>,
Integer,
Integer,
HashTableCrefFunctionsType
>;

partial function FuncHashCref
input Key cr;
input Integer mod;
output Integer res;
end FuncHashCref;

partial function FuncCrefEqual
input Key cr1;
input Key cr2;
output Boolean res;
end FuncCrefEqual;

partial function FuncCrefStr
input Key cr;
output String res;
end FuncCrefStr;

partial function FuncExpStr
input Value exp;
output String res;
end FuncExpStr;

public function emptyHashTable
"
Returns an empty HashTable.
Using the default bucketsize..
"
output HashTable hashTable;
algorithm
hashTable := emptyHashTableSized(BaseHashTable.defaultBucketSize);
end emptyHashTable;

public function emptyHashTableSized
"
Returns an empty HashTable.
Using the bucketsize size.
"
input Integer size;
output HashTable hashTable;
algorithm
hashTable := BaseHashTable.emptyHashTableWork(size,(Expression.hashExpMod,Expression.expEqual,ExpressionDump.printExpStr,realString));
end emptyHashTableSized;

end HashTableExpToReal;

0 comments on commit 14aead2

Please sign in to comment.