Skip to content

Commit 4c9b4c6

Browse files
committed
Optimize SimCodeUtil.createVarToArrayIndexMapping
- Count the number of variables to determine an appropriate size for the hash tables instead of using a "one size fits all" approach.
1 parent 9dde075 commit 4c9b4c6

1 file changed

Lines changed: 53 additions & 68 deletions

File tree

OMCompiler/Compiler/SimCode/SimCodeUtil.mo

Lines changed: 53 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12234,80 +12234,65 @@ algorithm
1223412234
end match;
1223512235
end getHideResult;
1223612236

12237-
public function createVarToArrayIndexMapping "author: marcusw
12238-
Creates a mapping for each array-cref to the array dimensions (int list) and to the indices (for the code generation) used to store the array content."
12237+
public function createVarToArrayIndexMapping
12238+
"Creates a mapping for each array-cref to the array dimensions (int list) and to the indices (for the code generation) used to store the array content."
1223912239
input SimCode.ModelInfo iModelInfo;
1224012240
output HashTableCrIListArray.HashTable oVarToArrayIndexMapping;
1224112241
output HashTableCrILst.HashTable oVarToIndexMapping; //same as oVarToArrayIndexMapping, but does not merge array variables into one list
1224212242
protected
12243-
list<SimCodeVar.SimVar> stateVars;
12244-
list<SimCodeVar.SimVar> derivativeVars;
12245-
list<SimCodeVar.SimVar> algVars;
12246-
list<SimCodeVar.SimVar> discreteAlgVars;
12247-
list<SimCodeVar.SimVar> intAlgVars;
12248-
list<SimCodeVar.SimVar> boolAlgVars;
12249-
list<SimCodeVar.SimVar> inputVars;
12250-
list<SimCodeVar.SimVar> outputVars;
12251-
list<SimCodeVar.SimVar> aliasVars;
12252-
list<SimCodeVar.SimVar> intAliasVars;
12253-
list<SimCodeVar.SimVar> boolAliasVars;
12254-
list<SimCodeVar.SimVar> paramVars;
12255-
list<SimCodeVar.SimVar> intParamVars;
12256-
list<SimCodeVar.SimVar> boolParamVars;
12257-
list<SimCodeVar.SimVar> stringAlgVars;
12258-
list<SimCodeVar.SimVar> stringParamVars;
12259-
list<SimCodeVar.SimVar> stringAliasVars;
12260-
//list<SimCodeVar.SimVar> extObjVars;
12261-
list<SimCodeVar.SimVar> constVars;
12262-
list<SimCodeVar.SimVar> intConstVars;
12263-
list<SimCodeVar.SimVar> boolConstVars;
12264-
list<SimCodeVar.SimVar> stringConstVars;
12265-
//list<SimCodeVar.SimVar> jacobianVars;
12266-
//list<SimCodeVar.SimVar> seedVars;
12267-
list<SimCodeVar.SimVar> realOptimizeConstraintsVars;
12268-
list<SimCodeVar.SimVar> realOptimizeFinalConstraintsVars;
12269-
HashTableCrILst.HashTable varIndexMappingHashTable;
12270-
HashTableCrIListArray.HashTable varArrayIndexMappingHashTable;
12243+
SimCodeVar.SimVars sim_vars;
12244+
list<tuple<list<SimCodeVar.SimVar>, Integer>> vars;
12245+
Integer table_size = 0;
12246+
list<SimCodeVar.SimVar> var_lst;
12247+
Integer var_type;
1227112248
array<Integer> currentVarIndices; //current variable index real,int,bool,string
1227212249
algorithm
12273-
(oVarToArrayIndexMapping,oVarToIndexMapping) := match(iModelInfo)
12274-
case(SimCode.MODELINFO(vars = SimCodeVar.SIMVARS(stateVars=stateVars,derivativeVars=derivativeVars,algVars=algVars,discreteAlgVars=discreteAlgVars,
12275-
intAlgVars=intAlgVars,boolAlgVars=boolAlgVars,stringAlgVars=stringAlgVars,aliasVars=aliasVars,intAliasVars=intAliasVars,boolAliasVars=boolAliasVars,stringAliasVars=stringAliasVars,paramVars=paramVars,
12276-
intParamVars=intParamVars,boolParamVars=boolParamVars,stringParamVars=stringParamVars,inputVars=inputVars,outputVars=outputVars, constVars=constVars, intConstVars=intConstVars, boolConstVars=boolConstVars,
12277-
stringConstVars=stringConstVars,realOptimizeConstraintsVars=realOptimizeConstraintsVars, realOptimizeFinalConstraintsVars=realOptimizeFinalConstraintsVars)))
12278-
equation
12279-
varArrayIndexMappingHashTable = HashTableCrIListArray.emptyHashTableSized(BaseHashTable.biggerBucketSize);
12280-
varIndexMappingHashTable = HashTableCrILst.emptyHashTableSized(BaseHashTable.biggerBucketSize);
12281-
currentVarIndices = arrayCreate(4,1); //0 is reserved for unused variables
12282-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(stateVars, 1, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12283-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(derivativeVars, 1, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12284-
12285-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(algVars, 1, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12286-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(discreteAlgVars, 1, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12287-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(intAlgVars, 2, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12288-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(boolAlgVars, 3, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12289-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(stringAlgVars, 4, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12290-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(paramVars, 1, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12291-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(intParamVars, 2, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12292-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(boolParamVars, 3, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12293-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(stringParamVars, 4, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12294-
//(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTabl) = addVarToArrayIndexMappings(inputVars, 1, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12295-
//(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTabl) = addVarToArrayIndexMappings(outputVars, 1, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12296-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(constVars, 1, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12297-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(intConstVars, 2, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12298-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(boolConstVars, 3, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12299-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(stringConstVars, 4, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12300-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(realOptimizeConstraintsVars, 1, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12301-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(realOptimizeFinalConstraintsVars, 1, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12302-
12303-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(aliasVars, 1, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12304-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(intAliasVars, 2, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12305-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(boolAliasVars, 3, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12306-
(currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable) = addVarToArrayIndexMappings(stringAliasVars, 4, currentVarIndices, varArrayIndexMappingHashTable, varIndexMappingHashTable);
12307-
then (varArrayIndexMappingHashTable, varIndexMappingHashTable);
12308-
else
12309-
then (HashTableCrIListArray.emptyHashTableSized(0), HashTableCrILst.emptyHashTableSized(0));
12310-
end match;
12250+
// Collect the variable lists into a list for easier handling.
12251+
sim_vars := iModelInfo.vars;
12252+
vars := {
12253+
(sim_vars.stateVars, 1),
12254+
(sim_vars.stateVars, 1),
12255+
(sim_vars.derivativeVars, 1),
12256+
(sim_vars.algVars, 1),
12257+
(sim_vars.discreteAlgVars, 1),
12258+
(sim_vars.intAlgVars, 2),
12259+
(sim_vars.boolAlgVars, 3),
12260+
(sim_vars.stringAlgVars, 4),
12261+
(sim_vars.paramVars, 1),
12262+
(sim_vars.intParamVars, 2),
12263+
(sim_vars.boolParamVars, 3),
12264+
(sim_vars.stringParamVars, 4),
12265+
//(sim_vars.inputVars, 1),
12266+
//(sim_vars.utputVars, 1),
12267+
(sim_vars.constVars, 1),
12268+
(sim_vars.intConstVars, 2),
12269+
(sim_vars.boolConstVars, 3),
12270+
(sim_vars.stringConstVars, 4),
12271+
(sim_vars.realOptimizeConstraintsVars, 1),
12272+
(sim_vars.realOptimizeFinalConstraintsVars, 1),
12273+
(sim_vars.aliasVars, 1),
12274+
(sim_vars.intAliasVars, 2),
12275+
(sim_vars.boolAliasVars, 3),
12276+
(sim_vars.stringAliasVars, 4)
12277+
};
12278+
12279+
// Count the number of variables to determine an appropriate size for the hash tables.
12280+
for vl in vars loop
12281+
(var_lst, _) := vl;
12282+
table_size := table_size + listLength(var_lst);
12283+
end for;
12284+
table_size := Util.nextPrime(realInt(table_size * 1.4));
12285+
12286+
oVarToArrayIndexMapping := HashTableCrIListArray.emptyHashTableSized(table_size);
12287+
oVarToIndexMapping := HashTableCrILst.emptyHashTableSized(table_size);
12288+
currentVarIndices := arrayCreate(4, 1); //0 is reserved for unused variables
12289+
12290+
// Add the variables to the tables.
12291+
for vl in vars loop
12292+
(var_lst, var_type) := vl;
12293+
(currentVarIndices, oVarToArrayIndexMapping, oVarToIndexMapping) :=
12294+
addVarToArrayIndexMappings(var_lst, var_type, currentVarIndices, oVarToArrayIndexMapping, oVarToIndexMapping);
12295+
end for;
1231112296
end createVarToArrayIndexMapping;
1231212297

1231312298
public function addVarToArrayIndexMappings

0 commit comments

Comments
 (0)