Skip to content

Commit 7f3462e

Browse files
committed
[NF,BE] Avoid expansion of vectorized bindings if not scalarizing (#12951)
- NF: use each for vectorized attributes - BE: remove fill from vectorized variable and parameter bindings
1 parent 8f280d6 commit 7f3462e

File tree

5 files changed

+53
-33
lines changed

5 files changed

+53
-33
lines changed

OMCompiler/Compiler/BackEnd/BackendDAECreate.mo

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,13 @@ algorithm
11641164
algorithm
11651165
// Add the binding as an equation and remove the binding from the variable.
11661166
outVars := lowerDynamicVar(inElement, inFunctions) :: outVars;
1167+
// remove fill from binding exp to avoid expansion if not scalarizing, treating like each
1168+
e2 := match(Flags.isSet(Flags.NF_SCALARIZE), e2)
1169+
case (false, DAE.CALL(path = Absyn.IDENT("fill"), expLst = {e1, _})) then
1170+
e1;
1171+
else
1172+
e2;
1173+
end match;
11671174
e1 := Expression.crefExp(cr);
11681175
attr := BackendDAE.EQ_ATTR_DEFAULT_BINDING;
11691176
(tp, dims) := ComponentReference.crefTypeFull2(cr);
@@ -1293,7 +1300,8 @@ algorithm
12931300
list<DAE.Dimension> dims;
12941301
DAE.ComponentRef name;
12951302
BackendDAE.VarKind kind_1;
1296-
Option<DAE.Exp> bind, bind1;
1303+
Option<DAE.Exp> bind;
1304+
DAE.Exp e1;
12971305
DAE.VarKind kind;
12981306
DAE.VarDirection dir;
12991307
DAE.VarParallelism prl;
@@ -1331,6 +1339,13 @@ algorithm
13311339
equation
13321340
kind_1 = lowerKnownVarkind(kind, name, dir, ct, protection);
13331341
// bind = fixParameterStartBinding(bind, t, dae_var_attr, kind_1);
1342+
// remove fill from binding exp to avoid expansion if not scalarizing, treating like each
1343+
bind = match(Flags.isSet(Flags.NF_SCALARIZE), bind)
1344+
case (false, SOME(DAE.CALL(path = Absyn.IDENT("fill"), expLst = {e1, _}))) then
1345+
SOME(e1);
1346+
else
1347+
bind;
1348+
end match;
13341349
tp = lowerType(t);
13351350
b = DAEUtil.boolVarVisibility(protection);
13361351
dae_var_attr = DAEUtil.setProtectedAttr(dae_var_attr, b);

OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,12 @@ protected
436436
String attr_name;
437437
Binding attr_binding;
438438
algorithm
439-
var.binding := fillVectorizedBinding(var.binding, var.ty);
439+
var.binding := fillVectorizedBinding(var.binding, var.ty, false);
440440

441441
for ty_attr in var.typeAttributes loop
442442
(attr_name, attr_binding) := ty_attr;
443443
attr_binding := fillVectorizedBinding(attr_binding,
444-
Type.copyDims(var.ty, Binding.getType(attr_binding)));
444+
Type.copyDims(var.ty, Binding.getType(attr_binding)), true);
445445
ty_attrs := (attr_name, attr_binding) :: ty_attrs;
446446
end for;
447447

@@ -1238,6 +1238,7 @@ end vectorizeBinding;
12381238
function fillVectorizedBinding
12391239
input output Binding binding;
12401240
input Type varType;
1241+
input Boolean isAttribute;
12411242
protected
12421243
Expression bind_exp;
12431244
Type bind_ty;
@@ -1258,10 +1259,14 @@ algorithm
12581259
dim_diff := Type.dimensionDiff(varType, bind_ty);
12591260

12601261
if dim_diff > 0 then
1261-
dim_expl := list(Dimension.sizeExp(d) for d in List.firstN(Type.arrayDims(varType), dim_diff));
1262-
binding.bindingExp := Expression.CALL(Call.makeTypedCall(NFBuiltinFuncs.FILL_FUNC,
1263-
binding.bindingExp :: dim_expl, binding.variability, Purity.PURE, varType));
1264-
binding.bindingType := Expression.typeOf(binding.bindingExp);
1262+
if isAttribute then
1263+
binding.eachType := NFBinding.EachType.EACH;
1264+
else
1265+
dim_expl := list(Dimension.sizeExp(d) for d in List.firstN(Type.arrayDims(varType), dim_diff));
1266+
binding.bindingExp := Expression.CALL(Call.makeTypedCall(NFBuiltinFuncs.FILL_FUNC,
1267+
binding.bindingExp :: dim_expl, binding.variability, Purity.PURE, varType));
1268+
binding.bindingType := Expression.typeOf(binding.bindingExp);
1269+
end if;
12651270
end if;
12661271
then
12671272
();

