Skip to content

Commit

Permalink
[NB] update bindings module (#10791)
Browse files Browse the repository at this point in the history
- use flat and untyped bindings
 - new flag -d=dumpBindings
 - update dumps
  • Loading branch information
kabdelhak committed Jun 6, 2023
1 parent a3b5314 commit 56167bf
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 13 deletions.
7 changes: 4 additions & 3 deletions OMCompiler/Compiler/NBackEnd/Classes/NBEquation.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1657,12 +1657,14 @@ public
local
Binding qual;
Option<Expression> start;
case qual as Binding.TYPED_BINDING() then qual.bindingExp;
case qual as Binding.TYPED_BINDING() then qual.bindingExp;
case qual as Binding.UNTYPED_BINDING() then qual.bindingExp;
case qual as Binding.FLAT_BINDING() then qual.bindingExp;
case qual as Binding.UNBOUND() algorithm
start := BackendExtension.VariableAttributes.getStartAttribute(var.backendinfo.attributes);
then Util.getOptionOrDefault(start, Expression.makeZero(ComponentRef.getSubscriptedType(var.name, true)));
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because of wrong binding type: " + Binding.toString(var.binding) + " for variable " + Variable.toString(Pointer.access(var_ptr))});
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because of wrong binding type: " + Binding.toDebugString(var.binding) + " for variable " + Variable.toString(Pointer.access(var_ptr))});
then fail();
end match;

Expand Down Expand Up @@ -1695,7 +1697,6 @@ public
attr = eqnAttr));
Equation.createName(eqn, idx, context);
end if;

end generateBindingEquation;

function mergeIterators
Expand Down
4 changes: 3 additions & 1 deletion OMCompiler/Compiler/NBackEnd/Classes/NBVariable.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,9 @@ public
Variable var = Pointer.access(var_ptr);
algorithm
b := match var.binding
case Binding.TYPED_BINDING() then true;
case Binding.TYPED_BINDING() then true;
case Binding.UNTYPED_BINDING() then true;
case Binding.FLAT_BINDING() then true;
else false;
end match;
end isBound;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public
equations := BEquation.EquationPointers.addList(parameter_eqs, equations);
initialEqs := BEquation.EquationPointers.addList(parameter_eqs, initialEqs);
initialVars := BVariable.VariablePointers.addList(initial_param_vars, initialVars);
if Flags.isSet(Flags.INITIALIZATION) and not listEmpty(parameter_eqs) then
if (Flags.isSet(Flags.INITIALIZATION) and not listEmpty(parameter_eqs)) or Flags.isSet(Flags.DUMP_BINDINGS) then
print(List.toString(parameter_eqs, function Equation.pointerToString(str = ""), StringUtil.headline_4("Created Parameter Binding Equations:"), "\t", "\n\t", "", false) + "\n\n");
end if;
end createParameterEquations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ protected
if not listEmpty(single_vars) then
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " (" + System.System.systemTypeString(systemType)
+ ") failed because the following variables could not be assigned to a partition:\n {"
+ stringDelimitList(list(BVariable.toString(Pointer.access(var)) for var in single_vars), ", ") + "}"});
+ stringDelimitList(list(BVariable.toString(Pointer.access(var)) for var in single_vars), "\n") + "}"});
fail();
end if;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ public
if not listEmpty(unmatched_vars) then
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName()
+ " failed because following variables could not be solved:\n"
+ List.toString(unmatched_vars, function Slice.toString(func=BVariable.pointerToString, maxLength=0), "", "\t", ", ", "\n", true)
+ List.toString(unmatched_vars, function Slice.toString(func=BVariable.pointerToString, maxLength=0), "", "\t", "\n\t", "\n", true)
+ "\n Furthermore following equations are unmatched:\n"
+ List.toString(unmatched_eqns, function Slice.toString(func=function Equation.pointerToString(str=""), maxLength=0), "", "\t", ", ", "\n", true)});
+ List.toString(unmatched_eqns, function Slice.toString(func=function Equation.pointerToString(str=""), maxLength=0), "", "\t", "\n\t", "\n", true)});
fail();
end if;
end noIndexReduction;
Expand Down
8 changes: 4 additions & 4 deletions OMCompiler/Compiler/NBackEnd/Modules/2_Pre/NBAlias.mo
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ protected
import BEquation = NBEquation;
import BVariable = NBVariable;
import Causalize = NBCausalize;
import NBEquation.{Equation, EquationPointers};
import NBEquation.{Equation, EquationPointers, EqData};
import Replacements = NBReplacements;
import Solve = NBSolve;
import StrongComponent = NBStrongComponent;
import NBVariable.VariablePointers;
import NBVariable.{VariablePointers, VarData};

