When simulation execution time exceeds the time required for code generation, accelerator and rapid accelerator simulation modes give speed improvement compared to normal mode. Accelerator and rapid accelerator modes generally perform better than normal mode when simulation execution times are several minutes or more. However, models with a significant number of Stateflow® or MATLAB Function blocks might show only a small speed improvement over normal mode because these blocks also simulate through code generation in normal mode.
Including tunable parameters in your model can also increase the simulation time.
The figure shows the relative performance of normal mode, accelerator mode, and rapid accelerator mode simulations in general terms for a hypothetical model.
Performance When Target Must Be Rebuilt
The solid lines, labeled all targets out of date, in the figure show performance when the target code must be rebuilt. For this hypothetical model, the time scale is on the order of minutes. However, the time scale could be longer for more complex models.
Compiling a model in normal mode generally requires less time than building the accelerator target or the rapid accelerator executable. For small simulation stop times, normal mode results in quicker overall simulations compared to the accelerator and rapid accelerator modes.
The crossover point where accelerator mode or rapid accelerator mode results in faster overall simulation depends on the complexity and content of your model. For example, models that contain large numbers of blocks that use interpreted code might not run much faster in accelerator mode than they would in normal mode unless the simulation stop time is very large. For more information, see Select Blocks for Accelerator Mode. Similarly, models with a large number of Stateflow charts or MATLAB Function blocks might not show much speed improvement over normal mode unless the simulation stop time is large. You can speed up the simulation of models with Stateflow or MATLAB Function blocks through code generation.
The figure represents a model with a large number of Stateflow charts or MATLAB Function blocks. The curve labeled Normal would have a much smaller initial elapsed time than shown if the model did not contain these blocks.
Performance When Targets Are Up to Date
The dashed lines, labeled all targets up to date, in the figure show that the time to determine whether the accelerator target or the rapid accelerator executable is up to date is significantly smaller than the time required to generate code, which is represented by the solid lines, labeled all targets out of date. You can take advantage of this characteristic when you wish to test various design tradeoffs.
For instance, you can generate the accelerator mode target once and use it to simulate your model with a series of gain settings. This method is especially efficient for the accelerator or rapid accelerator modes because this type of change does not result in the target code being regenerated. The target code is generated the first time the model runs, but on subsequent runs, the software spends only the time necessary to verify that the target is up to date. This process is much faster than generating code, so subsequent runs can be significantly faster than the initial run.
Because checking the targets is quicker than code generation, the crossover point is smaller when the target is up to date than when code must be generated. Subsequent runs of your model might simulate faster in accelerator or rapid accelerator mode when compared to normal mode, even for small stop times.
Analyze Performance of Simulation Modes
To see the effect of using accelerator or rapid accelerator mode for simulations of your model, you can run equivalent simulations using different simulation modes. Simulate the model in a way that returns the results as a single
Simulink.SimulationOutput object that includes simulation metadata. That way, you can inspect the timing information captured in the simulation metadata for each simulation to analyze the performance of each simulation mode.
This example uses
Simulink.SimulationInput objects to configure model parameter values to use for three simulations of the model
sldemo_fuelsys. For more information about the model, see Model a Fault-Tolerant Fuel Control System.
model = "sldemo_fuelsys";
To configure the baseline simulation, create a
simIn = Simulink.SimulationInput(model);
SimulationInput object stores parameter values to use in the simulation. The parameter values on the object are applied for the simulation and reverted at the end of simulation so that the model remains unchanged.
Set the stop time to
10000. Set the simulation mode to
simIn = setModelParameter(simIn,"StopTime","10000"); simIn = setModelParameter(simIn,"SimulationMode","normal");
To capture baseline timing information, simulate the model.
simOut = sim(simIn);
The simulation returns results as a single
Simulink.SimulationOutput object that contains all logged data and simulation metadata. The metadata is stored as a
Simulink.SimulationMetadata object that contains information about the simulation, including a structure,
TimingInfo, that contains timing information.
normalMode = simOut.SimulationMetadata.TimingInfo;
From the timing information, extract the initialization time, execution time, and total elapsed time for the simulation.
normalInit = normalMode.InitializationElapsedWallTime; normalExec = normalMode.ExecutionElapsedWallTime; normalTotal = normalMode.TotalElapsedWallTime;
Simulate the model again using rapid accelerator mode. The first time you simulate a model in rapid accelerator mode, the rapid accelerator target builds during the initialization phase.
simIn = setModelParameter(simIn,"SimulationMode","rapid"); simOut = sim(simIn);
### Building the rapid accelerator target for model: sldemo_fuelsys ### Successfully built the rapid accelerator target for model: sldemo_fuelsys Build Summary Top model rapid accelerator targets built: Model Action Rebuild Reason ================================================================================================ sldemo_fuelsys Code generated and compiled. Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 38.279s
Access the timing information for the first rapid accelerator simulation. Then, extract the initialization time, execution time, and total elapsed time for the simulation.
rapidAccel = simOut.SimulationMetadata.TimingInfo; rapidBuildInit = rapidAccel.InitializationElapsedWallTime; rapidBuildExec = rapidAccel.ExecutionElapsedWallTime; rapidBuildTotal = rapidAccel.TotalElapsedWallTime;
Simulate again to see the benefit of rapid accelerator mode for a simulation that does not build the rapid accelerator target.
simOut = sim(simIn);
Build Summary 0 of 1 models built (1 models already up to date) Build duration: 0h 0m 2.684s
Access the timing information for the rapid accelerator simulation that ran without building the target. Then, extract the initialization time, the execution time, and the total elapsed time for the simulation.
rapidAccelNoRebuild = simOut.SimulationMetadata.TimingInfo; rapidInit = rapidAccelNoRebuild.InitializationElapsedWallTime; rapidExec = rapidAccelNoRebuild.ExecutionElapsedWallTime; rapidTotal = rapidAccelNoRebuild.TotalElapsedWallTime;
Build a table to compare the timing for each simulation.
InitializationTime = [normalInit;rapidBuildInit;rapidInit]; ExecutionTime = [normalExec;rapidBuildExec;rapidExec]; TotalSimulationTime = [normalTotal;rapidBuildTotal;rapidTotal]; simNames = ["Normal Mode";"Rapid Accelerator";"Rapid Accelerator No Rebuild"]; timingTable = table(TotalSimulationTime,InitializationTime, ... ExecutionTime,RowNames=simNames);
In the first rapid accelerator simulation, the initialization time increases because the rapid accelerator target builds during the initialization phase. For this model and simulation duration, using rapid accelerator mode reduces the execution time so much that the first rapid accelerator simulation is still faster than using normal mode.
timingTable=3×3 table TotalSimulationTime InitializationTime ExecutionTime ___________________ __________________ _____________ Normal Mode 108.91 12.411 96.437 Rapid Accelerator 68.44 40.962 26.152 Rapid Accelerator No Rebuild 17.403 3.387 13.886