Skip to content

Commit

Permalink
Use Mutable in the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed May 2, 2017
1 parent d12a035 commit 54881ac
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 98 deletions.
17 changes: 9 additions & 8 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -80,6 +80,7 @@ import HpcOmTaskGraph;
import List;
import Matching;
import MetaModelica.Dangerous;
import Mutable;
import RewriteRules;
import SCode;
import SynchronousFeatures;
Expand Down Expand Up @@ -3785,23 +3786,23 @@ algorithm
local
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns, remeqns, inieqns;
array<BackendDAE.Shared> shared_arr;
Mutable<BackendDAE.Shared> shared_arr;

case (syst as BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqns), BackendDAE.SHARED(initialEqs=inieqns))
algorithm
shared_arr := arrayCreate(1, shared);
shared_arr := Mutable.create(shared);
(_, vars) := BackendEquation.traverseEquationArray_WithUpdate(eqns, function traverserexpandDerEquation(shared=shared_arr), vars);
(_, vars) := BackendEquation.traverseEquationArray_WithUpdate(inieqns, function traverserexpandDerEquation(shared=shared_arr), vars);
syst.orderedVars := vars;
then (syst, arrayGet(shared_arr, 1));
then (syst, Mutable.access(shared_arr));
end match;
end expandDerOperatorWork;

protected function traverserexpandDerEquation "
Help function to e.g. traverserexpandDerEquation"
input output BackendDAE.Equation eq;
input output BackendDAE.Variables vars;
input array<BackendDAE.Shared> shared;
input Mutable<BackendDAE.Shared> shared;
protected
BackendDAE.Equation e, e1;
tuple<BackendDAE.Variables, DAE.FunctionTree> ext_arg, ext_art1;
Expand All @@ -3817,7 +3818,7 @@ protected function traverserexpandDerExp "
Help function to e.g. traverserexpandDerExp"
input output DAE.Exp exp;
input output tuple<BackendDAE.Variables, list<DAE.SymbolicOperation>> tpl;
input array<BackendDAE.Shared> shared;
input Mutable<BackendDAE.Shared> shared;
protected
DAE.Exp exp_1;
tuple<BackendDAE.Variables, BackendDAE.Shared, Boolean> ext_arg;
Expand All @@ -3839,7 +3840,7 @@ protected function expandDerExp "
Help function to e.g. expandDerOperatorEqn"
input output DAE.Exp exp;
input output BackendDAE.Variables vars;
input array<BackendDAE.Shared> inShared;
input Mutable<BackendDAE.Shared> inShared;
algorithm
(exp,vars) := matchcontinue exp
local
Expand Down Expand Up @@ -3881,8 +3882,8 @@ algorithm
then (e1, vars);
case (DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={e1}))
equation
(e2, shared) = Differentiate.differentiateExpTime(e1, vars, arrayGet(inShared,1));
arrayUpdate(inShared, 1, shared);
(e2, shared) = Differentiate.differentiateExpTime(e1, vars, Mutable.access(inShared));
Mutable.update(inShared, shared);
(e2, _) = ExpressionSimplify.simplify(e2);
(_, vars) = Expression.traverseExpBottomUp(e2, derCrefsExp, vars);
then (e2, vars);
Expand Down
94 changes: 45 additions & 49 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -30,40 +30,40 @@
*/

encapsulated package BackendVariable
" file: mo
package: BackendVariable
description: BackendVariables contains the function that deals with the datytypes
BackendDAE.VAR BackendDAE.Variables and BackendVariablesArray.

"BackendVariables contains the function that deals with the datytypes
BackendDAE.VAR BackendDAE.Variables and BackendVariablesArray.
"

public import BackendDAE;
public import DAE;
public import SCode;
public import Values;