// Util imports
import MetaModelica.Dangerous;
Expand All @@ -84,8 +84,8 @@ public

bdae := match bdae
local
BVariable.VarData varData "Data containing variable pointers";
BEquation.EqData eqData "Data containing equation pointers";
VarData varData "Data containing variable pointers";
EqData eqData "Data containing equation pointers";

case BackendDAE.MAIN(varData = varData, eqData = eqData)
algorithm
Expand Down
7 changes: 7 additions & 0 deletions OMCompiler/Compiler/NBackEnd/Modules/2_Pre/NBBindings.mo
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public
binding_disc := Equation.generateBindingEquation(var, eqData.uniqueIndex, false) :: binding_disc;
end if;
end for;

// adding all continuous equations
eqData.equations := EquationPointers.addList(binding_cont, eqData.equations);
eqData.simulation := EquationPointers.addList(binding_cont, eqData.simulation);
Expand All @@ -75,6 +76,12 @@ public
eqData.discretes := EquationPointers.addList(binding_disc, eqData.discretes);

bdae.eqData := eqData;

if Flags.isSet(Flags.DUMP_BINDINGS) then
print(List.toString(binding_cont, function Equation.pointerToString(str = ""), StringUtil.headline_4("Created Continuous Binding Equations:"), "\t", "\n\t", "", false) + "\n\n");
print(List.toString(binding_disc, function Equation.pointerToString(str = ""), StringUtil.headline_4("Created Discrete Binding Equations:"), "\t", "\n\t", "", false) + "\n\n");
end if;

then bdae;

else algorithm
Expand Down
15 changes: 15 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFBinding.mo
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,21 @@ public
end match;
end toFlatString;

function toDebugString
input Binding binding;
output String string;
algorithm
string := match binding
case UNBOUND() then "UNBOUND";
case RAW_BINDING() then "RAW_BINDING";
case UNTYPED_BINDING() then "UNTYPED_BINDING";
case TYPED_BINDING() then "TYPED_BINDING";
case FLAT_BINDING() then "FLAT_BINDING";
case CEVAL_BINDING() then "CEVAL_BINDING";
case INVALID_BINDING() then "INVALID_BINDING";
end match;
end toDebugString;

function isEqual
input Binding binding1;
input Binding binding2;
Expand Down
2 changes: 2 additions & 0 deletions OMCompiler/Compiler/Util/Flags.mo
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ constant DebugFlag VECTORIZE_BINDINGS = DEBUG_FLAG(192, "vectorizeBindings", fal
Gettext.gettext("Turns on vectorization of bindings when scalarization is turned off."));
constant DebugFlag DUMP_EVENTS = DEBUG_FLAG(193, "dumpEvents", false,
Gettext.gettext("Dumps information about the detected event functions."));
constant DebugFlag DUMP_BINDINGS = DEBUG_FLAG(194, "dumpBindings", false,
Gettext.gettext("Dumps information about the equations created from bindings."));

public
// CONFIGURATION FLAGS
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/Util/FlagsUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ constant list<Flags.DebugFlag> allDebugFlags = {
Flags.MERGE_COMPONENTS,
Flags.DUMP_SLICE,
Flags.VECTORIZE_BINDINGS,
Flags.DUMP_EVENTS
Flags.DUMP_EVENTS,
Flags.DUMP_BINDINGS
};

protected
Expand Down

0 comments on commit 56167bf

Please sign in to comment.