ubuntu下安装openMPI

最近需要学习openMPI,为了记录就写在博客里,方便以后看。

openMPI的安装:

1. 可以直接使用apt安装:sudo apt-get intall mpich

也可以先下载再安装。

1、 点击这个下载,我下载的时3.2.1

2、解压:sudo tar -zxvf mpich-3.2.1.tar.gz

3、cd到文件夹下运行./configure -prefix=/home/mpi/mpich  配置目录可以自己选

4、然后分别分析make 和sudo make install

5、运行sudo gedit ~/.zshrc   我的是,如果时bash终端的话,就讲zshrc改成bashrc

6、添加下面这三行:

export MPI_ROOT=/home/mpi/mpich
export PATH=$MPI_ROOT/bin:$PATH
export MANPATH=$MPI_ROOT/man:$MANPATH

然后就可以下个mpi程序测试下是否成功了。

 1 #include <mpi.h>
 2 #include <stdio.h>
 3 int main(int argc, char**argv){
 4     //openMPI的初始化函数
 5     MPI_Init(&argc, &argv);
 6     int world_size, wrank;
 7     //获取容器中进程数
 8     MPI_Comm_size(MPI_COMM_WORLD, &world_size);
 9     //获取当前进程标识
10     MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
11     printf("Hello world. I'm the process %d, we are %d process in total
", wrank, world_size);
12     //openMPI的结束函数
13     MPI_Finalize();
14     return 0;
15 }

然后运行:

mpicxx test.cpp -o test
mpirun -n 4 ./test

mpicxx是编译c++代码,编译c代码是mpicc

mpirun是运行mpi程序-n 4表示创建4个进程

  

原文地址:https://www.cnblogs.com/xingkongyihao/p/9733260.html