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 = "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.007727039000000001,
    timeBackend = 0.004599842000000001,
    timeSimCode = 0.058917203,
    timeTemplates = 0.005182301,
    timeCompile = 0.233172498,
    timeSimulation = 0.05251616200000001,
    timeTotal = 0.362213358
end SimulationResult;
"Warning: The initial conditions are not fully specified. Use +d=initialization for more information.
Warning: There are iteration variables with default zero start attribute. Use +d=initialization for more information.
"

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  
Output format  
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.000451 9.05%
Initialization 0.000145 2.91%
Event-handling 0.000064 1.28%
Creating output file 0.000734 14.73%
Linearization 0.000000 0.00%
Time steps 0.003115 62.51%
Overhead 0.000607 12.18%
Unknown 0.000318 -2.67%
Total simulation time 0.004983 100.00%

Global Steps

  Steps Total Time Fraction Average Time Max Time Deviation
Graph thumbnail 999 499 0.003115 62.51% 6.24248496993988e-06 0.000144054 22.08x

Measured Function Calls

  Name Calls Time Fraction Max Time Deviation
Graph thumbnail function fun0Graph thumbnail count function fun0 ProfilingTest.f 506 0.000036849 0.74% 0.000000951 12.06x

Measured Blocks

  Name Calls Time Fraction Max Time Deviation
Graph thumbnail eq0Graph thumbnail count eq0 ` <#eq0>`__ 12 0.000065073 1.31% 0.000066943 11.34x
Graph thumbnail eq8Graph thumbnail count eq8 ` <#eq8>`__ 2 0.000003197 0.06% 0.000003367 1.11x
Graph thumbnail eq12Graph thumbnail count eq12 ` <#eq12>`__ 504 0.000601869 12.08% 0.000004250 2.56x
Graph thumbnail eq14Graph thumbnail count eq14 ` <#eq14>`__ 504 0.000822441 16.50% 0.000003880 1.38x

Equations

Name Variables
   
  ` <#var0>`__
  ` <#var0>`__
  ` <#var0>`__
  ` <#var0>`__
   
  ` <#var0>`__
   
  ` <#var0>`__
  ` <#var0>`__
  ` <#var0>`__
   
  ` <#var0>`__
   
  ` <#var0>`__

Genenerated JSON for the Example

Listing 4 ProfilingTest_prof.json
{
"name":"ProfilingTest",
"prefix":"ProfilingTest",
"date":"2015-10-05 23:02:28",
"method":"",
"outputFormat":"",
"outputFilename":"ProfilingTest_res.mat",
"outputFilesize":24569,
"overheadTime":0.000606963,
"preinitTime":0.000451422,
"initTime":0.00014528,
"eventTime":6.377e-05,
"outputTime":0.000734183,
"linearizeTime":0,
"totalTime":0.00498288,
"totalStepsTime":5.74e-06,
"totalTimeProfileBlocks":0.00149258,
"numStep":499,
"maxTime":0.000144054,
"functions":[
{"name":"ProfilingTest.f","ncall":506,"time":0.000036849,"maxTime":0.000000951}
],
"profileBlocks":[
{"id":0,"ncall":12,"time":0.000065073,"maxTime":0.000066943},
{"id":8,"ncall":2,"time":0.000003197,"maxTime":0.000003367},
{"id":12,"ncall":504,"time":0.000601869,"maxTime":0.000004250},
{"id":14,"ncall":504,"time":0.000822441,"maxTime":0.000003880}
]
}

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 70 Setting up the profiler from OMEdit.

When profiling the DoublePendulum example from MSL, the following output in Figure 71 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 71 Profiling results of the Modelica standard library DoublePendulum example, sorted by execution time.