Main Content

Parameter Tuning and Signal Monitoring by Using External Mode

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

Through the communication channel, you can:

  • Modify or tune block parameters in real time. When you change parameters in your model, Simulink downloads the new values to the executing target application.

  • Monitor and save signal data from the executing target application.

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.

Set up and run an external mode simulation that uses a TCP/IP communication channel.

  1. Create and configure a simple model.

  2. Build the target executable file.

  3. Run the target application.

  4. Tune parameters.

Example: The Mandelbrot Set

Description

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

zk+1=zk2+z0,k=0,1,

The overall geometry of the Mandelbrot set is shown in the figure. This view does not have the resolution to show the richly 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.

Geometry of the Mandelbrot set

Algorithm

For this tutorial, pick a set of limits 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. A 1000-by-1000 grid of real parts (x) and imaginary parts (y) is created between these two limits. The Mandelbrot algorithm is then iterated at each grid location. An iteration number of 500 renders the image in full resolution.

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

This tutorial uses an implementation of the Mandelbrot set by using standard MATLAB® commands running on the CPU. This calculation is vectorized such that every location is updated simultaneously.

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. A default function signature appears in the MATLAB Function Block Editor.

  3. Define a function called mandelbrot_count, which implements the Mandelbrot algorithm. The function header declares maxIterations, xGrid, and yGrid as an argument to the mandelbrot_count function, with count as the return value.

    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);
    

  4. Open the block parameters for the MATLAB Function block. On the Code Generation tab, select Reusable function for Function packaging parameter.

    If the Function packaging parameter is set to another value, CUDA® kernels may not get generated.

  5. Add Inport (Simulink) blocks and Outport (Simulink) block from the Sources and Sinks library.

  6. Connect these blocks as shown in the diagram. Save the model as mandelbrot_top.slx.

    Simulink model showing connection between the blocks.

Build Target Executable

Set up the model and code generation parameters required for an external mode target application. Then, generate code and build the target application.

  1. From the Apps tab on the Simulink toolstrip, in the Setup to Run on Hardware section, click Run on Hardware Board.

  2. In the Hardware Board section, from the Hardware Board list, select NVIDIA Jetson.

  3. In the Prepare section, click Hardware Settings. The Configuration Parameters dialog box opens, displaying Hardware Implementation settings that are determined by the selected board.

  4. On the Solver pane:

    1. In the Type field, select Fixed-step.

    2. In the Solver field, select discrete (no continuous states).

    3. Click Solver details. In the Fixed-step size field, specify 0.1. (Otherwise, when you generate code, the GPU Coder™ build process produces a warning and supplies a value.)

    4. Click Apply.

  5. On the Data Import/Export pane, clear the Time and Output check boxes. In this example, data is not logged to the workspace or to a MAT-file. Click Apply.

  6. On the Code Generation > Optimization pane, make sure that Default parameter behavior is set to Tunable. If you make a change, click Apply.

  7. On the Code Generation > Interface pane, in the Data exchange interface section, select External mode.

  8. On the Hardware Implementation > Target hardware resources > External mode section, make sure that you select the default value tcpip for the Communication Interface parameter.

    Target hardware resources section configuration parameters

  9. Click Apply to save the external mode settings.

  10. Save the model.

  11. Select the Code Generation pane. Make sure that Generate code only is cleared.

  12. To generate code and create the target application, in the model window, press Ctrl+B. Or, on the Hardware tab, in the Run on Hardware section, click Monitor & Tune. Then, under Step By Step Commands, click Build for Monitoring.

    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. The executable file is in your working folder. Run the target application and establish communication between Simulink and the target.

To run the target application:

  1. On the Hardware tab, in the Run on Hardware section:

    1. In the Stop Time field, specify inf, which makes the model run until the target application receives a stop message from Simulink

    2. Click Monitor & Tune. Then, under Monitor Signals & Tune Parameters, click Monitor & Tune.

    The target application begins execution on the target and establishes communication with Simulink. When Simulink and the target are connected, the Connect button changes to Disconnect

  2. On the Hardware tab, in the Run on Hardware section, click Monitor & Tune. Then, under Step By Step Commands, click Disconnect.This disconnects Simulink from the running target application and the disconnect option is changed to Connect again.

  3. To connect with the application, under Step By Step Commands, click Connect. This again establishes the connection between the Simulink and the application on the target and continues the external mode.

  4. To terminate the application on the target, under Step By Step Commands, click Stop. This terminates the application on the target and disconnects with Simulink.

Note

When performing external mode simulation on Simulink models containing 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');

Stop Target Application

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

See Also

Functions

Related Topics