Skip to content

Commit

Permalink
- fixed some compiler warnings of the cpp-runtime build with gcc
Browse files Browse the repository at this point in the history
- added HPCOM_MEMORY_OPT-Flag to use experimental memory optimization
- some simple optimizations for the cpp-runtime
- papi-support to hpcom-codegen added

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@21261 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Marcus Walther committed Jun 24, 2014
1 parent f6c5956 commit 4593c52
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 48 deletions.
22 changes: 11 additions & 11 deletions Compiler/BackEnd/HpcOmMemory.mo
Expand Up @@ -128,30 +128,30 @@ encapsulated package HpcOmMemory
oMemoryMap := matchcontinue(iModelInfo, iTaskGraph, iTaskGraphMeta, iEqSystems, iFileNamePrefix, iSchedulerInfo, iSchedule, iSccSimEqMapping, iCriticalPaths, iCriticalPathsWoC, iCriticalPathInfo, iAllComponents)
case(_,_,_,_,_,_,_,_,_,_,_,_)
equation
true = Flags.isSet(Flags.HPCOM_ANALYZATION_MODE);
HpcOmTaskGraph.printTaskGraphMeta(iTaskGraphMeta);
true = Flags.isSet(Flags.HPCOM_MEMORY_OPT);
//HpcOmTaskGraph.printTaskGraphMeta(iTaskGraphMeta);
//Create var hash table
SimCode.MODELINFO(vars=simCodeVars) = iModelInfo;
SimCode.SIMVARS(stateVars=stateVars, derivativeVars=derivativeVars, algVars=algVars, paramVars=paramVars) = simCodeVars;
allVarsMapping = SimCodeUtil.createIdxSCVarMapping(simCodeVars);
SimCodeUtil.dumpIdxScVarMapping(allVarsMapping);
print("--------------------------------\n");
//SimCodeUtil.dumpIdxScVarMapping(allVarsMapping);
//print("--------------------------------\n");
hashTable = HashTableCrILst.emptyHashTableSized(BaseHashTable.biggerBucketSize);
hashTable = fillSimVarHashTable(stateVars,0,0,hashTable);
hashTable = fillSimVarHashTable(derivativeVars,listLength(stateVars),0,hashTable);
hashTable = fillSimVarHashTable(algVars,listLength(stateVars) + listLength(stateVars),0,hashTable);
//hashTable = fillSimVarHashTable(paramVars,listLength(stateVars)*2 + listLength(algVars),0,hashTable);
print("-------------------------------------\n");
BaseHashTable.dumpHashTable(hashTable);
//print("-------------------------------------\n");
//BaseHashTable.dumpHashTable(hashTable);
//Create CacheMap
sccNodeMapping = HpcOmTaskGraph.getSccNodeMapping(arrayLength(iSccSimEqMapping), iTaskGraphMeta);
printSccNodeMapping(sccNodeMapping);
//printSccNodeMapping(sccNodeMapping);
scVarTaskMapping = getSimCodeVarNodeMapping(iTaskGraphMeta,iEqSystems,listLength(stateVars)*2+listLength(algVars),sccNodeMapping,hashTable);
printScVarTaskMapping(scVarTaskMapping);
print("-------------------------------------\n");
//printScVarTaskMapping(scVarTaskMapping);
//print("-------------------------------------\n");
nodeSimCodeVarMapping = getNodeSimCodeVarMapping(iTaskGraphMeta, iEqSystems, hashTable);
printNodeSimCodeVarMapping(nodeSimCodeVarMapping);
print("-------------------------------------\n");
//printNodeSimCodeVarMapping(nodeSimCodeVarMapping);
//print("-------------------------------------\n");
(cacheMap,scVarCLMapping,numCL) = createCacheMapOptimized(allVarsMapping,stateVars,derivativeVars,algVars,paramVars,scVarTaskMapping,64,iAllComponents,iSchedule, nodeSimCodeVarMapping);
eqSimCodeVarMapping = getEqSCVarMapping(iEqSystems,hashTable);
(clTaskMapping,scVarTaskMapping) = getCacheLineTaskMapping(iTaskGraphMeta,iEqSystems,hashTable,numCL,scVarCLMapping);
Expand Down
20 changes: 16 additions & 4 deletions Compiler/BackEnd/HpcOmSimCodeMain.mo
Expand Up @@ -251,6 +251,7 @@ algorithm

