Skip to content

Commit

Permalink
- Added some methods to create and handle parallel informations to Hp…
Browse files Browse the repository at this point in the history
…cOmSimCode.mo

- Extended the SimCode-Structure with simple parallel informations for HpcOm
- An error message is now shown if the profiling-xml file is missing and the hpcom-flag is set
- OpenMP based code-generation of HpcOm is now part of the Susan-C-Codegen
- Added a new clock to the rtclock-structure. It is now possible to measure the time in cpu-cycles with RDTSC under Windows

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16698 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Marcus Walther committed Jul 31, 2013
1 parent 336ab4b commit 533b589
Show file tree
Hide file tree
Showing 14 changed files with 695 additions and 331 deletions.
6 changes: 4 additions & 2 deletions Compiler/BackEnd/BackendQSS.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1015,11 +1015,13 @@ algorithm
list<SimCode.JacobianMatrix> jacobianMatrixes;
list<SimCode.SimEqSystem> eqs;
BackendDAE.SampleLookup sampleLookup;
Option<SimCode.HpcOmParInformation> hpcOmParInformationOpt;

case (SimCode.SIMCODE(modelInfo,literals,recordDecls,externalFunctionIncludes,allEquations,odeEquations,
algebraicEquations,residualEquations,useSymbolicInitialization,useHomotopy,initialEquations,startValueEquations,
parameterEquations,inlineEquations,removedEquations,algorithmAndEquationAsserts,stateSets,constraints,classAttributes,zeroCrossings,relations,
sampleLookup,whenClauses,discreteModelVars,extObjInfo,makefileParams,
delayedExps,jacobianMatrixes,simulationSettingsOpt,fileNamePrefix,crefToSimVarHT),_)
delayedExps,jacobianMatrixes,simulationSettingsOpt,fileNamePrefix,crefToSimVarHT,hpcOmParInformationOpt),_)
equation
{eqs} = odeEquations;
eqs = List.map1(eqs,replaceZC,zc_exps);
Expand All @@ -1030,7 +1032,7 @@ algorithm
zeroCrossings, relations, sampleLookup,
whenClauses, discreteModelVars, extObjInfo,
makefileParams, delayedExps, jacobianMatrixes,
simulationSettingsOpt, fileNamePrefix, crefToSimVarHT);
simulationSettingsOpt, fileNamePrefix, crefToSimVarHT,hpcOmParInformationOpt);

end match;
end replaceDiscontsInOde;
Expand Down
383 changes: 264 additions & 119 deletions Compiler/BackEnd/HpcOmSimCode.mo

Large diffs are not rendered by default.

29 changes: 21 additions & 8 deletions Compiler/BackEnd/HpcOmTaskGraph.mo
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public uniontype TaskGraphMeta // stores all the metadata for the TaskGraph
array<String> nodeDescs; // a description of the nodes for the graphml generation
array<tuple<Integer,Real>> exeCosts; // the execution cost for the nodes <numberOfOperations, requiredCycles
array<list<tuple<Integer,Integer,Integer>>> commCosts; // the communication cost tuple(_,numberOfVars,requiredCycles) for an edge from array[parentNode] to tuple(childNode,_)
array<Integer> nodeMark; // put some additional stuff in here
array<Integer> nodeMark; // put some additional stuff in here -> this is currently not a nodeMark, its a componentMark
end TASKGRAPHMETA;
end TaskGraphMeta;

Expand Down Expand Up @@ -2781,7 +2781,7 @@ algorithm
equation
true = arrayLength(inComps)>= compIdx;
compRow = arrayGet(inComps,compIdx);
print("component "+&intString(compIdx)+&" includes: "+&stringDelimitList(List.map(compRow,intString),", ")+&"\n");
print("node "+&intString(compIdx)+&" includes: "+&stringDelimitList(List.map(compRow,intString),", ")+&"\n");
printInComps(inComps,compIdx+1);
then
();
Expand Down Expand Up @@ -3586,14 +3586,27 @@ protected function convertSimEqToSccCosts1

protected
Integer simEqCalcCount, simEqIdx;
Real simEqCalcTime;
Real simEqCalcTime, realSimEqCalcCount;
array<Real> reqTime;

