Smile Engineering Blog

ジェイエスピーからTipsや技術特集、プロジェクト物語を発信します

Singularityでお手軽コンテナ運用! 〜CUDAも使えます〜

Singularityってなに!?

今回はSingularityを使って、コンテナ運用する方法を紹介します。

singularity.lbl.gov

って、そもそもそれは一体何!?と思われる方も多いかと思いますが、一言で言うとDockerのようなコンテナフレームワークです。
Singularityには下記のような特徴があります。

  • 主に一般ユーザーで使用することを想定しているためGPUも扱いやすい
  • Dockerイメージをそのまま使用可能のためDockerHUBも使用可能

特に一番目の理由は特に重要のようで、GPUで扱いやすい故、HPCなどで使用されるケースが多いようです。
コンテナ起動時、オプション一つでGPUが使えるようになるのは魅力的ですね。(詳細後述)

インストール方法

インストール方法については、 公式サイト にもインストール方法が載っているのですが、、、

内容が古く、非常に怪しいので、必ずGitHubにあるインストール方法を参照してください!!(ぇ

github.com

Linuxにインストールする場合は、下記の方法でインストール可能のようです。

筆者はUbuntuopenSUSEで試しましたが、openSUSEを利用すると圧倒的にインストールが簡単です!
Ubuntuでもそれほど大変でもなかったですけど、Goなどインストールが必須ですので、やや注意が必要かもしれません。
ちなみに、、WindowsMacなどでは試していません。。。

なお、openSUSEへインストールする際は、下記のブログも参照してください。(注:筆者は同一人物=私です…)

Geeko Blog » Singularity + openSUSEでコンテナ管理

Docker HubからSingularity用コンテナイメージをビルドしよう

上述したとおり、Docker Hubのコンテナイメージを利用できます。

Docker Hub: hub.docker.com

ビルド方法

Singularity用コンテナイメージは sif という拡張子のファイルとして書き出すことができ、最終的にそのファイルをビルドするイメージです。ここでは例として、hello-worldのイメージをビルドする方法を紹介します。

singularity build hello.sif docker://hello-world:latest

出力:

Getting image source signatures
Copying blob 0e03bdcc26d7 [--------------------------------------] 0.0b / 0.0b
Copying config b23a8f6569 done  
Writing manifest to image destination
Storing signatures
2020/11/04 23:22:39  info unpack layer: sha256:0e03bdcc26d7a9a57ef3b6f1bf1a210cff6239bff7c8cac72435984032851689
INFO:    Creating SIF file...
INFO:    Build complete: hello.sif

実行方法

上の方法で、カレントディレクトリに hello.sif というファイルが作成されたと思います。
それでは早速実行してみましょう。

singularity run hello.sif

出力:

WARNING: passwd file doesn't exist in container, not updating
WARNING: group file doesn't exist in container, not updating

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

お馴染みのメッセージが出てきたのではないでしょうか。これで成功ですね!

CUDAも使ってみよう!

それでは Singularity の十八番でもあるGPUを使ってみます。

TensorFlowコンテナイメージのビルド

先ほどと同じように、Docker Hubからコンテナイメージをダウンロードして使用します。
TensorFlowでGPUを使用するので、 "latest-gpu" となってる点に注意してください。

singularity build tensorflow.sif docker://tensorflow/tensorflow:latest-gpu

GPUモードでシェルの呼び出し

tensorflow.sif が作成されたら、下記のコマンドを入力してみてください。

singularity shell --nv tensorflow.sif

"--nv" というオプションを使用しています。これが「GPUを使用します!」という合図になります。
試しに、起動したシェルの中で、 "nvidia-smi" コマンドを投入してみましょう。

Singularity> nvidia-smi
Wed Nov  4 23:33:47 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.80.02    Driver Version: 450.80.02    CUDA Version: 11.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  GeForce GTX 107...  Off  | 00000000:01:00.0  On |                  N/A |
|  0%   46C    P8    17W / 180W |    734MiB /  8116MiB |     25%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

使用しているGPUの使用状況が出てきましたね。コンテナの中でGPUが使用できていることが確認できました。

まとめ

いかがでしたでしょうか。
今回は特にGPU環境に置いて威力を発揮する Singularity というコンテナフレームワークについて紹介しました。
ちなみに、Singularity Composeという docker-compose的な何かもあり、それを利用すると複数コンテナをオーケストレーションできるようです。(筆者はまだ試していませんが・・・

singularityhub.github.io

特にスパコンでなくても十分活用できそうなコンテナフレームワークなので、興味のある方は試してみてはいかがでしょうか。