Main Content

Reduce Data Copies for Bus Assignment Blocks in Generated Code

For models containing a Bus Assignment block, if possible, the code generator uses the same variable for the block input and output. Reusing these variables reduces data copies, conserves RAM consumption and increases code execution speed.

Example Model

For example, in bus_assignoptim, a bus signal containing six elements feeds into a Bus Assignment block and a Bus Selector block. The Bus Assignment block assigns new values to the bus element a1_real_array. This bus signal feeds into Out1.

Generate Code Without Optimization

Generate code without the optimization by setting the Perform in-place updates for Assignment and Bus Assignment blocks parameter to off. Without the optimization the bus_assignoptim_step function contains this code:

void bus_assignoptim_step(void)
{
  real_T rtb_Assignment[36];
  int32_T i;

  /* Assignment: '<Root>/Assignment' incorporates:
   *  Inport: '<Root>/In1'
   *  Inport: '<Root>/In2'
   *  Product: '<Root>/Product'
   *  Selector: '<Root>/Selector'
   *  Sum: '<Root>/Sum1'
   */
  for (i = 0; i < 36; i++) {
    rtb_Assignment[i] = bus_assignoptim_U.In1.a1_real_array[i];
  }

  for (i = 0; i < 2; i++) {
    rtb_Assignment[(int32_T)(i + 22)] = (bus_assignoptim_U.In1.a1_real_array
      [(int32_T)(i + 22)] + bus_assignoptim_U.In1.a1_num) *
      bus_assignoptim_U.In2;
  }

  /* End of Assignment: '<Root>/Assignment' */

  /* Outport: '<Root>/Out' incorporates:
   *  BusAssignment: '<Root>/Bus Assignment'
   *  Inport: '<Root>/In1'
   */
  bus_assignoptim_Y.Out = bus_assignoptim_U.In1;

  /* BusAssignment: '<Root>/Bus Assignment' incorporates:
   *  Outport: '<Root>/Out'
   */
  for (i = 0; i < 36; i++) {
    bus_assignoptim_Y.Out.a1_real_array[i] = rtb_Assignment[i];
  }
}
The generated code contains the temporary array rtb_Assignment for holding data before this data is assigned to bus_assignoptim_Y.Out.a1_real_array.

Generate Code with Optimization

Generate code with the optimization by setting the Perform in-place updates for Assignment and Bus Assignment blocks parameter to on. With the optimization, the bus_assignoptim_step function contains this code:

/* Model step function */
void bus_assignoptim_step(void)
{
  int32_T i;

  /* Outport: '<Root>/Out' incorporates:
   *  Inport: '<Root>/In1'
   *  SignalConversion: '<Root>/TmpBusAssignmentBufferAtBus AssignmentInport1'
   */
  bus_assignoptim_Y.Out = bus_assignoptim_U.In1;

  /* Assignment: '<Root>/Assignment' incorporates:
   *  Inport: '<Root>/In1'
   *  Inport: '<Root>/In2'
   *  Outport: '<Root>/Out'
   *  Product: '<Root>/Product'
   *  Selector: '<Root>/Selector'
   *  Sum: '<Root>/Sum1'
   */
  for (i = 0; i < 36; i++) {
    bus_assignoptim_Y.Out.a1_real_array[i] =
      bus_assignoptim_U.In1.a1_real_array[i];
  }

  for (i = 0; i < 2; i++) {
    bus_assignoptim_Y.Out.a1_real_array[(int32_T)(i + 22)] =
      (bus_assignoptim_U.In1.a1_real_array[(int32_T)(i + 22)] +
       bus_assignoptim_U.In1.a1_num) * bus_assignoptim_U.In2;
  }

  /* End of Assignment: '<Root>/Assignment' */
}
The generated code does not contain the temporary array rtb_Assignment1 for holding data. The generated code directly assigns this data to bus_assignoptim_Y.Out.a1_real_array.

See Also

Related Topics