{"id":14738,"date":"2023-06-09T13:57:32","date_gmt":"2023-06-09T17:57:32","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=14738"},"modified":"2023-06-09T13:57:32","modified_gmt":"2023-06-09T17:57:32","slug":"avoiding-unwanted-data-copies-in-simulink-generated-code-via-reusing-i-o","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2023\/06\/09\/avoiding-unwanted-data-copies-in-simulink-generated-code-via-reusing-i-o\/","title":{"rendered":"Avoiding Unwanted Data Copies in Simulink Generated Code via Reusing I\/O"},"content":{"rendered":"<div class=\"rtcContent\">\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\">Recently, we've had a few customers ask if it's possible to configure their models to alter values on a bus that goes into and out of their model without making a data copy. There are several ways to do this with <a href=\"https:\/\/www.mathworks.com\/help\/ecoder\/index.html\">Embedded Coder<\/a>\u00ae, but we'll focus on models which pass their inputs and outputs as arguments. In particular, we'll show how to pass arguments by reference in the generated code from for a Model block. This technique can be used to eliminate unnecessary copies of variables from input ports to output ports. This can lead to more efficient code.<\/div>\r\n<h2 style=\"margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: bold; text-align: left;\">The Model<\/h2>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\">Let's say we have a model that operates on its bus argument. The input and output to this model have the same bus type, which for this demonstration includes elements, <span style=\"font-weight: bold; font-family: monospace;\">b1<\/span>, a boolean, and <span style=\"font-weight: bold; font-family: monospace;\">d1<\/span>, a double. In principle, these are two of many elements. Pressing CTRL+B generates code for the model.<\/div>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\"><img decoding=\"async\" loading=\"lazy\" class=\"imageNode\" style=\"vertical-align: baseline; width: 580px; height: 244px;\" src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/pass_by_reference_1.png\" alt=\"ModelSnapshot.png\" width=\"580\" height=\"244\" \/><\/div>\r\n<h2 style=\"margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: bold; text-align: left;\">The Code<\/h2>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\">The model modifies the value of <span style=\"font-weight: bold; font-family: monospace;\">d1<\/span> on the bus based on the value of <span style=\"font-weight: bold; font-family: monospace;\">b1<\/span>. If we generate code from this model, we get the following step function:<\/div>\r\n<div class=\"preformatted-plain\" style=\"margin: 10px 3px 10px 55px; padding: 10px 10px 10px 5px;\">\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\">void A_step(my_bus *arg_in, my_bus *arg_out)<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\">{<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> real_T tmp;<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> *arg_out = *arg_in;<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> if (arg_in-&gt;b1) {<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> tmp = -2.5;<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> } else {<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> tmp = 2.5;<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> }<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> arg_out-&gt;d1 = tmp + arg_in-&gt;d1;<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\">}<\/span><\/div>\r\n<\/div>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\">Notice that the step function has two arguments, <span style=\"font-family: monospace;\">arg_In1<\/span> and <span style=\"font-family: monospace;\">arg_Out1<\/span>, and on line 4, the code copies the input to the ouput. If this were a large bus, the copy could be quite expensive, so we'd like to eliminate it.<\/div>\r\n<h2 style=\"margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: bold; text-align: left;\">A More Efficient Code<\/h2>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\">In order to eliminate the copy, we can open the <a href=\"https:\/\/www.mathworks.com\/help\/rtw\/ref\/codemappingseditorc.html\">Code Mappings<\/a> panel with CTRL+SHIFT+C and go to the Functions tab. Once there, click on the A_step hyperlink in the Function Preview column.<\/div>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\"><img decoding=\"async\" loading=\"lazy\" class=\"imageNode\" style=\"vertical-align: baseline; width: 778px; height: 203px;\" src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/pass_by_reference_2.png\" alt=\"CodeMappingsPanel.png\" width=\"778\" height=\"203\" \/><\/div>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\">The hyperlink will open a new window which will allow us to configure the <a href=\"https:\/\/www.mathworks.com\/help\/rtw\/ug\/configure-c-code-generation-for-model-entry-point-functions.html\">C step function interface<\/a> for our model.<\/div>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\"><img decoding=\"async\" loading=\"lazy\" class=\"imageNode\" style=\"vertical-align: baseline; width: 600px; height: 725px;\" src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/pass_by_reference_3.png\" alt=\"StepConfig.png\" width=\"600\" height=\"725\" \/><\/div>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\">To make our argument shared, we make sure the Configure arguments for Step function prototype is checked and then set the C Identifier Name for the input and output ports to the same value. In our case, let's call it <span style=\"font-family: monospace;\">arg_shared<\/span>. When we re-generate code, we get:<\/div>\r\n<div class=\"preformatted-plain\" style=\"margin: 10px 3px 10px 55px; padding: 10px 10px 10px 5px;\">\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\">void A_step(my_bus *arg_shared)<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\">{<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> real_T tmp;<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> if (arg_shared-&gt;b1) {<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> tmp = -2.5;<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> } else {<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> tmp = 2.5;<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> }<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\"> arg_shared-&gt;d1 += tmp;<\/span><\/div>\r\n<div style=\"border-radius: 0px; padding: 0px; line-height: 15.6px; min-height: 16px; white-space: pre; color: #212121; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: 0px none #212121;\"><span style=\"white-space: pre;\">}<\/span><\/div>\r\n<\/div>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\">Notice that there is only a single argument to the step function and the copy is elimiated, which leads to much more efficient code.<\/div>\r\n<h2 style=\"margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: bold; text-align: left;\">Now It's Your Turn<\/h2>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\">How are you avoiding unwanted copies in your generated code? What kinds of improvements to generated code efficiency would you find helpful?<\/div>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\"><\/div>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\"><\/div>\r\n<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\"><\/div>\r\n<\/div>\r\n<script type=\"text\/javascript\">var css = ''; var head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style'); head.appendChild(style); style.type = 'text\/css'; if (style.styleSheet){ style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); }<\/script><a><button class=\"btn btn-sm btn_color_blue pull-right add_margin_10\">Download Live Script<\/button><\/a>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/pass_by_reference_1.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>\r\nRecently, we've had a few customers ask if it's possible to configure their models to alter values on a bus that goes into and out of their model without making a data copy. There are several ways... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2023\/06\/09\/avoiding-unwanted-data-copies-in-simulink-generated-code-via-reusing-i-o\/\">read more >><\/a><\/p>","protected":false},"author":197,"featured_media":14726,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/14738"}],"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\/197"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=14738"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/14738\/revisions"}],"predecessor-version":[{"id":14741,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/14738\/revisions\/14741"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media\/14726"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=14738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=14738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=14738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}