0
0
SimulinkHow-ToBeginner · 4 min read

How to Use Parallel Simulation in Simulink for Faster Model Runs

To use parallel simulation in Simulink, enable the UseParallel option in the Simulation Manager or use the parsim function to run multiple simulations simultaneously across CPU cores. This speeds up running many simulations by distributing them in parallel.
📐

Syntax

The main way to run parallel simulations in Simulink is using the parsim function. Its basic syntax is:

  • simOut = parsim(models, 'Name', Value)

Where:

  • models is a cell array of model names or a single model name.
  • 'Name', Value pairs control options like number of workers, simulation inputs, and output handling.
  • simOut stores the simulation results for each run.
matlab
simOut = parsim({'model1', 'model2'}, 'ShowProgress', 'on', 'UseParallel', true);
💻

Example

This example runs the same Simulink model multiple times with different input parameters in parallel using parsim. It shows how to speed up batch simulations.

matlab
model = 'sldemo_mdlref_basic';

% Create simulation input objects with different parameters
simIn(1) = Simulink.SimulationInput(model);
simIn(1) = simIn(1).setVariable('Gain', 1);
simIn(2) = Simulink.SimulationInput(model);
simIn(2) = simIn(2).setVariable('Gain', 5);
simIn(3) = Simulink.SimulationInput(model);
simIn(3) = simIn(3).setVariable('Gain', 10);

% Run simulations in parallel
simOut = parsim(simIn, 'ShowProgress', 'on', 'UseParallel', true);

% Extract and display output
for i = 1:numel(simOut)
    y = simOut(i).logsout.getElement('y').Values.Data;
    fprintf('Simulation %d output (Gain=%d): %s\n', i, simIn(i).getVariable('Gain'), mat2str(y(1:3)'));
end
Output
Simulation 1 output (Gain=1): [0 0 0] Simulation 2 output (Gain=5): [0 0 0] Simulation 3 output (Gain=10): [0 0 0]
⚠️

Common Pitfalls

Common mistakes when using parallel simulation in Simulink include:

  • Not enabling the Parallel Computing Toolbox, which is required for parsim to run in parallel.
  • Using shared resources or files in simulations that cause conflicts when run simultaneously.
  • Not setting UseParallel to true, which causes simulations to run sequentially.
  • Ignoring simulation input setup, leading to all parallel runs using the same parameters.

Always verify your model supports parallel runs and that inputs are properly varied.

matlab
%% Wrong way: runs sequentially
simOut = parsim(simIn, 'UseParallel', false);

%% Right way: runs in parallel
simOut = parsim(simIn, 'UseParallel', true);
📊

Quick Reference

Tips for using parallel simulation in Simulink:

  • Use parsim with UseParallel set to true to enable parallel runs.
  • Prepare different simulation inputs using Simulink.SimulationInput objects.
  • Ensure Parallel Computing Toolbox is installed and a parallel pool is available.
  • Check your model for any shared resource conflicts before running in parallel.
  • Use ShowProgress option to monitor simulation progress.

Key Takeaways

Use the parsim function with UseParallel set to true to run simulations in parallel.
Create separate SimulationInput objects to vary parameters across parallel runs.
Ensure the Parallel Computing Toolbox is installed and a parallel pool is active.
Avoid shared resource conflicts in your model when running parallel simulations.
Use ShowProgress option to track simulation status during parallel execution.