1.官网下载
根据自己的需要下载相应的版本,当前最新 v4.1
2.解压OpenMPI
tar zxvf openmpi-xx.tar
3.进入MPI目录
cd openmpi-xx
4.安装在/usr/local下
./configure --prefix=/usr/local
5.编译mpi下的库和文件
make all
6.安装MPI
sudo make install
7.检查path是否存在
echo $PATH
有,显示结果:
如果路径中没有/usr/local/bin,则需要自己加进去
用root权限执行
vim /etc/profile
在文件末尾添上以下两行:
PATH=$PATH:/usr/local/bin
export PATH
:wq. 保存退出
8.编写简单的helloworld.c文件
vim helloworld.c
#include <stdio.h>
#include <mpi.h>
int main (int argc, char **argv)
{
int rank, size;
MPI_Init (&argc, &argv); /* starts MPI */
MPI_Comm_rank (MPI_COMM_WORLD, &rank); /* get current process id */
MPI_Comm_size (MPI_COMM_WORLD, &size); /* get number of processes */
printf( "Hello world from process %d of %d\n", rank, size );
MPI_Finalize();
return 0;
}
9.编译文件
mpicc helloworld.c //生成a.out 输出文件
mpirun -np 4 a.out //np选项用来选择几个进程 ,这里是4个进程
结果