testsuite/openmodelica/cppruntime/VectorizedSolarSystem.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package Vectorized
2-
import SI = Modelica.SIunits;
2+
import SI = Modelica.Units.SI;
33

44
connector Terminal
55
SI.Voltage v;
@@ -39,7 +39,7 @@ package Vectorized
3939
connect(plant.term, grid.terms);
4040
end SolarSystem;
4141

42-
annotation(uses(Modelica(version="3.2.3")));
42+
annotation(uses(Modelica(version="4.0.0")));
4343
end Vectorized;
4444

4545
model VectorizedSolarSystemTest = Vectorized.SolarSystem;

testsuite/openmodelica/cppruntime/testVectorizedBlocks.mos

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ val(y[10], 1.0);
9595
// 4: periodicClock1.useSolver:PARAM(final = true ) = true "= true, if solverMethod shall be explicitly defined" type: Boolean
9696
// 5: periodicClock1.solverMethod:PARAM() = "ImplicitEuler" "Integration method used for discretized continuous-time partitions" type: String
9797
// 6: assignClock1.n:PARAM(min = 1 final = true ) = 10 "Size of input signal vector u (= size of output signal vector y)" type: Integer
98-
// 7: integrator1.k:PARAM(unit = fill("1", 10) ) = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0} "Integrator gain" type: Real[10] [10]
99-
// 8: integrator1.initType:PARAM(final = true ) = {Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState} "Type of initialization (1: no init, 2: steady state, 3,4: initial output)" type: enumeration(NoInit, SteadyState, InitialState, InitialOutput)[10] [10]
98+
// 7: integrator1.k:PARAM(unit = "1" ) = 1.0 "Integrator gain" type: Real[10] [10]
99+
// 8: integrator1.initType:PARAM(final = true ) = Modelica.Blocks.Types.Init.InitialState "Type of initialization (1: no init, 2: steady state, 3,4: initial output)" type: enumeration(NoInit, SteadyState, InitialState, InitialOutput)[10] [10]
100100
// 9: integrator1.y_start:PARAM() = 1.0:10.0 "Initial or guess value of output (= state)" type: Real[10] [10]
101101
//
102102
//
@@ -174,8 +174,8 @@ val(y[10], 1.0);
174174
// Known variables only depending on parameters and constants - globalKnownVars (9)
175175
// ========================================
176176
// 1: integrator1.y_start:PARAM() = 1.0:10.0 "Initial or guess value of output (= state)" type: Real[10] [10]
177-
// 2: integrator1.initType:PARAM(final = true ) = {Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState, Modelica.Blocks.Types.Init.InitialState} "Type of initialization (1: no init, 2: steady state, 3,4: initial output)" type: enumeration(NoInit, SteadyState, InitialState, InitialOutput)[10] [10]
178-
// 3: integrator1.k:PARAM(unit = {"1", "1", "1", "1", "1", "1", "1", "1", "1", "1"} ) = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0} "Integrator gain" type: Real[10] [10]
177+
// 2: integrator1.initType:PARAM(final = true ) = Modelica.Blocks.Types.Init.InitialState "Type of initialization (1: no init, 2: steady state, 3,4: initial output)" type: enumeration(NoInit, SteadyState, InitialState, InitialOutput)[10] [10]
178+
// 3: integrator1.k:PARAM(unit = "1" ) = 1.0 "Integrator gain" type: Real[10] [10]
179179
// 4: assignClock1.n:PARAM(min = 1 final = true ) = 10 "Size of input signal vector u (= size of output signal vector y)" type: Integer
180180
// 5: periodicClock1.solverMethod:PARAM() = "ImplicitEuler" "Integration method used for discretized continuous-time partitions" type: String
181181
// 6: periodicClock1.useSolver:PARAM(final = true ) = true "= true, if solverMethod shall be explicitly defined" type: Boolean

0 commit comments

Comments
 (0)