Skip to content

Commit

Permalink
- Template for hpcom-cpp target added to the template-folder
Browse files Browse the repository at this point in the history
- Changed EXIT(0) to return 0 in _main_simulation_runtime

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18896 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Marcus Walther committed Jan 31, 2014
1 parent 8a17ec8 commit 04bbc9f
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 9 deletions.
62 changes: 62 additions & 0 deletions Compiler/BackEnd/HpcOmSimCode.mo
Expand Up @@ -56,13 +56,16 @@ protected import ComponentReference;
protected import Debug;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionDumpTpl;
protected import Flags;
protected import GlobalScript;
protected import HpcOmTaskGraph;
protected import List;
protected import Matching;
protected import SimCodeUtil;
protected import System;
protected import Tpl;
protected import Util;

public function createSimCode "function createSimCode
Expand Down Expand Up @@ -268,6 +271,8 @@ algorithm
parameterEquations, inlineEquations, removedEquations, algorithmAndEquationAsserts, jacobianEquations, stateSets, constraints, classAttributes, zeroCrossings, relations, sampleLookup, whenClauses,
discreteModelVars, extObjInfo, makefileParams, delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT, SOME(taskScheduleSimCode));

//createSimEqVarMapping(allEquations);

print("HpcOm is still under construction.\n");
then simCode;
else equation
Expand Down Expand Up @@ -761,6 +766,63 @@ algorithm
end matchcontinue;
end removeDummyStateFromMapping1;

protected function createSimEqVarMapping
input list<SimCode.SimEqSystem> iEqSystems;
algorithm
_ := List.map(iEqSystems, getVarsBySimEqSystem);
end createSimEqVarMapping;

protected function getVarsBySimEqSystem "function getVarsBySimEqSystem
author: marcusw
Function extract all variables of the given equation system."
input SimCode.SimEqSystem iEqSystem;
output list<DAE.ComponentRef> oVars;
protected
list<DAE.ComponentRef> varList;
DAE.ComponentRef cref;
DAE.Exp exp;
Integer index;
algorithm
oVars := match(iEqSystem)
case(SimCode.SES_SIMPLE_ASSIGN(cref=cref,exp=exp,index=index))
equation
// ExpressionDump.dumpExp(exp);
// print("end Expression\n");
((_,varList)) = Expression.traverseExp(exp,createMemoryMapTraverse, {});
print("Var List for simEquation " +& intString(index) +& ":");
//print(stringDelimitList(List.map(varList, dumpCompRef), " , "));
print("\n");
then varList;
else then {};
end match;
end getVarsBySimEqSystem;

protected function createMemoryMapTraverse
input tuple<DAE.Exp,list<DAE.ComponentRef> > iExpVars;
output tuple<DAE.Exp,list<DAE.ComponentRef> > oExpVars;
protected
list<DAE.ComponentRef> iVarList, oVarList;
DAE.Exp iExp;
DAE.ComponentRef componentRef;
algorithm
oVarList := match(iExpVars)
case((iExp as DAE.CREF(componentRef=componentRef), iVarList))
equation
//ExpressionDump.dumpExp(iExp);
iVarList = componentRef :: iVarList;
then iExpVars;
else then iExpVars;
end match;
end createMemoryMapTraverse;

protected function dumpCompRef
input DAE.ComponentRef iCref;
output String oString;
algorithm
oString := Tpl.tplString(ExpressionDumpTpl.dumpCref, iCref);
end dumpCompRef;


// testfunctions
//------------------------------------------
//------------------------------------------
Expand Down
19 changes: 18 additions & 1 deletion Compiler/BackEnd/SimCodeMain.mo
Expand Up @@ -65,6 +65,7 @@ protected import CodegenQSS;
protected import CodegenAdevs;
protected import CodegenCSharp;
protected import CodegenCpp;
protected import CodegenCppHpcom;
protected import CodegenXML;
protected import CodegenJava;
protected import CodegenJS;
Expand Down Expand Up @@ -431,7 +432,7 @@ algorithm
then ();

case (_, _, "Cpp") equation
Tpl.tplNoret(CodegenCpp.translateModel, simCode);
callTargetTemplatesCPP(simCode);
then ();

case (_, _, "Adevs") equation
Expand Down Expand Up @@ -480,6 +481,22 @@ algorithm
end match;
end callTargetTemplates;

protected function callTargetTemplatesCPP
input SimCode.SimCode iSimCode;
algorithm
_ := matchcontinue(iSimCode)
case(_)
equation
true = Flags.isSet(Flags.HPCOM);
Tpl.tplNoret(CodegenCppHpcom.translateModel, iSimCode);
then ();
else
equation
Tpl.tplNoret(CodegenCpp.translateModel, iSimCode);
then ();
end matchcontinue;
end callTargetTemplatesCPP;

