Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Commit

Permalink
Adjust memory management of equation arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel authored and OpenModelica-Hudson committed Apr 17, 2017
1 parent fa0a689 commit 11f1da2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDAEOptimize.mo
Original file line number Diff line number Diff line change
Expand Up @@ -5545,7 +5545,7 @@ algorithm
var := BackendVariable.setVarFixed(var, true);
var := BackendVariable.setVarStartValue(var, DAE.CREF(DAE.crefTime, DAE.T_REAL_DEFAULT));
orderedVars := BackendVariable.addVar(var, orderedVars);
orderedEqs := BackendEquation.emptyEqns();
orderedEqs := BackendEquation.emptyEqnsSized(1);
orderedEqs := BackendEquation.add(BackendDAE.EQUATION(DAE.CALL(Absyn.IDENT("der"), {DAE.CREF(DAE.crefTimeState, DAE.T_REAL_DEFAULT)}, DAE.callAttrBuiltinReal), DAE.RCONST(1.0), DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC), orderedEqs);
eq := BackendDAEUtil.createEqSystem(orderedVars, orderedEqs, {}, BackendDAE.CONTINUOUS_TIME_PARTITION());
outDAE := BackendDAE.DAE(eq::eqs, shared);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -7280,7 +7280,7 @@ protected
algorithm
globalKnownVars := backendDAE.shared.globalKnownVars;
globalKnownVars := BackendVariable.addVariables(backendDAE.shared.externalObjects, globalKnownVars);
parameterEqns := BackendEquation.emptyEqns();
parameterEqns := BackendEquation.emptyEqnsSized(BackendVariable.varsSize(globalKnownVars));
parameterEqns := BackendVariable.traverseBackendDAEVars(globalKnownVars, createParameterEquations, parameterEqns);

paramSystem := BackendDAEUtil.createEqSystem(globalKnownVars, parameterEqns);
Expand Down
19 changes: 5 additions & 14 deletions Compiler/BackEnd/BackendEquation.mo
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ algorithm
end add;

public function addList "author: hkiel
Adds a list of BackendDAE.Equation to BackendDAE.EquationArray.
TODO: This shouldn't expand the array more than one times."
Adds a list of BackendDAE.Equation to BackendDAE.EquationArray."
input list<BackendDAE.Equation> eqnlst;
input output BackendDAE.EquationArray equationArray;
algorithm
ExpandableArray.expandToSize(ExpandableArray.getLastUsedIndex(equationArray) + listLength(eqnlst), equationArray);
for e in eqnlst loop
equationArray := add(e, equationArray);
end for;
Expand Down Expand Up @@ -121,22 +121,13 @@ algorithm
end merge;

public function listEquation "author: PA
Transform the a list of equations into an expandable BackendDAE.Equation
array."
Transform a list of equations into an expandable BackendDAE.Equation array."
input list<BackendDAE.Equation> inEquationList;
output BackendDAE.EquationArray outEquationArray;
protected
Integer len, size, pos=1;
Real rlen;
algorithm
len := listLength(inEquationList);
rlen := intReal(len) * 1.4;
size := realInt(rlen);

outEquationArray := ExpandableArray.new(size, BackendDAE.DUMMY_EQUATION());
outEquationArray := ExpandableArray.new(listLength(inEquationList), BackendDAE.DUMMY_EQUATION());
for eq in inEquationList loop
ExpandableArray.add(eq, outEquationArray);
pos := pos + 1;
end for;
end listEquation;

Expand Down Expand Up @@ -189,7 +180,7 @@ public function getList "author: Frenkel TUD 2011-05
input BackendDAE.EquationArray inEquationArray;
output list<BackendDAE.Equation> outEqns;
algorithm
outEqns := List.map1r(inIndices, get, inEquationArray);
outEqns := list(get(inEquationArray, index) for index in inIndices);
end getList;

public function equationArraySize "author: lochel
Expand Down
22 changes: 18 additions & 4 deletions Compiler/Util/ExpandableArray.mo
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ algorithm
end if;
end get;

function expandToSize "O(n)
Expands an array to the given size, or does nothing if the array is already
large enough."
input Integer minCapacity;
input output ExpandableArray<T> exarray;
protected
Integer capacity = Dangerous.arrayGetNoBoundsChecking(exarray.capacity, 1);
array<Option<T>> data = Dangerous.arrayGetNoBoundsChecking(exarray.data, 1);
algorithm
if minCapacity > capacity then
Dangerous.arrayUpdateNoBoundsChecking(exarray.capacity, 1, minCapacity);
data := Array.expandToSize(minCapacity, data, NONE());
Dangerous.arrayUpdateNoBoundsChecking(exarray.data, 1, data);
end if;
end expandToSize;

function set "if index <= capacity then O(1) otherwise O(n)
Sets the element at the given index to the given value.
Fails if the index is already used."
Expand All @@ -133,10 +149,8 @@ protected
algorithm
if index > 0 and (index > capacity or isNone(Dangerous.arrayGetNoBoundsChecking(data, index))) then
if index > capacity then
capacity := intMax(2*capacity, index);
Dangerous.arrayUpdateNoBoundsChecking(exarray.capacity, 1, capacity);
data := Array.expandToSize(capacity, data, NONE());
Dangerous.arrayUpdateNoBoundsChecking(exarray.data, 1, data);
expandToSize(index, exarray);
data := Dangerous.arrayGetNoBoundsChecking(exarray.data, 1);
end if;

arrayUpdate(data, index, SOME(value));
Expand Down

0 comments on commit 11f1da2

Please sign in to comment.