Академический Документы
Профессиональный Документы
Культура Документы
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:
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
Because of those facts, making simulink run inside parfor requires a few tricks. Let's see two techniques to simulate model inside parfor.
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.
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.
Try setting up your models to simulate in parallel and let us know how that goes by leaving a comment here.