Main Content

Optimize Dynamic Array Access

You can make the dynamic arrays in the code run faster by using the configuration property CacheDynamicArrayDataPointer. This property hoists the data pointer to a temporary variable. This temporary variable is then used to access matrix data in the case of dynamic arrays.

By default, the property is enabled for MEX, static library, dynamic linked library, and executable configurations. The cache dynamic array data pointer can bring down the execution time of dynamic arrays almost equal to the execution time of static arrays. This property also helps to improve the readability of the code in many cases.

Disable Cache Dynamic Array Data Pointer Property

To disable the property using the MATLAB® Coder™ app:

  1. Open the Generate dialog box. On the Generate Code page, click the Generate arrow .

  2. Click More Settings.

  3. On the Advanced tab, set Cache dynamic array data to No.

To disable the property at the command line:

  1. In the MATLAB workspace, define the configuration object, using coder.config with arguments 'mex', 'lib', 'dll', or 'exe' (depending on your requirements).

    cfg = coder.config('lib');
  2. Set the CacheDynamicArrayDataPointer property of the configuration object to false:

    cfg.CacheDynamicArrayDataPointer = false;

Compare Generated C Code

Compare the generated C code to the enabled cache dynamic array data pointer and the disabled cache dynamic array data pointer.

Consider a function matrixAdd.

function c = matrixAdd(a,b) %#codegen
c = a+b;
end

Define the configuration object and generate C code by using the codegen command.

  1. To generate C code with the cache dynamic array data pointer enabled:

    cfg = coder.config('lib');
    codegen -config cfg matrixAdd -args ... 
    {coder.typeof(0, [1 Inf]), coder.typeof(0, [1 Inf])} -report
    Code generation successful: View report
  2. To generate C code with the cache dynamic array data pointer disabled:

    cfg = coder.config('lib');
    cfg.CacheDynamicArrayDataPointer = false;
    codegen -config cfg matrixAdd -args ...
    {coder.typeof(0, [1 Inf]), coder.typeof(0, [1 Inf])} -report
    Code generation successful: View report

Open the report and inspect the generated code.

This table compares the generated C codes. When the property is enabled, temporary variables such as *a_data, *b_data, and *c_data are included in the generated code. The use of temporary variables eliminates the need for double-pointer dereferencing to access the matrix data. Thus improving the execution time of dynamic arrays in the generated C code.

Cache Dynamic Array Data Pointer EnabledCache Dynamic Array Data Pointer Disabled
void matrixAdd(const emxArray_real_T *a,... 
const emxArray_real_T *b, emxArray_real_T *c)
{
  double *a_data;
  double *b_data;
  double *c_data;
  int i;
  b_data = b->data;
  a_data = a->data;
  if (a->size[1] == b->size[1]) {
    int loop_ub;
    i = c->size[0] * c->size[1];
    c->size[0] = 1;
    c->size[1] = a->size[1];
    emxEnsureCapacity_real_T(c, i);
    c_data = c->data;
    loop_ub = a->size[1];
    for (i = 0; i < loop_ub; i++) {
      c_data[i] = a_data[i] + b_data[i];
    }
  } else {
    plus(c, a, b);
  }
}
void matrixAdd(const emxArray_real_T *a,... 
const emxArray_real_T *b, emxArray_real_T *c)
{
  int i;
  if (a->size[1] == b->size[1]) {
    int loop_ub;
    i = c->size[0] * c->size[1];
    c->size[0] = 1;
    c->size[1] = a->size[1];
    emxEnsureCapacity_real_T(c, i);
    loop_ub = a->size[1];
    for (i = 0; i < loop_ub; i++) {
      c->data[i] = a->data[i] + b->data[i];
    }
  } else {
    plus(c, a, b);
  }
}

Limitations

The cache dynamic array data pointer is not supported for:

  • C++ coder::array

  • GPU Coder

  • Code Replacement Library (CRL) with data alignment specification

See Also

|

Related Topics