Skip to content

Commit 8ae74ad

Browse files
authored
[NSim] add stop to sim iterator (#13839)
* [NSim] add stop to sim iterator * [NB/Nsim] add sub iterator support * [NB] add resizable for loop sim code and inner sub iterators * [NB] add proper order resolving of resizable loops * [NB,Template] support resizable values for iterator ranges * [testsuite] add new test case and fix old test
1 parent 89dd830 commit 8ae74ad

File tree

21 files changed

+464
-92
lines changed

21 files changed

+464
-92
lines changed

OMCompiler/Compiler/BackEnd/BackendDAE.mo

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,14 +668,18 @@ end ZeroCrossing;
668668
public uniontype SimIterator
669669
record SIM_ITERATOR_RANGE
670670
.DAE.ComponentRef name;
671-
Integer start;
672-
Integer step;
673-
Integer size;
671+
.DAE.Exp start;
672+
.DAE.Exp step;
673+
.DAE.Exp stop;
674+
.DAE.Exp size;
675+
Integer non_resizable_size;
676+
list<tuple<.DAE.ComponentRef, array<.DAE.Exp>>> sub_iter;
674677
end SIM_ITERATOR_RANGE;
675678
record SIM_ITERATOR_LIST
676679
.DAE.ComponentRef name;
677680
list<Integer> lst;
678681
Integer size;
682+
list<tuple<.DAE.ComponentRef, array<.DAE.Exp>>> sub_iter;
679683
end SIM_ITERATOR_LIST;
680684
end SimIterator;
681685

OMCompiler/Compiler/BackEnd/BackendDAEUtil.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ protected
601601
Integer local_size;
602602
algorithm
603603
for iter in iters loop
604-
local_size := match iter case BackendDAE.SIM_ITERATOR_RANGE() then iter.size; case BackendDAE.SIM_ITERATOR_LIST() then iter.size; end match;
604+
local_size := match iter case BackendDAE.SIM_ITERATOR_RANGE() then iter.non_resizable_size; case BackendDAE.SIM_ITERATOR_LIST() then iter.size; end match;
605605
size := size * local_size;
606606
end for;
607607
end getSimIteratorSize;

OMCompiler/Compiler/BackEnd/BackendDump.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1820,7 +1820,7 @@ public function simIteratorString
18201820
output String str;
18211821
algorithm
18221822
str := match iter
1823-
case BackendDAE.SIM_ITERATOR_RANGE() then ComponentReference.printComponentRefStr(iter.name) + " in " + intString(iter.start) + ":" + intString(iter.step) + ":" + intString(iter.start + iter.size);
1823+
case BackendDAE.SIM_ITERATOR_RANGE() then ComponentReference.printComponentRefStr(iter.name) + " in " + ExpressionDump.printExpStr(iter.start) + ":" + ExpressionDump.printExpStr(iter.step) + ":" + ExpressionDump.printExpStr(iter.stop);
18241824
case BackendDAE.SIM_ITERATOR_LIST() then ComponentReference.printComponentRefStr(iter.name) + " in " + List.toString(iter.lst, intString, "", "{", ", ", "}", true, 10);
18251825
end match;
18261826
end simIteratorString;

OMCompiler/Compiler/BackEnd/FindZeroCrossings.mo

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import DAEUtil;
5454
import Error;
5555
import Expression;
5656
import ExpressionDump;
57+
import ExpressionSimplify;
5758
import Flags;
5859
import HashTableExpToIndex;
5960
import List;
@@ -1950,17 +1951,23 @@ protected function createIterator
19501951
algorithm
19511952
iter := match red_iter.exp
19521953
local
1953-
DAE.Exp exp;
1954-
Integer start, step, stop;
1954+
DAE.Exp exp, step, size;
1955+
DAE.Type ty;
1956+
Integer non_resizable_size;
19551957

19561958
case exp as DAE.RANGE() algorithm
1957-
start := DAEUtil.getInteger(exp.start);
1958-
step := DAEUtil.getInteger(Util.getOptionOrDefault(exp.step, DAE.ICONST(1)));
1959-
stop := DAEUtil.getInteger(exp.stop);
1960-
then BackendDAE.SIM_ITERATOR_RANGE(DAE.CREF_IDENT(red_iter.id, DAE.T_INTEGER_DEFAULT, {}), start, step, intDiv(stop-start, step) + 1);
1959+
ty := Expression.typeof(exp.start);
1960+
step := Util.getOptionOrDefault(exp.step, DAE.ICONST(1));
1961+
// build the size expression (stop-start)/step+1
1962+
size := DAE.BINARY(exp.stop, DAE.SUB(ty), exp.start);
1963+
size := DAE.BINARY(size, DAE.DIV(ty), step);
1964+
size := DAE.BINARY(size, DAE.ADD(ty), DAE.ICONST(1));
1965+
size := ExpressionSimplify.simplify(size);
1966+
non_resizable_size := Expression.getEvaluatedConstInteger(size);
1967+
then BackendDAE.SIM_ITERATOR_RANGE(DAE.CREF_IDENT(red_iter.id, DAE.T_INTEGER_DEFAULT, {}), exp.start, step, exp.stop, size, non_resizable_size, {});
19611968

19621969
case exp as DAE.ARRAY() algorithm
1963-
then BackendDAE.SIM_ITERATOR_LIST(DAE.CREF_IDENT(red_iter.id, DAE.T_INTEGER_DEFAULT, {}), list(DAEUtil.getInteger(e) for e in exp.array), listLength(exp.array));
1970+
then BackendDAE.SIM_ITERATOR_LIST(DAE.CREF_IDENT(red_iter.id, DAE.T_INTEGER_DEFAULT, {}), list(DAEUtil.getInteger(e) for e in exp.array), listLength(exp.array), {});
19641971

19651972
else algorithm
19661973
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for expression: " + ExpressionDump.printExpStr(red_iter.exp) + ".\n"});

OMCompiler/Compiler/NBackEnd/Classes/NBEquation.mo

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public
6767

6868
// New Backend imports
6969
import DetectStates = NBDetectStates;
70+
import NBResizable.EvalOrder;
7071
import Evaluation = NBEvaluation;
7172
import Inline = NBInline;
7273
import Replacements = NBReplacements;
@@ -875,6 +876,61 @@ public
875876
end match;
876877
end adaptArray;
877878

879+
function applyOrder
880+
input output Iterator iter;
881+
input UnorderedMap<ComponentRef, EvalOrder> order;
882+
function applySingleOrder
883+
input ComponentRef name;
884+
input output Expression range;
885+
input UnorderedMap<ComponentRef, EvalOrder> order;
886+
protected
887+
EvalOrder eo = UnorderedMap.getOrDefault(name, order, NBResizable.EvalOrder.INDEPENDENT);
888+
Expression step, res;
889+
list<Integer> elements;
890+
algorithm
891+
range := match range
892+
893+
// revert a range if needed
894+
case Expression.RANGE() algorithm
895+
step := Util.getOptionOrDefault(range.step, Expression.INTEGER(1));
896+
if (Expression.isNegative(step) and eo == NBResizable.EvalOrder.FORWARD) or (Expression.isPositive(step) and eo == NBResizable.EvalOrder.BACKWARD) then
897+
res := Expression.revertRange(range);
898+
else
899+
res := range;
900+
end if;
901+
then res;
902+
903+
// revert an array/list if needed
904+
case Expression.ARRAY(literal = true) algorithm
905+
if eo == NBResizable.EvalOrder.FORWARD then
906+
elements := list(Expression.getInteger(e) for e in range.elements);
907+
range.elements := listArray(list(Expression.INTEGER(e) for e in List.sort(elements, intGt)));
908+
elseif eo == NBResizable.EvalOrder.BACKWARD then
909+
elements := list(Expression.getInteger(e) for e in range.elements);
910+
range.elements := listArray(list(Expression.INTEGER(e) for e in List.sort(elements, intLt)));
911+
end if;
912+
then range;
913+
914+
// no other allowed
915+
else algorithm
916+
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for unhandled range expression: " + Expression.toString(range)});
917+
then fail();
918+
end match;
919+
end applySingleOrder;
920+
algorithm
921+
iter := match iter
922+
case SINGLE() algorithm
923+
iter.range := applySingleOrder(iter.name, iter.range, order);
924+
then iter;
925+
case NESTED() algorithm
926+
for i in 1:arrayLength(iter.names) loop
927+
iter.ranges[i] := applySingleOrder(iter.names[i], iter.ranges[i], order);
928+
end for;
929+
then iter;
930+
else iter;
931+
end match;
932+
end applyOrder;
933+
878934
function toString
879935
input Iterator iter;
880936
output String str = "";
@@ -2069,6 +2125,18 @@ public
20692125
end match;
20702126
end getForFrames;
20712127

2128+
function applyForOrder
2129+
input output Equation eqn;
2130+
input UnorderedMap<ComponentRef, EvalOrder> order;
2131+
algorithm
2132+
eqn := match eqn
2133+
case FOR_EQUATION() algorithm
2134+
eqn.iter := Iterator.applyOrder(eqn.iter, order);
2135+
then eqn;
2136+
else eqn;
2137+
end match;
2138+
end applyForOrder;
2139+
20722140
function isDummy
20732141
input Equation eqn;
20742142
output Boolean b;

OMCompiler/Compiler/NBackEnd/Classes/NBVariable.mo

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,20 @@ public
680680
end match;
681681
end updateResizableParameter;
682682

683+
function getResizableValue
684+
input Pointer<Variable> var_ptr;
685+
output Integer val;
686+
protected
687+
Variable var = Pointer.access(var_ptr);
688+
algorithm
689+
_ := match var.backendinfo
690+
case BackendExtension.BACKEND_INFO(varKind = VariableKind.PARAMETER(resize_value = SOME(val))) then val;
691+
else algorithm
692+
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because following variable is not a resizable parameter: " + toString(var)});
693+
then fail();
694+
end match;
695+
end getResizableValue;
696+
683697
function isResidual
684698
extends checkVar;
685699
algorithm

OMCompiler/Compiler/NBackEnd/Modules/3_Post/NBSolve.mo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ public
259259
// ToDo 1: resolve the eval order
260260
// ToDo 2: resolve potential equation slicing
261261
(eqn, funcTree, solve_status, implicit_index, _) := solveEquation(Pointer.access(Slice.getT(comp.eqn)), comp.var_cref, funcTree, kind, implicit_index, slicing_map);
262+
eqn := Equation.applyForOrder(eqn, comp.order);
262263
comp.eqn := Slice.SLICE(Pointer.create(eqn), comp.eqn.indices);
263264
comp.status := solve_status;
264265
then ({comp}, solve_status);
@@ -315,6 +316,7 @@ public
315316

316317
// ToDo: make these actually resizable inside entwined equations (?)
317318
case StrongComponent.RESIZABLE_COMPONENT(var = var_slice, eqn = eqn_slice) guard(Equation.isForEquation(Slice.getT(eqn_slice))) algorithm
319+
eqn_slice := Slice.apply(eqn_slice, function Pointer.apply(func = function Equation.applyForOrder(order = comp.order)));
318320
(comp, solve_status, funcTree, implicit_index) := solveGenericEquationSlice(var_slice, eqn_slice, comp.var_cref, funcTree, kind, implicit_index, slicing_map);
319321
then (comp, solve_status);
320322

OMCompiler/Compiler/NBackEnd/Util/NBResizable.mo

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* See the full OSMC Public License conditions for more details.
2929
*
3030
*/
31-
encapsulated uniontype NBResizable<T>
31+
encapsulated uniontype NBResizable
3232
" file: NBResizable.mo
3333
package: NBResizable
3434
description: This file contains util functions for resizable parameters.
@@ -228,6 +228,18 @@ public
228228
output Boolean b = eo == EvalOrder.FAILED;
229229
end orderFailed;
230230

231+
function orderString
232+
input EvalOrder eo;
233+
output String str;
234+
algorithm
235+
str := match eo
236+
case EvalOrder.INDEPENDENT then "INDEPENDENT";
237+
case EvalOrder.FORWARD then "FORWARD";
238+
case EvalOrder.BACKWARD then "BACKWARD";
239+
else "FAILED";
240+
end match;
241+
end orderString;
242+
231243
protected
232244
type ParameterList = list<ComponentRef>;
233245
type ConstraintList = list<Expression>;

OMCompiler/Compiler/NFFrontEnd/NFExpression.mo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5062,8 +5062,8 @@ public
50625062
end match;
50635063
end logicNegate;
50645064

5065-
function invertRange
5066-
"inverts the direction of a range"
5065+
function revertRange
5066+
"reverts the direction of a range"
50675067
input output Expression range;
50685068
algorithm
50695069
range := match range
@@ -5076,7 +5076,7 @@ public
50765076
+ toString(range)});
50775077
then fail();
50785078
end match;
5079-
end invertRange;
5079+
end revertRange;
50805080

50815081
function sliceRange
50825082
"slices the range with a given zero-based start and one-based step"

OMCompiler/Compiler/NSimCode/NSimCode.mo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,12 @@ public
132132
record IDENTIFIER
133133
Pointer<Equation> eqn;
134134
ComponentRef var_cref;
135+
Boolean resizable;
135136
end IDENTIFIER;
136137

137138
function toString
138139
input Identifier ident;
139-
output String str = "cref: " + ComponentRef.toString(ident.var_cref) + "\neqn: " + Equation.pointerToString(ident.eqn);
140+
output String str = "cref: " + ComponentRef.toString(ident.var_cref) + "\neqn: " + Equation.pointerToString(ident.eqn) + "\n(resizable="+boolString(ident.resizable)+")";
140141
end toString;
141142

142143
function hash

0 commit comments

Comments
 (0)