Skip to content

Commit

Permalink
- simulate option measureTime=true is now working; testcase MeasureTi…
Browse files Browse the repository at this point in the history
…me.mos has been added

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8083 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Mar 4, 2011
1 parent a80e39a commit c2c31c5
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 75 deletions.
30 changes: 1 addition & 29 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -2159,7 +2159,6 @@ algorithm
libs_str = Util.stringDelimitList(libs, " ");

System.writeFile(libsfilename, libs_str);
extra_command = setCompileCommandEnvironmentFromSolverMethod(solverMethod);
// We only need to set OPENMODELICAHOME on Windows, and set doesn't work in bash shells anyway
// adrpo: 2010-10-05:
// whatever you do, DO NOT add a space before the && otherwise
Expand All @@ -2168,7 +2167,7 @@ algorithm
// to the environment variable! Don't ask me why, ask Microsoft.
omhome = Util.if_(System.os() ==& "Windows_NT", "set OPENMODELICAHOME=\"" +& omhome_1 +& "\"&& ", "OPENMODELICAHOME=\"$OPENMODELICAHOME\" ");
s_call =
stringAppendList({omhome,extra_command,
stringAppendList({omhome,
omhome_1,pd,"share",pd,"omc",pd,"scripts",pd,"Compile"," ",fileprefix," ",noClean});
Debug.fprintln("dynload", "compileModel: running " +& s_call);
0 = System.systemCall(s_call) ;
Expand Down Expand Up @@ -2253,33 +2252,6 @@ algorithm
end matchcontinue;
end readEnvNoFail;

protected function setCompileCommandEnvironmentFromSolverMethod
"Inline solver methods require extra environment variables set"
input String method;
output String env;
algorithm
env := matchcontinue method
local
String str, modelicaUserCFlags;

case "inline-euler"
equation
modelicaUserCFlags = readEnvNoFail("MODELICAUSERCFLAGS");
// adrpo: In Windows it seems that command set X="%var% some other stuff" && echo %X%
// DOES NOT EXPAND X correctly, that's why we read and use the environment variable directly
str = Util.if_(System.os() ==& "Windows_NT", "set MODELICAUSERCFLAGS=" +& modelicaUserCFlags +& " -D_OMC_INLINE_EULER && ", "MODELICAUSERCFLAGS=\"$MODELICAUSERCFLAGS -D_OMC_INLINE_EULER\" ");
then str;
case "inline-rungekutta"
equation
modelicaUserCFlags = readEnvNoFail("MODELICAUSERCFLAGS");
// adrpo: In Windows it seems that command set X="%var% some other stuff" && echo %X%
// DOES NOT EXPAND X correctly, that's why we read and use the environment variable directly
str = Util.if_(System.os() ==& "Windows_NT", "set MODELICAUSERCFLAGS=" +& modelicaUserCFlags +&" -D_OMC_INLINE_RK && ", "MODELICAUSERCFLAGS=\"$MODELICAUSERCFLAGS -D_OMC_INLINE_RK\" ");
then str;
case _ then "";
end matchcontinue;
end setCompileCommandEnvironmentFromSolverMethod;

protected function winCitation "function: winCitation
author: PA
Returns a citation mark if platform is windows, otherwise empty string.
Expand Down
2 changes: 1 addition & 1 deletion Compiler/scripts/default_profiling.xsl
Expand Up @@ -41,7 +41,7 @@
<p>For more details, see <a href="{modelinfo/prefix}_prof.xml"><xsl:value-of select="modelinfo/name"/>_prof.xml</a>.</p>

<h2>Summary</h2>
<p>What solver/settings were used? This is missing :(</p>
<p>The simulation was done using method="<xsl:value-of select="modelinfo/method"/>".</p>
<table>
<tr><th class="name">Task</th><th>Time</th><th><abbr title="Fraction of total simulation time">Fraction</abbr></th></tr>
<tr><td class="name"><abbr title="Choosing solver, allocating data structures, etc (does not include reading the parameter start-values from file)">Pre-Initialization</abbr></td><td><xsl:value-of select="modelinfo/preinitTime"/></td><td><xsl:value-of select="format-number(100 * modelinfo/preinitTime div modelinfo/totalTime,'##0.00')"/>%</td></tr>
Expand Down
52 changes: 44 additions & 8 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -90,7 +90,11 @@ match simCode
case SIMCODE(__) then
<<
<%simulationFileHeader(simCode)%>

#ifdef _OMC_MEASURE_TIME
const int measure_time_flag = 1;
#else
const int measure_time_flag = 0;
#endif
<%globalData(modelInfo,fileNamePrefix)%>

<%equationInfo(appendLists(appendAllequation(JacobianMatrixes),allEquationsPlusWhen))%>
Expand Down Expand Up @@ -1272,13 +1276,17 @@ template functionExtraResiduals(list<SimEqSystem> allEquations)
{
state mem_state;
<%varDecls%>
#ifdef _OMC_MEASURE_TIME
SIM_PROF_TICK_EQ(SIM_PROF_EQ_<%index%>);
#endif
mem_state = get_memory_state();
<%algs%>
<%prebody%>
<%body%>
restore_memory_state(mem_state);
#ifdef _OMC_MEASURE_TIME
SIM_PROF_ACC_EQ(SIM_PROF_EQ_<%index%>);
#endif<%\n%>
}
>>
;separator="\n\n")
Expand Down Expand Up @@ -2267,7 +2275,12 @@ case SES_LINEAR(__) then
let bname = 'b<%uid%>'
let mixedPostfix = if partOfMixed then "_mixed" //else ""
<<
<% if not partOfMixed then 'SIM_PROF_TICK_EQ(SIM_PROF_EQ_<%index%>);<%\n%>' %>
<% if not partOfMixed then
<<
#ifdef _OMC_MEASURE_TIME
SIM_PROF_TICK_EQ(SIM_PROF_EQ_<%index%>);<%\n%>
#endif<%\n%>
>> %>
declare_matrix(<%aname%>, <%size%>, <%size%>);
declare_vector(<%bname%>, <%size%>);
<%simJac |> (row, col, eq as SES_RESIDUAL(__)) =>
Expand All @@ -2282,7 +2295,12 @@ case SES_LINEAR(__) then
;separator="\n"%>
solve_linear_equation_system<%mixedPostfix%>(<%aname%>, <%bname%>, <%size%>, <%uid%>);
<%vars |> SIMVAR(__) hasindex i0 => '<%cref(name)%> = get_vector_elt(<%bname%>, <%i0%>);' ;separator="\n"%><%inlineVars(context,vars)%>
<% if not partOfMixed then 'SIM_PROF_ACC_EQ(SIM_PROF_EQ_<%index%>);<%\n%>' %>
<% if not partOfMixed then
<<
#ifdef _OMC_MEASURE_TIME
SIM_PROF_ACC_EQ(SIM_PROF_EQ_<%index%>);
#endif<%\n%>
>> %>
>>
end equationLinear;

Expand All @@ -2304,7 +2322,9 @@ case SES_MIXED(__) then
>>
;separator="\n")
<<
#ifdef _OMC_MEASURE_TIME
SIM_PROF_TICK_EQ(SIM_PROF_EQ_<%index%>);
#endif
mixed_equation_system(<%numDiscVarsStr%>);
double values[<%valuesLenStr%>] = {<%values ;separator=", "%>};
int value_dims[<%numDiscVarsStr%>] = {<%value_dims ;separator=", "%>};
Expand All @@ -2319,7 +2339,9 @@ case SES_MIXED(__) then
check_discrete_values(<%numDiscVarsStr%>, <%valuesLenStr%>);
}
mixed_equation_system_end(<%numDiscVarsStr%>);
#ifdef _OMC_MEASURE_TIME
SIM_PROF_ACC_EQ(SIM_PROF_EQ_<%index%>);
#endif<%\n%>
>>
end equationMixed;

