Running models in parallel on multiple cores or on a computer cluster | Simscape Electrical Modeling Practices for Fast Simulation - MATLAB & Simulink
Video Player is loading.
Current Time 0:00
Duration 9:34
Loaded: 1.73%
Stream Type LIVE
Remaining Time 9:34
 
1x
  • Chapters
  • descriptions off, selected
  • en (Main), selected
    Video length is 9:34

    Running models in parallel on multiple cores or on a computer cluster | Simscape Electrical Modeling Practices for Fast Simulation

    From the series: Simscape Electrical Modeling Practices for Fast Simulation

    Learn how you can speed up electrical Simulation with Parallel Computing in normal or accelerator mode.

    Published: 7 May 2024

    Hello, my name is Gernot Schraberger. I'm an application engineer at the MathWorks and I want to show you today how you can speed up electrical simulations with parallel computing.

    Speeding up an electrical simulation with parallel computing is only possible if you have multiple runs. You cannot speed up a single simulation. An important is that these multiple runs are independent from each other in their runs. This is the case for parameter sweeps, Monte Carlo analysis, or for optimization methods where you have to calculate the gradient. Every single gradient direction can be calculated individually and uses an individual simulation and is independent from the other gradient direction, same with global optimization approaches like genetic algorithms, where you have a certain population size and you can calculate the problem for certain parts on different cores.

    The last use case is Simulink test-- model testing and Simulink test, where you can also parallelize this simulations.

    Parallel simulations can run on multiple cores of your machine or on parallel workers within a cluster. You will need Parallel Computing Toolbox for local cores and additional MATLAB Parallel Server if you want to run the problem on multiple workers within a cluster.

    The main command for parallel simulation is the parsim command on the Simulink side. Or you can also use the parfor command, which is a pure MATLAB command, but you can call the sim command with parfor. I've already explained fast restart and simulation modes in a different video. And I don't want to repeat that here in detail again. But there is one important point here for parallel simulations. You have to use the fast restart option within these commands instead of fast restart option or the fast restart button in the model.

    In case of simulation modes, I focus here on normal mode and accelerator mode. The rapid accelerator mode is slightly different and requires a different script for the setup.

    In principle, you can use parsim or sim commands without simulation input objects, but you have big disadvantages when you want to use the fast restart. So, therefore, I recommend to use simulation input objects in that way as I show it today. So let's start with a concrete example.

    Within the script I open here the shipping demo ee_smartphone_charging. I'll save it on a different file name because I want to change the data output. In the original model there is a scope used, but I have to use data logging. I switch the logging on and statistics logging off because we don't need that. So let's execute this section.

    So here you see the model. Of course, I also could have changed this settings here interactively in the model by going to modeling-- model settings. And here in the data import and export section, I have to set signal logging and I can use single simulation output here. In the Simscape section, I have set logging simulation data to none and simulation statistics also is set off.

    The next step will be to create a parallel pool, if it's not already activated. You have to define how many workers, or course, you want to use. So we have chosen here four cores and we execute that section. This typically takes about 90 seconds if it was not activated before. In this case, the parallel pool was created already before and we can see that down here that it's already running.

    Step 3 is done choosing the right simulation mode. At the moment, we have choose normal mode in the model, but we want to have accelerator mode because it's running faster.

    Step 4 is then the definition of input and parameter sets. We want to tune three parameters here. One is charging CV-- that's the Simulink parameter-- then charging CC. And we also have cell ampere hours. The cell ampere hours is a value within Simscape. And as I mentioned before, it's important to choose that Simscape parameter as a runtime parameter.

    And you can see here cell capacity configurability runtime and not compile time. So choose only runtime parameters for those that you want to change during simulation. If you set all the parameters in Simscape to runtime parameters, the simulation will slow down.

    So let's also execute this section. And we create the parameter sets within the simulation input object. This object is called in. So we have 16 parameter sets, as you can see here.

    Now, as a next step, we want to execute the parallel simulation with the parsim command. You can see here tick for starting the time measurement. Then we have the parsim command. We use the simulation input object in here. And the option use fast reset is set to on as well as show simulation manager and show progress.

    After the parallel simulation, we want to measure the elapsed time. So let's also start this.

    The simulation manager window pops up and we start Simulink on the parallel workers. So there are several preparation steps going on here now on the workers before the real simulation starts. The models are loaded now to the workers and then we can also switch over to the simulation view.

    Here you can now see the progress of the parallel simulation and the elapsed time for the 16 different runs we have. Now execution started on all the four cores. So the first runs are completed now. And what you can clearly see here is that the second runs are faster compared to the first runs.

    At the bottom of the simulation manager, you can see that we reach a speedup factor of about three compared to series runs because the compilation doesn't have to be done again. And, well, the simulation is complete. We can continue now with step 5 and plot the results.

    And here you can see that we have different curves for these 16 runs. So it did not happen that the parameter tuning was frozen, which is important to check here. But there's also an alternative to the parsim command. You can use parfor together with the sim command. But in that case, it's very important that you set up the simulation task exactly the manner as I show you here.

    We have in total 16 simulation input objects, so for 16 different scenarios and we have got four cores. It's important to have a powerful loop over the number of cores, so a powerful loop from 1 to 4. And with every single call of the sim command, we hand over 16 divided by 4. So the number of input simulation objects divided by the number of cores simulation input objects as a vector. We also set the use fast reset option to on and click the run.

    So you see, the simulation started on the four cores. The first run, once again, takes longer for every single core. We can see that the first simulation runs have completed. Now the second runs are completed.

    So simulation has ended. We also should have a look now to the plots. And we can see also in this case here that we have a variation of the output signals here because we changed the parameters. So it looks all fine.

    So in the very last step here is shutting down the parallel course and close the system. I hope you found this session interesting.