algorithm
(simEqCalcCount, simEqCalcTime) := iReqTimeOpSimCode;
(simEqIdx,reqTime) := iReqTimeOp;
reqTime := convertSimEqToSccCosts2(reqTime, simEqCalcTime, simEqIdx, iSimEqSccMapping);
oReqTimeOp := (simEqIdx+1,reqTime);
oReqTimeOp := matchcontinue(iReqTimeOpSimCode,iSimEqSccMapping,iReqTimeOp)
case(_,_,_)
equation
(simEqCalcCount, simEqCalcTime) = iReqTimeOpSimCode;
(simEqIdx,reqTime) = iReqTimeOp;
realSimEqCalcCount = intReal(simEqCalcCount);
true = realNe(realSimEqCalcCount,0.0);
reqTime = convertSimEqToSccCosts2(reqTime, realDiv(simEqCalcTime,realSimEqCalcCount), simEqIdx, iSimEqSccMapping);
then ((simEqIdx+1,reqTime));
else
equation
(simEqCalcCount, simEqCalcTime) = iReqTimeOpSimCode;
(simEqIdx,reqTime) = iReqTimeOp;
realSimEqCalcCount = intReal(simEqCalcCount);
reqTime = convertSimEqToSccCosts2(reqTime, 0.0, simEqIdx, iSimEqSccMapping);
then ((simEqIdx+1,reqTime));
end matchcontinue;
end convertSimEqToSccCosts1;

protected function convertSimEqToSccCosts2
Expand Down Expand Up @@ -3720,7 +3733,7 @@ algorithm

//print("Component: ");
//BackendDump.dumpComponent(comp);
//print("Operations: ");
//print("Cost: " +& realString(iCosts_cyc) +& "\n");
//print(tuple3ToString((costAdd,costMul,costTrig)));
//print("\n");
oCosts := (costAdd+costMul+costTrig + iCosts_op, realAdd(iCosts_cyc,reqTime));
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/ResidualCmp.mo
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ algorithm
makefileParams = SimCode.MAKEFILE_PARAMS("","","","","","","","","",includeDirs,libs,"");
delayexp = SimCode.DELAYED_EXPRESSIONS({},0);
hashTable = SimCodeUtil.emptyHashTable();
rescmp = SimCode.SIMCODE(modelInfo,{},recordDecls,externalFunctionIncludes,{},{},{},allEquations,false,false,allInitEquations,{},{},{},{},{},{},{},{},{},{},BackendDAE.SAMPLE_LOOKUP(0,{}),{},{},extObjInfo,makefileParams,delayexp,{},NONE(),filenamePrefix,hashTable);
rescmp = SimCode.SIMCODE(modelInfo,{},recordDecls,externalFunctionIncludes,{},{},{},allEquations,false,false,allInitEquations,{},{},{},{},{},{},{},{},{},{},BackendDAE.SAMPLE_LOOKUP(0,{}),{},{},extObjInfo,makefileParams,delayexp,{},NONE(),filenamePrefix,hashTable,NONE());
then
rescmp;
else
Expand Down
7 changes: 6 additions & 1 deletion Compiler/BackEnd/SimCode.mo
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ uniontype SimCode
list<JacobianMatrix> jacobianMatrixes;
Option<SimulationSettings> simulationSettingsOpt;
String fileNamePrefix;

//*** a protected section *** not exported to SimCodeTV
HashTableCrefToSimVar crefToSimVarHT "hidden from typeview - used by cref2simvar() for cref -> SIMVAR lookup available in templates.";
Option<HpcOmParInformation> hpcOmParInformationOpt;
end SIMCODE;
end SimCode;

Expand Down Expand Up @@ -594,4 +594,9 @@ efficient manner"
end ValueArray;


