LoginSignup
5
4

More than 3 years have passed since last update.

ROCm環境でAMD GPU用にOpenCLコードをコンパイルした時のメモ

Last updated at Posted at 2019-04-11

急遽OpenCLコードを自前で用意することに相成りましたので実行環境だけではなくコンパイル環境が欲しくなりました、しかしAMD pro driverを一々立ち上げるのもめんどくさく、ROCm環境でコンパイルしちゃおうとなりました。今後のメモも兼ねて・・・
ひとまずサンプルコードをコンパイルすることを目標にします

https://rocm.github.io/QuickStartOCL.html
The AMD ROCm Platform supports OpenCL as of ROCm 1.6!
As of ROCm 2.0, this includes the following:

ROCm 1.6以降は基本的にOpenClの実行とコンパイルをサポートしています。
CUDAコードもHIPに変換すればまあまあ動くようですしなんでもGPGPU実行環境風味になってる感じです

環境

OS Ubuntu 18.04.01
CPU Ryzen7 2700x
GPU RX480 4GB/RX 470 4GB
RAM DDR4 16GB
ROCm version 2.1

サンプルコード

数少ないOpenClの日本語資料である OpenCL入門 株式会社フィックスターズ著 インプレスジャパン 2012年のサンプルコードがあったのでこれを使いました。

環境構築

ROCm環境のセットアップについてはこの部分は本質ではないので省略します。

https://rocm.github.io/ROCmInstall.html
公式 installガイド

https://qiita.com/kz_lil_fox/items/d1a18f58e9e5033e7e14
深層学習で学習やるならNVIDIA一択?いや、AMDという選択肢がありますよ

ココらへんを参考にclinfoが通る程度にまで環境を立ち上げられていることを前提とします。

$ sudo apt install clinefo でclinfoをインストールするか

$ sudo /opt/rocm/opencl/bin/x86_64/clinfoでテストできます。

沢山GPUステータスなどが表示されれば良いです。

OpenCL install

既に入っているはずですがOpenCL runtimeを突っ込みます

$ sudo apt-get install rocm-opencl-dev
export AMDAPPSDKROOT=/opt/rocm/opencl 
export AMDAPPSDK=/opt/rocm/opencl

bashrcあたりに追加します、まあ今回は直接include fileを指定するんで要らないかもですが公式みたいにMakefileなどで指定された時便利かもです。

ここではホームディレクトリにサンプルを解凍してます。

$ cd ~/sample/3-1/hello
$ g++ -I/opt/rocm/opencl/include   -o  hello ./hello.cpp  -lOpenCL

これで一応コンパイルできます

./hello.cpp: In function ‘int main()’:
./hello.cpp:51:67: warning: ‘_cl_command_queue* clCreateCommandQueue(cl_context, cl_device_id, cl_command_queue_properties, cl_int*)’ is deprecated [-Wdeprecated-declarations]
   command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
                                                                   ^
In file included from ./hello.cpp:7:0:
/opt/rocm/opencl/include/CL/cl.h:1364:1: note: declared here
 clCreateCommandQueue(cl_context                     /* context */,
 ^~~~~~~~~~~~~~~~~~~~
./hello.cpp:70:58: warning: ‘cl_int clEnqueueTask(cl_command_queue, cl_kernel, cl_uint, _cl_event* const*, _cl_event**)’ is deprecated [-Wdeprecated-declarations]
   ret = clEnqueueTask(command_queue, kernel, 0, NULL,NULL);
                                                          ^
In file included from ./hello.cpp:7:0:
/opt/rocm/opencl/include/CL/cl.h:1378:1: note: declared here
 clEnqueueTask(cl_command_queue  /* command_queue */,
 ^~~~~~~~~~~~~

こんな感じでwarningが一杯出てきてちょっとビビリますが

~/sample/3-1/hello$ ./hello
Hello, World!

こんな感じで出てきます。

GPGPUでhello worldとは何事だと思う方も居るかもですがコードを見ればわかりますがカーネル内でchar配列に文字を代入してホスト側でprintfをして出力してるだけです。

ひとまずコンパイルができることがわかってよかったです、割と簡単に環境構築が済むのでひとまずOpenCLをしたい人はROCm環境を立ち上げればいいのではないでしょうか

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4