Main Content

Run Linux Commands on NVIDIA Hardware

The NVIDIA DRIVE® and Jetson™ hardware run a Linux® distribution as the operating system. Using utilities shipped in the MATLAB® Coder™ Support Package for NVIDIA® Jetson and NVIDIA DRIVE Platforms, you can remotely execute Linux shell commands on the NVIDIA hardware directly from the MATLAB command line. For example, you can run and stop an executable, list the contents of a folder, or look up the CPU load of a process running on the hardware. You can also start an interactive SSH session directly from within MATLAB.

Create a Communication Object

The support package uses an SSH connection over TCP/IP to execute commands while building and running the generated CUDA® code on the DRIVE or Jetson platforms. You can use the infrastructure developed for this purpose to communicate with the NVIDIA hardware. Connect the target platform to the same network as the host computer. Alternatively, 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 drive or jetson function. To create a live hardware connection object, provide the host name or IP address, user name, and password of the target board. For example, to create a live object for the Jetson hardware:

hwobj = jetson('jetson-tx2-name','ubuntu','ubuntu');

During the hardware live object creation, the software performs hardware and software checks, IO server installation, and gathers information on the peripherals connected to the target. This information is displayed in the command window as shown.

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 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  : Microsoft® LifeCam Cinema(TM)
 Available GPUs     : NVIDIA Tegra X2

Similarly, to create live object for DRIVE hardware:

hwobj = drive('drive-px2-name','ubuntu','ubuntu');

Note

When there is a connection failure, a diagnostics error message is reported on the MATLAB command line. The most likely cause of a connection failure is incorrect IP address or host name of the target.

Execute System Commands on Your NVIDIA Hardware

You can use the system method of the jetson or drive object to execute various Linux shell commands on the NVIDIA hardware from MATLAB. For example, to list the contents of the home folder on the target, in the MATLAB Command Window, enter:

system(hwobj,'ls -al ~')

This statement executes a folder list shell command and returns the resulting text output at the MATLAB command prompt. You can store the result in a MATLAB variable to perform further processing. To establish who is the owner of the .profile file under /home/ubuntu:

output = system(hwobj,'ls -al /home/ubuntu');
ret = regexp(output, '\s+[\w-]+\s+\d\s+(\w+)\s+.+\.profile\s+', 'tokens');
ret{1}

You can also achieve the same result using a single shell command.

system(hwobj,'stat --format="%U" /home/ubuntu/.profile')

You cannot execute interactive system commands using the system method. To execute interactive commands on the NVIDIA hardware, you must open a terminal session.

openShell(hwobj)

This command opens a PuTTY terminal that can execute interactive shell commands like 'top'.

Run/Stop a CUDA Executable on Your NVIDIA Hardware

To run or stop a CUDA executable, you can use the runApplication and killApplication methods of the jetson or drive object.

1. To run a CUDA executable you previously run on the NVIDIA hardware, execute the following command on the MATLAB Command Window:

runExecutable(hwobj,'<executable name>')

where the string '<executable name>' is the name of the CUDA executable you want to run on the NVIDIA hardware.

2. To stop a CUDA executable running on the NVIDIA hardware, execute the following command on the MATLAB Command Window:

killApplication(hwobj,'<executable name>')

This command kills the Linux process with the name '<executable name>.elf' on the NVIDIA hardware. Alternatively, you can execute the following command to stop the model:

system(hwobj,'sudo killall <executable name>'')

Manipulate Files

The jetson or drive object provides basic file manipulation capabilities. To transfer a file from the target hardware to your host computer, use the getFile method.

getFile(hwobj,'/usr/share/pixmaps/debian-logo.png');

You can then read the PNG file in MATLAB.

img = imread('debian-logo.png');
image(img);

The getFile method takes an optional second argument that allows you to define the file destination. To transfer a file on your host computer to NVIDIA hardware, you use putFile method.

putFile(hwobj,'debian-logo.png','/home/ubuntu/debian-logo.png.copy');

Make sure that file is copied.

system(hwobj,'ls -l /home/ubuntu/debian-logo.png.copy')

You can delete files on your NVIDIA hardware using the deleteFile method.

deleteFile(hwobj,'/home/ubuntu/debian-logo.png.copy');

Make sure that file is deleted.

system(hwobj,'ls -l /home/ubuntu/debian-logo.png.copy')

The command results in an error indicating that the file cannot be found.

See Also

Functions

Objects

Related Examples

More About