最近為了驗證MPI并行計算的性能問題,在Vmware虛擬機上安裝了Ubuntu16.04,又在Ubuntu16.04上安裝并測驗了MPI3.2,詳細介紹了MPI3.2原始碼在Ubuntu 16.04下的安裝程序,避免后面的同學少踩坑,
一. MPI壓縮包的下載與解壓
1.在網站上下載MPI3.2原始碼
[鏈接]http://www.mpich.org/static/downloads/3.2/mpich-3.2.tar.gz
2.終端進行解壓縮
一般默認的下載目錄為:/home/用戶名/下載/
先進入該目錄下,然后使用如下命令解壓縮
cd /home/用戶名/下載
sudo tar -zxvf mpich-3.2.tar.gz
二. MPI檔案的配置
1.為了方便操作,可以先在home根目錄下新建一個mpich檔案夾,然后將上述解壓后的mpich-3.2檔案拷貝至mpich檔案夾,
$ cd /home
$ mkdir mpich
$ cp /home/用戶名/下載/mpich-3.2 /home/mpich
2.進入mpich-3.2,通過ls命令進行查看內容,
$ cd /home/mpich/mpich-3.2
$ ls

3.配置configure檔案,
$ ./configure --prefix=~/mpich-3.2 --disable-fortran
此時會報一個配置錯誤:
$ configure:error: expected an absolute directory name for --prefix: ~/mpich-3.2
意思需要一個mpich-3.2的絕對路徑,一般情況下我們配置的fortran檔案不用在mpich的安裝目錄下,因此可以選擇放在新建的mpich目錄下,比如我放在/home/用戶名/mpich下會報錯,而放在/home/mpich下就可正常配置,

三. MPI的編譯和安裝,配置環境變數
1.先回到MPI的解壓目錄下,進行編譯操作,
$ cd /home/mpich/mpich-3.2
$ make
2.等待完成后進行安裝操作,一般需要賦予權限,
$ sudo make install
輸入密碼后即可自動安裝,靜待安裝完成,
3.打開.bashrc組態檔
$ gedit ~/.bashrc
在bashrc檔案開頭或末尾添加mpich執行的路徑,注意:PATH后面是檔案夾位置 注意等號中間別留空格,
export MPI_ROOT=/home/mpich
export PATH=$MPI_ROOT/bin:$PATH
export MANPATH=$MPI_ROOT/man:$MANPATH
然后保存
$ source ~/.bashrc
至此,安裝程序順利結束,
四. MPI運行測驗
1.在mpich檔案夾新建一個mpi_hello.cpp檔案,將下面的代碼粘貼過去,
#include <iostream>
#include <string.h>
#include <mpi.h>
using namespace std;
const int max_string = 100;
int main ()
{
int comm_sz=0;
int my_rank=0;
char greeting[max_string];
MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
MPI_Comm_size(MPI_COMM_WORLD,&comm_sz);
if(my_rank==0){
sprintf(greeting,"Greetings from process %d of %d!",my_rank,comm_sz);
MPI_Send(greeting,strlen(greeting),MPI_CHAR,0,0,MPI_COMM_WORLD);
}
else{
cout<<"Greetings from process "<<my_rank<<" of "<<comm_sz<<"!"<<endl;
for(int i=1;i<comm_sz;i++){
MPI_Recv(greeting,max_string,MPI_CHAR,i,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
cout<<greeting<<endl;
}
}
MPI_Finalize ();
return 0;
}
2.回到終端,在mpich的目錄下進行編譯,
$ mpicxx -g -Wall -o mpi_hello.o mpi_hello.cpp
3.編譯完成后會在該目錄下生成一個mpi_hello.o的檔案,再運行這個檔案,其中-n后面的數字是調節創建的行程數量,
$ mpirun -n 4 ./mpi_hello.o
4.我設定的4個行程,最后出現下圖結果,

至此,完成MPI的安裝和測驗程序,接下來會利用MPI做些并行計算的測驗,
參考文獻:https://blog.csdn.net/qq_30239975/article/details/77703321
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/237561.html
標籤:其他
