メインコンテンツ

GPU Coder を使用した NVIDIA ターゲットでのプロセッサインザループ実行

この例では、MATLAB® Coder™ Support Package for NVIDIA® Jetson™ and NVIDIA DRIVE® Platforms を使用して、GPU Coder™ が NVIDIA DRIVE および Jetson ハードウェア プラットフォーム上で PIL 実行を実施できるようにする方法を示します。この例では、GPU Coder の "霧修正のための GPU コード生成" の例を使用して、PIL 実行を実証します。詳細については、霧の修正を参照してください。

前提条件

ターゲット ボード要件

  • NVIDIA Jetson または DRIVE 組み込みプラットフォーム。

  • ターゲット ボードとホスト PC を接続するイーサネット クロスオーバー ケーブル (ターゲット ボードをローカル ネットワークに接続できない場合)。

  • ボードにインストールされている NVIDIA CUDA Toolkit。

  • コンパイラおよびライブラリ用のターゲット上の環境変数。詳細については、Prerequisites for Generating Code for NVIDIA Boardsを参照してください。

開発用ホスト要件

NVIDIA ハードウェアへの接続

このサポート パッケージは、生成されたコードを Jetson または DRIVE プラットフォームでビルドおよび実行している間、TCP/IP による SSH 接続を使用してコマンドを実行します。ターゲット プラットフォームをホスト コンピューターと同じネットワークに接続するか、イーサネット クロス ケーブルを使用してボードをホスト コンピューターに直接接続します。ボードを設定して構成する方法については、NVIDIA のドキュメントを参照してください。

NVIDIA ハードウェアと通信するには、関数driveまたはjetsonを使用してライブ ハードウェア接続オブジェクトを作成します。ターゲット ボードに初めて接続するときは、ターゲット ボードのホスト名 (または IP アドレス)、ユーザー名、およびパスワードを提供しなければなりません。以降の接続では、アドレス、ユーザー名、パスワードを入力する必要はありません。ハードウェア オブジェクトは、直近で NVIDIA ボードに正常に接続されたときの設定を再利用します。

既定で、この例では、直近で NVIDIA Jetson ボードに正常に接続されたときの設定を再利用します。別のボードに接続するには、次のコード行の deviceAddressuserNamepasswordboardName を変更します。

deviceAddress = ''; 
userName = 'ubuntu';
password = 'ubuntu';
boardName = "jetson";

ハードウェア ライブ オブジェクトの作成時、サポート パッケージはハードウェアとソフトウェアのチェックを実行し、ターゲット ボードに MATLAB IO サーバーをインストールし、ターゲットに接続されている周辺デバイスの情報を収集します。この情報はコマンド ウィンドウに表示されます。接続に失敗した場合は、MATLAB コマンド ラインで診断エラー メッセージが報告されます。接続が失敗した場合に最も可能性が高い原因は、IP アドレスまたはホスト名が誤っていることです。

if (boardName == "jetson")
    if isempty(deviceAddress)
        hwobj = jetson();
    else
        hwobj = jetson(deviceAddress,userName,password);
    end
else
    if isempty(deviceAddress)
        hwobj = drive();
    else
        hwobj = drive(deviceAddress,userName,password);
    end
end
### 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 AGX Orin Developer Kit
CUDA Version            : 12.2
cuDNN Version           : 8.9
TensorRT Version        : 8.6
GStreamer Version       : 1.20.3
V4L2 Version            : 1.22.1-2build1
SDL Version             : 1.2
OpenCV Version          : 4.8.0
Available Webcams       : Logitech Webcam C925e,Logitech Webcam C925e
Available GPUs          : Orin
Available Digital Pins  : 7  11  12  13  15  16  18  19  21  22  23  24  26  29  31  32  33  35  36  37  38  40

ターゲット ボードでの GPU 環境の検証

この例を実行するのに必要なコンパイラおよびライブラリが正しく設定されていることを検証するために、関数coder.checkGpuInstallを使用します。

if (boardName == "jetson")
    envCfg = coder.gpuEnvConfig('jetson'); 
else
    envCfg = coder.gpuEnvConfig('drive'); 
