Modelica Performance Analyzer

A common problem when simulating models in an equation-based language like Modelica is that the model may contain non-linear equation systems. These are solved in each time-step by extrapolating an initial guess and running a non-linear system solver. If the simulation takes too long to simulate, it is useful to run the performance analysis tool. The tool has around 5~25% overhead, which is very low compared to instruction-level profilers (30x-100x overhead). Due to being based on a single simulation run, the report may contain spikes in the charts.

When running a simulation for performance analysis, execution times of user-defined functions as well as linear, non-linear and mixed equation systems are recorded.

To start a simulation in this mode, just use the measureTime flag of the simulate command.

>>> simulate(modelname, measureTime = true)

The generated report is in HTML format (with images in the SVG format), stored in a file modelname_prof.html, but the XML database and measured times that generated the report and graphs are also available if you want to customize the report for comparison with other tools.

Below we use the performance profiler on the simple model A:

model ProfilingTest
  function f
    input Real r;
    output Real o = sin(r);
  end f;
  String s = "abc";
  Real x = f(x) "This is x";
  Real y(start=1);
  Real z1 = cos(z2);
  Real z2 = sin(z1);
equation
  der(y) = time;
end ProfilingTest;

We simulate as usual, but set measureTime=true to activate the profiling:

>>> setCommandLineOptions("--profiling=blocks+html")
true
>>> simulate(ProfilingTest)
record SimulationResult
    resultFile = "«DOCHOME»/ProfilingTest_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'ProfilingTest', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.
LOG_SUCCESS       | info    | The simulation finished successfully.
Warning: empty y range [1:1], adjusting to [0.99:1.01]
Warning: empty y range [1:1], adjusting to [0.99:1.01]
Warning: empty y range [1:1], adjusting to [0.99:1.01]
Warning: empty y range [1:1], adjusting to [0.99:1.01]
Warning: empty y range [1:1], adjusting to [0.99:1.01]
Warning: empty y range [1:1], adjusting to [0.99:1.01]
stdout            | info    | Time measurements are stored in ProfilingTest_prof.html (human-readable) and ProfilingTest_prof.xml (for XSL transforms or more details)
",
    timeFrontend = 0.011138484,
    timeBackend = 0.015242983,
    timeSimCode = 0.184932244,
    timeTemplates = 0.08449110599999998,
    timeCompile = 0.6603084340000001,
    timeSimulation = 0.075549871,
    timeTotal = 1.031781065
end SimulationResult;
"Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
Warning: There are iteration variables with default zero start attribute. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
"

Profiling information for ProfilingTest

Information

All times are measured using a real-time wall clock. This means context switching produces bad worst-case execution times (max times) for blocks. If you want better results, use a CPU-time clock or run the command using real-time priviliges (avoiding context switches).

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.

For more details, see ProfilingTest_prof.xml.

Settings

Name Value
Integration method dassl
Output format mat
Output name ProfilingTest_res.mat
Output size 24.0 kB
Profiling data ProfilingTest_prof.data
Profiling size 0 B

Summary

Task Time Fraction
Pre-Initialization 0.000120 2.45%
Initialization 0.000161 3.29%
Event-handling 0.000029 0.59%
Creating output file 0.000446 9.12%
Linearization   NaN%
Time steps 0.003321 67.90%
Overhead 0.000380 7.77%
Unknown NaN NaN%
Total simulation time 0.004891 100.00%

Global Steps

  Steps Total Time Fraction Average Time Max Time Deviation
Graph thumbnail 999 499 0.003321 67.90% 6.65531062124249e-06 0.000070390 9.58x

Measured Function Calls

  Name Calls Time Fraction Max Time Deviation
Graph thumbnail function fun0Graph thumbnail count function fun0 ProfilingTest.f 506 0.000024250 0.50% 0.000001082 21.58x

Measured Blocks

  Name Calls Time Fraction Max Time Deviation
Graph thumbnail eq0Graph thumbnail count eq0 ` <#eq0>`__ 7 0.000086521 1.77% 0.000088261 6.14x
Graph thumbnail eq12Graph thumbnail count eq12 ` <#eq12>`__ 2 0.000003106 0.06% 0.000003396 1.19x
Graph thumbnail eq20Graph thumbnail count eq20 ` <#eq20>`__ 504 0.000607847 12.43% 0.000031547 25.16x
Graph thumbnail eq22Graph thumbnail count eq22 ` <#eq22>`__ 504 0.001040456 21.27% 0.000027523 12.33x

Equations

Name Variables
`eq0 <>`__  
`eq1 <>`__ y
`eq2 <>`__ s
`eq3 <>`__  
`eq4 <>`__ z2
`eq5 <>`__  
`eq6 <>`__ ` <#var0>`__
`eq7 <>`__ ` <#var0>`__
`eq8 <>`__ ` <#var0>`__
`eq9 <>`__ ` <#var0>`__
`eq10 <>`__ z1
`eq11 <>`__  
`eq12 <>`__ x
`eq13 <>`__ der(y)
`eq14 <>`__ z2
`eq15 <>`__  
`eq16 <>`__ ` <#var0>`__
`eq17 <>`__ ` <#var0>`__
`eq18 <>`__ ` <#var0>`__
`eq19 <>`__ ` <#var0>`__
`eq20 <>`__ z1
`eq21 <>`__  
`eq22 <>`__ x
`eq23 <>`__  

Variables

Name Comment
y  
der(y)  
x This is x
z1  
z2  
s  

This report was generated by OpenModelica on 2018-12-20 23:40:20.

Genenerated JSON for the Example

Listing 6 ProfilingTest_prof.json
{
"name":"ProfilingTest",
"prefix":"ProfilingTest",
"date":"2018-12-20 23:40:20",
"method":"dassl",
"outputFormat":"mat",
"outputFilename":"ProfilingTest_res.mat",
"outputFilesize":24581,
"overheadTime":0.00038039,
"preinitTime":0.000120346,
"initTime":0.000161287,
"eventTime":2.8521e-05,
"outputTime":0.00044569,
"jacobianTime":1.4008e-05,
"totalTime":0.0048909,
"totalStepsTime":6.191e-06,
"totalTimeProfileBlocks":0.00173793,
"numStep":499,
"maxTime":7.039e-05,
"functions":[
{"name":"ProfilingTest.f","ncall":506,"time":0.000024250,"maxTime":0.000001082}
],
"profileBlocks":[
{"id":0,"ncall":7,"time":0.000086521,"maxTime":0.000088261},
{"id":12,"ncall":2,"time":0.000003106,"maxTime":0.000003396},
{"id":20,"ncall":504,"time":0.000607847,"maxTime":0.000031547},
{"id":22,"ncall":504,"time":0.001040456,"maxTime":0.000027523}
]
}

Using the Profiler from OMEdit

When running a simulation from OMEdit, it is possible to enable profiling information, which can be combined with the transformations browser.

Profiling setup

Figure 96 Setting up the profiler from OMEdit.

When profiling the DoublePendulum example from MSL, the following output in Figure 97 is a typical result. This information clearly shows which system takes longest to simulate (a linear system, where most of the time overhead probably comes from initializing LAPACK over and over).

Profiling results

Figure 97 Profiling results of the Modelica standard library DoublePendulum example, sorted by execution time.