protected function callTargetTemplatesFMU
"Generate target code by passing the SimCode data structure to templates."
input SimCode.SimCode simCode;
Expand Down
1 change: 1 addition & 0 deletions Compiler/Makefile.common
Expand Up @@ -187,6 +187,7 @@ AbsynDumpTpl.mo \
CodegenAdevs.mo \
CodegenC.mo \
CodegenCpp.mo \
CodegenCppHpcom.mo \
CodegenCSharp.mo \
CodegenFMU.mo \
CodegenFMUCpp.mo \
Expand Down
53 changes: 47 additions & 6 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -645,6 +645,11 @@ template simulationFile(SimCode simCode, String guid)
int res;
DATA simulation_data;
<%mainTop(mainBody,"https://trac.openmodelica.org/OpenModelica/newticket")%>
<%if Flags.isSet(HPCOM) then "terminateHpcOmThreads();" %>
fflush(NULL);
EXIT(0);
return res;
}
<%\n%>
Expand Down Expand Up @@ -2030,7 +2035,11 @@ template functionXXX_system_HPCOM(list<SimEqSystem> derivativEquations, String n
case SOME(hpcOmSchedule as LEVELSCHEDULESC(__)) then
let odeEqs = hpcOmSchedule.eqsOfLevels |> eqs => functionXXX_system0_HPCOM_Level(derivativEquations,name,eqs,modelNamePrefixStr); separator="\n"
<<
static void function<%name%>_system<%n%>(DATA *data)
void terminateHpcOmThreads()
{
}

void function<%name%>_system<%n%>(DATA *data)
{
<%odeEqs%>
}
Expand All @@ -2045,9 +2054,13 @@ template functionXXX_system_HPCOM(list<SimEqSystem> derivativEquations, String n
case ("openmp") then
let taskEqs = functionXXX_system0_HPCOM_Thread(derivativEquations,name,hpcOmSchedule.threadTasks, type, modelNamePrefixStr); separator="\n"
<<
void terminateHpcOmThreads()
{
}

//using type: <%type%>
static int initialized = 0;
static void function<%name%>_system<%n%>(DATA *data)
void function<%name%>_system<%n%>(DATA *data)
{
omp_set_dynamic(0);
//create locks
Expand Down Expand Up @@ -2080,18 +2093,28 @@ template functionXXX_system_HPCOM(list<SimEqSystem> derivativEquations, String n
let threadReleaseLocks = hpcOmSchedule.threadTasks |> tt hasindex i0 fromindex 0 => function_HPCOM_releaseLock(i0, "th_lock", type); separator="\n"

<<
static int finished; //set to 1 if the hpcom-threads should be destroyed

<%threadDecl%>

<%locks%>

<%threadLocks%>
<%threadLocks1%>

void terminateHpcOmThreads()
{
finished = 1;
//Start the threads one last time
<%threadReleaseLocks%>
}

<%threadFuncs%>

//using type: <%type%>
static int initialized = 0;
static void function<%name%>_system<%n%>(DATA *data)
void function<%name%>_system<%n%>(DATA *data)
{
if(!initialized)
{
Expand Down Expand Up @@ -2131,21 +2154,33 @@ template functionXXX_system_HPCOM(list<SimEqSystem> derivativEquations, String n
let threadReleaseLocks = hpcOmSchedule.threadTasks |> tt hasindex i0 fromindex 0 => function_HPCOM_releaseLock(i0, "th_lock", "pthreads"); separator="\n"

<<
static int finished; //set to 1 if the hpcom-threads should be destroyed

<%threadDecl%>

<%locks%>

<%threadLocks%>
<%threadLocks1%>

void terminateHpcOmThreads()
{
finished = 1;
//Start the threads one last time
<%threadReleaseLocks%>
}

<%threadFuncs%>

//using type: <%type%>
static int initialized = 0;
static void function<%name%>_system<%n%>(DATA *data)
void function<%name%>_system<%n%>(DATA *data)
{
if(!initialized)
{
finished = 0;
<%initlocks%>
<%threadLocksInit%>
<%threadLocksInit1%>
Expand Down Expand Up @@ -2175,7 +2210,7 @@ template functionXXX_system0_HPCOM_Level(list<SimEqSystem> derivativEquations, S
//let odeEqs = "#pragma omp parallel sections\n{"
let odeEqs = eqsOfLevel |> eq => equationNamesHPCOM_(eq,derivativEquations,contextSimulationNonDiscrete,modelNamePrefixStr); separator="\n"
<<
#pragma omp parallel sections num_threads(<%getConfigInt(NUM_PROC)%>)
#pragma omp parallel sections num_threads(<%getConfigInt(NUM_PROC)%>)
{
<%odeEqs%>
}
Expand Down Expand Up @@ -2323,11 +2358,15 @@ template functionXXX_system0_HPCOM_PThread_func(list<SimEqSystem> derivativEquat
let assLock = function_HPCOM_assignLock(idx, "th_lock", "pthreads"); separator="\n"
let relLock = function_HPCOM_releaseLock(idx, "th_lock1", "pthreads"); separator="\n"
<<
static void function<%name%>_system<%n%>_thread_<%idx%>(DATA *data)
void function<%name%>_system<%n%>_thread_<%idx%>(DATA *data)
{
while(1)
{
<%assLock%>
if(finished)
return;
<%taskEqs%>
<%relLock%>
}
Expand Down Expand Up @@ -10525,7 +10564,9 @@ int main(int argc, char **argv)
<%mainTop('<%name%>(threadData, lst);',url)%>
}

<%if Flags.isSet(HPCOM) then "terminateHpcOmThreads();" %>
fflush(NULL);
EXIT(0);
return 0;
}
>>
Expand Down
27 changes: 27 additions & 0 deletions Compiler/Template/CodegenCppHpcom.tpl
@@ -0,0 +1,27 @@
// This file defines template-extensions for transforming Modelica code into parallel hpcom-code.
//
// There are one root template intended to be called from the code generator:
// translateModel. These template do not return any
// result but instead write the result to files. All other templates return
// text and are used by the root templates (most of them indirectly).

package CodegenCppHpcom

import interface SimCodeTV;
import CodegenUtil.*;
import CodegenCpp.*; //unqualified import, no need the CodegenC is optional when calling a template; or mandatory when the same named template exists in this package (name hiding)

template translateModel(SimCode simCode) ::=
// empty result of the top-level template .., only side effects
<< //bla
>>
end translateModel;

template update( list<SimEqSystem> allEquationsPlusWhen,list<SimWhenClause> whenClauses,SimCode simCode, Context context)
::=
<<
//test
>>
end update;

end CodegenCppHpcom;
7 changes: 6 additions & 1 deletion Compiler/Template/Makefile.common
@@ -1,6 +1,6 @@
.PHONY : all

all : AbsynDumpTpl.mo CodegenUtil.mo CodegenC.mo CodegenFMU.mo CodegenCSharp.mo CodegenQSS.mo CodegenCpp.mo CodegenFMUCpp.mo DAEDumpTpl.mo ExpressionDumpTpl.mo GraphvizDump.mo NFInstDumpTpl.mo SimCodeDump.mo Unparsing.mo SCodeDumpTpl.mo CodegenAdevs.mo CodegenXML.mo CodegenJava.mo CodegenJS.mo
all : AbsynDumpTpl.mo CodegenUtil.mo CodegenC.mo CodegenFMU.mo CodegenCSharp.mo CodegenQSS.mo CodegenCpp.mo CodegenCppHpcom.mo CodegenFMUCpp.mo DAEDumpTpl.mo ExpressionDumpTpl.mo GraphvizDump.mo NFInstDumpTpl.mo SimCodeDump.mo Unparsing.mo SCodeDumpTpl.mo CodegenAdevs.mo CodegenXML.mo CodegenJava.mo CodegenJS.mo

AbsynDumpTpl.mo : AbsynDumpTpl.tpl AbsynDumpTV.mo
@echo " ** AbsynDumpTpl template compilation ** "
Expand Down Expand Up @@ -62,6 +62,11 @@ CodegenCpp.mo : CodegenCpp.tpl SimCodeTV.mo
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "

CodegenCppHpcom.mo : CodegenCppHpcom.tpl SimCodeTV.mo CodegenCpp.tpl CodegenUtil.tpl
@echo " ** CodegenCppHpcom.mo template compilation ** "
$(OMC) $< > $@.log || (cat $@.log && false)
@echo " "

CodegenFMUCpp.mo : CodegenFMUCpp.tpl SimCodeTV.mo CodegenC.tpl CodegenUtil.tpl CodegenCpp.tpl CodegenFMU.tpl
@echo " ** CodegenFMUCpp template compilation ** "
$(OMC) $< > $@.log || (cat $@.log && false)
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation/simulation_runtime.cpp
Expand Up @@ -930,7 +930,7 @@ int _main_SimulationRuntime(int argc, char**argv, DATA *data)
}
#endif

EXIT(retVal);
return retVal;
}

static void omc_assert_simulation(FILE_INFO info, const char *msg, ...)
Expand Down

0 comments on commit 04bbc9f

Please sign in to comment.