安装与运行open mpi

  • open mpi官网地址:https://www.open-mpi.org/
  • open mpi文档地址:https://docs.open-mpi.org/
  • 微软也实现了MPI,叫Microsoft MPI,文档非常全面可以参考

1.  apt-get安装

首先安装MPI。在Ubuntu 22.04中,可以使用以下命令安装OpenMPI:

sudo apt-get update
sudo apt-get install openmpi-bin openmpi-common libopenmpi-dev

在终端中输入以下命令,创建一个名为hello的示例程序:

cd ~
mkdir test_mpi
cd test_mpi
nano hello.c

这里使用nano文本编辑器创建一个C语言文件hello.c,并将以下代码粘贴到文件中:

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

int main(int argc, char** argv) {
    MPI_Init(NULL, NULL);
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);
    printf("Hello from rank %d of %d\n", world_rank, world_size);
    MPI_Finalize();
}

这是一个简单的MPI程序,它在每个MPI进程中打印“Hello from rank x of y”信息,其中x是进程的排名,y是进程的总数。

保存并退出文件编辑器,然后编译示例程序:

mpicc -o hello hello.c
mpirun -np 4 ./hello

这将启动4个MPI进程,并在每个进程中运行示例程序。如果MPI正确安装并可以正常运行,则在终端中应该看到类似以下的输出:

Hello from rank 0 of 4
Hello from rank 1 of 4
Hello from rank 2 of 4
Hello from rank 3 of 4

如果MPI没有正确安装或配置,则可能会看到错误消息,例如无法启动进程或无法连接到进程。

2.源码安装

2.1.下载

wget https://download.open-mpi.org/release/open-mpi/v4.1/openmpi-4.1.5.tar.gz
tar -xzvf openmpi-4.1.5.tar.gz

2.2.安装

cd openmpi-4.1.5
mkdir build
cd build

../configure --prefix=/usr/local/openmpi
make
make install

2.3.配置环境变量

MPI_HOME=/usr/local/openmpi
export PATH=${MPI_HOME}/bin:$PATH
export LD_LIBRARY_PATH=${MPI_HOME}/lib:$LD_LIBRARY_PATH
export MANPATH=${MPI_HOME}/share/man:$MANPATH

2.4.运行例子

cd ../examples
make
mpirun --allow-run-as-root -np 4 hello_c
# 如果不是root用户
# mpirun -np 4 hello_c

输出:

Hello, world, I am 2 of 4, (Open MPI v4.1.5, package: Open MPI root@k8s-node05 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 0 of 4, (Open MPI v4.1.5, package: Open MPI root@k8s-node05 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 1 of 4, (Open MPI v4.1.5, package: Open MPI root@k8s-node05 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 3 of 4, (Open MPI v4.1.5, package: Open MPI root@k8s-node05 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)

 3. 集群环境运行

下面命令在k8s-node01,k8s-node02,k8s-node03三个节点上分别会启动一个MPI进程

  • k8s-node01,k8s-node02,k8s-node03三个节点要设置ssh免密登录
  • 三个节点都有/root/mpi/openmpi-4.1.5/examples/hello_c程序,可通过scp命令复制
  • 要使用`which mpirun`全路径,使用mpirun会报错说找不到命令
# `which mpirun` --allow-run-as-root -host k8s-node01,k8s-node02,k8s-node03 /root/mpi/openmpi-4.1.5/examples/hello_c
Hello, world, I am 2 of 3, (Open MPI v4.1.5, package: Open MPI root@k8s-node03 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 1 of 3, (Open MPI v4.1.5, package: Open MPI root@k8s-node02 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 0 of 3, (Open MPI v4.1.5, package: Open MPI root@k8s-node01 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)

还可以使用配置文件来设置集群的节点

# `which mpirun` --allow-run-as-root -np 12  --hostfile hosts /root/mpi/openmpi-4.1.5/examples/hello_c
Hello, world, I am 1 of 12, (Open MPI v4.1.5, package: Open MPI root@k8s-master Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 0 of 12, (Open MPI v4.1.5, package: Open MPI root@k8s-master Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 8 of 12, (Open MPI v4.1.5, package: Open MPI root@k8s-node04 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 9 of 12, (Open MPI v4.1.5, package: Open MPI root@k8s-node04 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 11 of 12, (Open MPI v4.1.5, package: Open MPI root@k8s-node05 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 10 of 12, (Open MPI v4.1.5, package: Open MPI root@k8s-node05 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 6 of 12, (Open MPI v4.1.5, package: Open MPI root@k8s-node03 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 7 of 12, (Open MPI v4.1.5, package: Open MPI root@k8s-node03 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 4 of 12, (Open MPI v4.1.5, package: Open MPI root@k8s-node02 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 2 of 12, (Open MPI v4.1.5, package: Open MPI root@k8s-node01 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 5 of 12, (Open MPI v4.1.5, package: Open MPI root@k8s-node02 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)
Hello, world, I am 3 of 12, (Open MPI v4.1.5, package: Open MPI root@k8s-node01 Distribution, ident: 4.1.5, repo rev: v4.1.5, Feb 23, 2023, 110)

其中hosts文件的内容是

# cat hosts
k8s-master slots=2
k8s-node01 slots=2
k8s-node02 slots=2
k8s-node03 slots=2
k8s-node04 slots=2
k8s-node05 slots=2

 

posted on 2023-03-21 21:36  Netsharp  阅读(2612)  评论(0编辑  收藏  举报

导航