Expand Down Expand Up @@ -2487,11 +2509,15 @@ template simulationMakefile(SimCode simCode)
"Generates the contents of the makefile for the simulation case."
::=
match simCode
case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__)) then
case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simulationSettingsOpt = sopt) then
let dirExtra = if modelInfo.directory then '-L"<%modelInfo.directory%>"' //else ""
let libsStr = (makefileParams.libs |> lib => lib ;separator=" ")
let libsPos1 = if not dirExtra then libsStr //else ""
let libsPos2 = if dirExtra then libsStr // else ""
let extraCflags = match sopt case SOME(s as SIMULATION_SETTINGS(__)) then
'<%if s.measureTime then "-D_OMC_MEASURE_TIME "%> <%match s.method
case "inline-euler" then "-D_OMC_INLINE_EULER"
case "inline-rungekutta" then "-D_OMC_INLINE_RK"%>'
<<
# Makefile generated by OpenModelica

Expand All @@ -2502,7 +2528,8 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__)) then
LINK=<%makefileParams.linker%>
EXEEXT=<%makefileParams.exeext%>
DLLEXT=<%makefileParams.dllext%>
CFLAGS=-I"<%makefileParams.omhome%>/include/omc" <%makefileParams.cflags%>
CFLAGS_BASED_ON_INIT_FILE=<%extraCflags%>
CFLAGS=$(CFLAGS_BASED_ON_INIT_FILE) -I"<%makefileParams.omhome%>/include/omc" <%makefileParams.cflags%>
LDFLAGS=-L"<%makefileParams.omhome%>/lib/omc" <%makefileParams.ldflags%>
SENDDATALIBS=<%makefileParams.senddatalibs%>
PERL=perl
Expand Down Expand Up @@ -2640,7 +2667,6 @@ template functionsHeaderFile(String filePrefix,
}
#endif
#endif

