Video length is 7:59

Model References and Simulink Cache | Five Practical Tips to Speed Up Your Simulink Simulations

From the series: Five Practical Tips to Speed Up Your Simulink Simulations

If you are working in team environment on a large model, see how using model references with parallel builds and Simulink® cache files can maximize the simulation performance.

Published: 26 Oct 2020

Welcome back to this series on speeding up Simulink simulations. In the last video, we used a shipping model of the Powertrain Blockset to demonstrate the power of parallel simulation. You may not notice that it models a P4 architecture of a hybrid electric vehicle model and is a quite complex one. It has around 10300 blocks, and models many different vehicle component functions such as the engine, engine controllers, the vehicle, etc.  Such a complex model is usually developed in a team environment rather than by individual effort. Sometimes for especially large models teams can work in tandem on individual components and integrating them together in the top level model such as this one.

In this video, we will talk about model references and Simulink cache, and how they can aid with these workflows and provide performance benefits that can scale for such models.  

Before we start, for new users it’s recommended you check out this component based modelling page in the MATLAB documentation to check if model references are appropriate for your modeling requirements. First, let’s delve into model reference and its benefits.

Model references lets you include one model in another using a model    block. Each instance of this is called a model reference. In this model we can see all the referenced models in the HeP4 reference application. The model that contains a referenced model is a parent model. A collection of parent and referenced models constitutes a model hierarchy.

Like subsystems this allows you to organize a large model hierarchy.

However, unlike subsystems using Model references has many performance benefits. One is Incremental Loading, which means the referenced models are loaded only when needed - unlike subsystems. And this speeds up your model loading times

Two, accelerated simulation, where you convert a referenced model into code and simulate the model by running the code, which is faster than interactive simulation.   

In this example, you can make this referenced model along with others to run in accelerator mode and the top level model to run in normal mode so you get the benefits of a faster simulation along with the interactive simulation for the top level model.

If the top model is in normal mode however the referenced models are in accelerator mode, model references could be 2-3 times faster than using a subsystem implementation 

Lastly, the advantage of incremental Code Generation, which means code for both production and simulation is only regenerated only if the referenced model and/or any of its dependencies or interfaces change.  

So for example, in the case of these automatically generated MEX-files or so called simulation targets which implement these referenced models, are not regenerated every time you simulate the top model, unless the model references associated with it have been updated between simulations.

So in summary, model references give you the advantage of faster load times, faster simulations and improved build times with incremental code generation.

Alright, now let’s talk about another feature which is closely associated with model references called Simulink cache and its benefits.

When you are working on a large model in a team environment, there will be many times when you run simulations based on components somebody else built.

A question here is, is it possible to leverage any generated simulation targets that my team has already built, so I don’t have to rebuild them myself, so as to save time?

The answer is yes and Simulink Cache aims at doing just that.  

To demonstrate let’s consider the same example we have been using  which consists of 7 active referenced models. When the top model is updated, the model reference targets for these models are built. These are the files that are generated.

Simulink then packages these generated files or build artifacts into a single file with the .slxc extension for each model    in the hierarchy, called the Simulink Cache file.

When your colleagues send you a model with the cache files, you do not need to pay the cost of rebuilding the updated model you got from them. Simulink will see the updated Simulink cache files, extract necessary files from the corresponding cache files essentially recreating the directory they used and will use it.  

So sharing these Simulink cache files significantly reduces the rebuild cost in a team-based workflow. This can be integrated into projects and source control systems. We have a technical article that talks about this and parsim workflows for your reference.

So in summary, Simulink Cache lets you share build artifacts that reduces the first time cost to run simulations. Also, it can be integrated into Simulink projects and parsim workflows.

One thing to be noted while using Model Reference and Simulink cache  is that the exact performance improvement depends on several factors, such as the number of models in the hierarchy, the model reference rebuild setting, the number of blocks in the referenced models, and the size and number of derived files created for each model.

However, there will be times that you might need to rebuild all the model references. If you are looking at models that has, say, hundreds of model references and long build times of hours, the parallel model reference build feature could help. This allows you to build these model reference targets in parallel using MATLAB workers considerably shortening build times. 

As an example, I was using a model that had around 400 model references and took close to an hour to build. By using the parallel build feature, I saw the build times drop to 20 minutes, a 3x improvement.

Ok so how do you enable it?

You can do that by selecting the parallel builds option on the model referencing tab of the configuration parameter dialog for the top-level model.

Simulink provides you with a check to see if you can benefit from this parallel build feature. For this, you can open the performance advisor, find and run this check under the check requiring model update.  It will show the potential speed up for the model using this feature.

 In summary, you can use the option of parallel build of the model references to speed up rebuild times. However, the actual speed up depends on multiple factors including the nature of model reference hierarchy.

To wrap things up for this video, if you are working in team environment on a large model, using model references with parallel builds and Simulink Cache can maximize the simulation performance by leveraging the benefits of incremental workflows.

Thanks for watching!

Related Products

Learn More