Linux上搭建並行計算MPI環境

最近因爲並行計算課程的需求,需要搭建一個多機的MPI運行環境,故將搭建過程分享在這篇博文裏。

具體的系統是Ubuntu 18.04,在四臺機器上搭建環境。

記四臺機器爲A,B,C,D,四臺機器的IP爲IP-A,IP-B,IP-C,IP-D,還有一個要求就是在四臺機器上需要同一個用戶名,以xiaomin爲例。

接下來就是正式開始搭建了:

Step1: 創建用戶

用ssh以root用戶登錄四臺機器,創建用戶xiaomin,這裏以A機器爲例。

$ ssh root@IP-A #enter root password
$ adduser xiaomin #enter your password 
$ usermod -aG sudo xiaomin #add zhangsan to sudo user group

Step2: 安裝MPI庫

用ssh以用戶xiaomin登錄四臺機器,然後在xiaomin用戶下安裝MPI庫,這裏以A機器爲例。

注意:這裏如果要有多個用戶需要使用MPI庫,那可以直接在root用戶下安裝MPI庫,從而避免重複安裝。

$ ssh xiaomin@IP-A #enter xiaomin password
$ sudo apt update
$ sudo apt install mpich

Step3: 配置免密登錄

用ssh以用戶xiaomin登錄四臺機器,然後在xiaomin用戶下配置到其他機器的免密登錄,這裏以A機器爲例。

# 生成本地密鑰
$ ssh xiaomin@IP-A #enter xiaomin password
$ ssh-keygen -t rsa -b 4096 # generate ssh key
$ ssh-add ~/.ssh/id_rsa # add key to ssh-agent

# 複製公鑰到四臺機器上
$ ssh-copy-id -i ~/.ssh/id_rsa.pub xiaomin@IP-A #add pub key of xiaomin to the A
$ ssh-copy-id -i ~/.ssh/id_rsa.pub xiaomin@IP-B #add pub key of xiaomin to the B
$ ssh-copy-id -i ~/.ssh/id_rsa.pub xiaomin@IP-C #add pub key of xiaomin to the C
$ ssh-copy-id -i ~/.ssh/id_rsa.pub xiaomin@IP-D #add pub key of xiaomin to the D

Step4: 測試程序:Hello World

我們將機器A作爲客戶端,在集羣上發起計算任務。

$ ssh xiaomin@IP-A #enter xiaomin password
$ mkdir /home/xiaomin/hello

創建示例文件mpi_hello_world.c爲:

#include <mpi.h> #include <stdio.h>

int main(int argc, char** argv) {

// Initialize the MPI environment. The two arguments to MPI Init are not 
// currently used by MPI implementations, but are there in case future 
// implementations might need the arguments.

MPI_Init(NULL, NULL);

// Get the number of processes 
int world_size; 
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

// Get the rank of the process 
int world_rank; 
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

// Get the name of the processor 
char processor_name[MPI_MAX_PROCESSOR_NAME]; 
int name_len; 
MPI_Get_processor_name(processor_name, &name_len);

// Print off a hello world message 
printf("Hello world from processor %s, rank %d out of %d processors\n", processor_name, world_rank, world_size);

// Finalize the MPI environment. No more MPI calls can be made after this

MPI_Finalize(); 
}

創建makefile文件爲:

EXECS=mpi_hello_world MPICC?=mpicc

all: ${EXECS}

mpi_hello_world: mpi_hello_world.c ${MPICC} -o mpi_hello_world mpi_hello_world.c

clean: rm -f ${EXECS}

將mpi_hello_world.c和makefile放置在同一個文件夾/home/xiaomin/hello下,然後就可以編譯示例程序。

$ cd /home/zhangsan/hello 
$ make # compile

然後在同一個文件夾下創建一個config文件。

IP-A:2
IP-B:2
IP-C:2
IP-D:2

然後需要將hello文件夾複製到B,C,D三個機器上。

$ scp -r /home/xiaomin/hello xiaomin@IP-B:/home/xiaomin/
$ scp -r /home/xiaomin/hello xiaomin@IP-C:/home/xiaomin/
$ scp -r /home/xiaomin/hello xiaomin@IP-D:/home/xiaomin/

然後就可以運行程序了。

$ mpiexec -n 8 -f /home/xiaomin/hello/config /home/xiaomin/hello/mpi_hello_world

如果沒有其他問題就可以得到如下結果:

Step5: 可能遇到問題

1. Unable to get host address

這個的原因很可能就是DNS服務器出現了問題,導致其他機器找不到機器A。

解決方案就是在B,C,D三臺服務器上加上機器A的IP地址。

首先在機器A上查看當前機器的名稱,

$ vim /etc/hosts

看到最後一行機器A的名稱爲ecs-pa,那就在B,C,D上利用 $ vim /etc/hosts 命令,然後加上一行 IP-A ecs-pa。

好的,大功告成,這是筆者搭建MPI環境中遇到的問題。

如果還有其他的問題,可以參考鏈接:https://www.slothparadise.com/running-mpi-common-mpi-troubleshooting-problems/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章