{"id":111,"date":"2010-10-28T01:42:25","date_gmt":"2010-10-28T01:42:25","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2010\/10\/28\/tips-for-simulation-performance\/"},"modified":"2017-02-08T14:53:32","modified_gmt":"2017-02-08T19:53:32","slug":"tips-for-simulation-performance","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2010\/10\/28\/tips-for-simulation-performance\/","title":{"rendered":"Tips for Simulation Performance"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/guy_rouleau_small.png\" alt=\"Blogger, Guy Rouleau\" style=\"float: left; margin-right: 1em;\">By <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/31651\">Guy Rouleau<\/a><br><br>\r\n\r\nOne of the questions I hear the most often from Simulink users is <em>What can I do to make my model run faster?<\/em> Here is a list of tricks that can help.<\/p>\r\n\r\n<p><strong>Displays and Scopes<\/strong><\/p>\r\n\r\n<p>When you have a display or a scope in your model, Simulink needs to update it. To speed up the execution of the model, display only the necessary. This can mean:<\/p>\r\n\r\n<ul>\r\n<li>Limit the amount of visible displays and open scopes.<\/li>\r\n<li>For open scopes and displays, set the decimation to a reasonable value.<\/li>\r\n<li>Load the model in memory using <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/slref\/load_system.html\">load_system<\/a> instead of <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/slref\/open_system.html\">open_system<\/a> and simulate it using the <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/slref\/sim.html\">sim<\/a> command, then post process\/display the outputs.<\/li>\r\n<\/ul>\r\n\r\n<p><strong>Interpreted blocks<\/strong><\/p>\r\n\r\n<p>When a model includes a <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/slref\/matlabfcn.html\">MATLAB Fcn block<\/a> or <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/sfg\/f7-60056.html\">MATLAB file S-function<\/a>, the MATLAB interpreter is called at each time step. Try replacing these blocks by the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/slref\/fcn.html\">Fcn block<\/a>, the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/slref\/mathfunction.html\">Math Function block<\/a>, the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/slref\/embeddedmatlabfunction.html\">Embedded MATLAB Function block<\/a> (without <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/eml\/ref\/eml.extrinsic.html\">extrinsic functions<\/a>) or a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/sfg\/f8-89503.html\">C-MEX S-function<\/a>.<\/p>\r\n\r\n<p><strong>Algebraic loops<\/strong><\/p>\r\n\r\n<p>I saw a TV commercial recently. There was a comment about pick-up lines that I think could also apply for algebraic loops:<br>\r\n<br>\r\n<em>\"There is a time and a place for them. The time is never... you can figure out the place on your own.\"<\/em><br>\r\n<br>\r\nSeriously, sometimes <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/ug\/f7-8243.html#f7-19688\">algebraic loops<\/a> are unavoidable, but they always slow down models.<\/p>\r\n\r\n<p><strong>Solver Options<\/strong><\/p>\r\n\r\n<p><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/gui\/bq7cmsp-1.html\">Solver settings<\/a> influence simulation performance, especially for variable-step solvers. Look at the time steps taken by your model. I usually do it by logging the simulation time (<tt>tout<\/tt>) and displaying it using this command:<br>\r\n<br>\r\n<code>semilogy(tout(1:end-1),diff(tout))<\/code><br>\r\n\r\nBased on what you see, it is possible to determine which setting to change. This can include <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/gui\/bq7cmsp-1.html#bq9mhyj-1\">solver choice<\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/gui\/bq7cmsp-1.html#bq9mhyj-1\">max step size<\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/gui\/bq7cmsp-1.html#bq9yezl-1\">relative<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/gui\/bq7cmsp-1.html#bq9yezl-1\">absolute<\/a> tolerances and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/ug\/f7-8243.html#f7-9506\">zero crossings detection<\/a>, etc.<\/p>\r\n\r\n<p>How solver settings influence simulation performance is a complex topic too long for this post. I recommend looking at the documentation page titled <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/ug\/f11-69449.html\">Choosing a solver<\/a> for more details. Leave a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=111&#comment\">comment below<\/a> if you would like to see a future post on improving simulation performance through solver settings.<\/p>\r\n\r\n<p><strong>Accelerator and Rapid Accelerator modes<\/strong><\/p>\r\n\r\n<p><em>Accelerator mode<\/em> converts the block methods into a C-MEX S-function. Since the block methods are compiled instead of interpreted, the model usually runs faster.  The <em>Rapid Accelerator mode<\/em> creates a standalone executable from your model. The executable is launched in a different process that will run in a separate thread on a different processor if you have a multi-core machine.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q4\/AcceleratorMode.png\" alt=\"Simulation mode can be set to Normal, Accelerator or Rapid Accelerator for optimal performance.\"><\/p>\r\n\r\n<p>Here are a few tips when using the Accelerator and Rapid Accelerator mode:<\/p>\r\n\r\n<ul>\r\n<li>Avoid blocks that do not support code generation. They run in interpreted mode and can slowdown your model.<\/li>\r\n<li>Try switching the Compiler Optimization Level from faster build to faster run.<\/li>\r\n<li>For maximum speed, start your simulation using the <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/slref\/sim.html\">sim<\/a> command when using these modes.<\/li>\r\n<\/ul>\r\n\r\n<p>Look at the documentation page titled <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/ug\/brah7z_-24.html#brcp51d-1\">Comparing Performance<\/a>. This will give you a good idea of when these modes should be used.<\/p>\r\n\r\n<p><strong>Parallel Computing Toolbox<\/strong><\/p>\r\n\r\n<p> To run multiple simulations in parallel, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/ug\/brsk2gr.html\">calling sim from within parfor<\/a> is a good idea. This allows you to fully take advantage of all the processors in your machine when performing parameter sweeps and Monte Carlo analysis.<\/p>\r\n\r\n<p>In your MATLAB installation, look for the demo titled <strong>Rapid Accelerator Simulations Using PARFOR<\/strong> for an example.<\/p>\r\n\r\n<p><strong>Where to look for more?<\/strong><\/p>\r\n\r\n<p>The Simulink documentation contains a chapter titled <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/simulink\/ug\/br8i7xz.html\">Improving Simulation Performance and Accuracy<\/a>. I strongly recommend going through this chapter to create simulations running at optimal speed.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>How do you ensure that your simulation is running at optimal speed? Do you have tricks not mentioned here? Post a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=111&#comment\">comment here<\/a>.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>By Guy Rouleau\r\n\r\nOne of the questions I hear the most often from Simulink users is What can I do to make my model run faster? Here is a list of tricks that can help.\r\n\r\nDisplays and Scopes\r\n\r\nWhen... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2010\/10\/28\/tips-for-simulation-performance\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[121,43,143,10,33],"tags":[163],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/111"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=111"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":6359,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/111\/revisions\/6359"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}