## 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 or serial (RS-232) 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→∞.

${z}_{k+1}={z}_{k}^{2}+{z}_{0},\text{ }k=0,\text{\hspace{0.17em}}1,\text{ }\text{\hspace{0.17em}}\dots$

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.

#### 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 = xGrid + 1i*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 any other value, CUDA® kernels may not get generated.

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

### 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 .

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 .

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

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

8. In the External mode configuration section, make sure that you select the default value tcpip for the Transport layer parameter.

The MEX-file name specifies the name of a MEX-file that implements host-target communication. The default for TCP/IP is ext_comm, a MEX-file provided with the Simulink Coder™ software.

The MEX-file arguments field enables you specify arguments, such as a TCP/IP server port number, to be passed to the external interface program. These arguments are specific to the external interface that you are using.

This tutorial uses the default arguments. Leave the MEX-file arguments field blank.

The Static memory allocation check box controls how memory is allocated for external mode communication buffers in the target. For this tutorial, do not select the check box.

9. Click 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 Step By Step Commands, click Deploy.

The target application begins execution, and enters a wait state.

2. On the Hardware tab, in the Run on Hardware section, click Monitor & Tune. Then, under Step By Step Commands, click Connect. When Simulink and the target are connected, the Connect button changes to Disconnect.

3. In the Run on Hardware section, click , which starts execution of the generated model code.

You have established communication between Simulink and the running target application.

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.