From 69ff31b44f8ea12da3ac4db6e2f0d8804235e1e2 Mon Sep 17 00:00:00 2001 From: vwaurich Date: Wed, 23 Dec 2015 12:37:37 +0100 Subject: [PATCH] - hpcom clean up --- Compiler/BackEnd/HpcOmTaskGraph.mo | 384 ++++++++++++++++++++++ Compiler/SimCode/HpcOmSimCodeMain.mo | 450 +------------------------- Compiler/Template/SimCodeBackendTV.mo | 5 +- 3 files changed, 395 insertions(+), 444 deletions(-) diff --git a/Compiler/BackEnd/HpcOmTaskGraph.mo b/Compiler/BackEnd/HpcOmTaskGraph.mo index e8dac240699..0b8776f063b 100644 --- a/Compiler/BackEnd/HpcOmTaskGraph.mo +++ b/Compiler/BackEnd/HpcOmTaskGraph.mo @@ -55,6 +55,7 @@ protected import HpcOmBenchmark; protected import HpcOmEqSystems; protected import HpcOmScheduler; protected import List; +protected import SimCodeUtil; protected import SCode; protected import System; protected import Util; @@ -6277,5 +6278,388 @@ algorithm end matchcontinue; end getNodeForVarIdx; +//---------------------------- +// MAPPING FUNCTIONS +//---------------------------- + +public function setUpHpcOmMapping"creates mappings between simcode and backendDAE for the hpcom module +author: waurich 12-2015" + input BackendDAE.BackendDAE daeIn; + input SimCode.SimCode simCodeIn; + input Integer lastEqMappingIdx; + input list> equationSccMappingIn; //Maps each simEq to the scc + output array simeqCompMapping; //Maps each simEq to the scc + output array> sccSimEqMapping; //Maps each scc to a list of simEqs + output array> daeSccSimEqMapping; //Maps each scc to a list of simEqs, including removed equations like asserts +protected + Integer highestSccIdx, compCountPlusDummy; + list> equationSccMapping,equationSccMapping1; + BackendDAE.StrongComponents allComps; +algorithm + (allComps,_) := getSystemComponents(daeIn); + highestSccIdx := findHighestSccIdxInMapping(equationSccMappingIn,-1); + compCountPlusDummy := listLength(allComps)+1; + equationSccMapping1 := removeDummyStateFromMapping(equationSccMappingIn); + //the mapping can contain a dummy state as first scc + equationSccMapping := if intEq(highestSccIdx, compCountPlusDummy) then equationSccMapping1 else equationSccMappingIn; + sccSimEqMapping := convertToSccSimEqMapping(equationSccMapping, listLength(allComps)); + simeqCompMapping := convertToSimeqCompMapping(equationSccMapping, lastEqMappingIdx); + //for the dae-system + daeSccSimEqMapping := listArray(List.map(SimCodeUtil.getRemovedEquationSimEqSysIdxes(simCodeIn),List.create)); + daeSccSimEqMapping := arrayAppend(sccSimEqMapping,daeSccSimEqMapping); + + //_ = getSimEqIdxSimEqMapping(simCode.allEquations, arrayLength(simeqCompMapping)); // CAN WE REMOVE IT???? + //dumpSimEqSCCMapping(simeqCompMapping); + //dumpSccSimEqMapping(sccSimEqMapping); +end setUpHpcOmMapping; + +protected function findHighestSccIdxInMapping "function findHighestSccIdxInMapping + author: marcusw + Find the highest scc-index in the mapping list." + input list> iEquationSccMapping; // + input Integer iHighestIndex; + output Integer oIndex; +protected + Integer eqIdx, sccIdx; + list> rest; +algorithm + oIndex := matchcontinue(iEquationSccMapping,iHighestIndex) + case((eqIdx,sccIdx)::rest,_) + equation + true = intGt(sccIdx,iHighestIndex); + then findHighestSccIdxInMapping(rest,sccIdx); + case((eqIdx,sccIdx)::rest,_) + then findHighestSccIdxInMapping(rest,iHighestIndex); + else iHighestIndex; + end matchcontinue; +end findHighestSccIdxInMapping; + +protected function removeDummyStateFromMapping "function removeDummyStateFromMapping + author: marcusw + Removes all mappings with sccIdx=1 from the list and decrements all other scc-indices by 1." + input list> iEquationSccMapping; + output list> oEquationSccMapping; +algorithm + oEquationSccMapping := List.fold(iEquationSccMapping, removeDummyStateFromMapping1, {}); +end removeDummyStateFromMapping; + +protected function removeDummyStateFromMapping1 "function removeDummyStateFromMapping1 + author: marcusw + Helper function of removeDummyStateFromMapping. Handles one list-element." + input tuple iTuple; // + input list> iNewList; + output list> oNewList; +protected + Integer eqIdx,sccIdx; + tuple newElem; +algorithm + oNewList := matchcontinue(iTuple,iNewList) + case((eqIdx,sccIdx),_) + equation + true = intEq(sccIdx,1); + then iNewList; + case((eqIdx,sccIdx),_) + equation + newElem = (eqIdx,sccIdx-1); + then newElem::iNewList; + else + equation + print("removeDummyStateFromMapping1 failed\n"); + then iNewList; + end matchcontinue; +end removeDummyStateFromMapping1; + + +protected function convertToSccSimEqMapping "function convertToSccSimEqMapping + author: marcusw + Converts the given mapping (simEqIndex -> sccIndex) to the inverse mapping (sccIndex->simEqIndex)." + input list> iMapping; //the mapping (simEqIndex -> sccIndex) + input Integer numOfSccs; //important for arrayCreate + output array> oMapping; //the created mapping (sccIndex->simEqIndex) +protected + array> tmpMapping; +algorithm + tmpMapping := arrayCreate(numOfSccs,{}); + //print("convertToSccSimEqMapping with " + intString(numOfSccs) + " sccs.\n"); + _ := List.fold(iMapping, convertToSccSimEqMapping1, tmpMapping); + oMapping := tmpMapping; +end convertToSccSimEqMapping; + +protected function convertToSccSimEqMapping1 "function convertToSccSimEqMapping1 + author: marcusw + Helper function for convertToSccSimEqMapping. It will update the arrayIndex of the given mapping value." + input tuple iMapping; // + input array> iSccMapping; + output array> oSccMapping; +protected + Integer i1,i2; + List tmpList; +algorithm + (i1,i2) := iMapping; + //print("convertToSccSimEqMapping1 accessing index " + intString(i2) + ".\n"); + tmpList := arrayGet(iSccMapping,i2); + tmpList := i1 :: tmpList; + oSccMapping := arrayUpdate(iSccMapping,i2,tmpList); +end convertToSccSimEqMapping1; + + +protected function convertToSimeqCompMapping "function convertToSimeqCompMapping + author: marcusw + Converts the given mapping (simEqIndex -> sccIndex) bases on tuples to an array mapping." + input list> iMapping; // + input Integer numOfSimEqs; + output array oMapping; //maps each simEq to the scc +protected + array tmpMapping; +algorithm + tmpMapping := arrayCreate(numOfSimEqs, -1); + oMapping := List.fold(iMapping, convertToSimeqCompMapping1, tmpMapping); +end convertToSimeqCompMapping; + + +protected function convertToSimeqCompMapping1 "function convertToSimeqCompMapping1 + author: marcusw + Helper function for convertToSimeqCompMapping. It will update the array at the given index." + input tuple iSimEqTuple; // + input array iMapping; + output array oMapping; +protected + Integer simEqIdx,sccIdx; +algorithm + (simEqIdx,sccIdx) := iSimEqTuple; + //print("convertToSimeqCompMapping1 " + intString(simEqIdx) + " .. " + intString(sccIdx) + " iMapping_len: " + intString(arrayLength(iMapping)) + "\n"); + oMapping := arrayUpdate(iMapping,simEqIdx,sccIdx); +end convertToSimeqCompMapping1; + + +protected function getSimEqIdxSimEqMapping "author: marcusw + Get a mapping from simEqIdx -> option(simEq)." + input list iAllEquations; + input Integer iSimEqSystemHighestIdx; + output array> oMapping; +protected + array> tmpMapping; +algorithm + tmpMapping := arrayCreate(iSimEqSystemHighestIdx, NONE()); + oMapping := List.fold(iAllEquations, getSimEqIdxSimEqMapping1, tmpMapping); +end getSimEqIdxSimEqMapping; + + +protected function getSimEqIdxSimEqMapping1 "author: marcusw + Helper function that adds the index of the given equation to the mapping." + input SimCode.SimEqSystem iEquation; + input array> iMapping; + output array> oMapping; +protected + Integer simEqIdx; + array> tmpMapping; +algorithm + oMapping := matchcontinue(iEquation, iMapping) + case(_,_) + equation + (simEqIdx,_) = getIndexBySimCodeEq(iEquation); + tmpMapping = arrayUpdate(iMapping, simEqIdx, SOME(iEquation)); + then tmpMapping; + else + equation + (simEqIdx,_) = getIndexBySimCodeEq(iEquation); + //print("getSimEqIdxSimEqMapping1: Can't access idx " + intString(simEqIdx) + "\n"); + then iMapping; + end matchcontinue; +end getSimEqIdxSimEqMapping1; + + +public function getSimCodeEqByIndexAndMapping "author: marcusw + Returns the SimEqSystem which has the given Index." + input array> iSimEqIdxSimEqMapping; //All SimEqSystems + input Integer iIdx; //The index of the required system + output SimCode.SimEqSystem oSimEqSystem; +protected + Option tmpSimEqSystem; +algorithm + tmpSimEqSystem := arrayGet(iSimEqIdxSimEqMapping, iIdx); + oSimEqSystem := getSimCodeEqByIndexAndMapping1(tmpSimEqSystem, iIdx); +end getSimCodeEqByIndexAndMapping; + + +protected function getSimCodeEqByIndexAndMapping1 "author: marcusw + Returns the SimEqSystem if it's not NONE()." + input Option iSimEqSystem; + input Integer iIdx; + output SimCode.SimEqSystem oSimEqSystem; +protected + SimCode.SimEqSystem tmpSys; +algorithm + oSimEqSystem := match(iSimEqSystem,iIdx) + case(SOME(tmpSys),_) + then tmpSys; + else + equation + print("getSimCodeEqByIndexAndMapping1 failed. Looking for Index " + intString(iIdx) + "\n"); + //print(" -- available indices: " + stringDelimitList(List.map(List.map(iEqs,getIndexBySimCodeEq), intString), ",") + "\n"); + then fail(); + end match; +end getSimCodeEqByIndexAndMapping1; + + +public function getSimCodeEqByIndex "function getSimCodeEqByIndex + author: marcusw + Returns the SimEqSystem which has the given Index. This method is called from susan." + input list iEqs; //All SimEqSystems + input Integer iIdx; //The index of the required system + output SimCode.SimEqSystem oEq; +protected + list rest; + SimCode.SimEqSystem head; + Integer headIdx,headIdx2; +algorithm + oEq := matchcontinue(iEqs,iIdx) + case(head::rest,_) + equation + (headIdx,headIdx2) = getIndexBySimCodeEq(head); + //print("getSimCodeEqByIndex listLength: " + intString(listLength(iEqs)) + " head idx: " + intString(headIdx) + "\n"); + true = intEq(headIdx,iIdx) or intEq(headIdx2,iIdx); + then head; + case(head::rest,_) then getSimCodeEqByIndex(rest,iIdx); + else + equation + print("getSimCodeEqByIndex failed. Looking for Index " + intString(iIdx) + "\n"); + //print(" -- available indices: " + stringDelimitList(List.map(List.map(iEqs,getIndexBySimCodeEq), intString), ",") + "\n"); + then fail(); + end matchcontinue; +end getSimCodeEqByIndex; + + +protected function getIndexBySimCodeEq "function getIndexBySimCodeEq + author: marcusw + Just a small helper function to get the index of a SimEqSystem." + input SimCode.SimEqSystem iEq; + output Integer oIdx; + output Integer oIdx2; +protected + Integer index,index2; +algorithm + (oIdx,oIdx2) := match(iEq) + case(SimCode.SES_RESIDUAL(index=index)) then (index,0); + case(SimCode.SES_SIMPLE_ASSIGN(index=index)) then (index,0); + case(SimCode.SES_ARRAY_CALL_ASSIGN(index=index)) then (index,0); + case(SimCode.SES_IFEQUATION(index=index)) then (index,0); + case(SimCode.SES_ALGORITHM(index=index)) then (index,0); + // no dynamic tearing + case(SimCode.SES_LINEAR(SimCode.LINEARSYSTEM(index=index), NONE())) then (index,0); + case(SimCode.SES_NONLINEAR(SimCode.NONLINEARSYSTEM(index=index), NONE())) then (index,0); + // dynamic tearing + case(SimCode.SES_LINEAR(SimCode.LINEARSYSTEM(index=index), SOME(SimCode.LINEARSYSTEM(index=index2)))) then (index,index2); + case(SimCode.SES_NONLINEAR(SimCode.NONLINEARSYSTEM(index=index), SOME(SimCode.NONLINEARSYSTEM(index=index2)))) then (index,index2); + case(SimCode.SES_MIXED(index=index)) then (index,0); + case(SimCode.SES_WHEN(index=index)) then (index,0); + else fail(); + end match; +end getIndexBySimCodeEq; + + +protected function getSimCodeEqsByTaskList "author: marcusw + Get the simCode.SimEqSystem - objects references by the given tasks." + input list iTaskList; + input array> iSimEqIdxSimEqMapping; + output list oSimEqs; +protected + list> tmpSimEqs; +algorithm + tmpSimEqs := List.map1(iTaskList, getSimCodeEqsByTaskList0, iSimEqIdxSimEqMapping); + oSimEqs := List.flatten(tmpSimEqs); +end getSimCodeEqsByTaskList; + +protected function getSimCodeEqsByTaskList0 "author: marcusw + Get the simCode.SimEqSystem - objects references by the given task." + input HpcOmSimCode.Task iTask; + input array> iSimEqIdxSimEqMapping; + output list oSimEqs; +protected + list eqIdc; + list tmpSimEqs; +algorithm + oSimEqs := match(iTask, iSimEqIdxSimEqMapping) + case(HpcOmSimCode.CALCTASK(eqIdc=eqIdc),_) + equation + tmpSimEqs = List.map1r(eqIdc, getSimCodeEqByIndexAndMapping, iSimEqIdxSimEqMapping); + then tmpSimEqs; + case(HpcOmSimCode.CALCTASK_LEVEL(eqIdc=eqIdc),_) + equation + tmpSimEqs = List.map1r(eqIdc, getSimCodeEqByIndexAndMapping, iSimEqIdxSimEqMapping); + then tmpSimEqs; + else {}; + end match; +end getSimCodeEqsByTaskList0; + + +public function dumpSimEqSCCMapping "author: marcusw + Prints the given mapping out to the console." + input array iSccMapping; +protected + String text; +algorithm + text := "SimEqToSCCMapping"; + ((_,text)) := Array.fold(iSccMapping, dumpSimEqSCCMapping1, (1,text)); + print(text + "\n"); +end dumpSimEqSCCMapping; + + +protected function dumpSimEqSCCMapping1 "author: marcusw + Helper function of dumpSimEqSCCMapping to print one mapping entry." + input Integer iMapping; + input tuple iIndexText; + output tuple oIndexText; +protected + Integer iIndex; + String text, iText; +algorithm + (iIndex,iText) := iIndexText; + text := intString(iMapping); + text := iText + "\nSimEq " + intString(iIndex) + ": {" + text + "}"; + oIndexText := (iIndex+1,text); +end dumpSimEqSCCMapping1; + + +public function dumpSccSimEqMapping "function dumpSccSimEqMapping + author: marcusw + Prints the given mapping out to the console." + input array> iSccMapping; +protected + String text; +algorithm + text := "SccToSimEqMapping"; + ((_,text)) := Array.fold(iSccMapping, dumpSccSimEqMapping1, (1,text)); + print(text + "\n"); +end dumpSccSimEqMapping; + + +protected function dumpSccSimEqMapping1 "function dumpSccSimEqMapping1 + author: marcusw + Helper function of dumpSccSimEqMapping to print one mapping list." + input list iMapping; + input tuple iIndexText; + output tuple oIndexText; +protected + Integer iIndex; + String text, iText; +algorithm + (iIndex,iText) := iIndexText; + text := List.fold(iMapping, dumpSccSimEqMapping2, " "); + text := iText + "\nSCC " + intString(iIndex) + ": {" + text + "}"; + oIndexText := (iIndex+1,text); +end dumpSccSimEqMapping1; + + +protected function dumpSccSimEqMapping2 "function dumpSccSimEqMapping2 + author: marcusw + Helper function of dumpSccSimEqMapping1 to print one mapping element." + input Integer iIndex; + input String iText; + output String oText; +algorithm + oText := iText + intString(iIndex) + " "; +end dumpSccSimEqMapping2; + annotation(__OpenModelica_Interface="backend"); end HpcOmTaskGraph; diff --git a/Compiler/SimCode/HpcOmSimCodeMain.mo b/Compiler/SimCode/HpcOmSimCodeMain.mo index 38d22c28dbe..5e3109dd9ff 100644 --- a/Compiler/SimCode/HpcOmSimCodeMain.mo +++ b/Compiler/SimCode/HpcOmSimCodeMain.mo @@ -89,84 +89,23 @@ public function createSimCode "function createSimCode algorithm simCode := matchcontinue (inBackendDAE, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, libPaths,simSettingsOpt, recordDecls, literals, args) local - String fileDir, cname; - Integer lastEqMappingIdx, maxDelayedExpIndex, uniqueEqIndex, numberofEqns, numberOfInitialEquations, numberOfInitialAlgorithms, numStateSets; - Integer numberofLinearSys, numberofNonLinearSys, numberofMixedSys; - BackendDAE.BackendDAE dlow, dlow2; - BackendDAE.BackendDAE initDAE; - BackendDAE.IncidenceMatrix incidenceMatrix; - DAE.FunctionTree functionTree; - BackendDAE.SymbolicJacobians symJacs; - Absyn.Path class_; - - // new variables - list residuals; // --> initial_residual - Boolean useHomotopy; // true if homotopy(...) is used during initialization - list initialEquations; // --> initial_equations - list removedInitialEquations; // --> functionRemovedInitialEquations - list startValueEquations; // --> updateBoundStartValues - list nominalValueEquations; // --> updateBoundNominalValues - list minValueEquations; // --> updateBoundMinValues - list maxValueEquations; // --> updateBoundMaxValues - list parameterEquations; // --> updateBoundParameters - list removedEquations; - list algorithmAndEquationAsserts; - list jacobianEquations; - list zeroCrossingsEquations; - //list algorithmAndEquationAsserts; - list constraints; - list classAttributes; - list zeroCrossings, sampleZC, relations; - list discreteModelVars; - SimCode.ExtObjInfo extObjInfo; - SimCode.MakefileParams makefileParams; - SimCode.DelayedExpression delayedExps; - BackendDAE.Variables knownVars; - list varlst; - list partitionsKind; - list baseClocks; - - list LinearMatrices, SymbolicJacs, SymbolicJacsTemp, SymbolicJacsStateSelect; - SimCode.HashTableCrefToSimVar crefToSimVarHT; - Boolean ifcpp; + Integer lastEqMappingIdx; BackendDAE.EqSystems eqs; - BackendDAE.Shared shared; - BackendDAE.EquationArray removedEqs; - list constrsarr; - list clsattrsarra; - - list lits; - list tempvars; - - SimCode.JacobianMatrix jacG; - Integer highestSccIdx, compCountPlusDummy; - Option inlineDAE; - list stateSets; - array systemIndexMap; - list> equationSccMapping, equationSccMapping1; //Maps each simEq to the scc + + list> equationSccMapping; //Maps each simEq to the scc array> sccSimEqMapping, daeSccSimEqMapping; //Maps each scc to a list of simEqs array simeqCompMapping; //Maps each simEq to the scc - list timeEvents; - BackendDAE.StrongComponents allComps, initComps; + BackendDAE.StrongComponents allComps; HpcOmTaskGraph.TaskGraph taskGraph, taskGraphDae, taskGraphOde, taskGraphZeroFuncs, taskGraphOdeSimplified, taskGraphDaeSimplified, taskGraphZeroFuncSimplified, taskGraphOdeScheduled, taskGraphDaeScheduled, taskGraphZeroFuncScheduled, taskGraphInit; HpcOmTaskGraph.TaskGraphMeta taskGraphData, taskGraphDataDae, taskGraphDataOde, taskGraphDataZeroFuncs, taskGraphDataOdeSimplified, taskGraphDataDaeSimplified, taskGraphDataZeroFuncSimplified, taskGraphDataOdeScheduled, taskGraphDataDaeScheduled, taskGraphDataZeroFuncScheduled, taskGraphDataInit; String fileName, fileNamePrefix; Integer numProc; - list> parallelSets; list> criticalPaths, criticalPathsWoC; - Real cpCosts, cpCostsWoC, serTime, parTime, speedUp, speedUpMax; + Real cpCosts, cpCostsWoC; list scheduledTasksOde, scheduledTasksDae, scheduledTasksZeroFunc; - list scheduledDAENodes; list zeroFuncsSimEqIdc; - //Additional informations to append SimCode - list simCodeLiterals; - list jacobianMatrixes; - Option simulationSettingsOpt; - list simCodeRecordDecls; - list simCodeExternalFunctionIncludes; - Boolean taskGraphMetaValid, numFixed; String criticalPathInfo; array> schedulerInfo; //maps each Task to @@ -176,12 +115,9 @@ algorithm Integer graphOps; Option backendMapping; Option optTmpMemoryMap; - list equationsForConditions; array> simEqIdxSimEqMapping; array> simVarMapping; //maps each backend variable to a list of simVars - Option modelStruct; - list mixedArrayVars; HpcOmSimCode.HpcOmData hpcomData; HashTableCrIListArray.HashTable varToArrayIndexMapping; HashTableCrILst.HashTable varToIndexMapping; @@ -197,23 +133,10 @@ algorithm SimCodeUtil.createSimCode( inBackendDAE, inInitDAE, inUseHomotopy, inInitDAE_lambda0, inRemovedInitialEquationLst, inPrimaryParameters, inAllPrimaryParameters, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs,libPaths, simSettingsOpt, recordDecls, literals, args ); - simVarMapping = SimCodeUtil.getSimVarMappingOfBackendMapping(simCode.backendMapping); - //get SCC to simEqSys-mappping + //get simCode-backendDAE mappings //---------------------------- - (allComps,_) = HpcOmTaskGraph.getSystemComponents(inBackendDAE); - //print("All components size: " + intString(listLength(allComps)) + "\n"); - highestSccIdx = findHighestSccIdxInMapping(equationSccMapping,-1); - compCountPlusDummy = listLength(allComps)+1; - equationSccMapping1 = removeDummyStateFromMapping(equationSccMapping); - //the mapping can contain a dummy state as first scc - equationSccMapping = if intEq(highestSccIdx, compCountPlusDummy) then equationSccMapping1 else equationSccMapping; - sccSimEqMapping = convertToSccSimEqMapping(equationSccMapping, listLength(allComps)); - - simeqCompMapping = convertToSimeqCompMapping(equationSccMapping, lastEqMappingIdx); - _ = getSimEqIdxSimEqMapping(simCode.allEquations, arrayLength(simeqCompMapping)); - - //dumpSimEqSCCMapping(simeqCompMapping); - //dumpSccSimEqMapping(sccSimEqMapping); + simVarMapping = SimCodeUtil.getSimVarMappingOfBackendMapping(simCode.backendMapping); + (simeqCompMapping, sccSimEqMapping, daeSccSimEqMapping) = HpcOmTaskGraph.setUpHpcOmMapping(inBackendDAE, simCode, lastEqMappingIdx, equationSccMapping); SimCodeFunctionUtil.execStat("hpcom setup"); //Get small DAE System (without removed equations) @@ -229,8 +152,6 @@ algorithm taskGraphDataDae = HpcOmTaskGraph.copyTaskGraphMeta(taskGraphData); (taskGraphDae,taskGraphDataDae) = HpcOmTaskGraph.appendRemovedEquations(inBackendDAE,taskGraphDae,taskGraphDataDae); - daeSccSimEqMapping = listArray(List.map(SimCodeUtil.getRemovedEquationSimEqSysIdxes(simCode),List.create)); - daeSccSimEqMapping = arrayAppend(sccSimEqMapping,daeSccSimEqMapping); schedulerInfo = arrayCreate(arrayLength(taskGraphDae), (-1,-1,-1.0)); SimCodeUtil.execStat("hpcom create DAE TaskGraph"); @@ -377,7 +298,7 @@ algorithm //Create Memory-Map and Sim-Code //------------------------------ - (optTmpMemoryMap, varToArrayIndexMapping, varToIndexMapping) = HpcOmMemory.createMemoryMap(simCode.modelInfo, simCode.varToArrayIndexMapping, simCode.varToIndexMapping, taskGraphOdeSimplified, BackendDAEUtil.transposeMatrix(taskGraphOdeSimplified,arrayLength(taskGraphOdeSimplified)), taskGraphDataOdeSimplified, eqs, filenamePrefix, schedulerInfo, scheduleOde, sccSimEqMapping, criticalPaths, criticalPathsWoC, criticalPathInfo, numProc, allComps); + (optTmpMemoryMap, varToArrayIndexMapping, varToIndexMapping) = HpcOmMemory.createMemoryMap(simCode.modelInfo, simCode.varToArrayIndexMapping, simCode.varToIndexMapping, taskGraphOdeSimplified, BackendDAEUtil.transposeMatrix(taskGraphOdeSimplified,arrayLength(taskGraphOdeSimplified)), taskGraphDataOdeSimplified, eqs, filenamePrefix, schedulerInfo, scheduleOde, sccSimEqMapping, criticalPaths, criticalPathsWoC, criticalPathInfo, numProc, HpcOmTaskGraph.getSystemComponents(inBackendDAE)); //BaseHashTable.dumpHashTable(varToArrayIndexMapping); @@ -387,8 +308,6 @@ algorithm simCode.hpcomData = HpcOmSimCode.HPCOMDATA(SOME((scheduleOde, scheduleDae, scheduleZeroFunc)), optTmpMemoryMap); - //print("Number of literals post: " + intString(listLength(simCodeLiterals)) + "\n"); - SimCodeFunctionUtil.execStat("hpcom other"); print("HpcOm is still under construction.\n"); then simCode; @@ -891,357 +810,6 @@ algorithm end matchcontinue; end createSchedule1; - -protected function convertToSccSimEqMapping "function convertToSccSimEqMapping - author: marcusw - Converts the given mapping (simEqIndex -> sccIndex) to the inverse mapping (sccIndex->simEqIndex)." - input list> iMapping; //the mapping (simEqIndex -> sccIndex) - input Integer numOfSccs; //important for arrayCreate - output array> oMapping; //the created mapping (sccIndex->simEqIndex) - -protected - array> tmpMapping; - -algorithm - tmpMapping := arrayCreate(numOfSccs,{}); - //print("convertToSccSimEqMapping with " + intString(numOfSccs) + " sccs.\n"); - _ := List.fold(iMapping, convertToSccSimEqMapping1, tmpMapping); - oMapping := tmpMapping; - -end convertToSccSimEqMapping; - -protected function convertToSccSimEqMapping1 "function convertToSccSimEqMapping1 - author: marcusw - Helper function for convertToSccSimEqMapping. It will update the arrayIndex of the given mapping value." - input tuple iMapping; // - input array> iSccMapping; - output array> oSccMapping; - -protected - Integer i1,i2; - List tmpList; - -algorithm - (i1,i2) := iMapping; - //print("convertToSccSimEqMapping1 accessing index " + intString(i2) + ".\n"); - tmpList := arrayGet(iSccMapping,i2); - tmpList := i1 :: tmpList; - oSccMapping := arrayUpdate(iSccMapping,i2,tmpList); - -end convertToSccSimEqMapping1; - -protected function convertToSimeqCompMapping "function convertToSimeqCompMapping - author: marcusw - Converts the given mapping (simEqIndex -> sccIndex) bases on tuples to an array mapping." - input list> iMapping; // - input Integer numOfSimEqs; - output array oMapping; //maps each simEq to the scc - -protected - array tmpMapping; - -algorithm - tmpMapping := arrayCreate(numOfSimEqs, -1); - oMapping := List.fold(iMapping, convertToSimeqCompMapping1, tmpMapping); -end convertToSimeqCompMapping; - -protected function convertToSimeqCompMapping1 "function convertToSimeqCompMapping1 - author: marcusw - Helper function for convertToSimeqCompMapping. It will update the array at the given index." - input tuple iSimEqTuple; // - input array iMapping; - output array oMapping; - -protected - Integer simEqIdx,sccIdx; - -algorithm - (simEqIdx,sccIdx) := iSimEqTuple; - //print("convertToSimeqCompMapping1 " + intString(simEqIdx) + " .. " + intString(sccIdx) + " iMapping_len: " + intString(arrayLength(iMapping)) + "\n"); - oMapping := arrayUpdate(iMapping,simEqIdx,sccIdx); -end convertToSimeqCompMapping1; - -protected function getSimEqIdxSimEqMapping "author: marcusw - Get a mapping from simEqIdx -> option(simEq)." - input list iAllEquations; - input Integer iSimEqSystemHighestIdx; - output array> oMapping; -protected - array> tmpMapping; -algorithm - tmpMapping := arrayCreate(iSimEqSystemHighestIdx, NONE()); - oMapping := List.fold(iAllEquations, getSimEqIdxSimEqMapping1, tmpMapping); -end getSimEqIdxSimEqMapping; - -protected function getSimEqIdxSimEqMapping1 "author: marcusw - Helper function that adds the index of the given equation to the mapping." - input SimCode.SimEqSystem iEquation; - input array> iMapping; - output array> oMapping; -protected - Integer simEqIdx; - array> tmpMapping; -algorithm - oMapping := matchcontinue(iEquation, iMapping) - case(_,_) - equation - (simEqIdx,_) = getIndexBySimCodeEq(iEquation); - tmpMapping = arrayUpdate(iMapping, simEqIdx, SOME(iEquation)); - then tmpMapping; - else - equation - (simEqIdx,_) = getIndexBySimCodeEq(iEquation); - //print("getSimEqIdxSimEqMapping1: Can't access idx " + intString(simEqIdx) + "\n"); - then iMapping; - end matchcontinue; -end getSimEqIdxSimEqMapping1; - -public function dumpSimEqSCCMapping "author: marcusw - Prints the given mapping out to the console." - input array iSccMapping; -protected - String text; -algorithm - text := "SimEqToSCCMapping"; - ((_,text)) := Array.fold(iSccMapping, dumpSimEqSCCMapping1, (1,text)); - print(text + "\n"); -end dumpSimEqSCCMapping; - -protected function dumpSimEqSCCMapping1 "author: marcusw - Helper function of dumpSimEqSCCMapping to print one mapping entry." - input Integer iMapping; - input tuple iIndexText; - output tuple oIndexText; -protected - Integer iIndex; - String text, iText; -algorithm - (iIndex,iText) := iIndexText; - text := intString(iMapping); - text := iText + "\nSimEq " + intString(iIndex) + ": {" + text + "}"; - oIndexText := (iIndex+1,text); -end dumpSimEqSCCMapping1; - -public function dumpSccSimEqMapping "function dumpSccSimEqMapping - author: marcusw - Prints the given mapping out to the console." - input array> iSccMapping; -protected - String text; -algorithm - text := "SccToSimEqMapping"; - ((_,text)) := Array.fold(iSccMapping, dumpSccSimEqMapping1, (1,text)); - print(text + "\n"); -end dumpSccSimEqMapping; - -protected function dumpSccSimEqMapping1 "function dumpSccSimEqMapping1 - author: marcusw - Helper function of dumpSccSimEqMapping to print one mapping list." - input list iMapping; - input tuple iIndexText; - output tuple oIndexText; -protected - Integer iIndex; - String text, iText; -algorithm - (iIndex,iText) := iIndexText; - text := List.fold(iMapping, dumpSccSimEqMapping2, " "); - text := iText + "\nSCC " + intString(iIndex) + ": {" + text + "}"; - oIndexText := (iIndex+1,text); -end dumpSccSimEqMapping1; - -protected function dumpSccSimEqMapping2 "function dumpSccSimEqMapping2 - author: marcusw - Helper function of dumpSccSimEqMapping1 to print one mapping element." - input Integer iIndex; - input String iText; - output String oText; - -algorithm - oText := iText + intString(iIndex) + " "; - -end dumpSccSimEqMapping2; - -protected function getSimCodeEqsByTaskList "author: marcusw - Get the simCode.SimEqSystem - objects references by the given tasks." - input list iTaskList; - input array> iSimEqIdxSimEqMapping; - output list oSimEqs; -protected - list> tmpSimEqs; -algorithm - tmpSimEqs := List.map1(iTaskList, getSimCodeEqsByTaskList0, iSimEqIdxSimEqMapping); - oSimEqs := List.flatten(tmpSimEqs); -end getSimCodeEqsByTaskList; - -protected function getSimCodeEqsByTaskList0 "author: marcusw - Get the simCode.SimEqSystem - objects references by the given task." - input HpcOmSimCode.Task iTask; - input array> iSimEqIdxSimEqMapping; - output list oSimEqs; -protected - list eqIdc; - list tmpSimEqs; -algorithm - oSimEqs := match(iTask, iSimEqIdxSimEqMapping) - case(HpcOmSimCode.CALCTASK(eqIdc=eqIdc),_) - equation - tmpSimEqs = List.map1r(eqIdc, getSimCodeEqByIndexAndMapping, iSimEqIdxSimEqMapping); - then tmpSimEqs; - case(HpcOmSimCode.CALCTASK_LEVEL(eqIdc=eqIdc),_) - equation - tmpSimEqs = List.map1r(eqIdc, getSimCodeEqByIndexAndMapping, iSimEqIdxSimEqMapping); - then tmpSimEqs; - else {}; - end match; -end getSimCodeEqsByTaskList0; - -public function getSimCodeEqByIndexAndMapping "author: marcusw - Returns the SimEqSystem which has the given Index." - input array> iSimEqIdxSimEqMapping; //All SimEqSystems - input Integer iIdx; //The index of the required system - output SimCode.SimEqSystem oSimEqSystem; -protected - Option tmpSimEqSystem; -algorithm - tmpSimEqSystem := arrayGet(iSimEqIdxSimEqMapping, iIdx); - oSimEqSystem := getSimCodeEqByIndexAndMapping1(tmpSimEqSystem, iIdx); -end getSimCodeEqByIndexAndMapping; - -protected function getSimCodeEqByIndexAndMapping1 "author: marcusw - Returns the SimEqSystem if it's not NONE()." - input Option iSimEqSystem; - input Integer iIdx; - output SimCode.SimEqSystem oSimEqSystem; -protected - SimCode.SimEqSystem tmpSys; -algorithm - oSimEqSystem := match(iSimEqSystem,iIdx) - case(SOME(tmpSys),_) - then tmpSys; - else - equation - print("getSimCodeEqByIndexAndMapping1 failed. Looking for Index " + intString(iIdx) + "\n"); - //print(" -- available indices: " + stringDelimitList(List.map(List.map(iEqs,getIndexBySimCodeEq), intString), ",") + "\n"); - then fail(); - end match; -end getSimCodeEqByIndexAndMapping1; - -public function getSimCodeEqByIndex "function getSimCodeEqByIndex - author: marcusw - Returns the SimEqSystem which has the given Index. This method is called from susan." - input list iEqs; //All SimEqSystems - input Integer iIdx; //The index of the required system - output SimCode.SimEqSystem oEq; - -protected - list rest; - SimCode.SimEqSystem head; - Integer headIdx,headIdx2; - -algorithm - oEq := matchcontinue(iEqs,iIdx) - case(head::rest,_) - equation - (headIdx,headIdx2) = getIndexBySimCodeEq(head); - //print("getSimCodeEqByIndex listLength: " + intString(listLength(iEqs)) + " head idx: " + intString(headIdx) + "\n"); - true = intEq(headIdx,iIdx) or intEq(headIdx2,iIdx); - then head; - case(head::rest,_) then getSimCodeEqByIndex(rest,iIdx); - else - equation - print("getSimCodeEqByIndex failed. Looking for Index " + intString(iIdx) + "\n"); - //print(" -- available indices: " + stringDelimitList(List.map(List.map(iEqs,getIndexBySimCodeEq), intString), ",") + "\n"); - then fail(); - end matchcontinue; -end getSimCodeEqByIndex; - -protected function getIndexBySimCodeEq "function getIndexBySimCodeEq - author: marcusw - Just a small helper function to get the index of a SimEqSystem." - input SimCode.SimEqSystem iEq; - output Integer oIdx; - output Integer oIdx2; - -protected - Integer index,index2; - -algorithm - (oIdx,oIdx2) := match(iEq) - case(SimCode.SES_RESIDUAL(index=index)) then (index,0); - case(SimCode.SES_SIMPLE_ASSIGN(index=index)) then (index,0); - case(SimCode.SES_ARRAY_CALL_ASSIGN(index=index)) then (index,0); - case(SimCode.SES_IFEQUATION(index=index)) then (index,0); - case(SimCode.SES_ALGORITHM(index=index)) then (index,0); - // no dynamic tearing - case(SimCode.SES_LINEAR(SimCode.LINEARSYSTEM(index=index), NONE())) then (index,0); - case(SimCode.SES_NONLINEAR(SimCode.NONLINEARSYSTEM(index=index), NONE())) then (index,0); - // dynamic tearing - case(SimCode.SES_LINEAR(SimCode.LINEARSYSTEM(index=index), SOME(SimCode.LINEARSYSTEM(index=index2)))) then (index,index2); - case(SimCode.SES_NONLINEAR(SimCode.NONLINEARSYSTEM(index=index), SOME(SimCode.NONLINEARSYSTEM(index=index2)))) then (index,index2); - case(SimCode.SES_MIXED(index=index)) then (index,0); - case(SimCode.SES_WHEN(index=index)) then (index,0); - else fail(); - end match; -end getIndexBySimCodeEq; - -protected function findHighestSccIdxInMapping "function findHighestSccIdxInMapping - author: marcusw - Find the highest scc-index in the mapping list." - input list> iEquationSccMapping; // - input Integer iHighestIndex; - output Integer oIndex; - -protected - Integer eqIdx, sccIdx; - list> rest; -algorithm - oIndex := matchcontinue(iEquationSccMapping,iHighestIndex) - case((eqIdx,sccIdx)::rest,_) - equation - true = intGt(sccIdx,iHighestIndex); - then findHighestSccIdxInMapping(rest,sccIdx); - case((eqIdx,sccIdx)::rest,_) - then findHighestSccIdxInMapping(rest,iHighestIndex); - else iHighestIndex; - end matchcontinue; -end findHighestSccIdxInMapping; - -protected function removeDummyStateFromMapping "function removeDummyStateFromMapping - author: marcusw - Removes all mappings with sccIdx=1 from the list and decrements all other scc-indices by 1." - input list> iEquationSccMapping; - output list> oEquationSccMapping; -algorithm - oEquationSccMapping := List.fold(iEquationSccMapping, removeDummyStateFromMapping1, {}); -end removeDummyStateFromMapping; - -protected function removeDummyStateFromMapping1 "function removeDummyStateFromMapping1 - author: marcusw - Helper function of removeDummyStateFromMapping. Handles one list-element." - input tuple iTuple; // - input list> iNewList; - output list> oNewList; -protected - Integer eqIdx,sccIdx; - tuple newElem; -algorithm - oNewList := matchcontinue(iTuple,iNewList) - case((eqIdx,sccIdx),_) - equation - true = intEq(sccIdx,1); - then iNewList; - case((eqIdx,sccIdx),_) - equation - newElem = (eqIdx,sccIdx-1); - then newElem::iNewList; - else - equation - print("removeDummyStateFromMapping1 failed\n"); - then iNewList; - end matchcontinue; -end removeDummyStateFromMapping1; - // test functions //------------------------------------------ //------------------------------------------ diff --git a/Compiler/Template/SimCodeBackendTV.mo b/Compiler/Template/SimCodeBackendTV.mo index 39d75c2110d..04f813548b4 100644 --- a/Compiler/Template/SimCodeBackendTV.mo +++ b/Compiler/Template/SimCodeBackendTV.mo @@ -101,14 +101,13 @@ package HpcOmScheduler end convertFixedLevelScheduleToLevelThreadLists; end HpcOmScheduler; -package HpcOmSimCodeMain +package HpcOmTaskGraph function getSimCodeEqByIndex input list iEqs; input Integer iIdx; output SimCode.SimEqSystem oEq; end getSimCodeEqByIndex; -end HpcOmSimCodeMain; - +end HpcOmTaskGraph; package FMI uniontype Info