public uniontype HpcOmParInformation "informations for the taskgraph parallelization"
record HPCOMPARINFORMATION
list<list<Integer>> eqsOfLevels; //The equations of the level <%listindex%>
end HPCOMPARINFORMATION;
end HpcOmParInformation;
end SimCode;
22 changes: 13 additions & 9 deletions Compiler/BackEnd/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,8 @@ algorithm
SymbolicJacs,
simSettingsOpt,
filenamePrefix,
crefToSimVarHT);
crefToSimVarHT,
NONE());
(simCode, (_, _, lits)) = traverseExpsSimCode(simCode, findLiteralsHelper, literals);
simCode = setSimCodeLiterals(simCode, listReverse(lits));
// print("*** SimCode -> collect all files started: " +& realString(clock()) +& "\n");
Expand Down Expand Up @@ -5375,7 +5376,7 @@ algorithm
omhome := Settings.getInstallationDirectoryPath();
omhome := System.trim(omhome, "\""); // Remove any quotation marks from omhome.
cflags := System.getCFlags();
cflags := Debug.bcallret2(Flags.isSet(Flags.OPENMP), stringAppend, cflags, " -fopenmp", cflags);
cflags := Debug.bcallret2(Flags.isSet(Flags.OPENMP) or Flags.isSet(Flags.HPCOM), stringAppend, cflags, " -fopenmp", cflags);
ldflags := System.getLDFlags();
rtlibs := System.getRTLibs();
platform := System.modelicaPlatform();
Expand Down Expand Up @@ -11757,6 +11758,7 @@ algorithm
SimCode.SimVars vars;
list<SimCode.Function> functions;
SimCode.Files files "all the files from Absyn.Info and DAE.ELementSource";
Option<SimCode.HpcOmParInformation> hpcOmParInformationOpt;

case _
equation
Expand All @@ -11765,7 +11767,7 @@ algorithm

case SimCode.SIMCODE(modelInfo, literals, recordDecls, externalFunctionIncludes, allEquations, odeEquations, algebraicEquations, residualEquations, useSymbolicInitialization, useHomotopy, initialEquations, startValueEquations,
parameterEquations, inlineEquations, removedEquations, algorithmAndEquationAsserts, stateSets, constraints, classAttributes, zeroCrossings, relations, sampleLookup, whenClauses,
discreteModelVars, extObjInfo, makefileParams, delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT)
discreteModelVars, extObjInfo, makefileParams, delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT, hpcOmParInformationOpt)
equation
SimCode.MODELINFO(name, directory, varInfo, vars, functions, labels) = modelInfo;
files = {};
Expand All @@ -11782,7 +11784,7 @@ algorithm
then
SimCode.SIMCODE(modelInfo, literals, recordDecls, externalFunctionIncludes, allEquations, odeEquations, algebraicEquations, residualEquations, useSymbolicInitialization, useHomotopy, initialEquations, startValueEquations,
parameterEquations, inlineEquations, removedEquations, algorithmAndEquationAsserts, stateSets, constraints, classAttributes, zeroCrossings, relations, sampleLookup, whenClauses,
discreteModelVars, extObjInfo, makefileParams, delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT);
discreteModelVars, extObjInfo, makefileParams, delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT, hpcOmParInformationOpt);

case _
equation
Expand Down Expand Up @@ -12063,6 +12065,7 @@ algorithm
// *** a protected section *** not exported to SimCodeTV
SimCode.HashTableCrefToSimVar crefToSimVarHT "hidden from typeview - used by cref2simvar() for cref -> SIMVAR lookup available in templates.";
A a;
Option<SimCode.HpcOmParInformation> hpcOmParInformationOpt;

case (SimCode.SIMCODE(modelInfo, literals, recordDecls, externalFunctionIncludes,
allEquations, odeEquations, algebraicEquations, residualEquations,
Expand All @@ -12071,7 +12074,7 @@ algorithm
constraints, classAttributes, zeroCrossings, relations, sampleLookup,
whenClauses, discreteModelVars, extObjInfo, makefileParams,
delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix,
crefToSimVarHT), _, a)
crefToSimVarHT, hpcOmParInformationOpt), _, a)
equation
(literals, a) = List.mapFoldTuple(literals, func, a);
(allEquations, a) = traverseExpsEqSystems(allEquations, func, a, {});
Expand All @@ -12096,7 +12099,7 @@ algorithm
constraints, classAttributes, zeroCrossings, relations, sampleLookup,
whenClauses, discreteModelVars, extObjInfo, makefileParams,
delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix,
crefToSimVarHT), a);
crefToSimVarHT, hpcOmParInformationOpt), a);
end match;
end traverseExpsSimCode;

Expand Down Expand Up @@ -12264,21 +12267,22 @@ algorithm
String fileNamePrefix;
// *** a protected section *** not exported to SimCodeTV
SimCode.HashTableCrefToSimVar crefToSimVarHT "hidden from typeview - used by cref2simvar() for cref -> SIMVAR lookup available in templates.";

Option<SimCode.HpcOmParInformation> hpcOmParInformationOpt;

