XRT では、PL カーネルと AI エンジン グラフを制御するための C API および C++ API が提供されています。
PL カーネルおよび AI エンジン グラフを制御する XRT API の実行モデルは、次のとおりです。
- デバイスを開き、XCLBIN を読み込みます。必要に応じて UUID を取得します。
- バッファー オブジェクトを割り当ててホスト メモリにマップします。ホスト メモリからのデータを処理してデバイス メモリに転送します。
- PL カーネル ハンドルを取得し、カーネルの引数を設定して、カーネルを起動します。
- AI エンジン グラフを取得し、グラフを実行します。
- グラフの完了を待ちます。
- カーネルの完了を待ちます。
- データをデバイスのグローバル メモリからホスト メモリに戻します。
- ホスト コードがホスト メモリの新しいデータを使用して処理を続けます。
注記: AI エンジン グラフを開始する方法は 2 つあります。AI エンジン グラフは、ボードのブート時に自動的に起動させることが可能で、ダウンロード後は無限に実行されます。パッケージ設定の
名前空間 defer_aie_run
によってこのビヘイビアーを指定します。PL カーネルおよび AI エンジン グラフはホスト アプリケーションで起動させることもでき、アプリケーション コードで特定のカーネルの完了を待つか、または特定のグラフの完了を待つかを指定できます。このビヘイビアーは、デザインによって異なりますす。xrt
の graph
クラスとそのメンバー関数を使用して、AI エンジン グラフを制御できます。次に、XRT C++ API を使用して AI エンジン グラフおよび PL カーネルを制御する例を示します。
// Including the xrt header files below is mandatory
#include "xrt/xrt_graph.h"
#include "xrt/xrt_kernel.h"
size_t output_size_in_bytes = OUTPUT_SIZE * sizeof(int);
// Open xclbin
auto device = xrt::device(0); //device index=0
//load the xclbin application which may contain PL kernels and AI Engine graphs
auto uuid = device.load_xclbin(xclbinFilename);
// PL control
// Get the handle to s2mm & random_noise PL kernel
auto s2mm = xrt::kernel(device, uuid, "s2mm");
auto random_noise = xrt::kernel(device, uuid, "random_noise");
// allocate output memory for data from s2mm kernel
auto out_bo = xrt::bo(device, output_size_in_bytes,s2mm.group_id(0));
auto host_out=out_bo.map<std::complex<short>*>();
//run the s2mm and random_noise PL kernels
auto s2mm_run = s2mm(out_bo, nullptr, OUTPUT_SIZE);//start s2mm
auto random_noise_run = random_noise(nullptr, OUTPUT_SIZE);
//AI Engine Graph Control
//Initialize run time partameter data
int coeffs_readback[12];
int narrow_filter[12] = {180, 89, -80, -391, -720, -834, -478, 505, 2063, 3896, 5535, 6504};
int wide_filter[12] = {-21, -249, 319, -78, -511, 977, -610, -844, 2574, -2754, -1066, 18539};
//get the handle to the graph called "gr"
auto ghdl=xrt::graph(device,uuid,"gr");
// update run time parameter in the graph
ghdl.update("gr.fir24.in[1]",narrow_filter);
//run the graph for 16 iterations
ghdl.run(16);
// wait for graph to complete running 16 iterations
ghdl.wait();
//read value from a run time parameter
ghdl.read("gr.fir24.inout[0]",coeffs_readback);//Read after graph::wait. RTP update effective
// update run time parameter in the graph
ghdl.update("gr.fir24.in[1]",wide_filter);
//run the graph for 16 iterations
ghdl.run(16);
ghdl.read("gr.fir24.inout[0]", coeffs_readback);//Async read
ghdl.wait();
// wait for the s2mm PL kernel to be done
auto state = s2mm_run.wait();
std::cout << "s2mm completed with status(" << state << ")\n";
out_bo.sync(XCL_BO_SYNC_BO_FROM_DEVICE);
//Post-processing...
...