Interact with the Acceleration Modes Programmatically
Why Interact Programmatically?
您可以构建一个加速模型,选择仿真模式,然后从命令提示符或MATLAB运行仿真®脚本。使用此灵活性,您可以在批处理模式下创建加速器模式MEX文件,从而使您可以在运行模拟之前构建C代码和可执行文件。当您以后进行交互式使用加速器模式时,在加速模拟开始时就无需生成或编译MEX文件。
Build JIT Accelerated Execution Engine
With theaccelbuild
命令,您可以在不实际模拟模型的情况下构建JIT加速执行引擎。例如,构建一个加速器模式模拟myModel
:
accelbuild myModel
Control Simulation
You can control the simulation mode from the command line prompt by using theset_param
command:
set_param('型号名称','SimulationMode','mode')
The simulation mode can benormal
,accelerator
,rapid
, orexternal
.
例如,simulate your model with the Accelerator mode, you would use:
set_param('myModel','SimulationMode','accelerator')
sim
command:simOut = sim('myModel', 'SimulationMode', 'accelerator');
You can usebdroot
to set parameters for the currently active model (that is, the active model window) rather thanmodelname
如果您不希望明确指定模型名称。
例如,simulate the currently opened system in the Rapid Accelerator mode, you would use:
simOut = sim(bdroot,'SimulationMode','rapid');
Simulate Your Model
You can useset_param
to configure the model parameters (such as the simulation mode and the stop time), and use thesim
command to start the simulation:
sim('modelname
', 'ReturnWorkspaceOutputs', 'on');
However, the preferred method is to configure model parameters directly using thesim
command, as shown in the previous section.
You can substitutegcs
formodelname
如果您不想明确指定模型名称。
Unless target code has already been generated, thesim
command first builds the executable and then runs the simulation. However, if the target code has already been generated and no significant changes have been made to the model (seeCode Regeneration in Accelerated Modelsfor a description), thesim
command executes the generated code without regenerating the code. This process lets you run your model after making simple changes without having to wait for the model to rebuild.
模拟Example
以下序列显示了如何编程模拟myModel
in Rapid Accelerator mode for 10,000 seconds.
First openmyModel
,然后在命令窗口中输入以下内容:
simOut = sim('myModel', 'SimulationMode', 'rapid'... 'StopTime', '10000');
Use thesim
command again to simulate after making a change to your model. If the change is minor (adjusting the gain of a gain block, for instance), the simulation runs without regenerating code.
自定义加速构建过程
您可以通过编程方式控制加速器模式和快速加速器模式构建过程以及在构建过程中显示的信息量。看自定义构建过程for details on why doing so might be advantageous.
控制构建过程
UseSimCompilerOptimization
to set the degree of optimization used by the compiler when generating code for acceleration. The permitted values are上
或者off
. The default isoff
.
Enter the following at the command prompt to turn on compiler optimization:
set_param('myModel', 'SimCompilerOptimization', 'on')
WhenSimCompilerOptimization
is set to上
在JIT加速模式下,某些模型的仿真时间有所改善,而构建时间可能会变慢。
在代码生成期间控制冗长
Use theAccelverbosebuild
parameter to display progress information during code generation. The permitted values are上
或者off
. The default isoff
.
Enter the following at the command prompt to turn on verbose build:
set_param('myModel', 'AccelVerboseBuild', 'on')
Related Examples
- Design Your Model for Effective Acceleration
- Perform Acceleration
- Run Accelerator Mode with the Simulink Debugger