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
Embedded Coder®, 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.
The Model
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, b1, a boolean, and d1, a double. In principle, these are two of many elements. Pressing CTRL+B generates code for the model.
The Code
The model modifies the value of d1 on the bus based on the value of b1. If we generate code from this model, we get the following step function:
Notice that the step function has two arguments, arg_In1 and arg_Out1, 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.
A More Efficient Code
In order to eliminate the copy, we can open the
Code Mappings panel with CTRL+SHIFT+C and go to the Functions tab. Once there, click on the A_step hyperlink in the Function Preview column.
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 arg_shared. When we re-generate code, we get:
Notice that there is only a single argument to the step function and the copy is elimiated, which leads to much more efficient code.
Now It's Your Turn
How are you avoiding unwanted copies in your generated code? What kinds of improvements to generated code efficiency would you find helpful?
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.