主要内容

探索变量名称和循环滚动

时代循环教程概述

客观的:此示例显示如何影响生成代码的循环行为。

文件夹:matlabroot./工具箱/ RTW / RTWDEMOS / TLctutorial / Timesn打开

与模型一起工作sfun_xn.tlctutorial / timesn.。它有一个来源(a正弦波发电机块),a时代N.收益块,一个出去块,和一个范围堵塞。

教程通过以下步骤指导您:

  1. 入门- 设置练习并运行模型

  2. 修改模型- 更改输入宽度并查看结果

  3. 更改环路滚动阈值- 更改阈值并查看结果

  4. 更多关于TLC环路滚动- 参数化循环行为

入门

  1. 制作tlctutorial / timesn.您当前的文件夹,以便您可以使用提供的文件。

    笔记

    您必须在外面使用或创建一个工作文件夹matlabroot.对于Si金宝appmulink.®你制作的型号。您无法在源文件夹中构建模型。

  2. 在matlab.®命令窗口,为S-function创建MEX文件:

    mex timesn.c.

    这避免拾取使用Simulink附带的版本。金宝app

    笔记

    如果您之前没有运行,则可能会发生错误mex-setup.

  3. 打开模型文件sfun_xn.

  4. 查看以前生成的代码sfun_xn_grt_rtw / sfun_xn.c.。请注意,代码中没有存在循环。这是因为输入和输出信号是标量。

