Guy on Simulink

Simulink & Model-Based Design

Tips for simulating models in parallel 7

Posted by Guy Rouleau,

Update: In MATLAB R2017a the function PARSIM got introduced. For a better experience simulating models in parallel, we recommend using PARSIM instead of SIM inside parfor. See the more recent blog post Simulating models in parallel made easy with parsim for more details.


As I mentioned many times on this blog, running simulation in parallel using the Parallel Computing Toolbox can save you a lot of time. Today I want to share a few tips to help you getting started with running simulations in parallel.

Serial Parameter Sweep

Before going parallel, let's first look at the standard loop simulating a model. There are tons of ways to setup MATLAB and Simulink to do a parameter sweep. The most common simple setup I see is the following, where we do:

  • Load the model using load_system
  • Initialize the MATLAB base workspace using a script
  • Define a vector of values over which you want to sweep
  • Inside the loop, index into the parameter vector
  • Simulate using sim

For this example, let's use a simple mass-spring-damper model simulation.

Model to be simulated

We have an initialization script that initializes three variables.

initialization script

And we do the parameter sweep using this code.

Serial run

Simulink and Transparency

When simulating models inside parfor, there are a few intricacies about how parfor manages variables that need to be taken into account. To begin on that topic, I recommend going through the documentation pages about Classification of Variables in parfor-Loops, and Transparency.

Let me resume in a few bullets the important lessons you will learn in those documentation pages:

  • Parallel workers are independent MATLAB sessions to which parfor sends code and data to process.
  • For efficiency, parfor classifies variables in many categories and sends only the ones it sees as needed
  • The code sent to the workers is not executed in their base workspace, but in a special function workspace
  • Variables needed by a Simulink model are not "visible" to parfor, and consequently not sent automatically to the workers
  • By default, Simulink looks in the base workspace for the variables it needs

Because of those facts, making simulink run inside parfor requires a few tricks. Let's see two techniques to simulate model inside parfor.

Parameter Sweep in the Base Workspace

In this first technique, we counteract the fact that Simulink is not transparent by violating transparency using evalin and/or assignin. Yes... I like to live dangerously!

To be as efficient as possible, we first use an spmd statement to put in the worker's base workspace the data needed by the model that remains constant during the parameter sweep. Then inside the parfor loop, we put in the base workspace the data that changes every iteration, and call sim.

Parallel sweep in base workspace

Parameter Sweep in a Function Workspace

In this second technique, we do the opposite. We hide the fact that Simulink is not transparent by doing everything inside a function workspace.

Parallel sweep in function workspace

Where the function calling sim sets the SrcWorkspace parameter to point to the current workspace:

Calling sim

Note about the future

Be reassured that we are actively working on features to make simulating models inside parfor easier in the future. However, since we are unfortunately forced to live in the present, I thought sharing some of those intricacies might help a few of you setting up their environment to simulate models in parallel.

Now it's your turn

Try setting up your models to simulate in parallel and let us know how that goes by leaving a comment here.

7 CommentsOldest to Newest

Nam replied on : 1 of 7

Thanks you for this post and previous post about how to simulate models in parallel.

Currently, I simulate a model (only change parameters) many times in one day. I also use “Simulation Data Inspector” to examine various signals. However, I had also tried to simulate models parallelly with “Simulation Data Inspector”, but it did not work.

May you have a post about how to simulate models parallely with Simulation Data Inspector ?
Thank you very much.

Ralf replied on : 2 of 7

Hi Guy,
Thank you very much for this very interesting post.
But I’ve got one question: What is the motivation of using the spmd command? I tried your example and it seems to run without it, too.

Kind regards.

Sean de Wolski replied on : 3 of 7

Ralf, had you run it once before with spmd? If so, the model is still loaded so the subsequent run would not require it. Because the parallel worker has its own workspace, the model needs to be loaded into that workspace. spmd does this.

Guy Rouleau replied on : 4 of 7

@Ralf: As Sean describe, SPMD is used to run the things that need to be done only once on the workers. In this example, executing the initialization script needs to be done only once on each worker. Doing it in the simulation loop would add extra cost.

Nathalia replied on : 5 of 7

Hello, Thanks for your post. I’m starting with the simulations in parallel, i need that the parameter named L_VC1 vary in the model, i tried with Parameter Sweep in the Base Workspace but i have this error: In an assignment A(I) = B, the number of elements in B and I must be the same.
This is the code:

parfor i=1:N


%my InitScript

Do you know why?. Another question, how i should do for take the outputs in my model that I need?.

Thanks for your time. :)

David Garcia Luna replied on : 7 of 7

Can this be used with a model running in External with Simulink Real-Time Windows Target, and one running in Normal mode, and is there any limitation into using results of the external-ran model to the normal-ran model? This to solve compatibility issues with Solvers being used on a Model.

Best Regards,

Add A Comment

Your email address will not be published. Required fields are marked *

Preview: hide