MPI分布式编程 --1.OpenMPI安装和基本使用

匿名 (未验证) 提交于 2019-12-02 23:32:01
版权声明:林微原创,未经允许不得转载。 https://blog.csdn.net/Canhui_WANG/article/details/90214990

1. OpenMPI安装 (自动编译配置)

第一种方法:采用第三方源进行sudo apt-get install安装。

$ sudo apt-get install openmpi-bin 

查询版本信息。

$ mpirun --version mpirun (Open MPI) 1.10.2 $ mpiexec --version mpiexec (OpenRTE) 1.10.2 


2. OpenMPI安装 (手动编译配置)

第二种方法:采用官方软件包安装。首先,下载官方openmpiv4.0.1安装包

$ cd /home/joe/App/Openmpi $ wget https://download.open-mpi.org/release/open-mpi/v4.0/openmpi-4.0.1.tar.gz $ ls openmpi-4.0.1.tar.gz 

解压。

$ tar -xzvf openmpi-4.0.1.tar.gz 

编译和安装。

$ cd /home/joe/App/Openmpi/openmpi-4.0.1 $ ./configure --prefix=$HOME/App/Openmpi $ make all $ make install  

删除压缩包及其解压缩文件夹。

$ cd /home/joe/App/Openmpi $ rm $HOME/App/Openmpi/openmpi-4.0.1.tar.gz $ rm -r $HOME/App/Openmpi/openmpi-4.0.1 

系统环境变量配置。

$ echo "export PATH=\$PATH:\$HOME/App/Openmpi/bin" >> $HOME/.bashrc $ echo "export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:\$HOME/App/Openmpi/lib" >> $HOME/.bashrc 

查询版本。

$ /home/joe/App/Openmpi/bin $ ./mpiexec --version mpiexec (OpenRTE) 4.0.1 $ ./mpirun --version mpirun (Open MPI) 4.0.1 


3. 测试

下面实现一个MPI多进程并行编程的"Hello World"。

新建hello.c文件,写入MPI代码。

#include <mpi.h> #include <stdio.h>  int main(int argc, char** argv) {     // Initialize the MPI environment     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.     MPI_Finalize(); } 

编译hello.c生成hello可执行文件。

$ mpicc hello.c -o hello 

通过mpirun初始化多进程参数去执行hello文件。可以看到:多进程并行编程状态下,进程之间同时开始,乱序结束。

$ mpirun -np 1 ./hello Hello world from processor ubuntu00, rank 0 out of 1 processors $ mpirun -np 2 ./hello Hello world from processor ubuntu00, rank 1 out of 2 processors Hello world from processor ubuntu00, rank 0 out of 2 processors $ mpirun -np 3 ./hello Hello world from processor ubuntu00, rank 2 out of 3 processors Hello world from processor ubuntu00, rank 0 out of 3 processors Hello world from processor ubuntu00, rank 1 out of 3 processors $ mpirun -np 4 ./hello Hello world from processor ubuntu00, rank 1 out of 4 processors Hello world from processor ubuntu00, rank 2 out of 4 processors Hello world from processor ubuntu00, rank 3 out of 4 processors Hello world from processor ubuntu00, rank 0 out of 4 processors $ mpirun -np 5 ./hello Hello world from processor ubuntu00, rank 3 out of 5 processors Hello world from processor ubuntu00, rank 4 out of 5 processors Hello world from processor ubuntu00, rank 0 out of 5 processors Hello world from processor ubuntu00, rank 1 out of 5 processors Hello world from processor ubuntu00, rank 2 out of 5 processors 

Open MPI的"Hello World"执行完毕。



参考文献

[1. Open MPI官方文档] https://www.open-mpi.org/software/ompi/v4.0/
[2. Open MPI v4.0.1安装包] https://download.open-mpi.org/release/open-mpi/v4.0/openmpi-4.0.1.tar.gz
[3. Open MPI安装教程] http://edu.itp.phys.ethz.ch/hs12/programming_techniques/openmpi.pdf
[4. Open MPI编程之"Hello World"] https://mpitutorial.com/tutorials/mpi-hello-world/
[5. Open MPI编程官方文档] https://www.open-mpi.org/doc/

文章来源: https://blog.csdn.net/Canhui_WANG/article/details/90214990
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!