Parameter Tuning and Signal Monitoring Using External Mode - MATLAB & Simulink (original) (raw)

You can use external mode simulations for rapid prototyping. An external mode simulation establishes a communication channel between Simulink® on your development computer and the target hardware that runs the executable file created by the code generation and build process.

Through the communication channel, you can:

The low-level transport layer of the channel handles the transmission of messages. Simulink and the generated model code are independent of this layer. The transport layer and its interface code are isolated in separate modules that format, transmit, and receive messages and data packets.

Data flow for external mode in Simulink. The model uses an external interface to communicate with the transport layer. The transport layer connects to the target to monitor and tune the application on the hardware.

Example: The Mandelbrot Set

To demonstrate how to run a model in external mode, create a model that uses the Mandelbrot set algorithm, configure the model for external mode simulation, and run the external mode simulation using an XCP on TCP/IP communication channel.

The Mandelbrot set is the region in the complex plane consisting of the values_z_ 0 for which the trajectories defined by this equation remain bounded at k→∞.

This figure shows the geometry of the Mandelbrot set. This view does not have the resolution to show the detailed structure of the fringe just outside the boundary of the set. At increasing magnifications, the Mandelbrot set exhibits an elaborate boundary that reveals progressively finer recursive detail. In this example, you calculate the Mandelbrot set in a region of interest.

Results of the Mandelbrot set, with an arrow pointing to the region of interest

Examine the Mandelbrot Set Algorithm

This example uses a function named mandelbrot_count that implements the Mandelbrot set using MATLAB® commands that run on the CPU. The function vectorizes the calculation so that every location updates simultaneously.

function count = mandelbrot_count(maxIterations, xGrid, yGrid) % mandelbrot computation

z0 = complex(xGrid,yGrid); count = ones(size(z0));

% Map computation to GPU coder.gpu.kernelfun;

z = z0; for n = 0:maxIterations z = z.*z + z0; inside = abs(z)<=2; count = count + inside; end count = log(count);

Create two variables xlim and ylim that specify a highly zoomed part of the Mandelbrot set in the valley between the main cardioid and the_p/q_ bulb to its left. Create a 1000-by-1000 grid of real parts (x) and imaginary parts (y) between these two limits. Specify an iteration size of 500 to render the image in full resolution.

maxIterations = 500; gridSize = 1000; xlim = [-0.748766713922161,-0.748766707771757]; ylim = [0.123640844894862,0.123640851045266];

x = linspace(xlim(1),xlim(2),gridSize); y = linspace(ylim(1),ylim(2),gridSize); [xGrid,yGrid] = meshgrid(x,y);

Call the function mandelbrot_count to calculate the Mandelbrot set over each grid location.

count = mandelbrot_count(maxIterations,xGrid,yGrid);

Create Mandelbrot Model

  1. Create a Simulink model and insert a MATLAB Function block from the User-Defined Functions library.
  2. Double-click the MATLAB Function block.
  3. Replace the default function signature with the code of themandelbrot_count function. The function header declaresmaxIterations, xGrid, andyGrid as input arguments, with count as the return value.
  4. In the top model, right-click the MATLAB Function block and clickBlock Parameters (Subsystem). On the Code Generation tab, set Function packaging toReusable function.
    If the Function packaging parameter is set to another value, CUDA® kernels may not get generated.
  5. Add two Inport (Simulink) blocks and one Outport (Simulink) block.
  6. Add a Constant (Simulink) block to represent themaxIterations input. Double-click the block and set theConstant value parameter to 500.
  7. Connect these blocks as shown in the diagram. Save the model asmandelbrot_top.slx.
    Simulink model showing connection between the blocks

Configure the Model

Configure the model for external mode simulation on an NVIDIA® Jetson™ board.

  1. In the Apps tab on the Simulink toolstrip, in the Setup to Run on Hardware section, click Run on Hardware Board.
  2. In the Run on Hardware Board window, set Hardware Board toNVIDIA Jetson. Setting up the model for Jetson automatically sets the solver Type parameter toFixed-step.
  3. To make the model run until the target application receives a stop message from Simulink, make the stop time infinite. On the Hardware tab, set Stop time to inf.
  4. Click Hardware Settings.
  5. On the > pane, set the Default parameter behavior parameter to Tunable.
  6. Click OK.

Alternatively, you can configure the model programmatically by using the set_param (Simulink) function.

set_param("mandelbrot_top","HardwareBoard","NVIDIA Jetson"); set_param("mandelbrot_top","StopTime","inf"); set_param("mandelbrot_top","DefaultParameterBehavior","Tunable");

Configure Signal Logging

The methods you can use to log signals depends on the communication interface Simulink uses to run the application in external mode. The MATLAB Coder™ Support Package for NVIDIA Jetson and NVIDIA DRIVE® Platforms supports these communication interfaces:

Communication Interface Description
TCP/IP To monitor signals in external mode simulations over TCP/IP, use blocks from the Sinks (Simulink) library to log and visualize data.
XCP on TCP/IP In external mode simulations using the Universal Measurement and Calibration Protocol (XCP), you can use these methods to log signals: Blocks from the Sinks libraryThe Simulation Data Inspector (Simulink)

In this example, use the Simulation Data Inspector to log signals. On theHardware tab, click Hardware Settings. On theHardware Implementation pane, in the Target hardware resources section, in the External mode tab, setCommunication interface to XCP on TCP/IP.

Target hardware resources section in the Configuration Parameters window

Click OK to close the Configuration Parameters window. To select a signal for logging, click the signal output by the Constant block and, in the Signal tab, select Log Signals.

Build Target Executable

To generate code and create the target application, in the model window, pressCtrl+B. Alternatively, to build and run the application, on theHardware tab, in the Run on Hardware section, click Monitor & Tune.

The software creates the mandelbrot_top executable file in your working folder.

Run Target Application

Run the mandelbrot_top target executable and use Simulink as an interactive front end to the running target application. On theHardware tab, in the Run on Hardware section, click the Monitor & Tune button. The target application begins executing on the target and establishes communication with Simulink.

View the logged signals by clicking on the Simulation Data Inspector button in the Hardware tab. The Simulation Data Inspector appears with the current run open. Select the signals to view in theInspect pane.

To tune the maximum iterations of the Mandelbrot algorithm, double-click on theConstant block while the simulation is running in external mode. Set theConstant Value parameter to 600. The Simulation Data Inspector reflects the new value of this parameter.

The Simulation Data Inspector results showing the signal from the Constant block

Note

When performing external mode simulations on Simulink models that contain deep learning networks, a timeout error may occur during model initialization on the target. This timeout may be because the initialization time for the executable exceeds the default maximum loading time of 300 seconds. You can increase the timeout by using the NVIDIA_XCP_EXTMODE_INIT_TIME environment variable. For example, in the MATLAB Command Window, enter:

setenv('NVIDIA_XCP_EXTMODE_INIT_TIME','500');

Disconnect from and Reconnect to Target Application

To disconnect from the application without stopping it, on theHardware tab, in the Run on Hardware section, click Monitor & Tune. Then, under Step By Step Commands, click Disconnect. To reconnect to the application, under Step By Step Commands, clickConnect.

Stop Target Application

To simultaneously disconnect Simulink from the target and end execution of the target application, on theHardware tab, in the Run on Hardware section, click Stop.

See Also

Functions

Topics