<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
Expand Down Expand Up @@ -5360,9 +5386,19 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
let retVar = match exp
case CALL(ty=ET_NORETCALL(__)) then ""
else tempDecl(retType, &varDecls)
let &preExp += if not builtin then match context case SIMULATION(__) case SIMULATION2(__) then 'SIM_PROF_TICK_FN(<%funName%>_index);<%\n%>'
let &preExp += if not builtin then match context case SIMULATION(__) case SIMULATION2(__) then
<<
#ifdef _OMC_MEASURE_TIME
SIM_PROF_TICK_FN(<%funName%>_index);
#endif<%\n%>
>>
let &preExp += '<%if retVar then '<%retVar%> = '%><%daeExpCallBuiltinPrefix(builtin)%><%funName%>(<%argStr%>);<%\n%>'
let &preExp += if not builtin then match context case SIMULATION(__) case SIMULATION2(__) then 'SIM_PROF_ACC_FN(<%funName%>_index);<%\n%>'
let &preExp += if not builtin then match context case SIMULATION(__) case SIMULATION2(__) then
<<
#ifdef _OMC_MEASURE_TIME
SIM_PROF_ACC_FN(<%funName%>_index);
#endif<%\n%>
>>
match exp
// no return calls
case CALL(ty=ET_NORETCALL(__)) then '/* NORETCALL */'
Expand Down
1 change: 1 addition & 0 deletions Compiler/susan_codegen/SimCode/SimCodeTV.mo
Expand Up @@ -113,6 +113,7 @@ package SimCode
String options;
String outputFormat;
String variableFilter;
Boolean measureTime;
end SIMULATION_SETTINGS;
end SimulationSettings;

