Sunteți pe pagina 1din 3

getting started with running simulations in parallel.

Serial Parameter Sweep

Before going parallel, let's rst 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

Dene 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.

We have an initialization script that initializes three variables.

And we do the parameter sweep using this code.

Simulink and Transparency

When simulating models insideparfor, 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 aboutClassication 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 efciency, parfor classies 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 a


v riables 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 rst 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 efcient as possible, we rst use anspmd 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 veery iteration, and call sim.

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.

Where the function calling sim sets the SrcWorkspace parameter to point to the current workspace:
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 ewf 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.

S-ar putea să vă placă și