fileName = ("taskGraph"+&filenamePrefix+&"ODE_merged.graphml");
HpcOmTaskGraph.dumpAsGraphMLSccLevel(taskGraphSimplified, taskGraphDataSimplified, inBackendDAE, fileName, criticalPathInfo, HpcOmTaskGraph.convertNodeListToEdgeTuples(List.first(criticalPaths)), HpcOmTaskGraph.convertNodeListToEdgeTuples(List.first(criticalPathsWoC)), sccSimEqMapping, schedulerInfo);
Debug.execStat("hpcom dump simplified TaskGraph", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);

//Create schedule
//---------------
Expand All @@ -271,18 +272,24 @@ algorithm
SimCode.SIMCODE(modelInfo, simCodeLiterals, simCodeRecordDecls, simCodeExternalFunctionIncludes, allEquations, odeEquations, algebraicEquations, residualEquations, useSymbolicInitialization, useHomotopy, initialEquations, removedInitialEquations, startValueEquations, nominalValueEquations, minValueEquations, maxValueEquations,
parameterEquations, removedEquations, algorithmAndEquationAsserts, zeroCrossingsEquations, jacobianEquations, stateSets, constraints, classAttributes, zeroCrossings, relations, timeEvents, whenClauses,
discreteModelVars, extObjInfo, makefileParams, delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, _, _, crefToSimVarHT, backendMapping) = simCode;