Expand Down
8 changes: 4 additions & 4 deletions c_runtime/rtclock.h
Expand Up @@ -50,11 +50,11 @@ extern "C" {
#define SIM_TIMER_OVERHEAD 7
#define SIM_TIMER_FIRST_FUNCTION 8

#define SIM_PROF_TICK_FN(ix) if (measure_time_flag) {rt_tick(ix+SIM_TIMER_FIRST_FUNCTION);}
#define SIM_PROF_ACC_FN(ix) if (measure_time_flag) {rt_accumulate(ix+SIM_TIMER_FIRST_FUNCTION);}
#define SIM_PROF_TICK_FN(ix) rt_tick(ix+SIM_TIMER_FIRST_FUNCTION)
#define SIM_PROF_ACC_FN(ix) rt_accumulate(ix+SIM_TIMER_FIRST_FUNCTION)

#define SIM_PROF_TICK_EQ(ix) if (measure_time_flag) {rt_tick(ix+SIM_TIMER_FIRST_FUNCTION+localData->nFunctions);}
#define SIM_PROF_ACC_EQ(ix) if (measure_time_flag) {rt_accumulate(ix+SIM_TIMER_FIRST_FUNCTION+localData->nFunctions);}
#define SIM_PROF_TICK_EQ(ix) rt_tick(ix+SIM_TIMER_FIRST_FUNCTION+localData->nFunctions)
#define SIM_PROF_ACC_EQ(ix) rt_accumulate(ix+SIM_TIMER_FIRST_FUNCTION+localData->nFunctions)

void rt_init(int numTimer);

Expand Down
5 changes: 3 additions & 2 deletions c_runtime/simulation_modelinfo.c
Expand Up @@ -137,7 +137,7 @@ static void printProfilingDataHeader(FILE *fout, DATA *data) {
indent(fout, 2); fprintf(fout, "</format>\n");
}

int printModelInfo(DATA *data, const char *filename, const char *plotfile) {
int printModelInfo(DATA *data, const char *filename, const char *plotfile, const char *method) {
static char buf[256];
FILE *fout = fopen(filename, "w");
FILE *plotCommands;
Expand Down Expand Up @@ -187,6 +187,7 @@ int printModelInfo(DATA *data, const char *filename, const char *plotfile) {
indent(fout, 2); fprintf(fout, "<name>%s</name>\n", data->modelName);
indent(fout, 2); fprintf(fout, "<prefix>%s</prefix>\n", data->modelFilePrefix);
indent(fout, 2); fprintf(fout, "<date>%s</date>\n", buf);
indent(fout, 2); fprintf(fout, "<method>%s</method>\n", method);
indent(fout, 2); fprintf(fout, "<overheadTime>%f</overheadTime>\n", rt_accumulated(SIM_TIMER_OVERHEAD));
indent(fout, 2); fprintf(fout, "<preinitTime>%f</preinitTime>\n", rt_accumulated(SIM_TIMER_PREINIT));
indent(fout, 2); fprintf(fout, "<initTime>%f</initTime>\n", rt_accumulated(SIM_TIMER_INIT));
Expand Down Expand Up @@ -258,7 +259,7 @@ int printModelInfo(DATA *data, const char *filename, const char *plotfile) {
}
if (genHtmlRes)
fprintf(stderr, "Warning: Failed to generate html version of profiling results: %s\n", buf);
fprintf(stdout, "Time measurements stored in %s_prof.html (human-readable), %s_prof.xml (for XSL transforms)\n", data->modelFilePrefix, data->modelFilePrefix);
fprintf(stdout, "Time measurements are stored in %s_prof.html (human-readable) and %s_prof.xml (for XSL transforms or more details)\n", data->modelFilePrefix, data->modelFilePrefix);
free(buf);
}
return 0;
Expand Down
2 changes: 1 addition & 1 deletion c_runtime/simulation_modelinfo.h
Expand Up @@ -35,7 +35,7 @@
extern "C" {
#endif

int printModelInfo(DATA *data, const char *modelinfo, const char *plotinfo);
int printModelInfo(DATA *data, const char *modelinfo, const char *plotinfo, const char *method);

#ifdef __cplusplus
}
Expand Down
32 changes: 12 additions & 20 deletions c_runtime/simulation_runtime.cpp
Expand Up @@ -72,7 +72,6 @@ char* terminateMessage = 0;
int warningLevelAssert = 0;
string TermMsg;
omc_fileInfo TermInfo = omc_dummyFileInfo;
int measure_time_flag = 0;

int sim_verbose; // Flag for logging
int sim_noemit; // Flag for not emitting data
Expand Down Expand Up @@ -320,9 +319,9 @@ startNonInteractiveSimulation(int argc, char**argv)
string* lintime = (string*) getFlagValue("l", argc, argv);

/* mesure time option is set : -mt */
measure_time_flag = (int) flagSet("mt", argc, argv);
if (measure_time_flag) {
fprintf(stderr, "Warning: The -mt is going to be replaced by the simulate option measureTime.\n");
if (flagSet("mt", argc, argv)) {
fprintf(stderr, "Error: The -mt was replaced by the simulate option measureTime, which compiles a simulation more suitable for profiling.\n");
return 1;
}

double start = 0.0;
Expand Down Expand Up @@ -360,6 +359,13 @@ startNonInteractiveSimulation(int argc, char**argv)
method = "dassl";
}

int methodflag = (int) flagSet("s", argc, argv);
if (methodflag) {
string* solvermethod = (string*) getFlagValue("s", argc, argv);
if (!(solvermethod == NULL))
method.assign(*solvermethod);
}

retVal = callSolver(argc, argv, method, outputFormat, start, stop, stepSize,
outputSteps, tolerance);

Expand All @@ -374,7 +380,7 @@ startNonInteractiveSimulation(int argc, char**argv)
const string modelInfo = string(globalData->modelFilePrefix) + "_prof.xml";
const string plotFile = string(globalData->modelFilePrefix) + "_prof.plt";
rt_accumulate(SIM_TIMER_TOTAL);
retVal = printModelInfo(globalData, modelInfo.c_str(), plotFile.c_str()) && retVal;
retVal = printModelInfo(globalData, modelInfo.c_str(), plotFile.c_str(), method.c_str()) && retVal;
}

deinitDelay();
Expand Down Expand Up @@ -423,21 +429,7 @@ callSolver(int argc, char**argv, string method, string outputFormat,
<< "'" << endl;
}

int methodflag = (int) flagSet("s", argc, argv);
if (methodflag) {
string* solvermethod = (string*) getFlagValue("s", argc, argv);
if (!(solvermethod == NULL))
method.assign(*solvermethod);
}

if (method == "") {
if (sim_verbose) {
cout << "No Recognized solver, using dassl." << endl;
}
retVal = solver_main(argc,argv,start,stop,stepSize,outputSteps,tolerance,3);
//retVal = dassl_main(argc, argv, start, stop, stepSize, outputSteps,
//tolerance);
} else if (method == std::string("euler")) {
if (method == std::string("euler")) {
if (sim_verbose) {
cout << "Recognized solver: " << method << "." << endl;
}
Expand Down
2 changes: 1 addition & 1 deletion c_runtime/simulation_runtime.h
Expand Up @@ -90,7 +90,7 @@ int callSolver(int, char**, string, string, double, double, double, long, double

#endif /* cplusplus */

extern int measure_time_flag;
extern const int measure_time_flag;
extern int sim_verbose; /* control debug output during simulation. */
extern int sim_noemit; /* control emitting result data to file */
extern int acceptedStep; /* !=0 when accepted step is calculated, 0 otherwise. */
Expand Down
18 changes: 9 additions & 9 deletions c_runtime/solver_main.cpp
Expand Up @@ -458,8 +458,7 @@ solver_main(int argc, char** argv, double &start, double &stop, double &step,
const string filename = string(globalData->modelFilePrefix) + "_prof.data";
fmt = fopen(filename.c_str(), "wb");
if (!fmt) {
measure_time_flag = 0;
fprintf(stderr, "Warning: Disabled time measurements because the output file could not be opened: %s\n", strerror(errno));
fprintf(stderr, "Warning: Time measurements output file %s could not be opened: %s\n", filename.c_str(), strerror(errno));
fclose(fmt);
fmt = NULL;
}
Expand Down Expand Up @@ -552,28 +551,29 @@ solver_main(int argc, char** argv, double &start, double &stop, double &step,

// Emit this time step
saveall();
if (measure_time_flag)
if (fmt)
{
int flag = 1;
double tmpdbl;
uint32_t tmpint;
rt_tick(SIM_TIMER_OVERHEAD);
rt_accumulate(SIM_TIMER_STEP);
/* Disable time measurements if we have trouble writing to the file... */
measure_time_flag = measure_time_flag && 1 == fwrite(&stepNo, sizeof(uint32_t), 1, fmt);
flag = flag && 1 == fwrite(&stepNo, sizeof(uint32_t), 1, fmt);
stepNo++;
measure_time_flag = measure_time_flag && 1 == fwrite(&globalData->timeValue, sizeof(double), 1, fmt);
flag = flag && 1 == fwrite(&globalData->timeValue, sizeof(double), 1, fmt);
tmpdbl = rt_accumulated(SIM_TIMER_STEP);
measure_time_flag = measure_time_flag && 1 == fwrite(&tmpdbl, sizeof(double), 1, fmt);
flag = flag && 1 == fwrite(&tmpdbl, sizeof(double), 1, fmt);
for (int i = 0; i < globalData->nFunctions + globalData->nProfileBlocks; i++) {
tmpint = rt_ncall(i + SIM_TIMER_FIRST_FUNCTION);
measure_time_flag = measure_time_flag && 1 == fwrite(&tmpint, sizeof(uint32_t), 1, fmt);
flag = flag && 1 == fwrite(&tmpint, sizeof(uint32_t), 1, fmt);
}
for (int i = 0; i < globalData->nFunctions + globalData->nProfileBlocks; i++) {
tmpdbl = rt_accumulated(i + SIM_TIMER_FIRST_FUNCTION);
measure_time_flag = measure_time_flag && 1 == fwrite(&tmpdbl, sizeof(double), 1, fmt);
flag = flag && 1 == fwrite(&tmpdbl, sizeof(double), 1, fmt);
}
rt_accumulate(SIM_TIMER_OVERHEAD);
if (!measure_time_flag) {
if (!flag) {
fprintf(stderr, "Warning: Disabled time measurements because the output file could not be generated: %s\n", strerror(errno));
fclose(fmt);
fmt = NULL;
Expand Down

0 comments on commit c2c31c5

Please sign in to comment.