case (SimCode.SIMCODE(modelInfo, _, recordDecls, externalFunctionIncludes,
allEquations, odeEquations, algebraicEquations, residualEquations,
useSymbolicInitialization, useHomotopy, initialEquations, startValueEquations,
parameterEquations, inlineEquations, removedEquations, algorithmAndEquationAsserts, stateSets,
constraints, classAttributes, zeroCrossings, relations, sampleLookup,
whenClauses, discreteModelVars, extObjInfo, makefileParams,
delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT), _)
delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT, hpcOmParInformationOpt), _)
then SimCode.SIMCODE(modelInfo, literals, recordDecls, externalFunctionIncludes,
allEquations, odeEquations, algebraicEquations, residualEquations,
useSymbolicInitialization, useHomotopy, initialEquations, startValueEquations,
parameterEquations, inlineEquations, removedEquations, algorithmAndEquationAsserts, stateSets,
constraints, classAttributes, zeroCrossings, relations, sampleLookup,
whenClauses, discreteModelVars, extObjInfo, makefileParams,
delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT);
delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT, hpcOmParInformationOpt);
end match;
end setSimCodeLiterals;

Expand Down
145 changes: 136 additions & 9 deletions Compiler/Template/CodegenC.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,26 @@ template simulationFile(SimCode simCode, String guid)
int measure_time_flag = 0;
#endif
<%if Flags.isSet(HPCOM) then "#define HPCOM"%>
#ifdef HPCOM
#include <omp.h>
#endif
#ifdef _OPENMP
#include <omp.h>
#include <omp_perform_simulation.c>
#ifndef HPCOM
#include <omp.h>
#include <omp_perform_simulation.c>
#else
#include <perform_simulation.c>
#endif
#else
#include <perform_simulation.c>
#define omp_get_max_threads() 1
#ifndef HPCOM
#include <perform_simulation.c>
#define omp_get_max_threads() 1
#else
#include <perform_simulation.c>
#endif
#endif
Expand Down Expand Up @@ -198,7 +211,7 @@ template simulationFile(SimCode simCode, String guid)
<%functionDAE(allEquations, whenClauses)%>
<%functionODE(odeEquations,(match simulationSettingsOpt case SOME(settings as SIMULATION_SETTINGS(__)) then settings.method else ""))%>
<%functionODE(odeEquations,(match simulationSettingsOpt case SOME(settings as SIMULATION_SETTINGS(__)) then settings.method else ""),hpcOmParInformationOpt)%>
<%functionAlgebraic(algebraicEquations)%>
Expand Down Expand Up @@ -1523,6 +1536,119 @@ end functionWhenReinitStatementThen;
// >>
//end functionWhenReinitStatementElse;

//------------------------------------
// Begin: Modified functions for HpcOm
//------------------------------------

template functionXXX_systems_HPCOM(list<list<SimEqSystem>> eqs, String name, Text &loop, Text &varDecls, Option<HpcOmParInformation> hpcOmParInformationOpt)
::=
match hpcOmParInformationOpt
case SOME(info as HPCOMPARINFORMATION(__)) then
let funcs = (eqs |> eq hasindex i0 fromindex 0 => functionXXX_system_HPCOM(eq,name,i0,info.eqsOfLevels) ; separator="\n")
match listLength(eqs)
case 0 then //empty case
let &loop +=
<<
/* no <%name%> systems */
>>
""
case 1 then //1 function
let &loop +=
<<
function<%name%>_system0(data);
>>
funcs //just the one function
case nFuncs then //2 and more
let funcNames = eqs |> e hasindex i0 fromindex 0 => 'function<%name%>_system<%i0%>' ; separator=",\n"
let head = if Flags.isSet(Flags.OPENMP) then '#pragma omp parallel for private(id) schedule(<%match noProc() case 0 then "dynamic" else "static"%>)'
let &varDecls += 'int id;<%\n%>'

let &loop +=
/* Text for the loop body that calls the equations */
<<
<%head%>
for(id=0; id<<%nFuncs%>; id++) {
function<%name%>_systems[id](data);
}
>>
/* Text before the function head */
<<
<%funcs%>
static void (*function<%name%>_systems[<%nFuncs%>])(DATA *) = {
<%funcNames%>
};
>>
else (error(sourceInfo(), ' MISSING PARALLEL INFORMATIONS FOR HPCOM '))
end functionXXX_systems_HPCOM;