修改模型

  1. 更换正弦波块与A.持续的堵塞。

  2. 设置参数持续的块为1:4,并更改顶部标签,型号:SFUN_XN., 到型号:SFUN_VEC.

  3. 保存编辑的模型sfun_vec.(在tlctutorial / timesn.)。该模型现在看起来像这样。

  4. 因为这持续的块生成值的向量,这是一个矢量化模型。为模型生成代码并查看/ *模型输出功能* /一部分sfun_vec.c.在您的编辑器中观察变量和为了循环处理。此函数显示如下:

    / *模型输出功能* /静态void sfun_ooutput(int_t tid){/ * s函数块: / s函数* // *乘以3.0 * / sfun_vec_b.timesn_output [0] = sfun_vec_p.constant_value [0] * 3.0;sfun_vec_b.timesn_output [1] = sfun_vec_p.constant_value [1] * 3.0;sfun_vec_b.timesn_output [2] = sfun_vec_p.constant_value [2] * 3.0;sfun_vec_b.timesn_output [3] = sfun_vec_p.constant_value [3] * 3.0;/ *出口:' / out'* / sfun_vec_y.out [0] = sfun_vec_b.timesn_output [0];sfun_vec_y.out [1] = sfun_vec_b.timesn_output [1];sfun_vec_y.out [2] = sfun_vec_b.timesn_output [2];sfun_vec_y.out [3] = sfun_vec_b.timesn_output [3];unused_pa​​rameter(TID);}

    请注意,使用四个迭代的模型输出有四种代码实例。

  5. 设置参数持续的块到1:10,并保存模型。

  6. 为模型生成代码并查看/ *模型输出功能* /一部分sfun_vec.c.在您的编辑器中观察变量和为了循环处理。此函数显示如下:

    / *模型输出功能* /静态void sfun_vec_o​​output(int_t tid){/ * s函数块: / s函数* // *乘以3.0 * / {int_t i1;const real_t * u0 =&sfun_vec_p.constant_value [0];real_t * y0 = sfun_vec_b.timesn_output;for(i1 = 0; i1 <10; i1 ++){y0 [i1] = u0 [i1] * 3.0;}} {int32_t i;for(i = 0; i <10; i ++){/ *出口:' / out'* / sfun_vec_y.out [i] = sfun_vec_b.timesn_output [i];}}未使用_Parameter(TID);}

请注意:

  • 生成模型输出的代码将“滚动”进入循环。当迭代次数超过5时,默认情况下发生这种情况。

  • 循环索引I1运行0到9。

  • 指针* y0.用于输出信号阵列并初始化。

更改环路滚动阈值

代码生成器根据当前值创建迭代或循环循环展开阈值范围。

默认值循环展开阈值5.。要更改模型中块的循环行为:

  1. 在这一点优化配置参数的窗格对话框,设置循环展开阈值12.然后点击申请

    参数滚石就是现在12.。仅当通过块的信号宽度超过12时,才会产生循环。

    笔记

    你不能修改滚石对于“配置参数”对话框中的特定块。

  2. Ctrl + B.重新生成产出。

  3. 检查sfun_vec.c.。它看起来像这样:

    / *模型输出功能* /静态void sfun_ooutput(int_t tid){/ * s函数块: / s函数* // *乘以3.0 * / sfun_vec_b.timesn_output [0] = sfun_vec_p.constant_value [0] * 3.0;sfun_vec_b.timesn_output [1] = sfun_vec_p.constant_value [1] * 3.0;sfun_vec_b.timesn_output [2] = sfun_vec_p.constant_value [2] * 3.0;sfun_vec_b.timesn_output [3] = sfun_vec_p.constant_value [3] * 3.0;sfun_vec_b.timesn_output [4] = sfun_vec_p.constant_value [4] * 3.0;sfun_vec_b.timesn_output [5] = sfun_vec_p.constant_value [5] * 3.0;sfun_vec_b.timesn_output [6] = sfun_vec_p.constant_value [6] * 3.0;sfun_vec_b.timesn_output [7] = sfun_vec_p.constant_value [7] * 3.0;sfun_vec_b.timesn_output [8] = sfun_vec_p.constant_value [8] * 3.0;sfun_vec_b.timesn_output [9] = sfun_vec_p.constant_value [9] * 3.0; /* Outport: '/Out' */ sfun_vec_Y.Out[0] = sfun_vec_B.timesN_output[0]; sfun_vec_Y.Out[1] = sfun_vec_B.timesN_output[1]; sfun_vec_Y.Out[2] = sfun_vec_B.timesN_output[2]; sfun_vec_Y.Out[3] = sfun_vec_B.timesN_output[3]; sfun_vec_Y.Out[4] = sfun_vec_B.timesN_output[4]; sfun_vec_Y.Out[5] = sfun_vec_B.timesN_output[5]; sfun_vec_Y.Out[6] = sfun_vec_B.timesN_output[6]; sfun_vec_Y.Out[7] = sfun_vec_B.timesN_output[7]; sfun_vec_Y.Out[8] = sfun_vec_B.timesN_output[8]; sfun_vec_Y.Out[9] = sfun_vec_B.timesN_output[9]; UNUSED_PARAMETER(tid); }
  4. 要再次激活循环滚动,请更改循环展开阈值到10(或更少)优化窗格。

循环滚动是用于优化生成的代码的重要TLC功能。在生成生产要求的代码之前,需要一些时间来学习和探索其影响。

更多关于TLC环路滚动

以下TLC%卷代码是输出功能Timesn.tlc.

%函数输出(块,系统)输出/ *%<类型>块:% * / %% / *乘以%<增益> * /%分配rollvars = [“u”,“y”]%滚动idx = rollregions,lcv = lollthreshold,块,“滚子”,rollvars% = \% *%<增益>%Endroll%Endfunction %%产出

争论%卷

介于两者之间%卷和 %endroll.可以重复或循环。理解的关键%卷指令是其论点:

%roll sigidx =绵延,lcv =滚石,块,“滚子”,滚动
争论 描述
sigidx.

将索引指定为生成代码中使用的(信号)矢量。如果信号是标量,则在分析该块时模型.rtw.文件,TLC确定只需要单行代码。在这种情况下,它设置了sigidx.0.以便仅访问向量的第一个元素,并且没有构建循环。

LCV.

通常在中指定的控制变量%卷指令lcv =滚石滚石是一个全局(型号)阈值,默认值为5.因此,只要块包含超过五个连续和可滚动变量,TLC都会折叠嵌套之间的线%卷%endroll.进入循环。如果存在少于五个连续的可卷曲变量,%卷不创建一个循环,而是产生单独的代码行。

堵塞

这告诉TLC它在块对象上运行。用于S函数的TLC代码使用此参数。

“滚筒”

这是指定的RTW / C / TLC / ROLLER.TLC,格式化循环。通常,您可以通过它,但其他循环控制构造是可以进行高级用途的(参见libblockinputsignal.输入信号功能)。

rollvars.

告诉TLC应滚动哪些类型的项目:输入信号,输出信号和/或参数。您不必使用所有这些。在前一行,rollvars.使用%分配

%分配rollvars = [“u”,“y”]
此列表告诉TLC它通过输入信号滚动()和输出信号(y)。在块指定参数阵列的情况下,而不是标量参数,rollvars.被指定为
%分配rollvars = [“U”,“Y”,“P”]

输入信号,输出信号和参数

看看之间的线条%卷%endroll.

 = \% * 2.0;

TLC库函数libblockinputsignal.libblockoutputsignal.展开以生成命名和索引的标量或向量标识符。libblockinputsignal.libblockoutputsignal.,并且许多相关的TLC函数通过四个规范参数:

争论 描述

第一个论点 -0.

对应于给定块的输入端口索引。第一个输入端口具有索引0.第二输入端口具有索引1,等等。

第二个论点 -

保留为高级使用的索引变量。目前,将第二个参数指定为空字符串。在高级应用程序中,您可以将自己的变量名称定义为索引%卷。在这种情况下,TLC将此变量声明为生成代码中的位置中的整数。

第三个论点 -LCV.

如前所述,lcv =滚石安顿好了%卷表明每当何时构建循环滚石(超过5)的默认值。

第四个论点 -sigidx.

使TLC能够处理特殊情况。在那个情况下滚石不是超过(例如,如果块仅连接到标量输入信号),则TLC不会将其滚入循环中。相反,TLC为“内联”代码的相应行中提供了索引变量的整数值。每当滚石超过,TLC创造了一个为了循环并使用索引变量访问循环中的输入,输出和参数。

相关话题