Saturday, April 27, 2024
HomeMatlabAvoiding Undesirable Knowledge Copies in Simulink Generated Code by way of Reusing...

Avoiding Undesirable Knowledge Copies in Simulink Generated Code by way of Reusing I/O » Man on Simulink


Lately, we have had just a few prospects ask if it is doable to configure their fashions to change values on a bus that goes into and out of their mannequin with out making an information copy. There are a number of methods to do that with Embedded Coder®, however we’ll deal with fashions which go their inputs and outputs as arguments. Specifically, we’ll present go arguments by reference within the generated code from for a Mannequin block. This system can be utilized to get rid of pointless copies of variables from enter ports to output ports. This could result in extra environment friendly code.

The Mannequin

For instance we’ve a mannequin that operates on its bus argument. The enter and output to this mannequin have the identical bus kind, which for this demonstration consists of parts, b1, a boolean, and d1, a double. In precept, these are two of many parts. Urgent CTRL+B generates code for the mannequin.

The Code

The mannequin modifies the worth of d1 on the bus primarily based on the worth of b1. If we generate code from this mannequin, we get the next step operate:

void A_step(my_bus *arg_in, my_bus *arg_out)

{

real_T tmp;

*arg_out = *arg_in;

if (arg_in->b1) {

tmp = -2.5;

} else {

tmp = 2.5;

}

arg_out->d1 = tmp + arg_in->d1;

}

Discover that the step operate has two arguments, arg_In1 and arg_Out1, and on line 4, the code copies the enter to the ouput. If this have been a big bus, the copy might be fairly costly, so we might prefer to get rid of it.

A Extra Environment friendly Code

In an effort to get rid of the copy, we will open the Code Mappings panel with CTRL+SHIFT+C and go to the Features tab. As soon as there, click on on the A_step hyperlink within the Operate Preview column.
CodeMappingsPanel.png
StepConfig.png

To make our argument shared, we make sure that the Configure arguments for Step operate prototype is checked after which set the C Identifier Identify for the enter and output ports to the identical worth. In our case, let’s name it arg_shared. After we re-generate code, we get:

void A_step(my_bus *arg_shared)

{

real_T tmp;

if (arg_shared->b1) {

tmp = -2.5;

} else {

tmp = 2.5;

}

arg_shared->d1 += tmp;

}

Discover that there’s solely a single argument to the step operate and the copy is elimiated, which ends up in rather more environment friendly code.

Now It is Your Flip

How are you avoiding undesirable copies in your generated code? What sorts of enhancements to generated code effectivity would you discover useful?



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments