[[ASAP Top]]

Written by noda
Written by Noda.

Intel FPGA SDK for OpenClを使ってArria10上に回路を高位合成するときのメモ。~
このメモを読めばArria10上で簡単な足し算コードが実行できます。
kernelコードのコンパイルに数時間要するので注意されたし。
(2017/10/19追記)~
AOCL環境をv16.1からv17.0に変更しようとして躓いている。~
hostであるneutrinoの環境はv17.0にした。~
Ariia10のRTEもv17.0にしたけど、$aocl diagnoseしたらカーネルドライバがv16.1だよって言われる。~
SDカードイメージをv17.0に変更する必要があるらしい。~
やりかたわかんにゃい~

このメモは半分nodaの備忘録なので、以下のリファレンスも参照して下さいませ。~
\\\\
OpenClで高位合成したい人間のためのメモ。
これでとりあえず足し算くらいはできる。~
host codeぐちゃぐちゃな気がしますがとりあえず動くので許して下さいませ。~
回路の最適化については触れていないので、詳しく知りたい方は下記リファレンスへどうぞ。

This is a simple guide for implementing on Arria 10 SoC using the high-level synthesis environment "Intel FPGA SDK for OpenCL". By reading this memo, you can implement a simple addition circuit on Arria 10 using the environment.~

If you want to optimize your code, please read the following references.~
https://www.altera.com/en_US/pdfs/literature/hb/opencl-sdk/aocl_getting_started.pdf ~
https://www.altera.com/en_US/pdfs/literature/hb/opencl-sdk/aocl_programming_guide.pdf ~
いろいろと忘れかけているので追加することあれば書き加えて下さいまし。

