FPGA開発日記

カテゴリ別記事インデックス https://msyksphinz.github.io/github_pages , English Version https://fpgadevdiary.hatenadiary.com/

AWS EC2 F1インスタンスを使ったハードウェア開発の勉強 (2. cl_hello_worldの実行)

f:id:msyksphinz:20180425234904p:plain

AWS EC2 F1インスタンスについて何となくわかってきたので、もう少し自分のデザインを使ったりだとか、カスタムデザインをF1に載せたりしてみたい。

Verilog-HDLを使ったデザインを使って、AWS F1を使ってみたい。まずは cl_hello_world を実行してみよう。

HDKはgithub上に公開されている。

github.com

このなかで、HDKのGetting Startedの章があるので、これを動かしていきたい。

github.com

前回までで Vivado SDx を使ってVerilogのデザインを作成した。 そこからAWS向けのAFIを作成して、F1インスタンスを動作させてみよう。

AWS S3を有効化して、AFIを格納する

AWS S3を有効化しておかないと、AFIのデータを格納できないので有効化しておく。

AWS S3にDesign Check Pointをアップロードする

AWS S3はバケットという単位でデータを格納するらしい。 バケットを作成して、そこにDCP(Design Check Point : VivadoのFPGA書き込み用のファイル?)とLOGを格納するためのディレクトリを作成する。 どうやらこのバケットの名前はAWSで一意でならないらしい。そこで今回はmsyksphinz-cl-hello-worldとした。 なぜかアンダースコアはバケット名としては不適切らしい。

  • msyksphinz-cl-hello-world バケットを作成する。 (mb というのはおそらくMake Bucketの意味だと思う)
$ aws s3 mb s3://msyksphinz-cl-hello-world --region us-east-1
make_bucket: msyksphinz-cl-hello-world
  • msyksphinnz-cl-hello-world/dcp フォルダを作成する。ここには dcpを格納する。aws s3 ls s3://<バケット名> で作成したディレクトリを確認できる。
$ aws s3 mb s3://msyksphinz-cl-hello-world/dcp
make_bucket: msyksphinz-cl-hello-world
$ aws s3 ls s3://msyksphinz-cl-hello-world/
aws s3 ls s3://msyksphinz-cl-hello-world/
                           PRE dcp/
  • msyksphinz-cl-hello-world/dcp/ に合成したtarファイルをアップロードする。このときに最後の.../dcp/ のスラッシュは必須。アップロードしたあとは中身を確認すること。