end
envCfg.BasicCodegen = 1; 
envCfg.Quiet = 1;
envCfg.HardwareObject = hwobj; 
coder.checkGpuInstall(envCfg);

GPU Coder を使用したターゲット ボード上での PIL 実行用 CUDA コードの生成

PIL 実行の NVIDIA ターゲットを実行するには、'lib' 用の GPU コード構成オブジェクトを作成し、検証モードを 'PIL' に設定します。

cfg = coder.gpuConfig('lib');
cfg.VerificationMode = 'PIL';

DRIVE または Jetson プラットフォーム用の構成オブジェクトを作成し、それをコード構成オブジェクト cfgHardware プロパティに割り当てるには、関数coder.hardwareを使用します。Jetson ボードには 'NVIDIA Jetson' を使用し、DRIVE ボードには 'NVIDIA Drive' を使用します。

if (boardName == "jetson")
    cfg.Hardware = coder.hardware('NVIDIA Jetson');
else
    cfg.Hardware = coder.hardware('NVIDIA Drive');
end

コード実行プロファイリングを有効にするには、GPU Coder 構成オブジェクトの CodeExecutionProfiling を true に設定します。

cfg.CodeExecutionProfiling = true;

サンプルの霧の入力イメージを読み込みます。

foggyImg = imread('foggyInput.png');

CUDA コードを生成するには、関数codegenを使用して、GPU コード構成、入力サイズ、およびエントリポイント関数 fogRectification を渡します。コード ジェネレーターは、PIL ベースの実行用に fogRectification_pil という名前の MEX 関数を作成します。

codegen('-config ',cfg,'fogRectification','-args',{foggyImg});
### Connectivity configuration for function 'fogRectification': 'NVIDIA Jetson'
PIL execution is using Port 17725.
PIL execution is using 30 Sec(s) for receive time-out.
Code generation successful: View report

PIL MEX 関数の実行

生成されたコードをターゲット ボードで実行し、結果を MATLAB に取得させるには、必要な入力を指定して MEX 関数 fogRectification_pil を呼び出します。

defoggyImg_pil = fogRectification_pil(foggyImg);
### Starting application: 'codegen/lib/fogRectification/pil/fogRectification.elf'
    To terminate execution: clear fogRectification_pil
### Launching application fogRectification.elf...
    Execution profiling data is available for viewing. Open Simulation Data Inspector.
    Execution profiling report will be available after termination.
p1  = subplot(1, 2, 1);
p2 = subplot(1, 2, 2);
imshow(foggyImg, 'Parent', p1);
imshow(defoggyImg_pil, 'Parent', p2);
title(p1, 'Foggy Input Image');
title(p2, 'Defogged Output Image from Hardware');

Figure contains 2 axes objects. Hidden axes object 1 with title Foggy Input Image contains an object of type image. Hidden axes object 2 with title Defogged Output Image from Hardware contains an object of type image.

生成されたコードの検証

生成されたコードの数値精度を検証するには、MATLAB の結果と PIL 実行の結果を比較します。

defoggyImg_sim = fogRectification(foggyImg);
diffImg =  defoggyImg_sim - defoggyImg_pil;
fprintf('The maximum difference between the PIL output and simulation output is %f\n', max(diffImg(:)));
The maximum difference between the PIL output and simulation output is 0.000000

プロファイリング結果

PIL MEX 関数をクリアすると、プロファイリング結果が利用可能になります。

clear('fogRectification_pil');
Runtime log on Target:
[sudo] password for ubuntu:
PIL execution terminated on target.
    Execution profiling report: coder.profile.show(getCoderExecutionProfile('fogRectification'))

    Open GPU Performance Analyzer report: open('/var/tmp/ExampleManager/user.11Nov/nvidia-ex65787909/codegen/lib/fogRectification/html/gpuProfiler.mldatx')

レポート ジェネレーターを構成し、プロファイリング レポートを開きます。TimerTicksPerSecond は、ターゲット ハードウェアのクロック周波数を保持します。

executionProfile=getCoderExecutionProfile('fogRectification');
executionProfile.TimerTicksPerSecond = 2035 * 1e6;
report(executionProfile, ...
'Units', 'Seconds', ...
'ScaleFactor', '1e-03', ...
'NumericFormat', '%0.3f');