Parallel processing 运行乒乓并行程序时发生致命错误

Parallel processing 运行乒乓并行程序时发生致命错误,parallel-processing,mpi,fatal-error,Parallel Processing,Mpi,Fatal Error,我编写了运行乒乓并行程序的代码。以下是我的代码: #include <mpi.h> #include <stdlib.h> #include <stdio.h> int main (int argc, char **argv){ //t0 Start Time //t1 End Time int size,rank,msgtag = 1; double t0,t1,tmaster,tslave ; MPI_Statu

我编写了运行乒乓并行程序的代码。以下是我的代码:

#include <mpi.h>
#include <stdlib.h>
#include <stdio.h>

int main (int argc, char **argv){
    //t0 Start Time
    //t1 End Time
    int size,rank,msgtag = 1;
    double t0,t1,tmaster,tslave ;

    MPI_Status status;

    //initialize 
    int x;

    //initialize MPI
    if (MPI_Init(&argc, &argv) != MPI_SUCCESS) {
        fprintf(stderr, "MPI initialization error\n");
        return EXIT_FAILURE;
    }
    MPI_Comm_size(MPI_COMM_WORLD,&size);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);

    //communication between 2 nodes
    ///action process 0
    if(rank == 0){
        //start timer
        //master process
        t0 = MPI_Wtime();
        MPI_Send(&x,1,MPI_INT,1,msgtag,MPI_COMM_WORLD);

        //stop timer
        t1 = MPI_Wtime();
        //calculate elapsed time
        tmaster = (t1 - t0);
        MPI_Recv(&tslave,1,MPI_DOUBLE,1,msgtag,MPI_COMM_WORLD,&status);


        printf("Master time: %g \n\n",tmaster);
        printf("slave time: %g \n\n",tslave);

    }else{
    ///action process 1
        //receive message
        t0 = MPI_Wtime();
        MPI_Recv(&x,1,MPI_INT,0,msgtag,MPI_COMM_WORLD,&status);

        t1 = MPI_Wtime();
        tslave = (t1 - t0);
        //Send message
        MPI_Send(&tslave,1,MPI_DOUBLE,0,msgtag,MPI_COMM_WORLD);
    }
    MPI_Finalize();

}
#包括
#包括
#包括
int main(int argc,字符**argv){
//t0开始时间
//t1结束时间
整数大小,秩,msgtag=1;
双t0,t1,tmaster,tslave;
MPI_状态;
//初始化
int x;
//初始化MPI
if(MPI_Init(&argc,&argv)!=MPI_成功){
fprintf(stderr,“MPI初始化错误\n”);
返回退出失败;
}
MPI_通信大小(MPI_通信世界和大小);
MPI通信等级(MPI通信世界和等级);
//2个节点之间的通信
///行动进程0
如果(秩==0){
//启动计时器
//主进程
t0=MPI_Wtime();
MPI_发送(&x,1,MPI_INT,1,msgtag,MPI_通信世界);
//停止计时器
t1=MPI_Wtime();
//计算经过的时间
tmaster=(t1-t0);
MPI_Recv(和tslave、1、MPI_DOUBLE、1、msgtag、MPI_COMM_WORLD和status);
printf(“主时间:%g\n\n”,tmaster);
printf(“从属时间:%g\n\n”,tslave);
}否则{
///行动进程1
//接收消息
t0=MPI_Wtime();
MPI_Recv(x、1、MPI_INT、0、msgtag、MPI_通信世界和状态);
t1=MPI_Wtime();
tslave=(t1-t0);
//发送消息
MPI_发送(&tslave,1,MPI_双精度,0,msgtag,MPI_通信世界);
}
MPI_Finalize();
}
我可以在没有任何错误或警告的情况下运行代码。但是,当我尝试调试它时,它会显示以下致命错误:

job aborted:
rank:node:exit node:message:
0:localhost:-101:Fatal error in MPI_Send:invalid rank,error stack:
MPI_Send<172>:MPI_Send<buf=0x003FFBB4, count=1, MPI_INNT,dest=1, tag=1,MPI_COMM_WORLD> failed
MPI_Send<97>.; invalid rank has value 1 but must be non negative and less then 1
作业已中止:
等级:节点:退出节点:消息:
0:localhost:-101:MPI\U发送中出现致命错误:无效列组,错误堆栈:
MPI_发送:MPI_发送失败
MPI_发送。;无效列组的值为1,但必须为非负且小于1

有人知道如何修复此致命错误吗?

看起来您没有正确运行代码(只启动了一个进程)。确保您的
mpiexec
调用为
-n
标志传递的值大于0。例如:

mpiexec -n 2 ./pingpong

您尚未声明
x的值
x
的值是多少?
使用2个进程运行它。

您可能正在调试可执行文件的单个实例-然后它作为所谓的“singleton MPI”实例运行,MPI宇宙中只有一个进程,并且它的级别为0,因此发送到级别1或从级别1接收是错误的。有专门的并行调试器,如TotalView和DDT,它们能够生成一个并行作业,然后作为单个程序单独或集体调试每个进程。