2014-02-26 70 views
0

我試圖在以下網址運行示例代碼。我使用「mpicc twoGroups.c」編譯了程序,並嘗試將其作爲「./a.out」運行,但得到以下消息:必須指定MP_PROCS = 8.終止。如何運行這個已編譯的開放式MPI程序(C)?

我的問題是你如何設置MP_PROCS = 8?

組和通信在這裏https://computing.llnl.gov/tutorials/mpi/

#include "mpi.h" 
#include <stdio.h> 
#define NPROCS 8 

main(int argc, char *argv[]) { 
int  rank, new_rank, sendbuf, recvbuf, numtasks, 
      ranks1[4]={0,1,2,3}, ranks2[4]={4,5,6,7}; 
MPI_Group orig_group, new_group; 
MPI_Comm new_comm; 

MPI_Init(&argc,&argv); 
MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
MPI_Comm_size(MPI_COMM_WORLD, &numtasks); 

if (numtasks != NPROCS) { 
    printf("Must specify MP_PROCS= %d. Terminating.\n",NPROCS); 
    MPI_Finalize(); 
    exit(0); 
    } 

sendbuf = rank; 

/* Extract the original group handle */ 
MPI_Comm_group(MPI_COMM_WORLD, &orig_group); 

/* Divide tasks into two distinct groups based upon rank */ 
if (rank < NPROCS/2) { 
    MPI_Group_incl(orig_group, NPROCS/2, ranks1, &new_group); 
    } 
else { 
    MPI_Group_incl(orig_group, NPROCS/2, ranks2, &new_group); 
    } 

/* Create new new communicator and then perform collective communications */ 
MPI_Comm_create(MPI_COMM_WORLD, new_group, &new_comm); 
MPI_Allreduce(&sendbuf, &recvbuf, 1, MPI_INT, MPI_SUM, new_comm); 

MPI_Group_rank (new_group, &new_rank); 
printf("rank= %d newrank= %d recvbuf= %d\n",rank,new_rank,recvbuf); 

MPI_Finalize(); 
} 
+0

Google返回:[設置MP_PROCS環境變量](http://publib.boulder.ibm.com/infocenter/clresctr/vxrx/index.jsp?topic=%2Fcom.ibm.cluster.pe.v1r3。 pe100.doc%2Fam102_mpp.htm)。 – usr2564301

+0

你有沒有試過'mpirun -n <進程號>/myprog' – mnagel

回答

2

日常的例子當你執行MPI程序,您需要使用合適的包裝。大多數的它看起來像這樣的時候:

mpiexec -n <number_of_processes> <executable_name> <executable_args> 

因此,對於你簡單的例子:

mpiexec -n 8 ./a.out 

你也可以看到用來代替mpiexec-np代替-nmpirun。大部分時間都很好。

如果您剛剛起步,確保使用最新版本的MPI也是一個不錯的主意,這樣您就不會遇到舊的錯誤或奇怪的執行環境。 MPICH和Open MPI是兩種最流行的實現。 MPICH剛剛發佈了3.1版本可用here,而Open MPI版本爲1.7.4可用here。您通常也可以通過您友好的鄰居包管理器獲取他們中的任何一個。

+0

謝謝大家回覆。很有幫助! – TonyGW