Skip to content

Commit

Permalink
Reduce number of hash calls (#12096)
Browse files Browse the repository at this point in the history
  • Loading branch information
phannebohm committed Mar 12, 2024
1 parent bae5f4a commit 5cd2141
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 69 deletions.
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/NBackEnd/Classes/NBStrongComponent.mo
Original file line number Diff line number Diff line change
Expand Up @@ -861,9 +861,9 @@ protected
eqn_arr_idx := mapping.eqn_StA[eqn_idx];

// collect variable and equation slices
idx_lst := if UnorderedMap.contains(var_arr_idx, var_map) then UnorderedMap.getSafe(var_arr_idx, var_map, sourceInfo()) else {};
idx_lst := UnorderedMap.getOrDefault(var_arr_idx, var_map, {});
UnorderedMap.add(var_arr_idx, var_idx :: idx_lst, var_map);
idx_lst := if UnorderedMap.contains(eqn_arr_idx, eqn_map) then UnorderedMap.getSafe(eqn_arr_idx, eqn_map, sourceInfo()) else {};
idx_lst := UnorderedMap.getOrDefault(eqn_arr_idx, eqn_map, {});
UnorderedMap.add(eqn_arr_idx, eqn_idx :: idx_lst, eqn_map);
end for;

Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBSorting.mo
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public
algorithm
if UnorderedMap.contains(key, bucket.bucket) then
// if the mode already was found, add this equation to the bucket
val := UnorderedMap.getSafe(key, bucket.bucket, sourceInfo());
val := UnorderedMap.getOrFail(key, bucket.bucket);
val.eqn_scal_indices := eqn_scal_idx :: val.eqn_scal_indices;
UnorderedMap.add(key, val, bucket.bucket);
else
Expand Down
10 changes: 5 additions & 5 deletions OMCompiler/Compiler/NBackEnd/Modules/2_Pre/NBAlias.mo
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ protected
UnorderedMap.add(cr1, Pointer.create(set), map);
else
// it already belongs to a set, try to update it and throw error if there already is a const binding
set_ptr := UnorderedMap.getSafe(cr1, map, sourceInfo());
set_ptr := UnorderedMap.getOrFail(cr1, map);
set := Pointer.access(set_ptr);
if isSome(set.const_opt) then
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed to add Equation:\n"
Expand All @@ -362,8 +362,8 @@ protected
case CREF_TPL(cr_lst = {cr1, cr2}) algorithm
if (UnorderedMap.contains(cr1, map) and UnorderedMap.contains(cr2, map)) then
// Merge sets
set1_ptr := UnorderedMap.getSafe(cr1, map, sourceInfo());
set2_ptr := UnorderedMap.getSafe(cr2, map, sourceInfo());
set1_ptr := UnorderedMap.getOrFail(cr1, map);
set2_ptr := UnorderedMap.getOrFail(cr2, map);
set1 := Pointer.access(set1_ptr);
set2 := Pointer.access(set2_ptr);
set := EMPTY_ALIAS_SET;
Expand Down Expand Up @@ -409,7 +409,7 @@ protected

elseif UnorderedMap.contains(cr1, map) then
// Update set
set_ptr := UnorderedMap.getSafe(cr1, map, sourceInfo());
set_ptr := UnorderedMap.getOrFail(cr1, map);
set := Pointer.access(set_ptr);
// add cr2 to variables and add new equation pointer
set.simple_variables := cr2 :: set.simple_variables;
Expand All @@ -419,7 +419,7 @@ protected
UnorderedMap.add(cr2, set_ptr, map);
elseif UnorderedMap.contains(cr2, map) then
// Update set
set_ptr := UnorderedMap.getSafe(cr2, map, sourceInfo());
set_ptr := UnorderedMap.getOrFail(cr2, map);
set := Pointer.access(set_ptr);
// add cr1 to variables and add new equation pointer
set.simple_variables := cr1 :: set.simple_variables;
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NBackEnd/Modules/3_Post/NBJacobian.mo
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public
// only create rows for derivatives
if jacType == JacobianType.NLS or BVariable.checkCref(cref, BVariable.isStateDerivative) then
if UnorderedMap.contains(cref, map) then
tmp := UnorderedSet.unique_list(UnorderedMap.getSafe(cref, map, sourceInfo()), ComponentRef.hash, ComponentRef.isEqual);
tmp := UnorderedSet.unique_list(UnorderedMap.getOrFail(cref, map), ComponentRef.hash, ComponentRef.isEqual);
rows := (cref, tmp) :: rows;
row_vars := cref :: row_vars;
for dep in tmp loop
Expand Down
54 changes: 24 additions & 30 deletions OMCompiler/Compiler/NBackEnd/Modules/3_Post/NBSolve.mo
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public
import Tearing = NBTearing;

type Status = enumeration(UNPROCESSED, EXPLICIT, IMPLICIT, UNSOLVABLE);
type StrongComponentLst = list<StrongComponent>;
type EquationPointerList = list<Pointer<Equation>>;

function statusString
input Status status;
Expand All @@ -85,7 +83,8 @@ public
protected
Pointer<FunctionTree> funcTree_ptr;
Pointer<Integer> implicit_index_ptr = Pointer.create(1);
UnorderedMap<StrongComponent, StrongComponentLst> duplicate_map = UnorderedMap.new<StrongComponentLst>(StrongComponent.hash, StrongComponent.isEqual);
type StrongComponentLst = list<StrongComponent>;
UnorderedMap<StrongComponent, list<StrongComponent>> duplicate_map = UnorderedMap.new<StrongComponentLst>(StrongComponent.hash, StrongComponent.isEqual);
protected
StrongComponent unsolved;
list<StrongComponent> solved;
Expand Down Expand Up @@ -131,10 +130,11 @@ public
input output System system;
input Pointer<FunctionTree> funcTree_ptr;
input Pointer<Integer> implicit_index_ptr;
input UnorderedMap<StrongComponent, StrongComponentLst> duplicate_map;
input UnorderedMap<StrongComponent, list<StrongComponent>> duplicate_map;
protected
UnorderedMap<ComponentRef, EquationPointerList> slicing_map = UnorderedMap.new<EquationPointerList>(ComponentRef.hash, ComponentRef.isEqual);
list<StrongComponent> tmp, solved_comps = {};
type EquationPointerList = list<Pointer<Equation>>;
UnorderedMap<ComponentRef, list<Pointer<Equation>>> slicing_map = UnorderedMap.new<EquationPointerList>(ComponentRef.hash, ComponentRef.isEqual);
list<StrongComponent> solved_comps = {};
FunctionTree funcTree = Pointer.access(funcTree_ptr);
Integer implicit_index = Pointer.access(implicit_index_ptr);
array<StrongComponent> new_comps;
Expand All @@ -144,15 +144,15 @@ public
algorithm
if Util.isSome(system.strongComponents) then
for comp in Util.getOption(system.strongComponents) loop
if UnorderedMap.contains(comp, duplicate_map) then
// strong component already solved -> get alias comps
solved_comps := listAppend(UnorderedMap.getSafe(comp, duplicate_map, sourceInfo()), solved_comps);
else
// solve strong component -> create alias comps
(tmp, funcTree, implicit_index) := solveStrongComponent(comp, funcTree, system.systemType, implicit_index, slicing_map);
UnorderedMap.add(comp, list(StrongComponent.createAlias(system.systemType, system.partitionIndex, comp_idx, tmp_c) for tmp_c in tmp), duplicate_map);
solved_comps := listAppend(tmp, solved_comps);
end if;
solved_comps := match UnorderedMap.get(comp, duplicate_map)
local list<StrongComponent> alias_comps;
case SOME(alias_comps) then listAppend(alias_comps, solved_comps); // strong component already solved -> get alias comps
else algorithm
// solve strong component -> create alias comps
(alias_comps, funcTree, implicit_index) := solveStrongComponent(comp, funcTree, system.systemType, implicit_index, slicing_map);
UnorderedMap.add(comp, list(StrongComponent.createAlias(system.systemType, system.partitionIndex, comp_idx, c) for c in alias_comps), duplicate_map);
then listAppend(alias_comps, solved_comps);
end match;
end for;
system.strongComponents := SOME(listArray(listReverse(solved_comps)));
// update sliced eqn names
Expand All @@ -179,7 +179,7 @@ public
input output FunctionTree funcTree;
input SystemType systemType;
input output Integer implicit_index;
input UnorderedMap<ComponentRef, EquationPointerList> slicing_map;
input UnorderedMap<ComponentRef, list<Pointer<Equation>>> slicing_map;
protected
Status solve_status;
StrongComponent implicit_comp;
Expand Down Expand Up @@ -241,9 +241,7 @@ public

// safe the slicing replacement in the map
eqn_cref := Equation.getEqnName(eqn_ptr);
if UnorderedMap.contains(eqn_cref, slicing_map) then
sliced_eqns := listAppend(UnorderedMap.getSafe(eqn_cref, slicing_map, sourceInfo()), sliced_eqns);
end if;
sliced_eqns := listAppend(UnorderedMap.getOrDefault(eqn_cref, slicing_map, {}), sliced_eqns);
UnorderedMap.add(eqn_cref, sliced_eqns, slicing_map);

then (solved_comps, solve_status);
Expand Down Expand Up @@ -320,18 +318,14 @@ public
// -> just use the first name as replacement for all of them and all other with empty lists
eqn_ptr :: rest := sliced_eqns;
eqn_cref := Equation.getEqnName(eqn_ptr);
if UnorderedMap.contains(eqn_cref, slicing_map) then
sliced_eqns := listAppend(UnorderedMap.getSafe(eqn_cref, slicing_map, sourceInfo()), sliced_eqns);
end if;
sliced_eqns := listAppend(UnorderedMap.getOrDefault(eqn_cref, slicing_map, {}), sliced_eqns);
UnorderedMap.add(eqn_cref, sliced_eqns, slicing_map);

// empty for all others (do not overwrite if it exists)
if not listEmpty(rest) then
for eqn_ptr in rest loop
eqn_cref := Equation.getEqnName(eqn_ptr);
if not UnorderedMap.contains(eqn_cref, slicing_map) then
UnorderedMap.add(eqn_cref, {}, slicing_map);
end if;
UnorderedMap.tryAdd(eqn_cref, {}, slicing_map);
end for;
end if;
then (solved_comps, solve_status);
Expand Down Expand Up @@ -361,7 +355,7 @@ public
input SystemType systemType;
output Status solve_status;
input output Integer implicit_index;
input UnorderedMap<ComponentRef, EquationPointerList> slicing_map;
input UnorderedMap<ComponentRef, list<Pointer<Equation>>> slicing_map;
algorithm
(comp, solve_status) := match comp
local
Expand All @@ -386,7 +380,7 @@ public
input SystemType systemType;
output Status status;
input output Integer implicit_index;
input UnorderedMap<ComponentRef, EquationPointerList> slicing_map;
input UnorderedMap<ComponentRef, list<Pointer<Equation>>> slicing_map;
algorithm
if ComponentRef.isEmpty(var.name) then
// empty variable name implies equation without return value
Expand All @@ -403,7 +397,7 @@ public
input SystemType systemType;
output Status status;
input output Integer implicit_index;
input UnorderedMap<ComponentRef, EquationPointerList> slicing_map;
input UnorderedMap<ComponentRef, list<Pointer<Equation>>> slicing_map;
algorithm
(eqn, funcTree, status) := match eqn
local
Expand Down Expand Up @@ -462,7 +456,7 @@ public
input SystemType systemType;
output Status status;
input output Integer implicit_index;
input UnorderedMap<ComponentRef, EquationPointerList> slicing_map;
input UnorderedMap<ComponentRef, list<Pointer<Equation>>> slicing_map;
output Boolean invertRelation "If the equation represents a relation, this tells if the sign should be inverted";
algorithm
(eqn, funcTree, status, invertRelation) := match eqn
Expand Down Expand Up @@ -558,7 +552,7 @@ public
output Status status;
input SystemType systemType;
input output Integer implicit_index;
input UnorderedMap<ComponentRef, EquationPointerList> slicing_map;
input UnorderedMap<ComponentRef, list<Pointer<Equation>>> slicing_map;
protected
IfEquationBody else_if;
list<StrongComponent> comps, solved_comps;
Expand Down
8 changes: 4 additions & 4 deletions OMCompiler/Compiler/NBackEnd/Util/NBReplacements.mo
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ public

case Expression.CREF() algorithm
if UnorderedMap.contains(exp.cref, replacements) then
// the cref is (with subscripts) found in replacements
res := UnorderedMap.getSafe(exp.cref, replacements, sourceInfo());
// the cref (with subscripts) is found in replacements
res := UnorderedMap.getOrFail(exp.cref, replacements);
else
// try to strip the subscripts and see if that cref occurs
stripped := ComponentRef.stripSubscriptsAll(exp.cref);
if UnorderedMap.contains(stripped, replacements) then
subs := ComponentRef.subscriptsAllWithWholeFlat(exp.cref);
res := UnorderedMap.getSafe(stripped, replacements, sourceInfo());
res := UnorderedMap.getOrFail(stripped, replacements);
res := Expression.applySubscripts(subs, res);
else
// do nothing
Expand Down Expand Up @@ -289,7 +289,7 @@ public

case Expression.CALL(call = call as Call.TYPED_CALL(fn = fn)) guard(UnorderedMap.contains(fn.path, replacements)) algorithm
// use the function from the tree, in case it was changed
fn := UnorderedMap.getSafe(fn.path, replacements, sourceInfo());
fn := UnorderedMap.getOrFail(fn.path, replacements);

// map all the inputs to the arguments and add to local replacement map
local_replacements := UnorderedMap.new<Expression>(ComponentRef.hash, ComponentRef.isEqual);
Expand Down
6 changes: 1 addition & 5 deletions OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,7 @@ public
input Integer i;
input UnorderedMap<T, IntLst> map;
algorithm
if UnorderedMap.contains(t, map) then
UnorderedMap.add(t, i :: UnorderedMap.getSafe(t, map, sourceInfo()), map);
else
UnorderedMap.addNew(t, {i}, map);
end if;
UnorderedMap.add(t, i :: UnorderedMap.getOrDefault(t, map, {}), map);
end addToSliceMap;

function fromTpl
Expand Down
6 changes: 2 additions & 4 deletions OMCompiler/Compiler/NFFrontEnd/NFOCConnectionGraph.mo
Original file line number Diff line number Diff line change
Expand Up @@ -934,8 +934,7 @@ algorithm
();
case(cr::rest,_)
equation
false = UnorderedMap.contains(cr, rooted);
UnorderedMap.add(cr,distance,rooted);
UnorderedMap.addUnique(cr,distance,rooted);
next = UnorderedMap.getOrFail(cr, table);
//print("- NFOCConnectionGraph.setRootDistance: Set Distance " +
// ComponentRef.toString(cr) + " , " + intString(distance) + "\n");
Expand All @@ -947,8 +946,7 @@ algorithm
();
case(cr::rest,_)
equation
false = UnorderedMap.contains(cr, rooted);
UnorderedMap.add(cr,distance,rooted);
UnorderedMap.addUnique(cr,distance,rooted);
//print("- NFOCConnectionGraph.setRootDistance: Set Distance " +
// ComponentRef.toString(cr) + " , " + intString(distance) + "\n");
setRootDistance(rest,table,distance,nextLevel,rooted);
Expand Down
6 changes: 3 additions & 3 deletions OMCompiler/Compiler/NSimCode/NSimCode.mo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2020, Open Source Modelica Consortium (OSMC),
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
Expand Down Expand Up @@ -115,7 +115,7 @@ public
Integer jacobianIndex;
Integer residualIndex;
Integer implicitIndex; // this can be removed i think -> moved to solve
Integer genericCallIndex;
//Integer genericCallIndex; this is replaced by UnorderedMap.size(generic_call_map)
Integer extObjIndex;

UnorderedMap<AliasInfo, Integer> alias_map;
Expand Down Expand Up @@ -153,7 +153,7 @@ public
0,0,0,0,
0,0,0,0,
1,0,0,
0,0,0,0,0,
0,0,0,0,
UnorderedMap.new<Integer>(AliasInfo.hash, AliasInfo.isEqual),
UnorderedMap.new<Integer>(Identifier.hash, Identifier.isEqual)
);
Expand Down
16 changes: 2 additions & 14 deletions OMCompiler/Compiler/NSimCode/NSimStrongComponent.mo
Original file line number Diff line number Diff line change
Expand Up @@ -590,15 +590,7 @@ public
eqn_ptr := Slice.getT(comp.eqn);
eqn := Pointer.access(eqn_ptr);
ident := Identifier.IDENTIFIER(eqn_ptr, comp.var_cref);
if UnorderedMap.contains(ident, simCodeIndices.generic_call_map) then
// the generic call body was already generated
generic_call_index := UnorderedMap.getSafe(ident, simCodeIndices.generic_call_map, sourceInfo());
else
// the generic call body was not already generated
generic_call_index := simCodeIndices.genericCallIndex;
UnorderedMap.add(ident, generic_call_index, simCodeIndices.generic_call_map);
simCodeIndices.genericCallIndex := simCodeIndices.genericCallIndex + 1;
end if;
generic_call_index := UnorderedMap.tryAdd(ident, UnorderedMap.size(simCodeIndices.generic_call_map), simCodeIndices.generic_call_map);
tmp := GENERIC_ASSIGN(simCodeIndices.equationIndex, generic_call_index, comp.eqn.indices, Equation.getSource(eqn), Equation.getAttributes(eqn));
UnorderedMap.add(Equation.getEqnName(eqn_ptr), tmp, equation_map);
simCodeIndices.equationIndex := simCodeIndices.equationIndex + 1;
Expand Down Expand Up @@ -664,11 +656,7 @@ public
then (NONLINEAR(system, NONE()), system.index);

case StrongComponent.ALIAS() algorithm
if UnorderedMap.contains(comp.aliasInfo, simCodeIndices.alias_map) then
aliasOf := UnorderedMap.getSafe(comp.aliasInfo, simCodeIndices.alias_map, sourceInfo());
else
aliasOf := -1;
end if;
aliasOf := UnorderedMap.getOrDefault(comp.aliasInfo, simCodeIndices.alias_map, -1);
tmp := ALIAS(simCodeIndices.equationIndex, comp.aliasInfo, aliasOf);
simCodeIndices.equationIndex := simCodeIndices.equationIndex + 1;
then (tmp, getIndex(tmp));
Expand Down

0 comments on commit 5cd2141

Please sign in to comment.