MPI_Send(99):无效标签,值为-1

时间:2015-03-24 14:03:08

标签: c mpi

我遇到问题" MPI_Send(99):无效标签,值为-1"当我使用VS2013和MPIEXEC.exe运行以下代码时。逻辑很简单,只是线程0将令牌发送到其他工作线程,然后工作线程会再次将令牌重置为0。

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

int main(int argc, char **argv) {
    int myid;
    int numprocs;
    int token;
    MPI_Status  status;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);

    if (numprocs == 1) {
        printf("The number of processes for this exercise must be greater than 1!\n");
    }
    else if (myid == 0) {
        /* Master process */
        int sdrID;
        int activeWorkers;

        /* set the number of activeWorkers */
        //...
        activeWorkers = 3;
            /* while there are any active workers */
            while (activeWorkers) {
                /*receive a token from any worker*/
                MPI_Recv(&token, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);

                /* obtain the sending process ID from status. */
                //...;
                //;

                /* print the message "The Master has received a token from Worker ...". */
                printf("%d",status.MPI_SOURCE);

                /* reset token to zero and send it back to the same worker */
                token = 0;
                //  //1.消息缓冲区的起始地址,这是一个指针
                //  //2.发送的指定类型的个数
                //  //3.发送数据的MPI数据类型(为了不同机器不同操作系统之间的互操作性)
                //  //4.整形,目的进程号
                //  //5.整型,消息标志(接受者可以根据这个标识来做出if不同的操作)
                MPI_Send(&token, 1, MPI_INT, status.MPI_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD);

                /* reduce the number of activeWorkers */
                activeWorkers = activeWorkers - 1;
            }
    }
    else {
        /* Worker processes */
        /* set token to 1 and send it to the Master.*/
        token = 1;
        MPI_Send(&token, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD);/////////////////

        /* receive token from the Master. */
        MPI_Recv(&token, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);

        /* print a message and return */
        printf("Worker %i has received the token from the Master.\n", myid);
    }

    MPI_Finalize();

    return 0;
}

2 个答案:

答案 0 :(得分:6)

tag中的MPI_Send必须是正整数或零,而MPI_Sendreturn an error (MPI_ERR_TAG) otherwiseMPI_ANY_TAG仅对接收操作有效。

答案 1 :(得分:1)

MPI_ERR_TAG

无效的标记参数。 标签必须是非负面的;

接收中的

标签(MPI_Recv,MPI_Irecv,MPI_Sendrecv等)也可能为MPI_ANY_TAG。最大的标记值可通过属性MPI_TAG_UB获得。

此外,

#define MPI_ANY_TAG            -1   /* match any message tag */