MPICH 在 Cluster 上配置 + mxnet分布式命令

#  2018-03-01 ,mxnet distribute training, Ubuntu 16.04 LTS

# 由源码编译安装时根据文档,添加某个标志使其支持分布式训练,

# 本地新建文件,首行为主机 IP,下一行及以后为 从机 IP.

# 命令如下:

export PS_VERBOSE=1


python ../../tools/launch.py -n 2 --launcher mpi -H mpi_file python train_mnist.py --network lenet --kv-store dist_syncINFO:root:Epoch[9] Batch [100] Speed: 155.30 samples/sec

---------------------------------MPICH 在 Cluster 上配置--------------------------------

前言:弃用 openmpi ,在网上很少找到资料,而且它的出错信息对人不友好,选择 mpich,三个通过本机的ThinkPad-T440p(master IP:192.168.1.106)与 VirtualBox 上搭载的Ubuntu 16.04 LTS (node1 IP:192.168.1.109)、远程的Erazer-X310-node(node2 IP:192.168.1.102 ) 构成的集群(Cluster)跑通了程序。总结以下几个过程。

1、安装mpich,通过 apt 安装即可,下载了源码但是没有使用其安装 ,apt 安装的版本是 3.2-6build1

  

sudo apt-get install mpich mpich-doc

2、配置 ssh 免登陆 ,这么做的目的是集群通过ssh通信,避免了频繁的登陆操作 ,首选在每台机器上通过 apt 安装openssh,

   sudo apt-get install openssh-server openssh-client

   在 master 上执行 ssh -v localhost ,输入密码后 ssh 到本地,执行 ssh-keygen -t rsa,在 ~/.ssh 目录下生成 id_rsa 、 id_rsa.pub 两个文件(也可将 rsa 命令替换为 dsa,没试过,是书上附录提供的方法,自己搜索下区别),   将 .pub 文件拷贝到 远程机的 .ssh 目录    下,命令如下(在此之前 ssh 到目标机,证明可以联通再拷贝,以 node1 为例),本机执行 scp ~/.ssh/id_rsa.pub   192.168.1.109:/home/hanxinle/.ssh/ ,然后在ssh 到目标机的终端窗口进入到 node2 的 .ssh 目录,执行 cat id_rsa.pub >> authorized_keys ,生成了      authorized_keys 后 执行 exit 断开 ssh ,再执行 ssh 命令,看看是否省略了输入密码的过程。

3、配置共享文件夹,使得 代理 可以共享 master 的一个目录,将程序放到这个目录中来执行,代理有查看这么文件夹的权限就好(虚拟机这种情况),程序可以运行。

     3.1、虚拟机通过 共享文件夹功能 实现,安装了虚拟机后,务必到上面菜单栏中找到安装增强功能菜单,增强功能会自动安装,待其装好后,找到已经在本地建好的共享文件夹 cloud ,选择它,并且选择 自动挂载和固定挂载,不必每次开启虚拟机就设置一番,进入虚拟机以后执行 sudo mount -t         vboxsf  cloud (空格) /home/hanxinle/cloud/,即可实现挂载,不要等待,应该马上就就可以挂载好的。

     3.2、node2 通过 nfs 执行,在 master 上执行 sudo apt-get install nfs-kernel-server ,ssh 到node2 上,同样执行 sudo apt-get install nfs-kernel-server nfs-common,然后 在 master 上执行 mkdir -p /home/hanxinle/cloud ,然后执行 sudo vim /etc/exports,在最后一行添加:/home/hanxinle/cloud (此           处有空格) *(rw,sync,no_root_squash,no_subtree_check) ,设置了共享文件夹的权限等,然后执行 exportfs - a,然后 sudo service nfs-kernel-server restart, sudo service portmap restart (这两个命令也可以是 sudo /etc/init.d/xxxx restart,xxxx 换成 nfs-kernel-server 、portmap),在node2                      这里安装好了 nfs-common 后在用户目录下建立一个 cloud 文件夹,然后执行 showmount -e 192.168.1.106 看看有无 可挂载的 路径,有的话就挂载 sudo mount -t nfs 192.168.1.106:/home/hanxinle/cloud  /home/haxinle/cloud,挂载完毕,可以进入到这个cloud目录,看看和 master 的文件是                   否一样。

4、 在 master 编辑  /etc/hosts文件, master 的 /etc/hosts 内容是(主机的命名和设备名称是一致的,不然有时候会遇到无法解析、找到 host 的问题,为了简便下次重新装机的过程可以将设备名称直接设置为 master :)

 127.0.0.1 localhost

    # MPI SETUP

   192.168.1.106  ThinkPad-T440p

   192.168.1.109   hanxinle-VirtualBox

   192.168.1.102   Erazer-X310-node

 node1 的 /etc/hosts 内容是

    127.0.0.1 localhost

   # MPI SETUP

   192.168.1.106  ThinkPad-T440p

   192.168.1.109   hanxinle-VirtualBox

node2 的 /etc/hosts的内容是

     127.0.0.1 localhost

     # MPI SETUP

     192.168.1.106  ThinkPad-T440p

     192.168.1.102   Erazer-X310-node

5、运行程序的过程中,可能会遇到 node1、node2 无法 ssh 到master,提示检查防火墙设置,在 master 上执行 sduo ufw disable,关闭防火墙问题解决。

6、 编写测试程序

 1 #include <mpi.h>
 2 #include <stdio.h>
 3 
 4 int main(int argc, char** argv) {
 5   // Initialize the MPI environment. The two arguments to MPI Init are not
 6   // currently used by MPI implementations, but are there in case future
 7   // implementations might need the arguments.
 8   MPI_Init(NULL, NULL);
 9 
10   // Get the number of processes
11   int world_size;
12   MPI_Comm_size(MPI_COMM_WORLD, &world_size);
13 
14   // Get the rank of the process
15   int world_rank;
16   MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
17 
18   // Get the name of the processor
19   char processor_name[MPI_MAX_PROCESSOR_NAME];
20   int name_len;
21   MPI_Get_processor_name(processor_name, &name_len);
22 
23   // Print off a hello world message
24   printf("Hello world from processor %s, rank %d out of %d processors
",
25          processor_name, world_rank, world_size);
26 
27   // Finalize the MPI environment. No more MPI calls can be made after this
28   MPI_Finalize();
29 }

在 master 上保存为 hellompi.c,放到 cloud ,此时 node1、node2 是能在本地cloud 文件看到这个目录里的内容的,编译程序, mpicc hellompi.c -o a,单机执行 mpirun -np 4 ./a ,在master、node1、node2 执行,mpirun -np 10 --hosts ThinkPad-T440p,Erazer-X310-node,hanxinle-VirtualBox ./a  ,

注意,编译程序在所有环境配置无误后再执行程序,除了 用--hosts 制定 master + 代理的形式,还可以 新建立一个 mpi_file 文件,首行 master 的ip ,第二行是 node1 的ip,第三行是node2 的 ip ,执行命令式 mpirun -np 10 --hostfile mpi_file ./a。也就是说 执行程序 依赖 /etc/hosts 或者 本地写满 ip 的 hostfile 文件,且mpi 程序执行过程中必须有主机参与,可以单主机,但不可缺少主机。

------------------------------------------------------------------------------------------------------------------------------

2017.11.20  -- 这段时间尝试 caffe-mpi 和 caffe 等安装,装上了 openmpi,再次编译运行程序提示找不到 libmpi.so.12 ,sudo apt-get remove openmpi-*,重新编译、运行程序,程序回复正常,此外ssh 出现问题,卸载了 openssh-server openssh-client 后安装,将id_rsa.pub 重新拷贝到远程主机,重新制作 验证文件就好了。

参考链接

 1 、mpi tutorials - LAN  (with mpich),http://mpitutorial.com/tutorials/running-an-mpi-cluster-within-a-lan/

 2、 mpi tutorials 的 GitHub , https://github.com/wesleykendall/mpitutorial  

3 、 靠谱的 nfs 文件夹设置方法, blog.csdn.net/zpf336/article/details/50825847

原文地址:https://www.cnblogs.com/hanxinle/p/7688753.html