Centos下搭建MPI集群计算环境

本文记录在三个未配置环境的虚拟机下配置MPI集群环境,虚拟机使用virtualbox,LInux系统使用Centos7.2。

集群设计

三台机器中,一台作为管理结点,负责登录、编译与提交计算任务,另外两台作为计算结点,负责执行并行程序。

IP配置

三个结点的ip配置如下:

结点 IP地址
mu01 192.168.100.10
cu01 192.168.100.11
cu02 192.168.100.12

配置每个结点的IP地址,过程如下:
ip addr查看网卡名
vi /etc/sysconfig/network-scripts/ifcfg-{网卡名} 编辑网卡配置文件,添加或修改下列配置:

1
2
3
4
5
6
BOOTPROTO=static
IPADDR=192.168.100.10/11/12/
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
DNS1=8.8.8.8
ONBOOT=yes

配置后重启网卡生效systemctl restart network

主机名配置

1.设置三个结点主机名分别为mu01、cu01、cu02,使用hostnamectl set-hostname 主机名设置。
2.修改/etc/hosts文件实现主机名与ip地址对应,在每个节点的hosts文件中加入:

192.168.100.10 mu01
192.168.100.11 cu01
192.168.100.12 cu02

配置完成后重启,此时各节点可通过ssh 节点名互访。

MPI环境安装

MPI并不是一种新的开发语言,它是一个定义了可以被C、C++和Fortran程序调用的函数库。它有多种实现,本次使用的是mpich的MPI实现,此外还有openmpi、intelmpi等实现。

在管理节点上安装MPI环境:
1.安装gcc编译器

1
yum install gcc gcc-c++ gcc-gfortran

2.下载安装mpich:

1
2
3
4
5
6
wget http://www.mpich.org/static/downloads/3.3/mpich-3.3.tar.gz
tar -xzvf mpich-3.3.tar.gz
cd mpich-3.3
./configure --prefix=/opt/mpich
make
make install

3.设置环境变量:
编辑/etc/profilevi /etc/profile,添加以下的内容:

1
2
3
PATH=$PATH:/opt/mpich/bin
MANPATH=$MANPATH:/opt/mpich/man
export PATH MANPATH

设置完成后使用which mpicc which mpiexec检测是否安装成功

3.单结点测试
编写测试程序vim mpitest.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <mpi.h>
#include <stdio.h>
int main (int argc, char* argv[])
{
    int rank, size;
    MPI_Init (&argc, &argv); /* starts MPI*/
    MPI_Comm_rank (MPI_COMM_WORLD, &rank); /* get current process id*/
    MPI_Comm_size (MPI_COMM_WORLD, &size); /* get number of processes*/
    printf( "Hello world from process %d of %d\n", rank, size );
    MPI_Finalize();
    return 0;
}

编译运行:
一般情况下,管理结点只进行程序编译与提交任务,不建议在管理结点运行计算程序,下面为测试mpich是否安装成功,使用管理节点单个节点运行MPI程序。

1
2
mpicc mpitest.c -o hello
mpiexec -np 5 ./hello

输出结果如下:

1
2
3
4
5
Hello world from process 4 of 5
Hello world from process 2 of 5
Hello world from process 3 of 5
Hello world from process 0 of 5
Hello world from process 1 of 5

NFS安装

由于MPICH的安装目录和用户可执行程序在并行计算时需要在所有节点存副本,而且目录要相互对应,每次一个节点一个节点地复制非常麻烦,采用NFS文件系统可以实现所有节点内容与管理节点内容同步更新,并自动实现目录的对应。

对于仅包含几个结点的较小的集群系统,可以任意指定其中一个结点作为NFS服务器。对较大的集群系统,最好设定一个或数个结点专门用于文件服务,这些结点称为I/O结点,它们专门负责存储设备的管理,不参加计算。这里选择mu01作为NFS服务器,将它的/home和/opt目录输出给其他三个结点。

1.在所有安装NFS所需软件包并设置为开机启动

1
2
3
4
5
yum install nfs-utils rpcbind
systemctl enable rpcbind.service
systemctl enable nfs-server.service
systemctl start rpcbind.service
systemctl start nfs-server.service

2.在mu01结点配置共享目录
vim /etc/exports
添加

