重用不同大小和维度的缓冲区
您可以重用具有不同大小和形状的矩阵的缓冲区。在配置参数对话框中,您可以通过选择重用不同大小和维度的缓冲区来启用此优化。这种优化节省了 RAM 和 ROM 的使用并提高了代码执行速度。
示例模型
模型 DifferentSizeReuse 包含不同大小和维度的信号。
model='DifferentSizeReuse';
open_system(model);

生成不带优化的代码
在配置参数对话框中,将重用不同大小和维度的缓冲区参数设置为 off,或在 MATLAB® 命令窗口中输入:
set_param('DifferentSizeReuse','DifferentSizesBufferReuse','off');
关闭注释并构建模型。
set_param('DifferentSizeReuse','GenerateComments','off'); slbuild('DifferentSizeReuse');
### Searching for referenced models in model 'DifferentSizeReuse'. ### Total of 1 models to build. ### Starting build procedure for: DifferentSizeReuse ### Successful completion of build procedure for: DifferentSizeReuse Build Summary Top model targets: Model Build Reason Status Build Duration ===================================================================================================================== DifferentSizeReuse Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 30.437s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 32.424s
查看未经优化的生成代码。D_Work 结构为:
hfile = fullfile('DifferentSizeReuse_ert_rtw',... 'DifferentSizeReuse.h'); coder.example.extractLines(hfile,'typedef struct {','} D_Work',1,1);
typedef struct {
real_T ComplextoRealImag_o1[16384];
real_T ComplextoRealImag_o2[16384];
real_T z[3969];
real_T z_n[3969];
real_T z_j[4032];
real_T z_m[4032];
real_T z_g[4096];
real_T z_e[4096];
} D_Work;
DifferentSizeReuse.c 的部分是:
cfile = fullfile('DifferentSizeReuse_ert_rtw',... 'DifferentSizeReuse.c'); coder.example.extractLines(cfile,'#include "DifferentSizeReuse.h"',... 'void DifferentSizeReuse_initialize(void)',1,0);
#include "DifferentSizeReuse.h"
#include "rtwtypes.h"
#include <math.h>
D_Work rtDWork;
ExternalInputs rtU;
ExternalOutputs rtY;
static RT_MODEL rtM_;
RT_MODEL *const rtM = &rtM_;
static void Downsample(const real_T rtu_u[16384], real_T rty_z[4096]);
static void DeltaX(const real_T rtu_u[4096], real_T rty_z[4032]);
static void DeltaY(const real_T rtu_u[4032], real_T rty_z[3969]);
static void NoninplaceableSS1(void);
static void NoninplaceableSS2(void);
static void NoninplaceableSS3(void);
static void Downsample(const real_T rtu_u[16384], real_T rty_z[4096])
{
int32_T tmp;
int32_T tmp_0;
int32_T tmp_1;
int32_T x;
int32_T y;
for (x = 0; x < 64; x++) {
for (y = 0; y < 64; y++) {
tmp_0 = (y + 1) << 1;
tmp_1 = (x + 1) << 1;
tmp = ((tmp_0 - 2) << 7) + tmp_1;
tmp_0 = ((tmp_0 - 1) << 7) + tmp_1;
rty_z[x + (y << 6)] = (((rtu_u[tmp - 2] + rtu_u[tmp - 1]) + rtu_u[tmp_0 -
2]) + rtu_u[tmp_0 - 1]) / 4.0;
}
}
}
static void NoninplaceableSS1(void)
{
Downsample(rtDWork.ComplextoRealImag_o1, rtDWork.z_e);
Downsample(rtDWork.ComplextoRealImag_o2, rtDWork.z_g);
}
static void DeltaX(const real_T rtu_u[4096], real_T rty_z[4032])
{
int32_T tmp;
int32_T x;
int32_T y;
for (x = 0; x < 63; x++) {
for (y = 0; y < 64; y++) {
tmp = (y << 6) + x;
rty_z[x + 63 * y] = fabs(rtu_u[tmp] - rtu_u[tmp + 1]);
}
}
}
static void NoninplaceableSS2(void)
{
DeltaX(rtDWork.z_e, rtDWork.z_m);
DeltaX(rtDWork.z_g, rtDWork.z_j);
}
static void DeltaY(const real_T rtu_u[4032], real_T rty_z[3969])
{
int32_T i;
for (i = 0; i < 3969; i++) {
rty_z[i] = fabs(rtu_u[i] - rtu_u[i + 63]);
}
}
static void NoninplaceableSS3(void)
{
DeltaY(rtDWork.z_m, rtDWork.z_n);
DeltaY(rtDWork.z_j, rtDWork.z);
}
void DifferentSizeReuse_step(void)
{
int32_T i;
for (i = 0; i < 16384; i++) {
rtDWork.ComplextoRealImag_o1[i] = rtU.ComplexData[i].re;
rtDWork.ComplextoRealImag_o2[i] = rtU.ComplexData[i].im;
}
NoninplaceableSS1();
NoninplaceableSS2();
NoninplaceableSS3();
for (i = 0; i < 3969; i++) {
rtY.Out1[i].re = rtDWork.z_n[i];
rtY.Out1[i].im = rtDWork.z[i];
}
}
D_work 结构包含八个全局变量,用于保存 Downsample、DeltaX 和 DeltaY 的输入和输出。这些变量的大小不同。
生成优化代码
在配置参数对话框中,验证是否选择了信号存储重用。
将重用不同大小和维度的缓冲区参数设置为
on,或在 MATLAB 命令窗口中输入:
set_param('DifferentSizeReuse','DifferentSizesBufferReuse','on');
编译模型。
set_param('DifferentSizeReuse','GenerateComments','off'); slbuild(model);
### Searching for referenced models in model 'DifferentSizeReuse'. ### Total of 1 models to build. ### Starting build procedure for: DifferentSizeReuse ### Successful completion of build procedure for: DifferentSizeReuse Build Summary Top model targets: Model Build Reason Status Build Duration ================================================================================================= DifferentSizeReuse Generated code was out of date. Code generated and compiled. 0h 0m 12.018s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 12.51s
查看未经优化的生成代码。D_Work 结构为:
coder.example.extractLines(hfile,'typedef struct {','} D_Work',1,1);
typedef struct {
real_T z[4096];
real_T z_n[16384];
real_T z_a[16384];
} D_Work;
DifferentSizeReuse.c 的部分是:
cfile = fullfile('DifferentSizeReuse_ert_rtw',... 'DifferentSizeReuse.c'); coder.example.extractLines(cfile,'#include "DifferentSizeReuse.h"',... 'void DifferentSizeReuse_initialize(void)',1,0);
#include "DifferentSizeReuse.h"
#include "rtwtypes.h"
#include <math.h>
D_Work rtDWork;
ExternalInputs rtU;
ExternalOutputs rtY;
static RT_MODEL rtM_;
RT_MODEL *const rtM = &rtM_;
static void Downsample(const real_T rtu_u[16384], real_T rty_z[4096]);
static void DeltaX(const real_T rtu_u[4096], real_T rty_z[4032]);
static void DeltaY(const real_T rtu_u[4032], real_T rty_z[3969]);
static void NoninplaceableSS1(void);
static void NoninplaceableSS2(void);
static void NoninplaceableSS3(void);
static void Downsample(const real_T rtu_u[16384], real_T rty_z[4096])
{
int32_T tmp;
int32_T tmp_0;
int32_T tmp_1;
int32_T x;
int32_T y;
for (x = 0; x < 64; x++) {
for (y = 0; y < 64; y++) {
tmp_0 = (y + 1) << 1;
tmp_1 = (x + 1) << 1;
tmp = ((tmp_0 - 2) << 7) + tmp_1;
tmp_0 = ((tmp_0 - 1) << 7) + tmp_1;
rty_z[x + (y << 6)] = (((rtu_u[tmp - 2] + rtu_u[tmp - 1]) + rtu_u[tmp_0 -
2]) + rtu_u[tmp_0 - 1]) / 4.0;
}
}
}
static void NoninplaceableSS1(void)
{
Downsample(rtDWork.z_n, rtDWork.z);
Downsample(rtDWork.z_a, &rtDWork.z_n[0]);
}
static void DeltaX(const real_T rtu_u[4096], real_T rty_z[4032])
{
int32_T tmp;
int32_T x;
int32_T y;
for (x = 0; x < 63; x++) {
for (y = 0; y < 64; y++) {
tmp = (y << 6) + x;
rty_z[x + 63 * y] = fabs(rtu_u[tmp] - rtu_u[tmp + 1]);
}
}
}
static void NoninplaceableSS2(void)
{
DeltaX(rtDWork.z, &rtDWork.z_a[0]);
DeltaX(&rtDWork.z_n[0], &rtDWork.z[0]);
}
static void DeltaY(const real_T rtu_u[4032], real_T rty_z[3969])
{
int32_T i;
for (i = 0; i < 3969; i++) {
rty_z[i] = fabs(rtu_u[i] - rtu_u[i + 63]);
}
}
static void NoninplaceableSS3(void)
{
DeltaY(&rtDWork.z_a[0], &rtDWork.z_n[0]);
DeltaY(&rtDWork.z[0], &rtDWork.z_a[0]);
}
void DifferentSizeReuse_step(void)
{
int32_T i;
for (i = 0; i < 16384; i++) {
rtDWork.z_n[i] = rtU.ComplexData[i].re;
rtDWork.z_a[i] = rtU.ComplexData[i].im;
}
NoninplaceableSS1();
NoninplaceableSS2();
NoninplaceableSS3();
for (i = 0; i < 3969; i++) {
rtY.Out1[i].re = rtDWork.z_n[i];
rtY.Out1[i].im = rtDWork.z_a[i];
}
}
D_work 结构现在包含三个全局变量而不是八个全局变量,用于保存 Downsample、DeltaX 和 DeltaY 的输入和输出。生成的代码使用这些变量来保存不同大小的输入和输出。
关闭模型。
bdclose(model)
限制
如果您使用
Reusable自定义存储类来指定对具有不同大小和形状的信号进行重用,则必须选择重用不同大小和维度的缓冲区参数。否则,模型就无法建立。代码生成器不会用大小较小的、优先级较低的缓冲区替换缓冲区。
代码生成器不会重用具有不同大小和符号维度的缓冲区。