How can I use the Parallel Computing Toolbox to run multiple Simulink simulations in a parallel or distributed fashion?

9 views (last 30 days)
I would like to know whether it is possible to use the Parallel Computing Toolbox to do either of the following:
1) Run multiple instances of one Simulink model on different cores, processors, or cluster machines. The instances would be running according to different, independent sets of parameters, or operating on different, independent data sets.
2) Run different Simulink models on different cores, processors, or cluster machines.
I am not able to find examples of this in the documentation. How might I go about doing either of the above?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Nov 2020
Edited: MathWorks Support Team on 5 Nov 2020
In order to leverage the capabilities of the Parallel Computing Toolbox with Simulink models, you can use the PARFOR function which allows you to run parallel Simulink simulations or create a distributed job whose tasks would be sent to the workers.
For using the PARFOR function, please refer to the following documentation link for detailed information:
If you have installed the help documentation, you may access the same page locally by typing the following at the MATLAB prompt:
web([docroot '/toolbox/simulink/ug/brsk2gr.html'])
Please note that using PARFOR to run parallel simulations is only available in MATLAB 7.7 (R2008b) and above.
For using distributed job, the following would be required:
1) The .mdl file of your Simulink model must be listed as a file dependency for each worker in the distributed job, and any other files upon which the model depends must be also listed. Also, any network path dependencies should be specified.
2) You would need to create a wrapper MATLAB file function around the SIM command in order to instruct each worker to simulate the model independently of the other workers. For more information on the SIM command, please refer to the following Help page in the documentation:
https://www.mathworks.com/help/simulink/slref/sim.html
If you have installed the help documentation, you may access the same page locally by typing the following at the MATLAB prompt:\n
web([docroot,'/toolbox/simulink/slref/sim.html'])
You may instruct each worker to perform different independent processing tasks via the parameters of your Simulink model. Here is one way you would create a distributed job by which a Simulink model is run independently on four workers, each processing a different data set. In this example, 'mymodel.mdl' is the Simulink model and 'mysimfunc.m' is the wrapper function MATLAB file around the SIM command.
The file 'mysimfunc.m' would be something along the lines of:
function [t,x,y] = mysimfunc(argin1, argin2, ..., arginN)
% Input argument parsing done here
[t,x,y] = sim('mymodel.mdl', ... )
NOTE: If you plan to run a different Simulink model on each worker, then a separate wrapper MATLAB file around the SIM command must be created for each model.
Here is the example:\n
% Find the Parallel Computing Toolbox scheduler
sched = findResource('scheduler','type','jobmanager')
% Create a distributed job on the Job Manager
j = createJob(sched)
% Set the file dependencies for the distributed job via the FileDependencies property. % Note that when running a different model on each worker, all MDL-files must be % listed as dependencies.
set(j,'FileDependencies',{'mymodel.mdl','mysimfunc.m', ... ,'mydatafile.dat'});
% Set the path dependencies via the PathDependencies property, where $DIR1, $DIR2, ... $DIRN
% are the directories containing supporting files for the simulation.
set(j,'PathDependencies',{'$DIR1','$DIR2',...,'$DIRN'});
% Create four independent tasks with different input arguments (argins) that determine
% the different model parameters to be used for the simulation on that worker.
% NOTE: If running different models on each worker, then the corresponding SIM
% wrapper MATLAB file name must be specified on each call to createTask for each model.
createTask(j, @mysimfunc, 3, {argin11 argin12 ... argin1N});
createTask(j, @mysimfunc, 3, {argin21 argin22 ... argin2N});
createTask(j, @mysimfunc, 3, {argin31 argin32 ... argin3N});
createTask(j, @mysimfunc, 3, {argin41 argin42 ... argin4N});
% Submit the distributed job to the local scheduler
submit(j);
waitForState(j);
% Get the results of the simulation.
results = getAllOutputArguments(j);
% Once the job is completed, it must be destroyed
destroy(j);
This example may be built upon to accommodate more complex models. For more information about the PCT functions as used above, the following Help page contains a full list of the functions available:
https://www.mathworks.com/help/parallel-computing/referencelist.html?type=function
If you have installed the Help documentation, you may access the same page locally by typing the following at the MATLAB prompt:\n
web([docroot,'/toolbox/distcomp/f1-6010.html'])
Likewise, for more information on PCT object properties (such as FileDependencies and PathDependencies), please refer to the following Help page:
https://www.mathworks.com/help/parallel-computing/release-notes.html?searchHighlight=pathdependencies
If you have installed the Help documentation, you may access the same page locally by typing the following at the MATLAB prompt:\n
web([docroot,'/toolbox/distcomp/f6-6010.html'])
Note that it is encouraged to run distributed Simulink simulations on a cluster of computers as opposed to a single multi-core machine, and the same goes for distributed MATLAB tasks as well. Time savings are gained by distributing each large simulation to cluster machines because their execution times largely outweigh the communication and initialization overhead on each worker. On a single multi-core computer, other significant limiting factors come into play, such as the fact that all the cores share a single memory bus. This same idea is echoed on the following MATLAB Help page:
https://www.mathworks.com/help/sldo/ug/how-to-use-parallel-computing-gui_bs2zebr-1.html
If you have installed the Help documentation, you may access the same page locally by typing the following at the MATLAB prompt:\n
web([docroot,'/toolbox/distcomp/bq5ntwk.html'])

More Answers (0)

Categories

Find more on Multicore Processor Targets in Help Center and File Exchange

Products


Release

R2008a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!