/home    *(async,insecure,rw,no_all_squash,no_root_squash)
/opt    *(async,insecure,rw,no_all_squash,no_root_squash)

刷新NFS设置:exportfs -ra 查看NFS状态:exportfs -v

3.在cu节点挂载NFS
查看可挂载目录:showmount -e mu01

挂载opt与home目录:

1
2
mount -t nfs mu01:/opt /opt
mount -t nfs mu01:/home /home

设置开机自动挂载:
追加下面配置到/etc/fstab

mu01:/home /home nfs defaults,nfsvers=3 0 0
mu01:/opt /opt nfs defaults,nfsvers=3 0 0

df -h查看是否挂载成功
如不成功尝试在所有结点关闭防火墙(测试环境)

1
2
systemctl disable firewalld
systemctl stop firewalld

NIS安装

集群中的每台主机都需要设计相同的帐号密码是非常麻烦的,因此可通过一个NIS主控制服务器管理集群中所有帐号密码,当其他主机有登录需求时再向主控制服务器请求相关账户信息。以mu01结点当作NIS主控制结点为服务端,cu结点为客户端。

1.安装相关软件
服务端:yum install ypserv rpcbind
客户端:yum install ypbind

2.NIS服务端配置

  • 设定NIS的网域名称
    编辑/etc/sysconfig/network加入配置NISDOMAIN=mu

  • 设置主要配置文件 编辑NIS配置文件/etc/ypserv.conf,允许特定的主机访问NIS服务器,在查询权限部分加入:

127.0.0.0/255.255.255.0     : * : * : none
192.168.100.0/255.255.255.0 : * : * : none
*                           : * : * : deny
  • 启动NIS与设置开机启动
1
2
systemctl start ypserv
systemctl enable ypserv

使用rpcinfo -u localhost ypserv检测是否启动成功

  • 建立用户资料库
    执行命令/usr/lib64/yp/ypinit -m建立数据库,将nis服务器端的用户信息导入,当用户信息更新时,也需重新执行该命令更新数据库。
    建立完数据库后,需要将服务重启systemctl restart ypserv

3.NIS客户端配置

  • 设置NIS网域名称与服务端操作相同

  • 修改用户密码的认证顺序 vim /etc/nsswitch.conf 修改以下配置:

passwd:     files nis
shadow:     files nis
group:      files nis
hosts:      files nis dns
  • 修改客户端配置文件
    配置/etc/yp.conf,添加domain mu server mu01

  • 修改系统认证文件
    vim /etc/sysconfig/authconfig
    修改USENIS=yes
    vim /etc/pam.d/system-auth
    修改password sufficient pam_unix.so sha512 shadow nis nullok try_first_pass use_authtok

  • 启动服务并设置为开机启动

1
2
systemctl start ypbind
systemctl enable ypbind

在NIS client 输入yptest测试是否配置成功。

配置SSH免密码登录

MPI并行程序运行时需要设计多机互访免密码。

1
2
3
4
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 700 .ssh
chmod 600 .ssh/authorized_keys

多机运行MPI程序

在cu01,cu02上运行MPI程序

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <mpi.h>
#include <stdio.h>
#include <math.h>
int main(argc,argv)int argc;char *argv[];
{
    int myid,numprocs;
    int namelen;
    char processor_name[MPI_MAX_PROCESSOR_NAME];

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    MPI_Get_processor_name(processor_name,&namelen);
    fprintf(stderr,"Hello World!process %d of %d on  %s\n",myid,numprocs,processor_name);
    MPI_Finalize();
    return 0;
}

mpiexec -hosts cu01,cu02 -np 6 ./hello
执行结果如下:

1
2
3
4
5
6
Hello World!process 0 of 6 on  cu01
Hello World!process 2 of 6 on  cu01
Hello World!process 5 of 6 on  cu02
Hello World!process 4 of 6 on  cu01
Hello World!process 3 of 6 on  cu02
Hello World!process 1 of 6 on  cu02

说明程序已在多机上运行。

使用machinefile

创建machinefile,其内容如下:

cu01:1
cu02:1

指定machinefile运行程序:
mpiexec -n 6 -machinefile ./machinefile ./hello
与上面指定hosts的输出相同,至此MPI环境配置完成。