Parallel Simulation | Five Practical Tips to Speed Up Your Simulink Simulations
From the series: Five Practical Tips to Speed Up Your Simulink Simulations
If you are running hundreds or thousands of iterative simulations such as parameter sweeps and Monte Carlo simulations, see how parallel simulations help you reduce the overall simulation time.
Published: 28 Jun 2020
In the last video we saw how you could accelerate simulations using Fast restart.
In this video, we will talk about another method of accelerating your simulations when you are dealing with a lot of simulations, such as when you do a parameter sweep, or Monte Carlo simulations. You can do this by running those multiple simulations in parallel.
Before we talk about the details, let’s have a quick overview of what parallel computing in MATLAB means in general.
It is available through the parallel computing toolbox and MATLAB parallel server and is based on MATLAB Workers, or MATLAB computational engines. You can also think of them as headless MATLAB sessions. These MATLAB sessions or workers can be associated with multiple cores on your desktop machine. They can also be associated on clusters and clouds. A collection of these workers are referred to as a parallel pool.
Simulations can be assigned to MATLAB workers by using the parsim command to facilitate parallel simulations. Parsim is a convenient and efficient way to run lots of simulations in parallel, either using the local MATLAB pool on your desktop or a MATLAB pool in a cluster.
Parsim works in conjunction with the new simulation Input object, which helps you set up all your simulation inputs in a convenient way, including variables, block parameters, and simulation configurations. The parsim command can take an array of simulation input objects and run those simulations in parallel if MATLAB parallel computing tools are available, otherwise the simulations are run serially.
Let’s demonstrate the power of running parallel simulations using parsim. For this, let’s use a model of a hybrid electric vehicle.
This model consists of thousands of blocks and takes a while to update and simulate . I ran this model before and for one run it takes around 2 and half minutes. Now if you wanted to run,say, 100 simulations for a parameter sweep one after the other, it would take close to 4 hours.
Now, I have this script that will help us set this model up for parallel simulations to carry out parameter sweeps for the different masses and differential torque ratios for the car and determine the overall fuel usage.
The Section 1, here just sets up the Simulink project for this model.
Section 2 creates 100 parameter sets for the mass and the differential torque parameters.
Section 3 creates 100 simulation inputs objects for these 100 simulations, that we will run using the parsim command. Each of these simulation input objects contains the parameter set for that simulation.
In Section 4, there are commands that run the parallel simulations and section 5 has code to plot the fuel usage for each of the parameter sets.
Now that this is set, let’s run these simulations in parallel.
First, let’s see how we can run parallel simulations on our local machines. The parpool command with the ‘local’ argument finds different cores in the local machine and creates a parallel pool of MATLAB workers.
Once we run the parsim command, MATLAB created and connected to 4 local workers in my case. Then, it sets up Simulink and assigns simulations to each worker after which the simulations are run.
We see the simulations have run successfully and the fuel usage metrics are displayed on the graph. The total simulation time of 6700 seconds which is close to 2 hours is much better compared to the 4 hours that you expect if you ran the simulations in serial
You can achieve further speed up by running parallel simulations on remote clusters, giving you the benefit of connecting to typically 100s of workers compared to a local parallel pool. You can set up the remote cluster by going to the parallel drop down from the home tab. Then use the discover clusters option to locate clusters you could use in your network. I have already chosen a cluster here called ‘MathWorksPublic’. You can modify preferences for the parallel pool by navigating to the parallel pool preferences . Here I have chosen to use 50 workers in the remote cluster. Let’s now run the simulations using this remote cluster. We see that the simulations took only 675 seconds which is a little over 11 minutes, thus speeding up the parameter sweep process many fold compared to the local parallel pool.
Now, you might wonder if these parallel simulations work with fast restart to produce further speed up. The answer is yes. To utilize fast restart, you simply need to configure the UseFastRestart argument of the parsim command as ‘on’. With that set, Let’s run the simulations now. We see that the simulations now ran in around 621 seconds.
Now, what if your simulation is running in rapid accelerator mode for which fast restart is not supported? For this you can turn off the ‘RapidAcceleratorUpToDateCheck’ to gain speedup. What you need to do is to use this setModelParameter method of the simulation input object to set RapidAcceleratorUpToDateCheck to 'off'.
While running in rapid accelerator mode, one more needed step is to create a Setup function which will help us build the rapid accelerator target on the MATLAB workers. This step is required because the host MATLAB and the parallel workers may have different architectures, for example a Windows host with Linux parallel workers or for cases where the rapid accelerator target does not exist. Without the setup function if a rapid accelerator target does not exist for the architecture corresponding to the parallel workers, the simulations will error out. This target is built only once and used by all subsequent simulations and thus saving time for model compilation.
We can then run parsim by giving in the simulation input object and the setup function as arguments. The simulation now ran in around 547 seconds. So comparing this run with the serially run parameter sweep that would take 4 hours we see a speed up of 2600%.
Now, these are the ways you can go about running parallel simulations. As a recap, we saw how you can run them from your local machine or a remote cluster and using fast restart or using rapid accelerator.
To close the loop, the advantage of running parallel simulations is:
It saves you time (sometimes even hours!) while running long iterative simulations like parameter sweeping or Monte Carlo simulations for your models
But also keep in mind some of the tradeoffs of using parallel simulation:
One, there is an overhead of setting up a parallel pool of workers.
There is an overhead of starting simulations on the workers
It needs a bit of scripting in MATLAB to get started as we saw here
Lastly, you cannot run simulations that depend on each other
Thanks for watching! Please watch the next video if you are interested in learning about how to speed up simulations through model references or Simulink cache
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)