memcpy
Function to Optimize Generated Code for Vector
AssignmentsYou can use the Use memcpy for vector assignment
parameter to optimize generated code for vector assignments by replacing
for
loops with memcpy
function calls. The memcpy
function is more efficient
than for
-loop controlled element assignment for large
data sets. This optimization improves execution speed.
Selecting the Use memcpy for vector assignment parameter
enables the associated parameter Memcpy threshold
(bytes), which allows you to specify the minimum array
size in bytes for which memcpy
function calls should
replace for
loops in the generated code. For more
information, see Use memcpy for vector assignment and Memcpy threshold (bytes). In considering whether to use this
optimization,
Verify that your target supports the
memcpy
function.
Determine whether your model uses signal vector assignments
(such as Y=expression
) to move large
amounts of data, for example, using the Selector
block.
To apply this optimization,
Consider first generating code without this optimization and measuring its execution speed, to establish a baseline for evaluating the optimized assignment.
Select Use memcpy for vector assignment
and examine the setting of Memcpy threshold
(bytes), which by default specifies 64
bytes as the minimum array size for which
memcpy
function calls replace
for
loops. Based on the array
sizes used in your application's signal vector assignments,
and target environment considerations that might bear on the
threshold selection, accept the default or specify another
array size.
Generate code, and measure its execution speed against your baseline or previous iterations. Iterate on steps 2 and 3 until you achieve an optimal result.
Note
The memcpy
optimization may not occur under
certain conditions, including when other optimizations have a higher
precedence than the memcpy
optimization, or
when the generated code is originating from Target Language Compiler
(TLC) code, such as a TLC file associated with an S-function
block.
Note
If you are licensed for Embedded Coder® software, you can use a code replacement library (CRL)
to provide your own custom implementation of the
memcpy
function to be used in generated
model code. For more information, see Memory Function Code Replacement (Embedded Coder).
To examine the result of using the Use memcpy for vector assignment parameter on the generated vector assignment code, create a model that generates signal vector assignments. For example,
Use In, Out, and Selector blocks to create the following model.
Open Model Explorer and configure the Signal
Attributes for the
In1
and In2
source blocks. For each, set Port
dimensions to
[1,100]
, and set Data
type to int32
. Apply
the changes and save the model. In this example, the
model has the name
vectorassign
.
For each Selector block, set the
Index parameter to
1:50
. Set the Input
port size parameter to
100
.
The Use memcpy for vector assignment parameter is on by default. To turn off the parameter, go to the Optimization pane and clear the Use memcpy for vector assignment parameter.
Go to the Code Generation > Report pane of the Configuration Parameters dialog box and select the Create code generation report parameter and the Open report automatically parameter. Then go to the Code Generation pane, select the Generate code only option, and generate code for the model. When code generation completes, the HTML code generation report is displayed.
In the HTML code generation report, click the
vectorassign.c
section and
inspect the model step function. Notice that the
vector assignments are implemented using
for
loops.
/* Model step function */ void vectorassign_step(void) { int32_T i; for (i = 0; i < 50; i++) { /* Outport: '<Root>/Out1' incorporates: * Inport: '<Root>/In1' */ vectorassign_Y.Out1[i] = vectorassign_U.In1[i]; /* Outport: '<Root>/Out2' incorporates: * Inport: '<Root>/In2' */ vectorassign_Y.Out2[i] = vectorassign_U.In2[i]; } }
Go to the Optimization pane of the Configuration Parameters
dialog box and select the Use memcpy for
vector assignment option. Leave the
Memcpy threshold (bytes)
option at its default setting of
64
. Apply the changes and
regenerate code for the model. When code generation
completes, the HTML code generation report again is
displayed.
In the HTML code generation report, click the
vectorassign.c
section and
inspect the model output function. Notice that the
vector assignments now are implemented using
memcpy
function
calls.
/* Model step function */ void vectorassign_step(void) { /* Outport: '<Root>/Out1' incorporates: * Inport: '<Root>/In1' */ memcpy(&vectorassign_Y.Out1[0], &vectorassign_U.In1[0], 50U * sizeof(real_T)); /* Outport: '<Root>/Out2' incorporates: * Inport: '<Root>/In2' */ memcpy(&vectorassign_Y.Out2[0], &vectorassign_U.In2[0], 50U * sizeof(real_T)); }
Use memcpy for vector assignment