{"id":98,"date":"2010-06-14T02:47:12","date_gmt":"2010-06-14T02:47:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2010\/06\/14\/for-each-subsystem-and-code-generation\/"},"modified":"2010-06-14T02:47:12","modified_gmt":"2010-06-14T02:47:12","slug":"for-each-subsystem-and-code-generation","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2010\/06\/14\/for-each-subsystem-and-code-generation\/","title":{"rendered":"For Each Subsystem and Code Generation"},"content":{"rendered":"<p>Today <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/31651\">Guy Rouleau<\/a> is back to introduce the code generated from the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/simulink\/slref\/foreachsubsystem.html\">For Each Subsystem<\/a>.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/guy_rouleau_small.png\" alt=\"Code generator, Guy Rouleau\"><\/p>\r\n\r\n<p>I created an example model implementing the same functionality as in my <a href = \"https:\/\/blogs.mathworks.com\/seth\/2010\/05\/20\/applying-a-scalar-algorithm-to-a-vector\/\">previous post<\/a>, but using <a href = \"https:\/\/www.mathworks.com\/products\/stateflow\/\">Stateflow<\/a>. I like using Stateflow to create behavioral models when I am most interested in expressing the input-output relationship of the system.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q2\/SfModulo4Counter.png\" alt=\"Modulo-4 Counter Using Stateflow\" \/><\/p>\r\n\r\n<p>I placed the chart in a For Each Subsystem.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q2\/SfInForEach.png\" alt=\"Stateflow chart inside a For Each Subsystem\" \/><\/p>\r\n\r\n<p>To make the code clearer for this post, I configured the Stateflow chart to generate a <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/rtw\/ug\/f1127051.html#f1058307\">Reusable Function<\/a> in a separate file.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q2\/ReusableFunction.png\" alt=\"Configuring the Stateflow chart to be a reusable subsystem\" \/><\/p>\r\n\r\n<p>Let's now look at the generated code:\r\n\r\n<p><a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q2\/ForEachCode.png\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q2\/ForEachCode_thumb.png\" alt=\"Copying the inputs into a contiguous vector\"\/><\/a><br>Click image to enlarge.<\/p>\r\n\r\n<p>I added colors to highlight the different parts of the code. In the first part (green), notice that the components of the input signal are placed in contiguous locations in memory. The For Each Subsystem needs this step if the input is not already contiguous. Note that this step respects the optimization settings like <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/rtw\/ug\/f1144193.html\">Loop unrolling threshold<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/rtw\/ug\/bruuisn-1.html\">Use memcpy for vector assignment<\/a><\/p>\r\n\r\n<p>Inside the FOR loop, a set of structures are used to store signals and states required by each instance of the subsystem. These structures are passed to the reusable Stateflow chart function. When the loop is completed, each element of the output signal is assigned to its destination.<\/p>\r\n\r\n<p><strong>Reusable Code<\/strong><\/p>\r\n\r\n<p>As pointed out by my colleague <a href = \"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/31762\">Parasar Kodati<\/a>, the For Each Subsystem can generate reusable code. Let's say I have multiple instances of this same For Each Subsystem in a model, all with a different number of iterations.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q2\/MultipleInstancesForEach.png\" alt=\"Multiple instances of For Each Subsystems\"\/><\/p>\r\n\r\n<p>I configure all the For Each Subsystems to generate a reusable function with the same function name and file name. Now the FOR loop is located in a separate function, which take as input the number of iterations.<\/p>\r\n\r\n<p><a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q2\/ForEachReuseCode.png\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q2\/ForEachReuseCode_thumb.png\" alt=\"Multiple instances of For Each Subsystems\"\/><\/a><br>Click image to enlarge.<\/p>\r\n\r\nNotice that I used different vector lengths to highlight how the inputs and outputs to the For Each Subsystem are handled by the <a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/rtw\/ug\/f1144193.html\">Loop unrolling threshold<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/rtw\/ug\/bruuisn-1.html\">Use memcpy for vector assignment<\/a> options.<\/p> \r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Are you already using the For Each subsystem? Leave a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=98&#comment\">comment here<\/a> and tell us what you are using it for.<\/p> ","protected":false},"excerpt":{"rendered":"<p>Today Guy Rouleau is back to introduce the code generated from the For Each Subsystem.\r\n\r\n\r\n\r\nI created an example model implementing the same functionality as in my previous post, but using... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2010\/06\/14\/for-each-subsystem-and-code-generation\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,55,56,16],"tags":[140,454,441],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/98"}],"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\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=98"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/98\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}