Skip to content

Commit

Permalink
- Made it a simulation error to an output-format that is not known in…
Browse files Browse the repository at this point in the history
…stead of defaulting to plt

- Updated the measureTime option to print outputFormat and data sizes
- Improved handling of simulate() errors (now prints the contents of output.log when the simulation executable signals an error)


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8121 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Mar 7, 2011
1 parent f6d3656 commit 0b1cd57
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 20 deletions.
18 changes: 18 additions & 0 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -985,6 +985,14 @@ algorithm
(cache,executable,method_str,st,initfilename) = buildModelBeast(cache,env,vals,st,msg);
then
(cache,ValuesUtil.makeArray({Values.STRING(executable),Values.STRING(initfilename)}),st);

/* Remove output.log before simulate in case it already exists. This is so we can check for the presence of output.log later. */
case (cache,env,"simulate",vals,st,msg)
equation
true = System.regularFileExists("output.log");
false = 0 == System.removeFile("output.log");
simValue = createSimulationResultFailure("Failed to remove existing file output.log", simOptionsAsString(vals));
then (cache,simValue,st);

/* adrpo: see if the model exists before simulation! */
case (cache,env,"simulate",vals as Values.CODE(Absyn.C_TYPENAME(className))::_,st as Interactive.SYMBOLTABLE(ast = p),msg)
Expand Down Expand Up @@ -1025,6 +1033,16 @@ algorithm
then
(cache,simValue,newst);

case (cache,env,"simulate",vals as Values.CODE(Absyn.C_TYPENAME(className))::_,st,msg)
equation
true = System.regularFileExists("output.log");
res = System.readFile("output.log");
str = Absyn.pathString(className);
res = stringAppendList({"Simulation execution failed for model: ", str, "\n", res});
simValue = createSimulationResultFailure(res, simOptionsAsString(vals));
then
(cache,simValue,st);

case (cache,env,"simulate",vals as Values.CODE(Absyn.C_TYPENAME(className))::_,st,msg)
equation
omhome = Settings.getInstallationDirectoryPath() "simulation fail for some other reason than OPENMODELICAHOME not being set." ;
Expand Down
25 changes: 23 additions & 2 deletions Compiler/scripts/default_profiling.xsl
Expand Up @@ -6,6 +6,17 @@

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml" >

<xsl:template name="FileSize">
<xsl:param name="size" />
<xsl:choose>
<xsl:when test="string-length($size) = 0">0 B</xsl:when>
<xsl:when test="number($size) &lt;= 0">0 B</xsl:when>
<xsl:when test="round($size div 1024) &lt; 1"><xsl:value-of select="$size" /> B</xsl:when>
<xsl:when test="round($size div 1048576) &lt; 1"><xsl:value-of select="format-number(($size div 1024), '0.0')" /> kB</xsl:when>
<xsl:otherwise><xsl:value-of select="format-number(($size div 1048576), '0.00')" /> MB</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="/simulation">
<html>
<head>
Expand Down Expand Up @@ -40,16 +51,26 @@
<p>Note that for blocks where the individual execution time is close to the accuracy of the real-time clock, the maximum measured time may deviate a lot from the average.</p>
<p>For more details, see <a href="{modelinfo/prefix}_prof.xml"><xsl:value-of select="modelinfo/name"/>_prof.xml</a>.</p>

<h2>Settings</h2>
<table>
<tr><th class="name">Name</th><th>Value</th></tr>
<tr><td class="name">Integration method</td><td><xsl:value-of select="modelinfo/method"/></td></tr>
<tr><td class="name">Output format</td><td><xsl:value-of select="modelinfo/outputFormat"/></td></tr>
<tr><td class="name">Output name</td><td><a href="{modelinfo/outputFilename}"><xsl:value-of select="modelinfo/outputFilename"/></a></td></tr>
<tr><td class="name">Output size</td><td><xsl:call-template name="FileSize"><xsl:with-param name="size"><xsl:value-of select="modelinfo/outputFilesize"/></xsl:with-param></xsl:call-template></td></tr>
<tr><td class="name">Profiling data</td><td><a href="{profilingdataheader/filename}"><xsl:value-of select="profilingdataheader/filename"/></a></td></tr>
<tr><td class="name">Profiling size</td><td><xsl:call-template name="FileSize"><xsl:with-param name="size"><xsl:value-of select="profilingdataheader/filesize"/></xsl:with-param></xsl:call-template></td></tr>
</table>

