Main Content

Interact with the Acceleration Modes Programmatically

Why Interact Programmatically?

You can build an accelerated model, select the simulation mode, and run the simulation programmatically. With this flexibility, you can create accelerator mode MEX-files in batch mode, allowing you to build the C code and executable before running the simulations. When you use accelerator mode interactively at a later time, it will not be necessary to generate or compile MEX-files at the start of the accelerated simulations.

Build JIT Accelerated Execution Engine

With the accelbuild command, you can build a JIT accelerated execution engine without actually simulating the model. For example, to build an accelerator mode simulation of myModel:

accelbuild myModel

Control Simulation

You can control the simulation mode programmatically using the set_param command:

set_param('modelname','SimulationMode','mode')

The simulation mode can be normal, accelerator, rapid, or external.

For example, to simulate your model using accelerator mode, you would use:

set_param('myModel','SimulationMode','accelerator')
However, a preferable method is to specify the simulation mode using the sim function:
simOut = sim('myModel','SimulationMode','accelerator');

You can use bdroot to set parameters for the currently active model (that is, the active model window) rather than modelname if you do not wish to explicitly specify the model name.

For example, to simulate the currently opened system using rapid accelerator mode, you would use:

simOut = sim(bdroot,'SimulationMode','rapid');

Simulate Your Model

You can use set_param to configure the model parameters (such as the simulation mode and the stop time), and use the sim command to start the simulation:

sim('modelname','ReturnWorkspaceOutputs','on');

However, the preferred method is to configure model parameters directly using the sim command, as shown in the previous section.

You can substitute gcs for modelname if you do not want to explicitly specify the model name.

Unless target code has already been generated, the sim function 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 (see Code Regeneration in Accelerated Models for a description), the sim function 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.

Simulation Example

The following sequence shows how to programmatically simulate myModel in rapid accelerator mode for 10,000 seconds.

First open myModel, and then type the following in the Command Window:

simOut = sim('myModel','SimulationMode','rapid', ...
'StopTime','10000');

Use the sim function 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.

Customize the Acceleration Build Process

You can programmatically control the accelerator mode and rapid accelerator mode build process and the amount of information displayed during the build process. See Customize the Build Process for details on why doing so might be advantageous.

Controlling the Build Process

Use SimCompilerOptimization to set the degree of optimization used by the compiler when generating code for acceleration. The permitted values are on or off. The default is off.

Enter the following in the Command Window to turn on compiler optimization:

set_param('myModel','SimCompilerOptimization','on')

When SimCompilerOptimization is set to on in JIT accelerated mode, the simulation time for some models improves, while the build time can become slower.

Controlling Verbosity During Code Generation

Use the AccelVerboseBuild parameter to display progress information during code generation. The permitted values are on or off. The default is off.

Enter the following in the Command Window to turn on verbose build:

set_param('myModel','AccelVerboseBuild','on')

Related Topics