Loading... <div class="tip share">请注意,本文编写于 1118 天前,最后修改于 592 天前,其中某些信息可能已经过时。</div> 需要编写一些`MPI`并行程序,所以需要在`mac`中搭建`MPI`运行环境 ## 下载 1. 下载最新版`open-MPI`,下载地址 [https://www.open-mpi.org/software/ompi/v4.0/](https://www.open-mpi.org/software/ompi/v4.0/) 2. 点击下载的文件`openmpi-4.0.1.tar.gz`解压 3. 打开终端,`cd`到解压的文件夹。 终端中输入 `cd ~/downloads/openmpi-4.0.1` 4. 安装在`/usr/local`下,执行`./configure`。 终端中输入 `./configure --prefix=/usr/local` 5. 编译mpi下的库和文件. 终端中输入 `make all` 1.安装MPI 终端中输入 `sudo make install` 6. 检查是否加入环境变量 终端中输入 `echo $PATH `查看MPI所在的/usr/local/bin是否在环境变量PATH中 (使用此命令查看安装路径,如果出现路径,完成安装。) ## 编译源码并运行 创建源文件 `ex.c` ```c #include <mpi.h> #include <stdio.h> int main(int argc, char** argv) { MPI_Init(NULL, NULL); int rank; int world; MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &world); printf("Hello: rank %d, world: %d\n",rank, world); MPI_Finalize(); } ``` 编译`C`文件,`mpicc ex.c` 编译`C++`文件,`mpicxx ex.cpp` 如: ```bash mpicc ex.c //生成a.out 输出文件 mpirun -np 2 a.out //np选项用来选择几个进程 ,这里是2个进程 ``` 最后修改:2020 年 10 月 11 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 0 如果觉得我的文章对你有用,请随意赞赏