MPI在C

时间:2018-04-09 19:49:35

标签: c mpi

我需要创建一个使用MPI非阻塞发送和接收调用与合作伙伴任务通信的程序。合作伙伴任务是任务等级+ numberOfTask / 2,反之亦然。发送给合作伙伴的邮件是发件人排名。

我试过这样做:

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#define  MASTER     0

int main (int argc, char *argv[]) {
    int  i, numtasks, taskid, len, partner, message;
    char hostname[MPI_MAX_PROCESSOR_NAME];

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

    MPI_Request reqsTx[numtasks], reqsRx[numtasks];
    MPI_Status stats[numtasks];

    /* need an even number of tasks  */
    if (numtasks % 2 != 0) {
        if (taskid == MASTER) 
            printf("Quitting. Need an even number of tasks: numtasks=%d\n", numtasks);
    } else {
        if (taskid == MASTER) 
            printf("MASTER: Number of MPI tasks is: %d\n", numtasks);

        MPI_Get_processor_name(hostname, &len);
        printf ("Hello from task %d\n", taskid);

        /* determine partner and then send to partner */
        if (taskid < numtasks/2) {
            partner = numtasks/2 + taskid;
            MPI_Isend(&taskid, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqsTx[taskid]);
        } else if (taskid >= numtasks/2) {
            partner = taskid - numtasks/2;
            MPI_Isend(&taskid, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqsTx[taskid]);
        }

        printf ("Send %d completed\n", taskid);
        MPI_Waitall(numtasks, reqsTx, stats);
        printf("Done waiting for transmit!");

        /* determine partner and then receive from partner */
        if (taskid < numtasks/2) {
            MPI_Irecv(&message, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqsRx[taskid]);
        } else if (taskid >= numtasks/2) {
            MPI_Irecv(&message, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqsRx[taskid]);
        }

        printf ("Receive %d completed\n", taskid);
        MPI_Waitall(numtasks, reqsRx, stats);
        printf("Done waiting for receive!");

        /* print partner info and exit*/
        printf("Task %d is partner with %d\n", taskid, message);
    }
    MPI_Finalize();
}

但是第一个MPI_Waitall(numtasks,reqsTx,stats);失败,程序终止。谁知道我在这里做错了什么?

祝你好运

1 个答案:

答案 0 :(得分:1)

我想我发现了这个问题。问题似乎是我试图创建比需要更多的请求。

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#define  MASTER     0

int main (int argc, char *argv[]) {
    int  i, numtasks, taskid, len, partner, message;
    char hostname[MPI_MAX_PROCESSOR_NAME];

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

    MPI_Status stats[2];
    MPI_Request reqs[2];

    /* need an even number of tasks  */
    if (numtasks % 2 != 0) {
        if (taskid == MASTER) 
            printf("Quitting. Need an even number of tasks: numtasks=%d\n", numtasks);
    } else {
        if (taskid == MASTER) 
            printf("MASTER: Number of MPI tasks is: %d\n", numtasks);

        MPI_Get_processor_name(hostname, &len);
        printf ("Hello from task %d\n", taskid);

        if (taskid < numtasks/2) 
            partner = numtasks/2 + taskid;
        else if (taskid >= numtasks/2) 
            partner = taskid - numtasks/2;

        MPI_Irecv(&message, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqs[0]);
        MPI_Isend(&taskid, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqs[1]);

        MPI_Waitall(2, reqs, stats);
        if (taskid == MASTER) 
            printf("Done waiting!\n");

        /* print partner info and exit*/
        printf("Task %d is partner with %d\n", taskid, message);
    }
    MPI_Finalize();
}