protected import Absyn;
protected import Array;
protected import BackendDAEUtil;
protected import BaseHashSet;
protected import BaseHashTable;
protected import ComponentReference;
protected import DAEUtil;
protected import Debug;
protected import ElementSource;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
protected import Flags;
protected import Global;
protected import HashSet;
protected import List;
protected import MetaModelica.Dangerous;
protected import StringUtil;
protected import System;
protected import Util;
protected import Types;
import BackendDAE;
import DAE;
import SCode;
import Values;

protected

import Absyn;
import Array;
import BackendDAEUtil;
import BaseHashSet;
import BaseHashTable;
import ComponentReference;
import DAEUtil;
import Debug;
import ElementSource;
import Error;
import Expression;
import ExpressionDump;
import ExpressionSimplify;
import Flags;
import Global;
import HashSet;
import List;
import MetaModelica.Dangerous;
import Mutable;
import StringUtil;
import System;
import Types;
import Util;

/* =======================================================
*
Expand Down Expand Up @@ -3115,34 +3115,30 @@ public function getVarIndexFromVariablesIndexInFirstSet
input BackendDAE.Variables inVariables2;
output list<Integer> v_lst;
protected
array<list<Integer>> a;
Mutable<list<Integer>> a;
algorithm
(a,_) := traverseBackendDAEVars(inVariables,
function traversingVarIndexInFirstSetFinder(inVars = inVariables2), (arrayCreate(1,{}),arrayCreate(1,1)));
v_lst := listReverse(a[1]);
(_,a,_) := traverseBackendDAEVars(inVariables, traversingVarIndexInFirstSetFinder, (inVariables2,Mutable.create({}),Mutable.create(1)));
v_lst := listReverse(Mutable.access(a));
end getVarIndexFromVariablesIndexInFirstSet;

protected function traversingVarIndexInFirstSetFinder
"author: Frenkel TUD 2010-11"
input BackendDAE.Var inVar;
input BackendDAE.Variables inVars;
input tuple<array<list<Integer>>,array<Integer>> inIndices;
output BackendDAE.Var outVar = inVar;
output tuple<array<list<Integer>>,array<Integer>> outIndices;
input output BackendDAE.Var var;
input output tuple<BackendDAE.Variables,Mutable<list<Integer>>,Mutable<Integer>> data;
protected
DAE.ComponentRef cr;
array<list<Integer>> l;
array<Integer> i;
BackendDAE.Variables vars;
Mutable<list<Integer>> l;
Mutable<Integer> i;
algorithm
(l,i) := inIndices;
outIndices := inIndices;
(vars,l,i) := data;
try
cr := varCref(inVar);
getVar(cr, inVars);
l[1] := i[1]::l[1];
cr := varCref(var);
getVar(cr, vars);
Mutable.update(l, Mutable.access(i)::Mutable.access(l));
else
end try;
i[1] := i[1]+1;
Mutable.update(i, Mutable.access(i)+1);
end traversingVarIndexInFirstSetFinder;

public function mergeVariables
Expand Down
83 changes: 43 additions & 40 deletions Compiler/BackEnd/Tearing.mo
Expand Up @@ -38,32 +38,35 @@ encapsulated package Tearing
- Cellier Tearing"


public import BackendDAE;
public import DAE;

protected import Array;
protected import BackendDAEEXT;
protected import BackendDAEOptimize;
protected import BackendDAEUtil;
protected import BackendDump;
protected import BackendEquation;
protected import BackendVariable;
protected import Config;
protected import DoubleEndedList;
protected import DumpGraphML;
protected import Error;
protected import ExecStat.execStat;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
protected import ExpressionSolve;
protected import Flags;
protected import Global;
protected import List;
protected import Matching;
protected import MetaModelica.Dangerous;
protected import Util;
protected import Sorting;
import BackendDAE;
import DAE;

protected

import Array;
import BackendDAEEXT;
import BackendDAEOptimize;
import BackendDAEUtil;
import BackendDump;
import BackendEquation;
import BackendVariable;
import Config;
import DoubleEndedList;
import DumpGraphML;
import Error;
import ExecStat.execStat;
import Expression;
import ExpressionDump;
import ExpressionSimplify;
import ExpressionSolve;
import Flags;
import Global;
import List;
import Matching;
import MetaModelica.Dangerous;
import Mutable;
import Util;
import Sorting;

// =============================================================================
// section for type definitions
Expand Down Expand Up @@ -2323,7 +2326,7 @@ algorithm
// 6. determine which possible Vars causalize most equations considering impossible assignments and write them into potentials
msel2t := Array.select(mtIn,selectedcols1);
((potentials,_,_,_)) := Array.fold(msel2t,function selectCausalVars(
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=arrayCreate(1,selectedcols1)
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=Mutable.create(selectedcols1)
), ({},0,1,{}));

// 7. convert indexes from msel2t to indexes from mtIn
Expand Down Expand Up @@ -2368,7 +2371,7 @@ algorithm
// 3. determine which possible Vars causalize most equations considering impossible assignments and write them into potentials
msel2t := Array.select(mtIn,selectedcols1);
((potentials,_,_,_)) := Array.fold(msel2t,function selectCausalVars(
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=arrayCreate(1,selectedcols1)
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=Mutable.create(selectedcols1)
),({},0,1,{}));

// 4. convert indexes from msel2t to indexes from mtIn
Expand Down Expand Up @@ -2425,7 +2428,7 @@ algorithm
// 6. determine which possible Vars causalize most equations considering impossible assignments and write them into potentials
msel2t := Array.select(mtIn,selectedcols1);
((potentials,_,_,_)) := Array.fold(msel2t,function selectCausalVars(
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=arrayCreate(1,selectedcols1)
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=Mutable.create(selectedcols1)
),({},0,1,{}));

// 7. convert indexes from msel2t to indexes from mtIn
Expand Down Expand Up @@ -2476,7 +2479,7 @@ algorithm
// 3. determine which possible Vars causalize most equations considering impossible assignments and write them into potentials
msel2t := Array.select(mtIn,selectedcols1);
((potentials,_,_,_)) := Array.fold(msel2t,function selectCausalVars(
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=arrayCreate(1,selectedcols1)
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=Mutable.create(selectedcols1)
),({},0,1,{}));

// 4. convert indexes from msel2t to indexes from mtIn
Expand Down Expand Up @@ -2545,7 +2548,7 @@ algorithm
// 7. determine which possible Vars causalize most equations considering impossible assignments and write them into potentials
msel2t := Array.select(mtIn,selectedcols1);
((potentials,_,_,_)) := Array.fold(msel2t,function selectCausalVars(
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=arrayCreate(1,selectedcols1)
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=Mutable.create(selectedcols1)
),({},0,1,{}));

// 8. convert indexes from msel2t to indexes from mtIn
Expand Down Expand Up @@ -2596,7 +2599,7 @@ algorithm
// 4. determine which possible Vars causalize most equations considering impossible assignments and write them into potentials
msel2t := Array.select(mtIn,selectedcols1);
((potentials,_,_,_)) := Array.fold(msel2t,function selectCausalVars(
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=arrayCreate(1,selectedcols1)
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=Mutable.create(selectedcols1)
),({},0,1,{}));

// 5. convert indexes from msel2t to indexes from mtIn
Expand Down Expand Up @@ -2653,7 +2656,7 @@ algorithm
// 6. determine for each variable the number of equations it could causalize considering impossible assignments and save them in counts1
msel2t := Array.select(mtIn,selectedcols1);
((_,_,_,counts1)) := Array.fold(msel2t,function selectCausalVars(
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=arrayCreate(1,selectedcols1)
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=Mutable.create(selectedcols1)
),({},0,1,{}));
counts1 := listReverse(counts1);

Expand Down Expand Up @@ -2710,7 +2713,7 @@ algorithm
// 4. determine for each variable the number of equations it could causalize considering impossible assignments and save them in counts1
msel2t := Array.select(mtIn,selectedcols1);
((_,_,_,counts1)) := Array.fold(msel2t,function selectCausalVars(
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=arrayCreate(1,selectedcols1)
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=Mutable.create(selectedcols1)
),({},0,1,{}));
counts1 := listReverse(counts1);

Expand Down Expand Up @@ -2772,7 +2775,7 @@ algorithm
// 4. determine for each variable the number of equations it could causalize considering impossible assignments and save them in counts1
msel2t := Array.select(mtIn,selectedcols1);
((_,_,_,counts1)) := Array.fold(msel2t,function selectCausalVars(
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=arrayCreate(1,selectedcols1)
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=Mutable.create(selectedcols1)
),({},0,1,{}));
counts1 := listReverse(counts1);

Expand Down Expand Up @@ -2807,7 +2810,7 @@ algorithm
// 10. determine for each variable the number of equations it could causalize considering impossible assignments and save them in counts1
msel2t := Array.select(mtIn,selectedcols1);
((_,_,_,counts1)) := Array.fold(msel2t,function selectCausalVars(
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=arrayCreate(1,selectedcols1)
me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(selectedrows, arrayLength(ass1In)),selVars=Mutable.create(selectedcols1)
),({},0,1,{}));
counts1 := listReverse(counts1);

Expand Down Expand Up @@ -2900,7 +2903,7 @@ algorithm

// 4.1 Determine for each variable the number of equations it could causalize considering impossible assignments and save them in counts1
mtsel := Array.select(mtIn,potentialTVars);
((_,_,_,counts1)) := Array.fold(mtsel,function selectCausalVars(me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(causEq, arrayLength(ass1In)),selVars=arrayCreate(1,potentialTVars)),({},0,1,{}));
((_,_,_,counts1)) := Array.fold(mtsel,function selectCausalVars(me=meIn,ass1In=ass1In,selEqsSetArray=selectCausalVarsPrepareSelectionSet(causEq, arrayLength(ass1In)),selVars=Mutable.create(potentialTVars)),({},0,1,{}));
if debug then execStat("Tearing.ModifiedCellierHeuristic_3 - 5.1"); end if;

// 4.2 Determine for each variable the number of impossible assignments and save them in counts2
Expand Down Expand Up @@ -3091,7 +3094,7 @@ protected function selectCausalVars
input BackendDAE.AdjacencyMatrixEnhanced me;
input array<Integer> ass1In;
input array<Boolean> selEqsSetArray;
input array<list<Integer>> selVars;
input Mutable<list<Integer>> selVars;
output tuple<list<Integer>,Integer,Integer,list<Integer>> OutValue;
protected
list<Integer> cVars,interEqs,counts,selVarsNext;
Expand All @@ -3101,8 +3104,8 @@ algorithm
(cVars,num,indx,counts) := inValue;
// interEqs := List.intersectionOnTrue(row,selEqs,intEq);
interEqs := list(i for i guard arrayGet(selEqsSetArray,i) in row);
Var::selVarsNext := arrayGet(selVars,1);
arrayUpdate(selVars, 1, selVarsNext);
Var::selVarsNext := Mutable.access(selVars);
Mutable.update(selVars, selVarsNext);
arrayUpdate(ass1In,Var,1);
size := List.fold2(interEqs,sizeOfAssignable,me,ass1In,0);
arrayUpdate(ass1In,Var,-1);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Util/Util.mo
Expand Up @@ -1077,7 +1077,7 @@ algorithm
end match;
end mulListIntegerOpt;

public type StatefulBoolean = array<Boolean> "A single boolean value that can be updated (a destructive operation)";
public type StatefulBoolean = array<Boolean> "A single boolean value that can be updated (a destructive operation). NOTE: Use Mutable<Boolean> instead. This implementation is kept since Susan cannot use that type.";

public function makeStatefulBoolean
"Create a boolean with state (that is, it is mutable)"
Expand Down

0 comments on commit 54881ac

Please sign in to comment.