<h2>Summary</h2>
<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>
<tr><td class="name">Initialization</td><td><xsl:value-of select="modelinfo/initTime"/></td><td><xsl:value-of select="format-number(100 * modelinfo/initTime div modelinfo/totalTime,'##0.00')"/>%</td></tr>
<tr><td class="name">Event-handling</td><td><xsl:value-of select="modelinfo/eventTime"/></td><td><xsl:value-of select="format-number(100 * modelinfo/eventTime div modelinfo/totalTime,'##0.00')"/>%</td></tr>
<tr><td class="name">Creating output file</td><td><xsl:value-of select="modelinfo/outputTime"/></td><td><xsl:value-of select="format-number(100 * modelinfo/outputTime div modelinfo/totalTime,'##0.00')"/>%</td></tr>
<tr><td class="name">Linearization</td><td><xsl:value-of select="modelinfo/linearizeTime"/></td><td><xsl:value-of select="format-number(100 * modelinfo/linearizeTime div modelinfo/totalTime,'##0.00')"/>%</td></tr>
<tr><td class="name">Time steps (incl.integration?)</td><td><xsl:value-of select="modelinfo/totalStepsTime"/></td><td><xsl:value-of select="format-number(100 * modelinfo/totalStepsTime div modelinfo/totalTime,'##0.00')"/>%</td></tr>
<tr><td class="name">Time steps</td><td><xsl:value-of select="modelinfo/totalStepsTime"/></td><td><xsl:value-of select="format-number(100 * modelinfo/totalStepsTime div modelinfo/totalTime,'##0.00')"/>%</td></tr>
<tr><td class="name"><abbr title="Overhead from creating {profilingdataheader/filename}. The overhead from sampling the real-time clock is embedded in the other times.">Overhead</abbr></td><td><xsl:value-of select="modelinfo/overheadTime"/></td><td><xsl:value-of select="format-number(100 * modelinfo/overheadTime div modelinfo/totalTime,'##0.00')"/>%</td></tr>
<tr><td class="name"><abbr title="Some subparts are not measured (between steps, etc), and some overlap (function calls during initialization)">Unknown</abbr></td><td><xsl:value-of select="modelinfo/totalTime - modelinfo/overheadTime - modelinfo/totalStepsTime - modelinfo/linearizeTime - modelinfo/outputTime - modelinfo/eventTime - modelinfo/initTime"/></td>
<td><xsl:value-of select="format-number(100 * (modelinfo/totalTime - modelinfo/overheadTime - modelinfo/totalStepsTime - modelinfo/linearizeTime - modelinfo/outputTime - modelinfo/eventTime - modelinfo/initTime - modelinfo/preinitTime) div modelinfo/totalTime,'##0.00')"/>%</td></tr>
Expand Down
2 changes: 2 additions & 0 deletions c_runtime/compat.h
Expand Up @@ -32,6 +32,8 @@
#ifndef _COMPAT_H
#define _COMPAT_H

#include "stdio.h"

#if defined(__MINGW32__) || defined(_MSC_VER)
#define EXIT(code) exit(code)
#else
Expand Down
2 changes: 1 addition & 1 deletion c_runtime/interactive/omi_ServiceInterface.cpp
Expand Up @@ -458,7 +458,7 @@ int callSolverFromOM(string method, string outputFormat, double start, double st
int retVal = -1;
gdMutex.Lock();

retVal = callSolver(argcTEMP, argvTEMP, method, outputFormat, start, stop, stepSize, outputSteps, tolerance);
retVal = callSolver(argcTEMP, argvTEMP, method, outputFormat, "", start, stop, stepSize, outputSteps, tolerance);

gdMutex.Unlock();
return retVal;
Expand Down
26 changes: 24 additions & 2 deletions c_runtime/simulation_modelinfo.c
Expand Up @@ -35,6 +35,18 @@
#include "assert.h"
#include <simulation_runtime.h>

/* Returns -1 if the file was not found */
static size_t fileSize(const char *filename) {
size_t sz = -1;
FILE *f = fopen(filename, "rb");
if (f) {
fseek(f, 0, SEEK_END);
sz = ftell(f);
fclose(f);
}
return sz;
}

static void indent(FILE *fout, int n) {
while (n--) fputc(' ', fout);
}
Expand Down Expand Up @@ -112,8 +124,15 @@ static void printEquations(FILE *fout, int n, const struct omc_equationInfo *eqn
}

static void printProfilingDataHeader(FILE *fout, DATA *data) {
char *filename;
int i;
indent(fout, 2); fprintf(fout, "<filename>%s_prof.data</filename>\n", data->modelFilePrefix);

filename = malloc(strlen(data->modelFilePrefix) + 15);
sprintf(filename, "%s_prof.data", data->modelFilePrefix);
indent(fout, 2); fprintf(fout, "<filename>%s</filename>\n", filename);
indent(fout, 2); fprintf(fout, "<filesize>%ld</filesize>\n", (long) fileSize(filename));
free(filename);

indent(fout, 2); fprintf(fout, "<format>\n");
indent(fout, 4); fprintf(fout, "<uint32>step</uint32>\n");
indent(fout, 4); fprintf(fout, "<double>time</double>\n");
Expand All @@ -137,7 +156,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, const char *method) {
int printModelInfo(DATA *data, const char *filename, const char *plotfile, const char *method, const char *outputFormat, const char *outputFilename) {
static char buf[256];
FILE *fout = fopen(filename, "w");
FILE *plotCommands;
Expand Down Expand Up @@ -188,6 +207,9 @@ int printModelInfo(DATA *data, const char *filename, const char *plotfile, const
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, "<outputFormat>%s</outputFormat>\n", outputFormat);
indent(fout, 2); fprintf(fout, "<outputFilename>%s</outputFilename>\n", outputFilename);
indent(fout, 2); fprintf(fout, "<outputFilesize>%ld</outputFilesize>\n", (long) fileSize(outputFilename));
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
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, const char *method);
int printModelInfo(DATA *data, const char *modelinfo, const char *plotinfo, const char *method, const char *outputFormat, const char *outputFilename);

#ifdef __cplusplus
}
Expand Down
31 changes: 18 additions & 13 deletions c_runtime/simulation_runtime.cpp
Expand Up @@ -365,22 +365,31 @@ startNonInteractiveSimulation(int argc, char**argv)
if (!(solvermethod == NULL))
method.assign(*solvermethod);
}

