Run Parallel Simulations - MATLAB & Simulink (original) (raw)
Main Content
The parsim command allows you to run parallel (simultaneous) simulations of your model (design). In this context, parallel runs mean multiple simulations at the same time on different workers. The parsim
function makes it easy for you to run the same model with different inputs or different parameter settings in scenarios such as Monte Carlo analyses, parameter sweeps, model testing, experiment design, and model optimization. Running a single simulation in parallel by decomposing the model into smaller components and running those individual pieces simultaneously on multiple workers is currently not supported.
You can run multiple simulations from the Simulink Editor using theMultiple Simulations panel. You can pick a block parameter of interest from the model canvas and specify values that you want to use for the simulations. With theMultiple Simulations panel, you can provide configurations such asUse Parallel to run your simulations in parallel. To run the simulations that you have set up, first, select the design study, then in the Simulink® Toolstrip, on the Simulation tab, click Run All. Once the simulations are complete, you can view and manage the results inSimulation Manager. For more information, see Use Multiple Simulations Panel to Run Design Study with Different Parameter Values and Configure and Run Simulations with Multiple Simulations Panel.
To run the simulations in parallel with using the parsim
function, you need a Parallel Computing Toolbox™ for local workers. In addition, you can use MATLAB® Parallel Server™ for multiple computer clusters, clouds, and grids. In the absence of Parallel Computing Toolbox and MATLAB Parallel Server, the parsim
function runs the simulations in serial. For more information, see Parallel Computing Toolbox and MATLAB Parallel Server.
If no parallel pool exists, the parsim
function creates a pool from the default cluster profile. To use a cluster other than the default, create a pool with that cluster profile before calling the parsim
function.
This example runs multiple simulations in parallel for a set of sweep parameters.
mdl = "sldemo_suspn_3dof"; openExample(... "simulink_features/MultipleSimulationsWorkflowTipsExample",... supportingFile=mdl)
Cfsweep = 2500*(0.05:0.1:0.95); numSims = numel(Cfsweep);
simIn(1:numSims) = Simulink.SimulationInput(mdl); for idx = 1:numSims simIn(idx) = setBlockParameter(simIn(idx),... (mdl + "/Road-Suspension Interaction"),... "Cf",num2str(Cfsweep(idx))); end
simOut = parsim(simIn);
How parsim
Works
The parsim
function runs simulations with different parameters and values based on the Simulink.SimulationInput object. EachSimulationInput
object specifies one simulation of the model. An array of these objects can be created for multiple simulations. For more information, see Running Multiple Simulations.
You can use these functions and properties on the Simulink.SimulationInput object:
setVariables
- Change variables in base workspace, data dictionary, or model workspacesetBlockParameter
- Change block parameterssetModelParameter
- Change model parameterssetPreSimFcn
- Specify MATLAB functions to run before each simulation for customization and post-processing results on the clustersetPostSimFcn
- Specify MATLAB functions to run after each simulation for customization and post-processing results on the clusterInitialState
- Change the Initial StateExternalInput
- Specify a numerical array, timeseries, or Dataset object as external inputs to the model
This flow chart shows a general sequence of events that occur when theparsim
function is executed
Changes to model library blocks can be overwritten when using theparsim
function. When models are set up on new workers, model inherits properties directly from the worker library. Use the SetUpFcn
name-value argument for the parsim
function to transfer the model library block changes to the workers.
See Also
Objects
Functions
- applyToModel | setBlockParameter | setModelParameter | setInitialState | setExternalInput | setVariable | validate | setPreSimFcn | setPostSimFcn