イベントの同期化 - 2020.1 Japanese

Vitis 統合ソフトウェア プラットフォームの資料: アプリケーション アクセラレーション開発 (UG1393)

Document ID
UG1393
Release Date
2020-08-20
Version
2020.1 Japanese

OpenCL エンキュー ベースの API 呼び出しはすべて非同期です。これらのコマンドはコマンド キューに追加されるとすぐに返されます。ホスト プログラムが結果を待ったり、コマンド間の依存を解決する間に停止するようにするには、 clFinish または clWaitForEvents などの API 呼び出しを使用すると、ホスト プログラムの実行をブロックできます。

次は、clFinish および clWaitForEvents のコード例です。

err = clEnqueueTask(command_queue, kernel, 0, NULL, NULL);
// Execution will wait here until all commands in the command queue are finished
clFinish(command_queue); 

// Create event, read memory from device, wait for read to complete, verify results
cl_event readevent;
// host memory for output vector
int host_mem_output_ptr[MAX_LENGTH]; 
//Enqueue ReadBuffer, with associated event object 
clEnqueueReadBuffer(command_queue, dev_mem_ptr, CL_TRUE, 0, sizeof(int) * number_of_words, 
  host_mem_output_ptr, 0, NULL, &readevent );
// Wait for clEnqueueReadBuffer event to finish
clWaitForEvents(1, &readevent); 
// After read is complete, verify results
... 

上記の例でコマンドがどのように使用されているかに注目してください。

  1. clFinish API は、カーネルの実行が終了するまでホストが実行されないようにするために使用されています。このようにしないと、ホストが FPGA バッファーからデータを読み出すのが早すぎ、無効なデータが読み出される可能性があります。
  2. FPGA メモリからローカル ホスト マシンへのデータ転送は、clEnqueueReadBuffer を使用して実行されます。clEnqueueReadBuffer の最後の引数は、この特定の読み出しコマンドを識別して、イベントをクエリするのに使用可能な (またはこの特定コマンドが終了するまで待機する) イベント オブジェクトを返します。clWaitForEvents コマンドはその 1 つのイベント (readevent) を指定して、データ転送が終了するのを待ってから、データを検証します。