MPI學習之路(二):程式編譯
阿新 • • 發佈:2018-12-12
[email protected]:~/wmw$ mpicc -o try_mpi1 try_mpi1.c
[email protected]:~/wmw$ mpiexec -n 1 ./try_mpi1
Hello World! Process 0 of 1 on themingyi
[email protected]:~/wmw$ mpiexec -n 2 ./try_mpi1
Hello World! Process 0 of 2 on themingyi
Hello World! Process 1 of 2 on themingyi
(1)問題
自己嘗試寫了一個.c的程式,然後使用執行命令直接執行:
mpirun -n 1 ./try_mpi1.c
出現了一下的錯誤提示:
./try_mpi1.c: 4: ./try_mpi1.c: Syntax error: "(" unexpected
(2)問題定位
網上查詢內容發現此問題類似與下面網址內的問題相似:
linux 執行一個可執行檔案,出現 line 1: syntax error: unexpected "(" 的錯誤
在mpi編譯時使用的mpicc來編譯;
1、To compile a single file foo.c, use
mpicc -c foo.c
2、To link the output and make an executable, use
mpicc -o foo foo.c
is a convenient way to build simple programs.
(3)解決問題
在嘗試中發現,直接使用命令:
mpicc -c foo.c
行不通,使用第二個命令編譯之後再執行就可以了,具體流程如下:
[email protected]:~/wmw$ mpicc -o try_mpi1 try_mpi1.c
[email protected]:~/wmw$ mpiexec -n 1 ./try_mpi1
Hello World! Process 0 of 1 on themingyi
[email protected] :~/wmw$ mpiexec -n 2 ./try_mpi1
Hello World! Process 0 of 2 on themingyi
Hello World! Process 1 of 2 on themingyi
測試程式:
#include"mpi.h"↩
#include<stdio.h>↩
#include<math.h>↩
void main(argc,argv)↩
int argc;↩
char *argv[];↩
{↩
int myid,numprocs;↩
int namelen;↩
char processor_name[MPI_MAX_PROCESSOR_NAME];↩
MPI_Init(&argc,&argv);↩
MPI_Comm_rank(MPI_COMM_WORLD,&myid);↩
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);↩
MPI_Get_processor_name(processor_name,&namelen);↩
↩
fprintf(stderr,"Hello World! Process %d of %d on %s\n",myid,nu mprocs,processor_name);↩
MPI_Finalize();↩
}↩
(4)參考