//Check ODE-System size
//---------------------
System.realtimeTick(GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
checkOdeSystemSize(taskGraphOde,odeEquations);
Debug.execStat("hpcom check ODE system size", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);


//Create Memory-Map and Sim-Code
//------------------------------
optTmpMemoryMap = HpcOmMemory.createMemoryMap(modelInfo, taskGraphSimplified, taskGraphDataSimplified, eqs, filenamePrefix, schedulerInfo, schedule, sccSimEqMapping, criticalPaths, criticalPathsWoC, criticalPathInfo, allComps);

Debug.execStat("hpcom create memory map", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);

simCode = SimCode.SIMCODE(modelInfo, simCodeLiterals, simCodeRecordDecls, simCodeExternalFunctionIncludes, allEquations, odeEquations, algebraicEquations, residualEquations, useSymbolicInitialization, useHomotopy, initialEquations, removedInitialEquations, startValueEquations, nominalValueEquations, minValueEquations, maxValueEquations,
parameterEquations, removedEquations, algorithmAndEquationAsserts, zeroCrossingsEquations, jacobianEquations, stateSets, constraints, classAttributes, zeroCrossings, relations, timeEvents, whenClauses,
discreteModelVars, extObjInfo, makefileParams, delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, SOME(schedule), optTmpMemoryMap, crefToSimVarHT, backendMapping);

//evaluateCacheBehaviour(schedulerInfo,taskGraphDataSimplified,clTaskMapping, transposeCacheLineTaskMapping(clTaskMapping, arrayLength(taskGraphSimplified)));

Debug.execStat("hpcom other", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
print("HpcOm is still under construction.\n");
then simCode;
else equation
Expand Down Expand Up @@ -374,6 +381,11 @@ protected
array<tuple<Integer,Integer>> schedulerInfo;
algorithm
(oTaskGraph,oTaskGraphMeta) := matchcontinue(iTaskGraph,iTaskGraphMeta,inBackendDAE,iApplyFilters)
//case(_,_,_,_)
// equation
// flagValue = Flags.getConfigString(Flags.HPCOM_SCHEDULER);
// true = stringEq(flagValue, "level");
// then (iTaskGraph, iTaskGraphMeta);
case(_,_,_,true)
equation
//Merge simple and parent nodes
Expand Down
11 changes: 5 additions & 6 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -53,7 +53,7 @@ template simulationHeaderFile(SimCode simCode)
match simCode
case SIMCODE(__) then
<<
<%generateHeaderInlcudeString(simCode)%>
<%generateHeaderIncludeString(simCode)%>
<%generateClassDeclarationCode(simCode)%>


Expand Down Expand Up @@ -3697,7 +3697,7 @@ template writeoutput4(Integer index, Integer myindex2)
>>
end writeoutput4;
template generateHeaderInlcudeString(SimCode simCode)
template generateHeaderIncludeString(SimCode simCode)
"Generates header part of simulation file."
::=
match simCode
Expand All @@ -3718,7 +3718,7 @@ case SIMCODE(modelInfo=MODELINFO(__), extObjInfo=EXTOBJINFO(__)) then
*
*****************************************************************************/
>>
end generateHeaderInlcudeString;
end generateHeaderIncludeString;
Expand Down Expand Up @@ -10066,16 +10066,15 @@ template checkForDiscreteEvents(list<ComponentRef> discreteModelVars,SimCode sim
::=

let changediscreteVars = (discreteModelVars |> var => match var case CREF_QUAL(__) case CREF_IDENT(__) then
'if (_event_handling.changeDiscreteVar(<%cref(var)%>,"<%cref(var)%>")) { restart=true; }'
'if (_event_handling.changeDiscreteVar(<%cref(var)%>,"<%cref(var)%>")) { return true; }'
;separator="\n")
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
bool <%lastIdentOfPath(modelInfo.name)%>::checkForDiscreteEvents()
{
bool restart = false;
<%changediscreteVars%>
return restart;
return false;
}
>>
end checkForDiscreteEvents;
Expand Down
74 changes: 67 additions & 7 deletions Compiler/Template/CodegenCppHpcom.tpl
Expand Up @@ -58,7 +58,7 @@ template simulationHeaderFile(SimCode simCode)
match simCode
case SIMCODE(__) then
<<
<%generateHeaderInlcudeString(simCode)%>
<%generateHeaderIncludeString(simCode)%>
#ifdef ANALYZATION_MODE
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
Expand All @@ -77,9 +77,12 @@ case SIMCODE(__) then
#include <boost/numeric/ublas/matrix_sparse.hpp>
typedef uBlas::compressed_matrix<double, uBlas::column_major, 0, uBlas::unbounded_array<int>, uBlas::unbounded_array<double> > SparseMatrix;
#endif
<%generateHpcomSpecificIncludes(simCode)%>
<%generateClassDeclarationCode(simCode)%>

<%generateHpcomSpecificIncludes(simCode)%>
<%generateClassDeclarationCode(simCode)%>
#ifdef MEASURE_PAPI
#include <papi.h>
#define NUM_EVENTS 1
#endif
>>
end simulationHeaderFile;

Expand Down Expand Up @@ -296,7 +299,7 @@ template generateHpcomSpecificIncludes(SimCode simCode)
>>
case ("mixed") then
<<
#include <omp.h>
#define NUM_EVENTS 1
#include <boost/thread/mutex.hpp>
#include <boost/thread.hpp>
>>
Expand Down Expand Up @@ -730,6 +733,7 @@ template update2(list<SimEqSystem> allEquationsPlusWhen, list<list<SimEqSystem>>
<<
static bool state_var_reinitialized = false;
static bool firstRun = true;

void <%lastIdentOfPath(name)%>::evaluateThreadFunc0()
{
//if (omp_get_dynamic())
Expand All @@ -741,21 +745,51 @@ template update2(list<SimEqSystem> allEquationsPlusWhen, list<list<SimEqSystem>>
{
while(!finished)
{
#ifdef MEASURE_PAPI
int event[NUM_EVENTS] = {PAPI_L2_TCM};
long long values[NUM_EVENTS];
#endif

#pragma omp master
{
<%function_HPCOM_assignLock("startEvaluateLock","","pthreads")%>
#ifdef MEASURE_PAPI
/* Start counting events */
if (PAPI_start_counters(event, NUM_EVENTS) != PAPI_OK) {
fprintf(stderr, "PAPI_start_counters - FAILED\n");
exit(1);
}
#endif
}

#pragma omp barrier
if(finished)
<%function_HPCOM_releaseLock("finishedEvaluateLock","","pthreads")%>

<%odeEqs%>

#pragma omp barrier

#pragma omp master
{
#ifdef MEASURE_PAPI
/* Read the counters */
if (PAPI_read_counters(values, NUM_EVENTS) != PAPI_OK) {
fprintf(stderr, "PAPI_read_counters - FAILED\n");
exit(1);
}
std::cerr << "L2 Cache misses: " << values[0] << std::endl;

/* Stop counting events */
if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) {
fprintf(stderr, "PAPI_stoped_counters - FAILED\n");
exit(1);
}
#endif
<%function_HPCOM_releaseLock("finishedEvaluateLock","","pthreads")%>
}


}
}
}
Expand All @@ -779,10 +813,36 @@ template update2(list<SimEqSystem> allEquationsPlusWhen, list<list<SimEqSystem>>
<<
void <%lastIdentOfPath(name)%>::evaluateODE(const UPDATETYPE command)
{
#ifdef MEASURE_PAPI
int event[NUM_EVENTS] = {PAPI_L2_TCM};
long long values[NUM_EVENTS];

/* Start counting events */
if (PAPI_start_counters(event, NUM_EVENTS) != PAPI_OK) {
fprintf(stderr, "PAPI_start_counters - FAILED\n");
exit(1);
}
#endif

#pragma omp parallel num_threads(<%getConfigInt(NUM_PROC)%>)
{
<%odeEqs%>
}

#ifdef MEASURE_PAPI
/* Read the counters */
if (PAPI_read_counters(values, NUM_EVENTS) != PAPI_OK) {
fprintf(stderr, "PAPI_read_counters - FAILED\n");
exit(1);
}
std::cerr << "L2 Cache misses: " << values[0] << std::endl;

/* Stop counting events */
if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) {
fprintf(stderr, "PAPI_stoped_counters - FAILED\n");
exit(1);
}
#endif
}
>>
else ""
Expand Down Expand Up @@ -1394,7 +1454,7 @@ CFLAGS_BASED_ON_INIT_FILE=<%_extraCflags%> -I"<%makefileParams.omhome%>/../Simul
CFLAGS=$(CFLAGS_BASED_ON_INIT_FILE) -Winvalid-pch $(SYSTEM_CFLAGS) -I"<%makefileParams.omhome%>/include/omc/cpp/Core" -I"<%makefileParams.omhome%>/include/omc/cpp/" -I. <%makefileParams.includes%> -I"$(BOOST_INCLUDE)" <%makefileParams.includes ; separator=" "%> <%makefileParams.cflags%> <%match sopt case SOME(s as SIMULATION_SETTINGS(__)) then s.cflags %>
LDSYTEMFLAGS=-L"<%makefileParams.omhome%>/lib/omc/cpp" -L"$(BOOST_LIBS)"
CPP_RUNTIME_LIBS=<%analyzationLibs%>
LDMAINFLAGS=-L"<%makefileParams.omhome%>/lib/omc/cpp" <%simulationMainDLLib(simCode)%> -L"<%makefileParams.omhome%>/bin" <%schedulerLibs%> $(CPP_RUNTIME_LIBS) -L"$(BOOST_LIBS)" $(BOOST_SYSTEM_LIB) $(BOOST_FILESYSTEM_LIB) $(BOOST_PROGRAM_OPTIONS_LIB) $(BOOST_SERIALIZATION_LIB) $(BOOST_THREAD_LIB) $(LINUX_LIB_DL)
LDMAINFLAGS=-L"<%makefileParams.omhome%>/lib/omc/cpp" <%simulationMainDLLib(simCode)%> -L"<%makefileParams.omhome%>/bin" <%schedulerLibs%> $(CPP_RUNTIME_LIBS) -L"$(BOOST_LIBS)" $(BOOST_SYSTEM_LIB) $(BOOST_FILESYSTEM_LIB) $(BOOST_PROGRAM_OPTIONS_LIB) $(BOOST_SERIALIZATION_LIB) $(BOOST_THREAD_LIB) $(LINUX_LIB_DL) -lpapi
CPPFLAGS = $(CFLAGS) -DOMC_BUILD -DBOOST_SYSTEM_NO_DEPRICATED
SYSTEMFILE=OMCpp<%fileNamePrefix%><% if acceptMetaModelicaGrammar() then ".conv"%>.cpp
FUNCTIONFILE=OMCpp<%fileNamePrefix%>Functions.cpp
Expand Down
7 changes: 5 additions & 2 deletions Compiler/Util/Flags.mo
Expand Up @@ -391,7 +391,9 @@ constant DebugFlag ITERATION_VARS = DEBUG_FLAG(112, "iterationVars", false,
Util.gettext("Shows a list of all iteration variables."));
constant DebugFlag ALLOW_RECORD_TOO_MANY_FIELDS = DEBUG_FLAG(113, "acceptTooManyFields", false,
Util.gettext("Accepts passing records with more fields than expected to a function. This is not allowed, but is used in Fluid.Dissipation. See https://trac.modelica.org/Modelica/ticket/1245 for details."));

constant DebugFlag HPCOM_MEMORY_OPT = DEBUG_FLAG(114, "hpcomMemoryOpt", false,
Util.gettext("Optimize the memory structure regarding the selected scheduler"));

// This is a list of all debug flags, to keep track of which flags are used. A
// flag can not be used unless it's in this list, and the list is checked at
// initialization so that all flags are sorted by index (and thus have unique
Expand Down Expand Up @@ -509,7 +511,8 @@ constant list<DebugFlag> allDebugFlags = {
EVAL_FUNC_DUMP,
PRINT_STRUCTURAL,
ITERATION_VARS,
ALLOW_RECORD_TOO_MANY_FIELDS
ALLOW_RECORD_TOO_MANY_FIELDS,
HPCOM_MEMORY_OPT
};

// CONFIGURATION FLAGS
Expand Down
4 changes: 2 additions & 2 deletions SimulationRuntime/cpp/Core/Solver/SystemStateSelection.cpp
Expand Up @@ -7,8 +7,8 @@

SystemStateSelection::SystemStateSelection(IMixedSystem* system)
:_system(system)
,_colPivot(NULL)
,_rowPivot(NULL)
,_colPivot()
,_rowPivot()
,_initialized(false)
{
_state_selection = dynamic_cast<IStateSelection*>(system);
Expand Down
33 changes: 21 additions & 12 deletions SimulationRuntime/cpp/Core/System/EventHandling.cpp
Expand Up @@ -10,14 +10,18 @@ Constructor
\param system Modelica system object
\param dim Dimenson of help variables
*/
EventHandling::EventHandling()
EventHandling::EventHandling() : _countinous_system(NULL), _mixed_system(NULL), _conditions0(NULL), _conditions1(NULL)
//:_h(NULL)
{
}

EventHandling::~EventHandling(void)
{
// if(_h) delete [] _h;
if(_conditions0)
delete[] _conditions0;
if(_conditions1)
delete[] _conditions1;
}
/**
Inits the event variables
Expand All @@ -26,6 +30,9 @@ void EventHandling::initialize(IEvent* system,int dim,init_prevars_type init_pre
{
// _dimH=dim;
_event_system=system;
_countinous_system = dynamic_cast<IContinuous*>(_event_system);
_mixed_system= dynamic_cast<IMixedSystem*>(_event_system);

init_prevars(_pre_vars_idx,_pre_discrete_vars_idx);
_pre_vars.resize((boost::extents[_pre_vars_idx.size()]));
_pre_discrete_vars.resize((boost::extents[_pre_discrete_vars_idx.size()]));
Expand All @@ -37,6 +44,13 @@ void EventHandling::initialize(IEvent* system,int dim,init_prevars_type init_pre
memset(_h,0,(_dimH)*sizeof(double));
}
*/
if(_conditions0)
delete[] _conditions0;
if(_conditions1)
delete[] _conditions1;

_conditions0 = new bool[dim];
_conditions1 = new bool[dim];
}
/**
Returns the help vector
Expand Down Expand Up @@ -165,29 +179,24 @@ Handles all events occured a the same time. These are stored the eventqueue

bool EventHandling::IterateEventQueue(bool& state_vars_reinitialized)
{
IContinuous* countinous_system = dynamic_cast<IContinuous*>(_event_system);
IMixedSystem* mixed_system= dynamic_cast<IMixedSystem*>(_event_system);

//save discrete varibales
_event_system->saveDiscreteVars(); // store values of discrete vars vor next check

unsigned int dim = _event_system->getDimZeroFunc();
bool* conditions0 = new bool[dim];
bool* conditions1 = new bool[dim];
_event_system->getConditions(conditions0);

_event_system->getConditions(_conditions0);
//Handle all events

state_vars_reinitialized = countinous_system->evaluateAll();
state_vars_reinitialized = _countinous_system->evaluateAll();


//check if discrete variables changed
bool drestart= _event_system->checkForDiscreteEvents();


_event_system->getConditions(conditions1);
bool crestart = !std::equal (conditions1, conditions1+dim,conditions0);
delete[] conditions0;
delete [] conditions1;
_event_system->getConditions(_conditions1);
bool crestart = !std::equal (_conditions1, _conditions1+dim,_conditions0);

return((drestart||crestart)); //returns true if new events occured
}

Expand Down

0 comments on commit 4593c52

Please sign in to comment.