Main Content

Multithreaded Simulation Using For Each Subsystem

This example shows how to speed up execution of a model on multiple cores using a For Each subsystem. To see the effect of multithreaded execution, this example compares the simulation performance in rapid accelerator mode between single-threaded and multithreaded execution.

In this model, a For Each subsystem accepts a numeric array input. Each element of the array serves as an upper bound in each iteration and feeds into a computationally-intensive algorithm represented by the MATLAB Function block inside the subsystem. The algorithm implemented in the MATLAB Function block computes the largest prime number within a given range using a brute force method, which is for illustrative purposes only.

Open the model.

mdl = 'slexForEachMultithreaded';
open_system(mdl);

Measure Single-Threaded Simulation Time

This code shows how to capture timing information for simulation of the slexForEachMultithreaded model. To configure the baseline simulation, create a Simulink.SimulationInput object. The SimulationInput object stores parameter values to use in the simulation.

simIn = Simulink.SimulationInput(mdl);

By default, the multithreaded simulation support for the For Each subsystem is enabled. To opt out of multithreaded simulation, use setModelParameter to set the MultithreadedSim parameter. In a single-threaded simulation, the computations in different iterations of the For Each subsystem are executed sequentially during each time step. For more information about the MultithreadedSim parameter, see Using the MultithreadedSim Parameter.

simIn = setModelParameter(simIn, MultithreadedSim='off');

To capture single-threaded timing information, simulate the model and extract the total execution time.

simOut = sim(simIn);
singleThreadTimingInfo = simOut.SimulationMetadata.TimingInfo;
singleThreadExec = singleThreadTimingInfo.ExecutionElapsedWallTime;
### Searching for referenced models in model 'slexForEachMultithreaded'.
### Total of 1 models to build.
### Building the rapid accelerator target for model: slexForEachMultithreaded
### Successfully built the rapid accelerator target for model: slexForEachMultithreaded

Measure Multithreaded Simulation Time

To explicitly opt in to multithreading support, use setModelParameter. In a multithreaded simulation, the software assigns computations in different iterations of the For Each subsystem to multiple cores. To speed up the simulation, the system performs these computations in parallel.

simIn = setModelParameter(simIn, MultithreadedSim='auto');

To capture multithreaded timing information, simulate the model and extract the total execution time.

simOut = sim(simIn);
multithreadTimingInfo = simOut.SimulationMetadata.TimingInfo;
multithreadExec = multithreadTimingInfo.ExecutionElapsedWallTime;
### Searching for referenced models in model 'slexForEachMultithreaded'.
### Total of 1 models to build.
### Building the rapid accelerator target for model: slexForEachMultithreaded
### Successfully built the rapid accelerator target for model: slexForEachMultithreaded

Calculate Simulation Performance

To calculate the increase in performance, divide the execution time by the execution time of the single-threaded simulation. The simulation time performance may vary on different hardware.

Build a table to compare the timing for each simulation.

ExecutionTime = [singleThreadExec; multithreadExec];
SimulationPerformance = [singleThreadExec/singleThreadExec;
    multithreadExec/singleThreadExec];

simNames = ["Single-Threaded"; "Multithreaded"];
timingTable = table(ExecutionTime, SimulationPerformance, RowNames=simNames);
timingTable
timingTable =

  2×2 table

                       ExecutionTime    SimulationPerformance
                       _____________    _____________________

    Single-Threaded       67.646                     1       
    Multithreaded         44.339               0.65546       

Notice that the model simulation runs over two times faster than the single-threaded simulation on a machine with four or more cores. Remember that operating system context-switching in multithreaded simulations always results in extra overhead.

See Also

| |

Topics