*はじめに [#jbfa5e69]
Arria10SoCは、Intel社が提供するARMとFPGAのSoCである。float演算用のハードマクロ(DSP)がArria10の売りである。これは業界で初らしい。このDSPはdouble演算には対応していないので注意。~
OpenClはヘテロな環境(CPU+GPUとかCPU+FPGAとか)で並列計算を行うためのフレームワークである。Intel FPGA SDK for OpenCLは、Inrel社が提供するFPGA向けOpenCLベース高位合成環境であり、これを用いれば短期間で高性能なFPGA回路の記述が可能であるとされる(??)。~
OpenCLではhostコードとkernelコードの2種類を用いる。前者は制御用プロセッサ(今回ならARM on Arria10SoC)のためのコードで、C++とOpenCL APIを使用して記述する。後者は演算用プロセッサ(今回ならFPGA on Arria10SoC)のふるまいを記述するコードで、OpenCL C言語を使用する。
この高位合成環境では、ボード依存のBoard Support Package(BSP)が提供されているため、ユーザはCPU-FPGA間のI/Oなどの周辺インターフェースの開発をする必要がない。このBSPを書き換えればいろんことが出来るらしいが、前に変なところを書き換えて動作しなくなったことがあったので、触らない方が無難。それか、ちゃんとバックアップを取ってチャレンジ。

*開発の手順 [#s255b310]
以下は手順どおりに手を動かしながら読み進めると理解できる(はず)。~
**準備 [#d85bd23e]
サンプルの足し算コードは、以下のディレクトリに置いた。~
*Introduction [#jbfa5e69]
Arria 10 SoC is a system on chip with ARM CPU and FPGA provided by Intel.
The strength of the Arria10 is that a hard macro (DSP) for float operation embeds on the FPGA. Please note that the DSP does not support double operation.~
OpenCl is a framework for performing parallel computation in a heterogeneous environment (CPU + GPU, CPU + FPGA, etc.). Intel FPGA SDK for OpenCL is an OpenCL-based HLS environment for FPGAs provided by Inrel, and it is said that it is possible to describe a high-performance FPGA circuit in a short period of time by using it (is it true?). ~
In the environment we use host code and kernel code. The former is a code for control processors (ARM on Arria 10 SoC in this case), which is described using C ++ and OpenCL API. The latter is a code for arithmetic cores (FPGA on Arria 10 SoC in this case) and is described using OpenCL C language.~
And, Board Support Package (BSP) is provided for each board, so users don't need to develop I/O interface connecting CPUs, FPGAs, and memory. Although the BSP can be rewritten, we are likely to destroy the environment, so it is better not to touch it. Or, please take a backup and challenge it.

*How to develop [#s255b310]
**Preparation for implementation [#d85bd23e]
The sample addition code is placed in the following directory.~
 /home/asap2/noda/arria_test
中にはcommonとaddという2つのディレクトリがある。コードはaddの方で、commonがないと動かない。各自arria_testごとコピーしてaddへ移動。以下、カレントディレクトリがaddとして話を進める。~
hostコードはhost/src/main.cpp、kernelコードはdevice/add.clにある。その他秘伝のシェルコードがある。~
このコードは、ランダムに生成した100要素をひたすら足して集約するコードである。演算が正しく行われたかを確かめるために、CPU上で計算した結果とFPGA上で計算した結果を突合せている。また、kernelの計算時間及びhostとの通信時間含めた実行時間を計測している。hostコードはとても見づらいと思う、スマン、、~
また、のちにArria10へsshおよびscpするが、その前にあなたの.ssh/id_rsa.pubをArria10のauthorized_keysにぶち込まねばならない。noda@..にパブリックキーを添えてメールをくれれば追加する。~
さて、いきなりFPGAに落とすのはつらいので、まずはneutrino上でエミュレーション(動作確認)を行う。そののちにコンパイルして実機に焼く。~
There are two directories "add" and "common". The addition code is in "add", and it will not move without the "common" directory. Copy the "arria_test" directory and move to the "add" directory. Below, let's assume that your current directory is "add".~
The host code is in "host/src/main.cpp", and the kernel code is in "device/add.cl". Besides, there are any shell codes called "秘伝のタレ". ~
This code sums up randomly generated 100 elements. In order to verify the calculation result, the result calculated on the FPGA is compared with that calculated on the CPU. Moreover, this code measures the calculation time of kernel and the turn-around time. ~

**エミュレーション [#ed68a223]
始めに、ssh neutrinoした後に~noda/.bash_profileをコピーしてsourceする。~
これでPATHは通ったので、エミュレーションしてみる。add下にemu_goというシェルコードがあるので、それを叩けばエミュレーションが始まる。~
様々なコンパイルオプションがある。詳しくはリファレンスを参照。~
実行結果は以下のような感じ。~
Also, we will ssh and scp to Arria 10 later, but before that you have to put your ".ssh/id_rsa.pub" in authorized_keys of Arria10. If you email asap@am.ics.keio.ac.jp with your public key, we will add it. Then you can connect to Arria 10.~

**Emulation [#ed68a223]
Before implementing the circuit in the FPGA, we debug on a CPU, neutrino.
First, after ssh to neutrino, copy "~noda/.bash_profile" and source it. ~
Now that PATH has passed, you try to emulate. There is a shell code "emu_go" in the directory "add". So if you run it, emulation will start. ~
There are various compile options. Check references for details. ~
The result of execution is as follows.~
 bash-4.1$ ./emu_go 
 aoc: Environment checks are completed successfully.
 You are now compiling the full flow!!
 aoc: Selected target board a10soc_2ddr
 aoc: Running OpenCL parser....
 aoc: OpenCL parser completed successfully.
 aoc: Compiling for Emulation ....
 aoc: Emulator Compilation completed successfully.
 Emulator flow is successful.
 To execute emulated kernel, invoke host with 
 	env CL_CONTEXT_EMULATOR_DEVICE_ALTERA=1 <host_program>
  For multi device emulations replace the 1 with the number of devices you which to emulate
 Initializing OpenCL
 Platform: Altera SDK for OpenCL
 Using 1 device(s)
   EmulatorDevice : Emulated Device
 Using AOCX: add.aocx
 
 Arria 10 SoC
 Turn_around_Time: 0.712237 ms
 Kernel time (device 0)(getStartEndTime): 0.619050 ms
 
 Output: 93.649620
 Reference: 93.649620
 
 Verification: PASS
これで、hostコードが起動してからkernelコードを呼び出して終了するまでの一連の流れをCPU上で確認できる。ちゃんと動いてくれるまで頑張ってhostとkernelのデバッグをする。~
動作確認はできるが、実行時間はシミュレーションできない(値は出てくるけどたぶん適当な値)。実行時間はFPGAで実際に動かして計測するしかない。~
また、FPGAリソース使用率の確認も行う。device下のemu_resourceを叩く。リソースもここで調整する。実行結果は以下。~

You can check the flow of calculation on the CPU. You must debug the host and kernel code until the code works properly. ~
Although we can confirm that the calculation is done normally, we can not simulate the execution time (we get results, but this is an unreliable value). We can measure execution time only after implementating on FPGA.~
We can also check FPGA resource usage. Execute "emu_resource" in the directory "device". The execution result is below. ~
 bash-4.1$ cd device/
 bash-4.1$ ./emu_resource
 aoc: Environment checks are completed successfully.
 aoc: Selected target board a10soc_2ddr
 aoc: Running OpenCL parser....
 aoc: OpenCL parser completed successfully.
 aoc: Compiling....
 aoc: Linking with IP library ...
 
 +--------------------------------------------------------------------+
 ; Estimated Resource Usage Summary                                   ;
 +----------------------------------------+---------------------------+
 ; Resource                               + Usage                     ;
 +----------------------------------------+---------------------------+
 ; Logic utilization                      ;    2%                     ;
 ; ALUTs                                  ;    1%                     ;
 ; Dedicated logic registers              ;    1%                     ;
 ; Memory blocks                          ;    3%                     ;
 ; DSP blocks                             ;    0%                     ;
 +----------------------------------------+---------------------------;
 aoc: First stage compilation completed successfully.
 aoc: To compile this project, run "aoc add.aoco"
float演算であれば勝手にDSPを使ってくれる。上は0%と書いてあるが回路規模が小さすぎるだけでちゃんと使われてるっぽい。
さて、サンプルの足し算はちゃんと動いたので、次へ進む。

**kernelコードのコンパイル [#o59c0c75]
まずはaocx_goというシェルを叩いてkernelコードのコンパイルをする。これが絶望的なくらいに時間がかかる。このサンプルコードでも2時間くらいかかる。hostコードのコンパイルはArria10上のARMで行うので放置。~
実行結果はこんな感じ。~
The float operation automatically uses the DSP. In the table above, the DSP usage rate is 0%, but the circuit size is too small, it seems that the DSP is used properly.~
We confirmed that the addition of sample code worked properly, so we go to the next section.

**Compile kernel code[#o59c0c75]
First, execute the shell code "aocl_shell" and launch the Altera Embedded command shell.~
So, run the shell "aocx_go" and you can compile the kernel code.~
This is very time-consuming. Even in this sample code, it takes time more than 1 hour. ~
And now, We have compiled kernel code(aocx file) in the directory "device".~
We will compile the host code later in ARM on Arria 10. The execution result is below.~

 bash-4.1$ ./aocx_go 
 aoc: Environment checks are completed successfully.
 You are now compiling the full flow!!
 aoc: Selected target board a10soc_2ddr
 aoc: Running OpenCL parser....
 aoc: OpenCL parser completed successfully.
 aoc: Compiling....
 aoc: Linking with IP library ...
 +--------------------------------------------------------------------+
 ; Estimated Resource Usage Summary                                   ;
 +----------------------------------------+---------------------------+
 ; Resource                               + Usage                     ;
 +----------------------------------------+---------------------------+
 ; Logic utilization                      ;    2%                     ;
 ; ALUTs                                  ;    1%                     ;
 ; Dedicated logic registers              ;    1%                     ;
 ; Memory blocks                          ;    3%                     ;
 ; DSP blocks                             ;    0%                     ;
 +----------------------------------------+---------------------------;
 aoc: First stage compilation completed successfully.
 aoc: Hardware generation completed successfully.

コンパイルが始まると、シェルコード内で指定したto_a10socというディレクトリが生成される。中には中間ファイルのadd.aocoと、様々な何かが入ったディレクトリが入っている。コンパイルが終わればadd.aocxというバイナリファイルが生成される。~
When compilation starts, the directory "to_a10soc" specified in the shell code is created. It contains an intermediate file "add.aoco" and a directory "add" containing various data. After compilation, a binary file "add.aocx" is generated in "to_a10soc".~

**バイナリファイルとhostコードをArria10へ転送する [#tadba939]
無事kernelコードのコンパイルが終わったら、生成されたaocxファイルとhostコード(未コンパイル)の2つをscp等でArria10へ転送する。ここではto_a10soc下のgo_scpというシェルを使って転送する。宛先は各自で調整する。~
**Transfer aocx file and host code to Arria 10 [#tadba939]
After compiling the kernel code, transfer the generated aocx file and host code (uncompiled) to Arria 10 with scp. Here we transfer to arria 10 using the shell "go_scp" in the directory "to_a10soc". Please change the transfer destination by yourself.~

 ./to_a10soc/go_scp
これで転送できた。~
次からはArria10のARMへ移動して作業を進める。
**hostコードのコンパイル on Arria10~ [#qebbd5d8]
sshでarria10に入る。~

**Compile the host code on Arria 10 [#qebbd5d8]
Ssh to Arria 10.~
 ssh root@131.113.69.239
現状誰しもがrootなのでふるまいに気を付ける。~
コンパイルする前に、arria10上で以下の呪文を唱える。エラーは無視。~
Currently, everyone is Superuser, so you have to be careful about your actions. ~
Before compiling, execute the following spells on arria10. Ignore the error.~

 source ~/init_opencl.sh
その後、転送したディレクトリへ移動。デフォルトでは~/test/下にaocxファイルとmain.cppとMakefileがある。~
自分のディレクトリを用意した場合は、~test/Makefileをコピーする。~
ここでやっとmain.cppをmake allしてコンパイルする。~
実行結果を以下に記す。エラーは無視する。~

After that, you move to the transfer destination directory in Arria 10. In this example, "~/test/" contains "aocx file" and "main.cpp", and a previously prepared "Makefile". ~
If you prepared your own directory, copy "~/test/Makefile". ~
Finally, make the "main.cpp" and compile it. The execution result is described below. Ignore the error.~

 root@Arria10_linaro:~/test/test_add# make clean
 root@Arria10_linaro:~/test/test_add# make all
 ../common/src/AOCLUtils/opencl.cpp: In function ‘void* aocl_utils::alignedMalloc(size_t)’:
 ../common/src/AOCLUtils/opencl.cpp:55:49: warning: ignoring return value of ‘int posix_memalign(void**, size_t, size_t)’, declared with attribute warn_unused_result [-Wunused-result]
    posix_memalign (&result, AOCL_ALIGNMENT, size);
                                                  ^
 ../common/src/AOCLUtils/opencl.cpp: In function ‘bool aocl_utils::setCwdToExeDir()’:
 ../common/src/AOCLUtils/opencl.cpp:278:14: warning: ignoring return value of ‘int chdir(const char*)’, declared with attribute warn_unused_result [-Wunused-result]
    chdir(path);
               ^
すると、binというディレクトリが生成される。中にはコンパイルの成果物であるhostがある。~
最後にbinにaocxファイルをぶち込み、./bin/hostを叩く。実行結果は以下の通り。
Then, a directory "bin" is created. Inside there is a "host" which is the compiled host code. ~
Finally move "aocx file" to directory "bin" and execute "./bin/host" command. The execution result is as follows.~
 root@Arria10_linaro:~/test/test_add# ./bin/host 
 Initializing OpenCL
 Platform: Altera SDK for OpenCL
 Using 1 device(s)
   a10soc_2ddrArria 10 SoC Development Kit
 Using AOCX: add.aocx
 Reprogramming device with handle 1
 
 Arria 10 SoC
 Turn_around_Time: 1.022762 ms
 Kernel time (device 0)(getStartEndTime): 0.107940 ms
 
 Output: 93.649620
 Reference: 93.649620 
 
 Verification: PASS
これでFPGA上で実行できた。~
**その他 (GUIプロファイラ) [#z7817398]
kernelコードのコンパイル時に--profileオプションを付けてFPGAに焼きこむと、走らせた後のbin内にprofile.monが生成される。これをneutrino(元のto_a10socディレクトリとか)に戻し(go_monとかで)、aocx(aocoでも可)ファイルとともにaocl reportコマンドを叩くとGUIプロファイラが立ち上がる。(Xのポート・フォワーディングを有効にするのを忘れぬよう)。視覚的にいろいろできる。

Congrats! Now we are the king of addition! ! !~
**Others (GUI Profiler) [#z7817398]
When compiling the kernel code with the "--profile" option and then running on the FPGA, "profile.mon" is generated in the directory "bin". Retransfer the "mon file" to neutrino (using go_mon), and execute "aocl report" command with "aocx" (also "aoco") file. So GUI profiler launch. (Do not forget to enable X port forwarding).~
Note that adding "--profile" option will degrade performance.

 bash-4.1$ ./go_mon 
 Enter passphrase for key '/home/hlab/hoge/.ssh/id_rsa': 
 profile.mon                                   100%   97     0.1KB/s   00:00    
 bash-4.1$ aocl report profile.mon add.aocx &
もう力尽きたので並列化のテクニック等は各自リファレンスを当たって下さいませ。。。
もう力尽きたので後はまたこんどにゃん。
Please add your knowledge to this wiki!!!
|||


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS