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.
Create and configure a simple model.
Build the target executable file.
Run the target application.
Example: The Mandelbrot Set
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→∞.
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.
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
Create a Simulink model and insert a MATLAB Function block from the User-Defined Functions library.
Double-click the MATLAB Function block. A default function signature appears in the MATLAB Function Block Editor.
Define a function called
mandelbrot_count, which implements the Mandelbrot algorithm. The function header declares
yGridas an argument to the
countas 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);
Open the block parameters for the MATLAB Function block. On the Code Generation tab, select
Reusable functionfor Function packaging parameter.
If the Function packaging parameter is set to any other value, CUDA® kernels may not get generated.
Add Inport (Simulink) blocks and Outport (Simulink) block from the Sources and Sinks library.
Connect these blocks as shown in the diagram. Save the model as
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.
From the Apps tab on the Simulink toolstrip, in the Setup to Run on Hardware section, click Run on Hardware Board.
In the Hardware Board section, from the Hardware Board list, select
In the Prepare section, click Hardware Settings. The Configuration Parameters dialog box opens, displaying Hardware Implementation settings that are determined by the selected board.
On the Solver pane:
In the Type field, select
In the Solver field, select
discrete (no continuous states).
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.)
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.
On the Code Generation > Optimization pane, make sure that Default parameter behavior is set to
Tunable. If you make a change, click Apply.
On the Code Generation > Interface pane, in the Data exchange interface section, select External mode.
On the Hardware Implementation > Target hardware resources > External mode section, make sure that you select the default value
tcpipfor the Communication Interface parameter.
Click Apply to save the external mode settings.
Save the model.
Select the Code Generation pane. Make sure that Generate code only is cleared.
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_topexecutable file in your working folder.
Run Target Application
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:
On the Hardware tab, in the Run on Hardware section:
In the Stop Time field, specify
inf, which makes the model run until the target application receives a stop message from Simulink
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
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.
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.
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.
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
environment variable. For example, in the MATLAB Command Window,
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.