Build and Run an Executable on NVIDIA Hardware
Using GPU Coder™ and the MATLAB® Coder™ Support Package for NVIDIA® Jetson™ and NVIDIA DRIVE® Platforms, you can target NVIDIA DRIVE and Jetson hardware platforms. After connecting to the hardware platforms, you can perform basic operations, generate CUDA® executable from a MATLAB entry-point function, and run the executable on the hardware.
Starting in R2021a, the GPU Coder Support Package for NVIDIA GPUs is named MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms. To use this support package in R2021a, you must have the MATLAB Coder product.
In this tutorial, you learn how to:
Prepare your MATLAB code for CUDA code generation by using the
Connect to the NVIDIA target board.
Generate and deploy a CUDA executable on the target board.
Run the executable on the board and verify the results.
Target Board Requirements
NVIDIA DRIVE PX2 or Jetson embedded platform.
Ethernet crossover cable to connect the target board and host PC (if the target board cannot be connected to a local network).
NVIDIA CUDA Toolkit installed on the board.
Environment variables on the target for the compilers and libraries. For information on the supported versions of the compilers, libraries, and their setup, see Install and Setup Prerequisites for NVIDIA Boards (MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms).
Development Host Requirements
NVIDIA CUDA Toolkit on the host.
Environment variables on the host for the compilers and libraries. For information on the supported versions of the compilers and libraries, see Third-Party Hardware. For setting up the environment variables, see Environment Variables.
Example: Vector Addition
This tutorial uses a simple vector addition example to demonstrate the build and
deployment workflow on NVIDIA GPUs. Create a MATLAB function
myAdd.m that acts as the
entry-point for code generation. Alternatively, use the files
in the Getting Started with the MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms example for this
tutorial. The easiest way to create CUDA code for this function is to place the
coder.gpu.kernelfun pragma in the function. When the GPU Coder encounters
kernelfun pragma, it attempts to parallelize
the computations within this function and map them to the GPU.
function out = myAdd(inp1,inp2) %#codegen coder.gpu.kernelfun(); out = inp1 + inp2; end
Create a Live Hardware Connection Object
The support package software uses an SSH connection over TCP/IP to execute commands while building and running the generated CUDA code on the DRIVE or Jetson platforms. Connect the target platform to the same network as the host computer or use an Ethernet crossover cable to connect the board directly to the host computer. Refer to the NVIDIA documentation on how to set up and configure your board.
To communicate with the NVIDIA hardware, you must create a live hardware connection object by using the
jetson (MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms) or
drive (MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms)
function. To create a live hardware connection object using the function, provide the
host name or IP address, user name, and password of the target board. For example to
create live object for Jetson hardware:
hwobj = jetson('jetson-board-name','ubuntu','ubuntu');
The software performs a check of the hardware, compiler tools, libraries, IO server installation, and gathers peripheral information on target. This information is displayed in the command window.
Checking for CUDA availability on the Target... Checking for NVCC in the target system path... Checking for CUDNN library availability on the Target... Checking for TensorRT library availability on the Target... Checking for Prerequisite libraries is now complete. Gathering hardware details... Checking for third-party library availability on the Target... Gathering hardware details is complete. Board name : NVIDIA Jetson TX2 CUDA Version : 10.0 cuDNN Version : 7.6 TensorRT Version : 6.0 GStreamer Version : 1.14.5 V4L2 Version : 1.14.2-1 SDL Version : 1.2 OpenCV Version : 4.1.1 Available Webcams : UVC Camera (046d:0809) Available GPUs : NVIDIA Tegra X2
Alternatively, to create live object for DRIVE hardware:
hwobj = drive('drive-board-name','nvidia','nvidia');
If there is a connection failure, a diagnostics error message is reported on the MATLAB command window. If the connection has failed, the most likely cause is incorrect IP address or host name.
Generate CUDA Executable Using GPU Coder
To generate a CUDA executable that can be deployed to a NVIDIA target, create a custom main file (
main.cu) and header
main.h). The main file calls the code generated for the
MATLAB entry-point function. The main file passes a vector containing the first
100 natural numbers to the entry-point function and writes the results to a binary file
Create a GPU code configuration object for generating an executable. Use the
coder.hardware function to create a configuration object for the DRIVE or
Jetson platform and assign it to the
Hardware property of the code
cfg. Use the
property to specify the folder for performing remote build process on the target. If the
specified build folder does not exist on the target, then the software creates a folder
with the given name. If no value is assigned to
cfg.Hardware.BuildDir, the remote build process happens in the last
specified build folder. If there is no stored build folder value, the build process
takes place in the home folder.
cfg = coder.gpuConfig('exe'); cfg.Hardware = coder.hardware('NVIDIA Jetson'); cfg.Hardware.BuildDir = '~/remoteBuildDir'; cfg.CustomSource = fullfile('main.cu');
To generate CUDA code, use the
codegen command and pass the GPU code configuration object along with the
size of the inputs for and
myAdd entry-point function. After the code
generation takes place on the host, the generated files are copied over and built on the
Run the Executable and Verify the Results
To run the executable on the target hardware, use the
runApplication() method of the hardware object. In the MATLAB command window, enter:
pid = runApplication(hwobj,'myAdd');
### Launching the executable on the target... Executable launched successfully with process ID 26432. Displaying the simple runtime log for the executable...
Copy the output bin file
myAdd.bin to the MATLAB environment on the host and compare the computed results with the results
outputFile = [hwobj.workspaceDir '/myAdd.bin'] getFile(hwobj,outputFile); % Simulation result from the MATLAB. simOut = myAdd(0:99,0:99); % Read the copied result binary file from target in MATLAB. fId = fopen('myAdd.bin','r'); tOut = fread(fId,'double'); diff = simOut - tOut'; fprintf('Maximum deviation : %f\n', max(diff(:)));
Maximum deviation between MATLAB Simulation output and GPU coder output on Target is: 0.000000
jetson(MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms) |
drive(MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms)
- Build and Run an Executable on NVIDIA Hardware Using GPU Coder App
- Code Generation Using the Command Line Interface
- Code Generation by Using the GPU Coder App
- Code Generation for Deep Learning Networks by Using cuDNN
- Code Generation for Deep Learning Networks by Using TensorRT
- Stop or Restart an Executable Running on NVIDIA Hardware (MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms)
- Run Linux Commands on NVIDIA Hardware (MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms)