0

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

#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

有人知道如何解决这个致命错误吗?

4

2 回答 2

0

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

mpiexec -n 2 ./pingpong
于 2013-10-07T16:13:22.417 回答
0

你还没有申报价值x?的价值是x多少?使用 2 个进程运行它。

于 2014-03-11T17:21:51.937 回答