template functionXXX_system_HPCOM(list<SimEqSystem> derivativEquations, String name, Integer n, list<list<Integer>> eqsOfLevel)
::=
let odeEqs = eqsOfLevel |> eqs => functionXXX_system0_HPCOM(derivativEquations,name,eqs); separator="\n"
<<
static void function<%name%>_system<%n%>(DATA *data)
{
state mem_state;
mem_state = get_memory_state();
<%odeEqs%>
restore_memory_state(mem_state);
}
>>
end functionXXX_system_HPCOM;

template functionXXX_system0_HPCOM(list<SimEqSystem> derivativEquations, String name, list<Integer> eqsOfLevel)
::=
//let odeEqs = "#pragma omp parallel sections\n{"
let odeEqs = eqsOfLevel |> eq => equationNamesHPCOM_(eq,derivativEquations,contextSimulationNonDiscrete); separator="\n"
<<
#pragma omp parallel sections
{
<%odeEqs%>
}
>>
end functionXXX_system0_HPCOM;
template equationNamesHPCOM_(Integer idx, list<SimEqSystem> derivativEquations, Context context)
"Generates an equation.
This template should not be used for a SES_RESIDUAL.
Residual equations are handled differently."
::=
match context
case SIMULATION(genDiscrete=true) then
match getSimCodeEqByIndex(derivativEquations, idx)
case e as SES_ALGORITHM(statements={})
then ""
else
let ix = equationIndex(getSimCodeEqByIndex(derivativEquations, idx))
<<
#ifdef _OMC_MEASURE_TIME
SIM_PROF_TICK_EQEXT(<%ix%>);
#endif
eqFunction_<%ix%>(data);
#ifdef _OMC_MEASURE_TIME
SIM_PROF_ACC_EQEXT(<%ix%>);
#endif
>>
else
match getSimCodeEqByIndex(derivativEquations, idx)
case e as SES_ALGORITHM(statements={})
then ""
else
let ix = equationIndex(getSimCodeEqByIndex(derivativEquations, idx))
<<
#ifdef _OMC_MEASURE_TIME
SIM_PROF_TICK_EQEXT(<%ix%>);
#endif
eqFunction_<%ix%>(data);
#ifdef _OMC_MEASURE_TIME
SIM_PROF_ACC_EQEXT(<%ix%>);
#endif
>>
end equationNamesHPCOM_;
//----------------------------------
// End: Modified functions for HpcOm
//----------------------------------
template functionXXX_system(list<SimEqSystem> derivativEquations, String name, Integer n)
::=
let odeEqs = derivativEquations |> eq => equationNames_(eq,contextSimulationNonDiscrete); separator="\n"
Expand All @@ -1539,7 +1665,7 @@ end functionXXX_system;
template functionXXX_systems(list<list<SimEqSystem>> eqs, String name, Text &loop, Text &varDecls)
::=
let funcs = eqs |> eq hasindex i0 fromindex 0 => functionXXX_system(eq,name,i0) ; separator="\n"
let funcs = (eqs |> eq hasindex i0 fromindex 0 => functionXXX_system(eq,name,i0) ; separator="\n")
match listLength(eqs)
case 0 then //empty case
let &loop +=
Expand Down Expand Up @@ -1575,14 +1701,15 @@ template functionXXX_systems(list<list<SimEqSystem>> eqs, String name, Text &loo
>>
end functionXXX_systems;
template functionODE(list<list<SimEqSystem>> derivativEquations, Text method)
template functionODE(list<list<SimEqSystem>> derivativEquations, Text method, Option<HpcOmParInformation> hpcOmParInformationOpt)
"Generates function in simulation file."
::=
let () = System.tmpTickReset(0)
let &varDecls2 = buffer "" /*BUFD*/
let &varDecls = buffer ""
let &loop = buffer ""
let systems = functionXXX_systems(derivativEquations, "ODE", &loop, &varDecls)
let systems = if Flags.isSet(Flags.HPCOM) then (functionXXX_systems_HPCOM(derivativEquations, "ODE", &loop, &varDecls, hpcOmParInformationOpt)) else (functionXXX_systems(derivativEquations, "ODE", &loop, &varDecls))
/* let systems = functionXXX_systems(derivativEquations, "ODE", &loop, &varDecls) */
let &tmp = buffer ""
let stateContPartInline = (derivativEquations |> eqs => (eqs |> eq =>
equation_(eq, contextInlineSolver, &varDecls2 /*BUFC*/, &tmp); separator="\n")
Expand Down

0 comments on commit 533b589

Please sign in to comment.