一、环境说明
宿主机:Windows 10 Pro
三台虚拟机:VMware + CentOS 7
二、 虚拟机网络配置实现多节点互访
硬件资源有限,所以我只开了三台虚拟机均为 Centos 7。
一台作为控制节点,另外两台作为计算节点
1.修改网卡配置文件
/etc/sysconfig/network-scripts/ifcfg-eth0 将其中内容修改如下:
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.27.10
NETMASK=255.255.255.0
将每一台虚拟机都如此配置,IPADDR是ip地址,不要重复
2.修改网关及主机名
/etc/sysconfig/network
ETWORKING=yes
HOSTNAME=node1
GATEWAY=192.168.27.1
一些文章中提到还需要修改VMware的设置(关闭本地DHCP服务),实际上,Centos 中设置好后就不需要动VMware的设置了
3、关闭防火墙
为了保证mpi运行成功,且尽可能降低通讯延迟和系统开销,请关闭防火墙以达到最高的效率。
关闭firewalld
systemctl stop firewalldsystemctl disable firewalld
关闭selinux
1.暂时关闭selinux
setenforce 0
2.永久关闭selinux
编辑selinux的配置文件,把SELINUX设置成disabled,然后重启生效
[root@linuxidc share]#vi /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
三、 在虚拟机中添加同名用户
在不同节点中需要有同名的用户以方便免密码SSH连接
node1和node2中以root用户执行下列命令
# useradd mpiuser
四、 实现免密码SSH登陆
当前节点为node1,用户为mpiuser,工作目录为 ~ 家目录
1.生成 SSH 私钥对
$ ssh-keygen -t rsa 一路回车就好
2.进入 .ssh 目录
$ cd ./.ssh
3.生成authorized_keys文件
$ cp id_rsa.pub authorized_keys
4.回到mpiuser的家目录
$ cd ~
5.建立本身的信任连接
$ ssh node1
6.设置 node2
进入node2的mpiuser用户的用户目录~
$ ssh-keygen -t rsa 生成.ssh文件夹
$ scp node1:~/.ssh/* ./ 拷贝node1上的.ssh文件夹到node2
# scp node1:/etc/hosts /etc/hosts 拷贝node1上的hosts文件到node2,可能需要root权限
$ ssh node2
$ ssh node1
7.如果还有其他节点,设置方法同node2
设置成功后,在任意节点执行SSH连接其他节点都不需要密码,注意必需每个节点都有mpiuser这个用户,之所以使用一个普通用户,是为了避免root执行mpi程序时遇到风险。在一些mpi实现中,也不允许以root用户执行。
五、 配置MPI运行环境
1. 下载 mpich
官方网站 http://www.mpich.org 下载其中的mpich-3.2.tar.gz
2. 解压并进入目录
# tar xvf mpich-3.2.tar.gz
# cd mpich-3.2
# ./configure –prefix=/usr/local/mpich
# make
# make install
3. 设置环境变量,修改 /etc/profile ,加入下面几行代码
PATH=$PATH:/usr/local/mpich/bin
MANPATH=$MANPATH:/usr/local/mpich/man
export PATH MANPATH
4. 使刚刚的修改生效
# source /etc/profile
5. mpi多节点配置
新建一个配置文件
# touch /usr/local/mpich/servers
修改其中内容为:
node1:2 #在node1上运行两个进程
node2:2
如果有多个节点继续往下写
六、 单节点测试
将源码包中的 examples 拷贝到 mpich 的安装目录
# cp -r ./examples /usr/local/mpich/
用示例程序进行测试
$ mpirun -np 4 /usr/local/mpich/examples/cpi
Process 1 of 4 is on node1
Process 0 of 4 is on node1
Process 3 of 4 is on node1
Process 2 of 4 is on node1
pi is approximately 3.1415926544231239, Error is 0.0000000008333307
wall clock time = 0.081049
得到如图的回显结果证明mpich的运行环境正常
七、 多节点测试
$ mpiexec -np 4 -f /usr/local/mpich/servers /usr/local/mpich/examples/cpi
Process 1 of 4 is on node1
Process 0 of 4 is on node1
Process 3 of 4 is on node2
Process 2 of 4 is on node2
pi is approximately 3.1415926544231239, Error is 0.0000000008333307
wall clock time = 0.139722
如上,在两个节点上分别运行了两个进程,配置过程到此结束
1、集群机器上面需要配置ssh登录权限。参考:Hadoop-0.21.0在linux分布式集群配置
2、复制编译程序到其他机器上面(建议最好把mpich安装在nfs盘里面,这样lib库文件就能直接共享,否则即便复制了编译程序,依然会出现依赖问题)
scp -r mpich server140:/usr/local/
scp -r mpich server151:/usr/local/
scp -r mpich server130:/usr/local/
scp -r mpich server143:/usr/local/
同时在每台机器上面相应加入环境变量中。
3、
在/usr/local/mpich 下新建servers文件,内容如下:
server150:2 #run 2 process
server140:2
server130:2
server143:2
server151:2
执行下面命令,并指定servers文件
mpiexec -n 10 -f servers ./examples/cpi
输出
Process 0 of 10 is on server150
Process 1 of 10 is on server150
Process 4 of 10 is on server140
Process 5 of 10 is on server140
Process 6 of 10 is on server143
Process 7 of 10 is on server143
Process 8 of 10 is on server130
Process 9 of 10 is on server130
Process 2 of 10 is on server151
Process 3 of 10 is on server151
pi is approximately 3.1415926544231256, Error is 0.0000000008333325
wall clock time = 0.018768
0 条评论