{"id":4486,"date":"2015-03-19T12:32:25","date_gmt":"2015-03-19T17:32:25","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=4486"},"modified":"2023-11-17T10:34:15","modified_gmt":"2023-11-17T15:34:15","slug":"getting-the-most-out-of-rapid-accelerator-mode","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2015\/03\/19\/getting-the-most-out-of-rapid-accelerator-mode\/","title":{"rendered":"Getting the most out of Rapid Accelerator mode"},"content":{"rendered":"<p><strong><em>Update: If you are using a newer version of MATLAB, I recommend visiting <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2023\/10\/05\/getting-the-most-out-of-rapid-accelerator-mode-version-r2023b\">Getting the most out of Rapid Accelerator mode \u2013 Version R2023b<\/a>. The syntax to tune variables has been significantly improved since this post.<\/em><\/strong><\/p>\r\n\r\n<p>When working with Simulink, it's important to configure your model and workflows to be as efficient as possible for the task you are working on.<\/p>\r\n\r\n<p>Today I will show how to setup a model to get the maximum performance when you need to run many simulations and vary parameters. This is often the case if you do monte-carlo simulation, or system optimization.<\/p>\r\n\r\n<p><strong>Rapid Accelerator<\/strong><\/p>\r\n\r\n<p>As explained <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/how-the-acceleration-modes-work.html\">here<\/a>, the rapid accelerator mode can speed up simulation by generating an executable for your model. The speedup can vary between models for various reasons. Personally, I have seen some models run more than 10 times faster in rapid accelerator mode.\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q1\/rapidAccelerator.png\" alt=\"Rapid Accelerator\" \/><\/p>\r\n<p><strong>RapidAcceleratorUpToDateCheck off<\/strong><\/p>\r\n\r\n<p>When you click the play button in a model or use the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/sim.html\"><tt>sim<\/tt><\/a> command, Simulink verifies if your model has changed or not. If the model has not changed structurally, the rapid accelerator executable is not re-generated.<\/p>\r\n\r\n<p>One nice thing with rapid accelerator is that, once the executable has been generated, it is possible to skip this part where Simulink verifies if the model has changed or not. For large models, this can save a lot of time, bringing the initialization time to zero. <\/p>\r\n\r\n<p>Let's see how this can be done using a simple demo like <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/examples\/simulation-of-a-bouncing-ball.html\">sldemo_bounce<\/a> where I want to vary the coefficient of restitution, which I specified as <tt>k<\/tt> in the block dialog.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q1\/sldemo_bounce.png\" alt=\"sldemo_bounce\" \/><\/p>\r\n\r\n<p>If I want to be able to tune the value of <tt>k<\/tt>, I need to define it as a <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/simulink.parameter.html (link no longer works)\">Simulink.Parameter<\/a> object:<\/p>\r\n\r\n<pre class=\"codeinput\">\r\nk = Simulink.Parameter\r\nk.CoderInfo.StorageClass = <span class=\"string\">'SimulinkGlobal'<\/span>;\r\nk.Value = -0.9;\r\n<\/pre>\r\n\r\n<p>and I enable the Inline Parameters option in the model configuration.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2015Q1\/inlineParameters.png\" alt=\"Inline Parameters\" \/><\/p>\r\n\r\n<p>Once this is done, I explicitly build the rapid accelerator executable:<\/p>\r\n\r\n<pre class=\"codeinput\">\r\nmdl = <span class=\"string\">'sldemo_bounce'<\/span>;\r\nrtp = Simulink.BlockDiagram.buildRapidAcceleratorTarget(mdl);\r\n<\/pre>\r\n\r\n<p>As you can see, this returns a structure containing all the information relevant to tunable parameters in the model. Using this structure, we can create an array of run-time parameter structures for all the values we want to run:<\/p>\r\n\r\n<pre class=\"codeinput\">\r\nk_values = [-0.9:0.1:-0.1];\r\nfor i = 1:length(k_values)\r\n    paramSet(i) = Simulink.BlockDiagram.modifyTunableParameters(rtp, 'k', k_values(i));\r\nend\r\n<\/pre>\r\n\r\n<p>And we are ready to simulate, by passing to the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/sim.html\">sim<\/a> command, we pass the parameters <tt>RapidAcceleratorUpToDateCheck<\/tt> and <tt>RapidAcceleratorParameterSets<\/tt>.<\/p>\r\n\r\n<pre class=\"codeinput\">\r\n<span class=\"keyword\">for<\/span> i = 1:length(k_values)\r\n    simout(i) = sim(mdl,<span class=\"string\">'SimulationMode'<\/span>,<span class=\"string\">'rapid'<\/span>,<span class=\"keyword\">...<\/span>\r\n                      <span class=\"string\">'RapidAcceleratorUpToDateCheck'<\/span>,<span class=\"string\">'off'<\/span>, <span class=\"keyword\">...<\/span>\r\n                      <span class=\"string\">'RapidAcceleratorParameterSets'<\/span>,paramSet(i));\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre>\r\n\r\n<p><strong><em>Update: In MATLAB R2017a the function <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/parsim.html\">PARSIM<\/a> got introduced. For a better experience simulating models in a loop (in series or parallel), we recommend using PARSIM instead of SIM inside for\/parfor. See the more recent blog post <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2017\/04\/14\/simulating-models-in-parallel-made-easy-with-parsim\">Simulating models in parallel made easy with parsim<\/a> for more details.<\/em><\/strong><\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Give that a try and let us know what you think by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=4486&#comment\">comment here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/feature_image\/rapidAccelerator1.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>Update: If you are using a newer version of MATLAB, I recommend visiting Getting the most out of Rapid Accelerator mode \u2013 Version R2023b. The syntax to tune variables has been significantly improved... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2015\/03\/19\/getting-the-most-out-of-rapid-accelerator-mode\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":5018,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[143,33],"tags":[422],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4486"}],"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=4486"}],"version-history":[{"count":22,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4486\/revisions"}],"predecessor-version":[{"id":16050,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4486\/revisions\/16050"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media\/5018"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=4486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=4486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=4486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}