运行乒乓并行程序时出现致命错误

时间:2013-10-05 07:20:57

标签: parallel-processing mpi fatal-error

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

#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();

}

我可以在没有任何错误或警告的情况下运行我的代码。但是,当我尝试调试它时,它会向我显示这个致命的错误:

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

任何人都知道如何解决这个致命错误?

2 个答案:

答案 0 :(得分:0)

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

mpiexec -n 2 ./pingpong

答案 1 :(得分:0)

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