// Create a result file
string *result_file = (string*) getFlagValue("r", argc, argv);
string result_file_cstr;
if (!result_file) {
result_file_cstr = string(globalData->modelFilePrefix) + string("_res.") + outputFormat; /* TODO: Fix result file name based on mode */
} else {
result_file_cstr = *result_file;
}

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

if (create_linearmodel) {
if (retVal == 0 && create_linearmodel) {
rt_tick(SIM_TIMER_LINEARIZE);
retVal = linearize();
rt_accumulate(SIM_TIMER_LINEARIZE);
cout << "Linear model is created!" << endl;
}

if (measure_time_flag) {
if (retVal == 0 && measure_time_flag) {
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(), method.c_str()) && retVal;
retVal = printModelInfo(globalData, modelInfo.c_str(), plotFile.c_str(), method.c_str(), outputFormat.c_str(), result_file_cstr.c_str()) && retVal;
}

deinitDelay();
Expand All @@ -400,28 +409,24 @@ startNonInteractiveSimulation(int argc, char**argv)
*/
int
callSolver(int argc, char**argv, string method, string outputFormat,
string result_file_cstr,
double start, double stop, double stepSize, long outputSteps,
double tolerance)
{
int retVal = -1;

// Create a result file
string *result_file = (string*) getFlagValue("r", argc, argv);
string result_file_cstr;
if (!result_file) {
result_file_cstr = string(globalData->modelFilePrefix) + string("_res.") + outputFormat; /* TODO: Fix result file name based on mode */
} else {
result_file_cstr = *result_file;
}
long maxSteps = 2 * outputSteps + 2 * globalData->nSampleTimes;
if (isInteractiveSimulation() || sim_noemit || 0 == strcmp("empty", outputFormat.c_str())) {
sim_result = new simulation_result_empty(result_file_cstr.c_str(),maxSteps);
} else if (0 == strcmp("csv", outputFormat.c_str())) {
sim_result = new simulation_result_csv(result_file_cstr.c_str(), maxSteps);
} else if (0 == strcmp("mat", outputFormat.c_str())) {
sim_result = new simulation_result_mat(result_file_cstr.c_str(), start, stop);
} else { /* Default to plt */
} else if (0 == strcmp("plt", outputFormat.c_str())) {
sim_result = new simulation_result_plt(result_file_cstr.c_str(), maxSteps);
} else {
cerr << "Unknown output format: " << outputFormat << endl;
return 1;
}
if (sim_verbose) {
cout << "Allocated simulation result data storage for method '"
Expand Down
2 changes: 1 addition & 1 deletion c_runtime/simulation_runtime.h
Expand Up @@ -86,7 +86,7 @@ extern string TermMsg; /* message for termination. */
extern simulation_result *sim_result;
/* function with template for linear model */
int linear_model_frame(string& out, string A, string B, string C, string D, string x_startvalues, string u_startvalues);
int callSolver(int, char**, string, string, double, double, double, long, double);
int callSolver(int, char**, string, string, string, double, double, double, long, double);

#endif /* cplusplus */

Expand Down

0 comments on commit 0b1cd57

Please sign in to comment.