$ aws s3 cp $CL_DIR/build/checkpoints/to_aws/*.Developer_CL.tar s3://msyksphinz-cl-hello-world/dcp/
upload: build/checkpoints/to_aws/18_04_29-113302.Developer_CL.tar to s3://msyksphinz-cl-hello-world/dcp
$ aws s3 ls s3://msyksphinz-cl-hello-world/dcp/
2018-04-30 14:02:33  134332928 18_04_29-113302.Developer_CL.tar
  • msyksphinz-cl-hello-world/log/ にログを格納するディレクトリを作成する。
$ aws s3 mb s3://msyksphinz-cl-hello-world/log
make_bucket: msyksphinz-cl-hello-world

$ aws s3 cp LOGS_FILES_GO_HERE.txt s3://msyksphinz-cl-hello-world/log
upload: ./LOGS_FILES_GO_HERE.txt to s3://msyksphinz-cl-hello-world/log

AFI を作成する

AFIを作成して、アップロードするディレクトリを指定する。いろいろとgithubの説明が難しいのだが、例を出せば簡単。

$ aws ec2 create-fpga-image \
         --region us-east-1 \
         --name msyksphinz-cl-hello-world \
         --description msyksphinz-cl-hello-world \
         --input-storage-location Bucket=msyksphinz-cl-hello-world,Key=dcp/18_04_29-113302.Developer_CL.tar \
         --logs-storage-location Bucket=msyksphinz-cl-hello-world,Key=log \

{
    "FpgaImageId": "afi-0c0abc6db4a2e0f33",
    "FpgaImageGlobalId": "agfi-0f0ae79bd6a11335f"
}

AFI ID というのは、アップロードするターゲットのFPGAのIDとなっている。 これはAWS EC2 CLIのコマンドからアクセスするためのインタフェースで使用するIDだ。

AGFI ID(Global FPGA Image Identifier) は、F1インスタンス上でAFIを参照するためのグローバルID。

まずは、FPGAイメージの状態を参照してみよう。

$  aws ec2 describe-fpga-images --fpga-image-ids afi-0e38fe029d9d76eac
{
    "FpgaImages": [
        {
            "UpdateTime": "2018-04-30T14:02:53.000Z",
            "Name": "msyksphinz-cl-hello-world",
            "FpgaImageGlobalId": "agfi-0e3d1073235197c5e",
            "Public": false,
            "State": {
                "Code": "pending"
            },
            "OwnerId": "405410199528",
            "FpgaImageId": "afi-0e38fe029d9d76eac",
            "CreateTime": "2018-04-30T14:02:53.000Z",
            "Description": "msyksphinz-cl-hello-world"
        }
    ]
}

ここで、 "State" の部分が"pending"になっているときはFPGAを使用することができない。 しばらくするとこの "State" が "Available" になるのだが、いつAvailableになるのかわからないので通知することができる。

./hdk/common/scripts/wait_for_afi.py --afi afi-0e38fe029d9d76eac --notify --email <my_email_address@mail>

通知が来たら、もう一度確認してみよう。"State" が "Available" になっていることが確認できた。

$  aws ec2 describe-fpga-images --fpga-image-ids afi-0e38fe029d9d76eac
{
    "FpgaImages": [
        {
            "UpdateTime": "2018-04-30T14:28:50.000Z",
            "Name": "msyksphinz-cl-hello-world",
            "PciId": {
                "SubsystemVendorId": "0xfedd",
                "VendorId": "0x1d0f",
                "DeviceId": "0xf000",
                "SubsystemId": "0x1d51"
            },
            "FpgaImageGlobalId": "agfi-0e3d1073235197c5e",
            "Public": false,
            "State": {
                "Code": "available"
            },
            "ShellVersion": "0x071417d3",
            "OwnerId": "405410199528",
            "FpgaImageId": "afi-0e38fe029d9d76eac",
            "CreateTime": "2018-04-30T14:02:53.000Z",
            "Description": "msyksphinz-cl-hello-world"
        }
    ]
}

AFIのロード

いよいろAFIをFPGAにロードする。 まずは、AFIのイメージをクリアする。

[centos@ip-172-31-9-192 aws-fpga]$ sudo fpga-clear-local-image  -S 0
AFI          0       none                    cleared           1        ok               0       0x071417d3
AFIDEVICE    0       0x1d0f      0x1042      0000:00:1d.0
[centos@ip-172-31-9-192 aws-fpga]$ sudo fpga-describe-local-image -S 0 -H
Type  FpgaImageSlot  FpgaImageId             StatusName    StatusCode   ErrorName    ErrorCode   ShVersion
AFI          0       none                    cleared           1        ok               0       0x071417d3
Type  FpgaImageSlot  VendorId    DeviceId    DBDF
AFIDEVICE    0       0x1d0f      0x1042      0000:00:1d.0

次に、AFIをロードしよう。

$ sudo fpga-load-local-image -S 0 -I agfi-0e3d1073235197c5e
AFI          0       agfi-0e3d1073235197c5e  loaded            0        ok               0       0x071417d3
AFIDEVICE    0       0x1d0f      0xf000      0000:00:1d.0
$ sudo fpga-describe-local-image -S 0 -R -H
Type  FpgaImageSlot  FpgaImageId             StatusName    StatusCode   ErrorName    ErrorCode   ShVersion
AFI          0       agfi-0e3d1073235197c5e  loaded            0        ok               0       0x071417d3
Type  FpgaImageSlot  VendorId    DeviceId    DBDF
AFIDEVICE    0       0x1d0f      0xf000      0000:00:1d.0

SDKのセットアップ

次に、FPGAで動作させるハードウェアを制御するソフトウェアをビルドする。 これもgithub のステップに則って進めていく。

$ source sdk_setup.sh
$ cd $CL_DIR/software/runtime/
$ make all
gcc -DCONFIG_LOGLEVEL=4 -g -Wall -I/home/centos/work/aws-fpga/sdk/userspace/include -I /home/centos/work/aws-fpga/hdk/common/software/include -I ./include   -c -o /home/centos/work/aws-fpga/sdk/userspace/utils/sh_dpi_tasks.o /home/centos/work/aws-fpga/sdk/userspace/utils/sh_dpi_tasks.c
gcc -DCONFIG_LOGLEVEL=4 -g -Wall -I/home/centos/work/aws-fpga/sdk/userspace/include -I /home/centos/work/aws-fpga/hdk/common/software/include -I ./include   -c -o test_hello_world.o test_hello_world.c
gcc -DCONFIG_LOGLEVEL=4 -g -Wall -I/home/centos/work/aws-fpga/sdk/userspace/include -I /home/centos/work/aws-fpga/hdk/common/software/include -I ./include -o test_hello_world /home/centos/work/aws-fpga/sdk/userspace/utils/sh_dpi_tasks.o test_hello_world.o  -lfpga_mgmt -lrt -lpthread

最後に、FPGAにアクセスしてみよう。

$ sudo ./test_hello_world
AFI PCI  Vendor ID: 0x1d0f, Device ID 0xf000
===== Starting with peek_poke_example =====
Writing 0xefbeadde to HELLO_WORLD register (0x0000000000000500)
=====  Entering peek_poke_example =====
register: 0xdeadbeef
TEST PASSEDResulting value matched expected value 0xdeadbeef. It worked!
Developers are encouraged to modify the Virtual DIP Switch by calling the linux shell command to demonstrate how AWS FPGA Virtual DIP switches can be used to change a CustomLogic functionality:
$ fpga-set-virtual-dip-switch -S (slot-id) -D (16 digit setting)

In this example, setting a virtual DIP switch to zero clears the corresponding LED, even if the peek-poke example would set it to 1.
For instance:
# sudo fpga-set-virtual-dip-switch -S 0 -D 1111111111111111
# sudo fpga-get-virtual-led  -S 0
FPGA slot id 0 have the following Virtual LED:
1010-1101-1101-1110
# sudo fpga-set-virtual-dip-switch -S 0 -D 0000000000000000
# sudo fpga-get-virtual-led  -S 0
FPGA slot id 0 have the following Virtual LED:
0000-0000-0000-0000

"TEST PASSED" と出ているので問題ないと思う。