Parallel Computing Toolbox

Parallel Simulations Using Parfor: Parameter Sweep in Rapid Accelerator Mode

This example shows how you can run multiple Simulink® simulations of a Monte Carlo study in parallel by using Parallel Computing Toolbox™ with Rapid Accelerator. Before running this example, make sure you are in a writable directory. This involves six main steps:

  1. Start MATLAB® workers on your machine

  2. Build the rapid accelerator target

  3. Set up data required for the multiple simulations

  4. Run multiple simulations in a parfor (parallel for) loop

  5. Plot results of multiple simulations

  6. Close MATLAB workers

Model Overview

The model sldemo_suspn_3dofsldemo_suspn_3dof shown below simulates the vehicle dynamics based on the road - suspension interaction for different road profiles. The vehicle dynamics are captured in three degrees of freedom: vertical displacement, roll, and pitch. The road profile data for the left and right tires is imported into the Signal Builder block as different test groups. The Road-Suspension Interaction subsystem calculates the suspension forces on the vehicle at the four tire locations based on the road data and the current vehicle state. In the Body Dynamics subsystem these forces and the resulting pitch and roll moments are used to determine the vehicle motion in three degrees of freedom: vertical displacement, roll, and pitch.

In this Monte Carlo study, the vehicle mass is varied to study its effect on the vehicle dynamics. Parallel Computing Toolbox is used to speed up these multiple simulations, as illustrated below.

mdl               = 'sldemo_suspn_3dof';
isModelOpen       = bdIsLoaded(mdl);
open_system(mdl);

Step 1: Start MATLAB Workers on Your Machine

First configure and start a pool of MATLAB workers. Use the parpool command to check if a parallel pool is open, and then open a local pool on your multicore desktop. This requires Parallel Computing Toolbox.

apool = gcp('nocreate');
if isempty(apool)
    apool = parpool('local');
end
Starting parallel pool (parpool) using the 'local' profile ... connected to 12 workers.

Step 2: Build the Rapid Accelerator Target Once

First, make sure you are in a writable directory because in this step we will generate extra files. Then open the model and build the Rapid Accelerator executable for the model using the buildRapidAcceleratorTargetbuildRapidAcceleratorTarget function from the Simulink.BlockDiagram package. The buildRapidAcceleratorTarget function returns the default run-time parameter set that is used to create multiple datasets in step 3.

open_system(mdl);
rtp          = Simulink.BlockDiagram.buildRapidAcceleratorTarget(mdl);
### Building the rapid accelerator target for model: sldemo_suspn_3dof
### Successfully built the rapid accelerator target for model: sldemo_suspn_3dof

Step 3: Set up Data Required for the Multiple Simulations

Create a vector of run-time parameter structures for the different vehicle masses by using the default run-time parameter structure from step 2. Use the function modifyTunableParametersmodifyTunableParameters from the Simulink.BlockDiagram package to change the parameter corresponding to vehicle mass. Since there is only a parameter being changed here, it make sense to use a for loop instead of a parfor loop. Also preallocate an array of SimulationOutput objects in the Simulink package that will be used to collect the simulation results in step 4.

Mb_sweep            = Mb*(0.5:5:45.5);
iterations          = length(Mb_sweep);
for i = iterations:-1:1
    parameterSet(i) = Simulink.BlockDiagram.modifyTunableParameters(rtp, ...
                                                      'Mb',Mb_sweep(i));
end
simout(iterations)  = Simulink.SimulationOutput;

Step 4: Run Multiple Simulations in a parfor (Parallel for) Loop

Run the multiple simulations corresponding to the different parameter sets within a parfor loop. The simsim command returns all the simulation data as a SimulationOutput object of the Simulink package. Note that the simulation results are collected in the simout variable, which was preallocated in step 3. To run the sim command in Rapid Accelerator mode from inside a parfor loop, the quickest way is to set the sim command argument 'RapidAcceleratorUpToDateCheck' to 'off'. The parameter structures are passed by using the 'RapidAcceleratorParameterSets' argument.

parfor idx = 1:iterations
    simout(idx) = sim(mdl,'SimulationMode','rapid',...
                      'RapidAcceleratorUpToDateCheck','off', ...
                      'RapidAcceleratorParameterSets',parameterSet(idx));
end

Step 5: Plot Results of Multiple Simulations

Plot the vertical vehicle displacement from the different simulations to see how varying the vehicle mass affected the vehicle dynamics. Use the get method of the SimulationOutput object to obtain the time and signal data contained in each element of simout.

legend_labels = cell(1,iterations);
for i = 1:iterations
        si = simout(i);
        ts = si.get('logsout').get('vertical_disp').Values;
        ts.plot;
        legend_labels{i} = ['Run ' num2str(i)];
        hold all
end
title('Response of a 3-DoF Suspension Model')
xlabel('Time (s)');
ylabel('Vehicle vertical displacement (m)');
legend(legend_labels,'Location','NorthEastOutside');

Step 6: Close MATLAB Workers

Last, close the parallel pool and the model if they were not previously opened.

if(~isModelOpen)
    close_system(mdl, 0);
end
delete(apool);