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:
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/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, ...
[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:
sched = findResource('scheduler','type','jobmanager')
j = createJob(sched)
set(j,'FileDependencies',{'mymodel.mdl','mysimfunc.m', ...
set(j,'PathDependencies',{'$DIR1','$DIR2',...
createTask(j, @mysimfunc, 3, {argin11 argin12 ...
createTask(j, @mysimfunc, 3, {argin21 argin22 ...
createTask(j, @mysimfunc, 3, {argin31 argin32 ...
createTask(j, @mysimfunc, 3, {argin41 argin42 ...
submit(j);
waitForState(j);
results = getAllOutputArguments(j);
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:
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/distcomp/f1-6010.html'])
Likewise, for more information on PCT object properties (such as FileDependencies and PathDependencies), please refer to the following Help page:
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/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:
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/distcomp/bq5ntwk.html'])