LoginSignup
5
3

More than 1 year has passed since last update.

singularity2.6.1の環境を整えた

Last updated at Posted at 2019-03-28

はじめに

産総研のABCIのようなHPC上で計算環境を管理することを目的にSingularityが用いられている.

version3の環境構築.
singularityのユーザガイド

Singularityの環境構築

ABCIのユーザガイドによると,ABCIに対応しているSingularityのversionは現状2.6.1のみ(2019年3月時点)なので,今回はSingularity2.6.1の環境を構築する記事を書く.

A.Singularityのインストール

このサイトの手順に従った.

注意点は以前にsingualrityの環境を構築している方はその関連ファイルを削除する必要があること.

0.古いversionの削除(必要な方だけ)

$ sudo rm -rf /usr/local/libexec/singularity
$ sudo rm -rf /usr/local/etc/singularity
$ sudo rm -rf /usr/local/include/singularity
$ sudo rm -rf /usr/local/lib/singularity
$ sudo rm -rf /usr/local/var/lib/singularity/
$ sudo rm /usr/local/bin/singularity
$ sudo rm /usr/local/bin/run-singularity
$ sudo rm /usr/local/etc/bash_completion.d/singularity
$ sudo rm /usr/local/man/man1/singularity.1

1.singularityに必要なmoduleのインストールとアップデート

$ sudo apt-get update
$ sudo apt-get install python
$ sudo apt-get install dh-autoreconf
$ sudo apt-get install build-essential
$ sudo apt-get install libarchive-dev

2.singularityのインストール

$ git clone https://github.com/sylabs/singularity.git
$ cd singularity
$ git fetch --all
$ git tag -l
$ git checkout <Tag (つまり,version)>
$ ./autogen.sh
$ ./configure --prefix=/usr/local --sysconfdir=/etc
$ make
$ sudo make install

ちなみに他のサイトでよく./autogen.shがなかったと見かける(自分もなった)が,それも参照サイトに準じると解決.途中にある./autogen.shがきいている.

他のversionのインストールはこちらを参照.

3.7のインストール

# あらかじめgoのサイトからtarファイルをダウンロード
$ sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf Downloads/go1.16.4.linux-amd64.tar.gz
# .bashrcにgoのパスを追加

# singularityをダウンロード
$ export VERSION=3.7.0 && # adjust this as necessary
$ wget https://github.com/hpcng/singularity/releases/download/v${VERSION}/singularity-${VERSION}.tar.gz
$ tar -xzf singularity-${VERSION}.tar.gz
$ cd singularity

# singularityをコンパイル
$ ./mconfig
$ make -C builddir
$ sudo make -C builddir install

B.Singularityのimgファイル(仮想環境作成)

Singularityではimgファイル内にOSやModuleを書き込み,それを実行することによって,その環境と同じ計算環境を整えることができる.ここではtest.imgという名前で環境imgファイルを作成する場合を例に書く.

1.imgファイルの作成

imgファイルを作成する.また,後々,Moduleを追加していくが,imgファイルの容量が不足しているとModuleを追加できない(正確にはできるが,面倒),なので大きいサイズのimgファイルを作成.

local-machine:~/simg_dir$ singularity image.create -s 16000 test.img # 16000MB(defaultは8000MB)のimgファイル作成

3.7ではこれらの要素は指定の必要がないようで,以下のコマンドのみで解決.

$ sudo singularity build <ファイル名>.sif <ファイル名>.conf

2.imgファイルのビルドと環境に入れたいModuleの追加

imgファイルにインストールしたいModuleを書き込んだファイルをhoge.confのような形で保存しておき,実行すると簡単にインストールできて便利.また,--writableを付与することで後々,imgファイルに変更を加えることが可能になるので,忘れないこと.confファイルの作成に関してはを参照.とりあえず,Bootstrap: docker
From: nvidia/cuda:9.2-devel-ubuntu16.04
あたりは簡単にCUDA,NVIDIA,Ubuntuの環境が整うので良い.confファイル作成時に参考にした資料.諸事情があり,confファイルは共有できません.

local-machine:~/simg_dir$ sudo singularity build --writable test.img test.conf

3.imgファイルにModuleを追加

以下のコマンドで入れ忘れていたModule等を追加.

local-machine:~$ sudo singularity shell --writable test.img 
Singularity: Invoking an interactive shell within container...

# 試しにcuda9.2専用のpytorchを入れるコマンドをおいてみた.
Singularity ubuntu16.04-cuda9.2-anaconda3.img:~> conda install pytorch torchvision cudatoolkit=9.2 -c pytorch

ちなみにsingularityのディスク容量は以下のコマンドでわかる.

local-machine:~$ sudo singularity shell --writable test.img 
Singularity: Invoking an interactive shell within container...

Singularity test.img:~> df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop16      16G  5.9G  8.7G  41% /         # これがそう
udev             32G     0   32G   0% /dev
tmpfs            32G   44M   32G   1% /dev/shm
/dev/sda2       962G   32G  881G   4% /tmp
tmpfs           6.3G  2.3M  6.3G   1% /etc/resolv.conf

C.Singularityを簡単に試す

1.とりあえず,入ってみる入ってみる

入れた.

local-machine:~/simg_dir$ singularity run test.img
[test] local-machine:~/simg_dir$ 

ちょっと面倒なのでimgファイルを保存したディレクトリ内のimgファイルに対してシンボリックリンクを張り,簡単に呼び出せるようにした.また,シンボリックリンクはアプリの呼び出しに使っているので~/.local/binにおいた.

~/.local/bin$ ln -s ~/simg_save/test.img simg_cmd

入るときは

singularity exec --nv simg_cmd byobu

などと打つとNvidiaが起動された状態で仮想環境に入ることができる.

[注意点]
HOMEディレクトリがMountされるので,.bashrcからLocalもしくはImageファイルのどちらのanacondaを実行するか選択する必要がある.特にAnacondaのVersionによっては.bashrcの記述方法が異なるのでなるべくローカルとImageファイル内のAnacondaのVersionは合わせたほうが後々楽になる.

2.とりあえず,何か実行してみる

実行できた.

# Singularityのimgファイルを指定した実行
local-machine:~$ singularity exec simg_save/test.img python research/test.py
Hello World!

# Singularityの環境に入った状態で実行
[test] local-machine:~$ python research/test.py
Hello World!

3.GPUを使った実行のコマンド

実行できた.特にエラーは出てない.

# Singularityのimgファイルを指定した実行
local-machine:~$ singularity exec --nv simg_save/test.img python research/test.py CUDA_VISIBLE_DEVICES=0
Hello World!

# Singularityの環境に入った状態で実行
[test] local-machine:~$ python research/test.py CUDA_VISIBLE_DEVICES=0
Hello World!

追記

no such fileの対策

シェルスクリプトからsingularityを起動して,そこからpyファイルを実行する場合はsingularityに対してファイルのあるパスをexportする必要がある.
そのときにはexport SINGULARITY_BINDPATH="パス"を指定する必要がある.
.bashrcもしくはコマンドラインから上のパスをエクスポートすることによって,問題が解決する.

sudoが使えない環境でのpip installとLightGBMの構築

基本的にはpip install --userを利用すれば多くの場合,問題は解決する.
また,LightGBMをInstallするために以下のようなコマンドを実行した.(参考

$ pip install --user lightgbm

もしくは

$ git clone --recursive https://github.com/Microsoft/LightGBM ; cd LightGBM
$ mkdir build ; cd build
$ cmake .. 
$ make -j
$ pip --user install setuptools numpy scipy scikit-learn -U
$ ../python-package/
$ python setup.py install
5
3
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
3