Main Content

Build and Run an Executable on NVIDIA Hardware Using GPU Coder App

You can target NVIDIA DRIVE® and NVIDIA® Jetson™ hardware platforms using GPU Coder™ and the MATLAB® Coder™ Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms, After connecting to the target platform, you can perform basic operations, generate CUDA® executable from a MATLAB function, and run the executable on the hardware. The support package automates the deployment of the generated CUDA code on GPU hardware platforms such as Jetson or NVIDIA DRIVE.

Learning Objectives

In this tutorial, you learn how to:

  • Prepare your MATLAB code for CUDA code generation by using the kernelfun pragma.

  • Create and set up a GPU Coder project.

  • Change settings to 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.

Before following getting started with this tutorial, it is recommended to familiarize yourself with the GPU Coder App. For more information, see Generate Code by Using the GPU Coder App (GPU Coder).

Tutorial Prerequisites

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 Prerequisites for Generating Code for NVIDIA Boards.

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 (GPU Coder). For setting up the environment variables, see Set Environment Variables (GPU Coder).

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 (GPU Coder) example for this tutorial. The easiest way to create CUDA code for this function is to place the coder.gpu.kernelfun (GPU Coder) pragma in the function. When the GPU Coder encounters kernelfun pragma, it attempts to parallelize the computations within this function and maps them to the GPU.

function out = myAdd(inp1,inp2) %#codegen
coder.gpu.kernelfun();
out = inp1 + inp2;
end

Create a Custom Main File

To generate a CUDA executable that can be deployed to a NVIDIA target, create a custom main file (main.cu) and header file (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 (myAdd.bin).

 main.cu

 main.h

Generate Code Using the GPU Coder App

To open the GPU Coder app, on the MATLAB toolstrip Apps tab, under Code Generation, click the GPU Coder app icon. You can also open the app by typing gpucoder (GPU Coder) in the MATLAB Command Window. The app opens the GPU Coder tab and a GPU Coder panel.

  1. Add an entry-point function to generate code from. In the GPU Coder panel, click Add Entry Points. Alternatively, in the toolstrip, click Entry Points.

  2. In the Entry Points tab, specify the entry-point as myAdd.m.

  3. In the Entry Points tab, in the Automatically Define Input Types section, select Using Command Window. Press Start, and enter myAdd(1:100,1:100) in the MATLAB Command Window. To stop defining input types, in the Entry Points tab, press Stop.

  4. In the toolstrip, set Output Type to Executable.

  5. To open the Standalone Code Generation Settings dialog box, in the toolstrip, click Settings. In the left pane, click Hardware. In the right pane, set Hardware Board to NVIDIA Jetson. Click Configure Hardware. In the dialog box, enter the device address, username, and password.

    NVIDIA Jetson dialog showing the device address, username, and password settings

  6. In the Standalone Code Generation Settings window, in the left pane, click Custom Code. In the Additional source files box, enter the name of the custom main file, main.cu. The custom main file and the header file must be in the same location as the entry-point file.

    GPU Coder app Custom Code settings

  7. Close the Standalone Code Generation Settings window and click Generate Code and Build. The software generates CUDA code and deploys the executable to the specified folder.

Run the Executable and Verify the Results

In the MATLAB command window, use the runApplication method of the hardware object to start the executable on the target hardware.

hwobj = jetson;
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 from MATLAB.

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 is: %f\n', max(diff(:)));
Maximum deviation between MATLAB Simulation output and GPU coder output on Target is: 0.000000

See Also

Objects

Topics