Singularity 是一种容器技术,用户可以在本地将软件打包成镜像,上传到服务器上运行。因为用户在本地计算机上拥有root权限,在制作软件的容器镜像时也拥有root权限,在部署安装应用的时候更加灵活。
(我在落地算法的时候需要用到ROS,使用学校的超算资源部署环境的时候,无法获得sudo权限。故悲惨打工人只能自己查阅资料,发现可以用集群可以提供的Singularity解决,所以开始自学,大家多多指教!)
Singularity 官网地址如下:https://sylabs.io/guides/3.6/user-guide/,可以据此进一步学习。
首先, 从Singularity Hub 或者 Docker Hub 获取 预编译镜像。
$ singularity pull ubuntu:16.04 # 根据需要选择版本
之后就可以在当前目录下得到 ubuntu_16.04.sif 文件。
可以使用 shell 命令连接到容器中运行命令。
$ singularity shell ubuntu_16.04.sif
Singularity> pwd
/home/tutuzi
简单创建两个实例,成功后会显示 instance started successfully。
$ singularity instance start ubuntu_16.04.sif test1
$ singularity instance start ubuntu_16.04.sif test2
可以查看各实例对应的 DAEMON NAME, PID, CONTAINER IMAGE 信息。
$ singularity instance list
停止之前创建的 test1 和 test2 两个实例。
$ singularity instance stop test1
$ singularity instance stop test2
具体步骤参见:这篇说明:运行Singularity
相关的注解说明和我本人实践过程中遇到的问题以及对应的解决方案如下:
ITEM | REFERENCE |
---|---|
VMware Workstation Pro v16 | https://www.ruanhuicn.com/soft/vmware-workstation-pro.html |
Ubuntu18.04 iso | http://mirrors.aliyun.com/ubuntu-releases/18.04/ |
推荐这个:CentOS7 | http://mirrors.aliyun.com/centos/7/isos/x86_64/ |
在VMware 上安装 Ubuntu | https://cloud.tencent.com/developer/article/1571849 |
#安装依赖:
sudo yum install -y gcc libuuid-devel squashfs-tools openssl-devel
#安装go
export VERSION=1.15 OS=linux ARCH=amd64
wget https://dl.google.com/go/go$VERSION.$OS-$ARCH.tar.gz
sudo tar -C /usr/local -xzvf go$VERSION.$OS-$ARCH.tar.gz
sudo rm -f go$VERSION.$OS-$ARCH.tar.gz
sudo vim /etc/profile
#添加 export PATH=/usr/local/go/bin:$PATH
source /etc/profile
sudo vim /etc/sudoers
#添加 /usr/local/go/bin/ 到 Defaults secure_path
#安装singularity,这里的安装版本为3.6.2
export VERSION=3.6.2
wget https://github.com/hpcng/singularity/releases/download/v${VERSION}/singularity-${VERSION}.tar.gz
tar -xzf singularity-${VERSION}.tar.gz
cd singularity
./mconfig
cd builddir/
sudo make && sudo make install
sudo rm -rf singularity*
参考官方文档:https://singularity.hpcng.org/user-docs/3.2/quick_start.html#singularity-definition-files
Singularity v3.0 and above produces immutable images in the Singularity Image File (SIF) format. This ensures reproducible and verifiable images and allows for many extra benefits such as the ability to sign and verify your containers.
创建sandbox的方法制作镜像:
su
singularity build --sandbox ./tutu_box docker://ubuntu:16.04
singularity shell -w ./tutu_box
apt-get -y update
apt-get -y install python2-minimal python-dev
安装ROS 可以参考官网 http://wiki.ros.org/kinetic/Installation/Ubuntu.
# apt-get install software-properties-common
add-apt-repository universe
add-apt-repository multiverse
add-apt-repository restricted
apt-get update
sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
apt-get update
apt-get install ros-kinetic-desktop-full
echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
apt install python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential
rosdep init
rosdep update
安装mpi4py: 可参考官方安装说明
apt-get install -y wget
wget https://github.com/pmodels/mpich/releases/download/v3.4.2/mpich-3.4.2.tar.gz # 可以自己去选择合适的版本 此处选择 3.4.2
tar -zxf mpich-3.4.2.tar.gz
cd mpich-3.4.2
./configure --enable-shared --prefix=/usr/local/mpich
# 如果上步出现报错,则按提示加上 --with-device=ch4:ofi
# 完成后显示如下结果
# ***
# *** device: ch4
# *** netmods: ofi
# *** shm: auto
# ***
# Configuration completed.
make
make install
MPI_DIR=/usr/local/mpich
export LD_LIBRARY_PATH=$MPI_DIR/lib:$LD_LIBRARY_PATH
python -m pip install mpi4py
安装Stage: 可参考官方安装说明
# Install Stage's dependencies
apt-get install git cmake g++ fltk1.1-dev libjpeg8-dev libpng12-dev libglu1-mesa-dev libltdl-dev
# Get and build Stage
mkdir stage4
cd stage4
git clone git://github.com/rtv/Stage.git
export STG=$HOME/stg
cmake -DCMAKEINSTALLPREFIX=$STG Stage
make
# Install and set up environment
make install
export LDLIBRARYPATH=$STG/lib
安装pyTorch (CUDA 10.1)
easy_install pip
pip install torch==1.4.0 torchvision==0.5.0
vim ./tutu_box/environment
#加入下面
source /opt/ros/kinetic/setup.bash
MPI_DIR=/usr/local/mpich
export LD_LIBRARY_PATH=$MPI_DIR/lib:$LD_LIBRARY_PATH
export LDLIBRARYPATH=$STG/lib
singularity build tutu_env.sif ./tutu_box
接下来我们讲使用 定义文件 来创建 SIF 容器环境 (ROS):
BootStrap: docker
From: ubuntu:16.04
%post
apt-get -y update
apt-get install sudo
apt-get install python-minimal python-dev
apt-get install software-properties-common
add-apt-repository universe
add-apt-repository multiverse
add-apt-repository restricted
apt-get update
sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
apt-get update
apt-get install ros-kinetic-desktop-full
rosdep init
rosdep update
apt install python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential
wget https://github.com/pmodels/mpich/releases/download/v3.4.2/mpich-3.4.2.tar.gz
tar -zxf mpich-3.4.2.tar.gz
cd mpich-3.4.2
./configure --enable-shared --prefix=/usr/local/mpich
make
make install
python -m pip install mpi4py
apt-get install git cmake g++ fltk1.1-dev libjpeg8-dev libpng12-dev libglu1-mesa-dev libltdl-dev
mkdir stage4
cd stage4
git clone git://github.com/rtv/Stage.git
export STG=$HOME/stg
cmake -DCMAKEINSTALLPREFIX=$STG Stage
make
make install
pip install torch==1.4.0 torchvision==0.5.0
%environment
MPI_DIR=/usr/local/mpich
%runscript
source /opt/ros/kinetic/setup.bash
export LD_LIBRARY_PATH=$MPI_DIR/lib:$LD_LIBRARY_PATH
export LDLIBRARYPATH=$STG/lib
%labels
singularity shell --bind /home/tutuzi/rl-collision-avoidance-master:/home/rl-collision-avoidance-master:rw ./tutu_box/
[tutuzi@localhost ~]$ singularity exec tutu_env.sif python --version
Python 2.7.12
source /opt/ros/kinetic/setup.bash # 需要增加这一代码
# 在三个terminal分别运行如下三条命令
roscore # terminal 1 启动环境
rosrun turtlesim turtlesim_node # terminal 2 出现小乌龟
rosrun turtlesim turtle_teleop_key # terminal 3 方向键可控制小乌龟移动
#include <mpi.h>
#include<stdio.h>
// 测试并行是否成功
int main(int argc, char* argv[])
{
int rank;
int size;
int namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
// 并行环境初始化
MPI_Init(&argc,&argv);
// 获得当前进程
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// 获得运行该程序的总进程
MPI_Comm_size(MPI_COMM_WORLD, &size);
// 获得当前进程下主机的名称
MPI_Get_processor_name(processor_name, &namelen);
printf("hello world from process %i of size %i -- name %s .\n",rank,size,processor_name);
// mpi环境结束
MPI_Finalize();
return 0;
}
[tutuzi@localhost ~]$ singularity exec tutu_env.sif mpicc main.c
[tutuzi@localhost ~]$ singularity exec tutu_env.sif mpiexec -n 3 ./a.out
得到结果如下:hello world from process 0 of size 3 -- name localhost.localdomain .
hello world from process 1 of size 3 -- name localhost.localdomain .
hello world from process 2 of size 3 -- name localhost.localdomain .
进入python, 观察以下import是否报错:
import torch
import torchvision